mirror of
https://github.com/grdl/git-get.git
synced 2026-02-04 22:04:41 +00:00
Rename git package to repo package
This commit is contained in:
@@ -2,7 +2,7 @@ package path
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"git-get/pkg/git"
|
||||
"git-get/pkg/repo"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
@@ -14,7 +14,7 @@ var (
|
||||
)
|
||||
|
||||
// ParseBundleFile opens a given gitgetfile and parses its content into a slice of CloneOpts.
|
||||
func ParseBundleFile(path string) ([]*git.CloneOpts, error) {
|
||||
func ParseBundleFile(path string) ([]*repo.CloneOpts, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "Failed opening gitgetfile %s", path)
|
||||
@@ -23,7 +23,7 @@ func ParseBundleFile(path string) ([]*git.CloneOpts, error) {
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
var opts []*git.CloneOpts
|
||||
var opts []*repo.CloneOpts
|
||||
var line int
|
||||
for scanner.Scan() {
|
||||
line++
|
||||
@@ -40,7 +40,7 @@ func ParseBundleFile(path string) ([]*git.CloneOpts, error) {
|
||||
|
||||
// parseLine splits a gitgetfile line into space-separated segments.
|
||||
// First part is the URL to clone. Second, optional, is the branch (or tag) to checkout after cloning
|
||||
func parseLine(line string) (*git.CloneOpts, error) {
|
||||
func parseLine(line string) (*repo.CloneOpts, error) {
|
||||
parts := strings.Split(line, " ")
|
||||
|
||||
if len(parts) > 2 {
|
||||
@@ -57,7 +57,7 @@ func parseLine(line string) (*git.CloneOpts, error) {
|
||||
branch = parts[1]
|
||||
}
|
||||
|
||||
return &git.CloneOpts{
|
||||
return &repo.CloneOpts{
|
||||
URL: url,
|
||||
Branch: branch,
|
||||
// When cloning a bundle we ignore errors about already cloned repos
|
||||
|
||||
@@ -3,7 +3,7 @@ package path
|
||||
import (
|
||||
"fmt"
|
||||
"git-get/pkg/cfg"
|
||||
"git-get/pkg/git"
|
||||
"git-get/pkg/repo"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -65,13 +65,13 @@ func ErrorCb(_ string, err error) godirwalk.ErrorAction {
|
||||
return godirwalk.Halt
|
||||
}
|
||||
|
||||
func OpenAll(paths []string) ([]*git.Repo, error) {
|
||||
var repos []*git.Repo
|
||||
reposChan := make(chan *git.Repo)
|
||||
func OpenAll(paths []string) ([]*repo.Repo, error) {
|
||||
var repos []*repo.Repo
|
||||
reposChan := make(chan *repo.Repo)
|
||||
|
||||
for _, path := range paths {
|
||||
go func(path string) {
|
||||
repo, err := git.OpenRepo(path)
|
||||
repo, err := repo.Open(path)
|
||||
|
||||
if err != nil {
|
||||
// TODO handle error
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package print
|
||||
|
||||
import (
|
||||
"git-get/pkg/git"
|
||||
"git-get/pkg/repo"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -9,22 +9,22 @@ type DumpPrinter struct{}
|
||||
|
||||
// Print generates a list of repos URLs. Each line contains a URL and, if applicable, a currently checked out branch name.
|
||||
// It's a way to dump all repositories managed by git-get and is supposed to be consumed by `git get --dump`.
|
||||
func (p *DumpPrinter) Print(_ string, repos []*git.Repo) string {
|
||||
func (p *DumpPrinter) Print(_ string, repos []*repo.Repo) string {
|
||||
var str strings.Builder
|
||||
|
||||
for i, repo := range repos {
|
||||
remotes, err := repo.Remotes()
|
||||
for i, r := range repos {
|
||||
remotes, err := r.Remotes()
|
||||
if err != nil || len(remotes) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// TODO: Needs work. Right now we're just assuming the first remote is the origin one and the one from which the current branch is checked out.
|
||||
url := remotes[0].Config().URLs[0]
|
||||
current := repo.Status.CurrentBranch
|
||||
current := r.Status.CurrentBranch
|
||||
|
||||
str.WriteString(url)
|
||||
|
||||
if current != git.StatusDetached && current != git.StatusUnknown {
|
||||
if current != repo.StatusDetached && current != repo.StatusUnknown {
|
||||
str.WriteString(" " + current)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,25 +2,25 @@ package print
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git-get/pkg/git"
|
||||
"git-get/pkg/repo"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type FlatPrinter struct{}
|
||||
|
||||
func (p *FlatPrinter) Print(root string, repos []*git.Repo) string {
|
||||
func (p *FlatPrinter) Print(root string, repos []*repo.Repo) string {
|
||||
val := root
|
||||
|
||||
for _, repo := range repos {
|
||||
path := strings.TrimPrefix(repo.Path, root)
|
||||
for _, r := range repos {
|
||||
path := strings.TrimPrefix(r.Path, root)
|
||||
path = strings.Trim(path, string(filepath.Separator))
|
||||
|
||||
val += fmt.Sprintf("\n%s %s", path, printWorktreeStatus(repo))
|
||||
val += fmt.Sprintf("\n%s %s", path, printWorktreeStatus(r))
|
||||
|
||||
for _, branch := range repo.Status.Branches {
|
||||
for _, branch := range r.Status.Branches {
|
||||
// Don't print the status of the current branch. It was already printed above.
|
||||
if branch.Name == repo.Status.CurrentBranch {
|
||||
if branch.Name == r.Status.CurrentBranch {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -2,12 +2,12 @@ package print
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git-get/pkg/git"
|
||||
"git-get/pkg/repo"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Printer interface {
|
||||
Print(root string, repos []*git.Repo) string
|
||||
Print(root string, repos []*repo.Repo) string
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -17,35 +17,35 @@ const (
|
||||
ColorYellow = "\033[1;33m%s\033[0m"
|
||||
)
|
||||
|
||||
func printWorktreeStatus(repo *git.Repo) string {
|
||||
func printWorktreeStatus(r *repo.Repo) string {
|
||||
clean := true
|
||||
var status []string
|
||||
|
||||
// if current branch status can't be found it's probably a detached head
|
||||
// TODO: what if current HEAD points to a tag?
|
||||
if current := repo.CurrentBranchStatus(); current == nil {
|
||||
status = append(status, fmt.Sprintf(ColorYellow, repo.Status.CurrentBranch))
|
||||
if current := r.CurrentBranchStatus(); current == nil {
|
||||
status = append(status, fmt.Sprintf(ColorYellow, r.Status.CurrentBranch))
|
||||
} else {
|
||||
status = append(status, printBranchStatus(current))
|
||||
}
|
||||
|
||||
// TODO: this is ugly
|
||||
// unset clean flag to use it to render braces around worktree status and remove "ok" from branch status if it's there
|
||||
if repo.Status.HasUncommittedChanges || repo.Status.HasUntrackedFiles {
|
||||
if r.Status.HasUncommittedChanges || r.Status.HasUntrackedFiles {
|
||||
clean = false
|
||||
}
|
||||
|
||||
if !clean {
|
||||
status[len(status)-1] = strings.TrimSuffix(status[len(status)-1], git.StatusOk)
|
||||
status[len(status)-1] = strings.TrimSuffix(status[len(status)-1], repo.StatusOk)
|
||||
status = append(status, "[")
|
||||
}
|
||||
|
||||
if repo.Status.HasUntrackedFiles {
|
||||
status = append(status, fmt.Sprintf(ColorRed, git.StatusUntracked))
|
||||
if r.Status.HasUntrackedFiles {
|
||||
status = append(status, fmt.Sprintf(ColorRed, repo.StatusUntracked))
|
||||
}
|
||||
|
||||
if repo.Status.HasUncommittedChanges {
|
||||
status = append(status, fmt.Sprintf(ColorRed, git.StatusUncommitted))
|
||||
if r.Status.HasUncommittedChanges {
|
||||
status = append(status, fmt.Sprintf(ColorRed, repo.StatusUncommitted))
|
||||
}
|
||||
|
||||
if !clean {
|
||||
@@ -55,7 +55,7 @@ func printWorktreeStatus(repo *git.Repo) string {
|
||||
return strings.Join(status, " ")
|
||||
}
|
||||
|
||||
func printBranchStatus(branch *git.BranchStatus) string {
|
||||
func printBranchStatus(branch *repo.BranchStatus) string {
|
||||
// ok indicates that the branch has upstream and is not ahead or behind it
|
||||
ok := true
|
||||
var status []string
|
||||
@@ -64,21 +64,21 @@ func printBranchStatus(branch *git.BranchStatus) string {
|
||||
|
||||
if branch.Upstream == "" {
|
||||
ok = false
|
||||
status = append(status, fmt.Sprintf(ColorYellow, git.StatusNoUpstream))
|
||||
status = append(status, fmt.Sprintf(ColorYellow, repo.StatusNoUpstream))
|
||||
}
|
||||
|
||||
if branch.Behind != 0 {
|
||||
ok = false
|
||||
status = append(status, fmt.Sprintf(ColorYellow, fmt.Sprintf("%d %s", branch.Behind, git.StatusBehind)))
|
||||
status = append(status, fmt.Sprintf(ColorYellow, fmt.Sprintf("%d %s", branch.Behind, repo.StatusBehind)))
|
||||
}
|
||||
|
||||
if branch.Ahead != 0 {
|
||||
ok = false
|
||||
status = append(status, fmt.Sprintf(ColorYellow, fmt.Sprintf("%d %s", branch.Ahead, git.StatusAhead)))
|
||||
status = append(status, fmt.Sprintf(ColorYellow, fmt.Sprintf("%d %s", branch.Ahead, repo.StatusAhead)))
|
||||
}
|
||||
|
||||
if ok {
|
||||
status = append(status, fmt.Sprintf(ColorGreen, git.StatusOk))
|
||||
status = append(status, fmt.Sprintf(ColorGreen, repo.StatusOk))
|
||||
}
|
||||
|
||||
return strings.Join(status, " ")
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package print
|
||||
|
||||
import (
|
||||
"git-get/pkg/git"
|
||||
"git-get/pkg/repo"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
@@ -16,13 +16,13 @@ type SmartTreePrinter struct {
|
||||
length int
|
||||
}
|
||||
|
||||
func (p *SmartTreePrinter) Print(root string, repos []*git.Repo) string {
|
||||
func (p *SmartTreePrinter) Print(root string, repos []*repo.Repo) string {
|
||||
tree := BuildTree(root, repos)
|
||||
|
||||
return p.PrintSmartTree(tree)
|
||||
}
|
||||
|
||||
func (p *SimpleTreePrinter) Print(root string, repos []*git.Repo) string {
|
||||
func (p *SimpleTreePrinter) Print(root string, repos []*repo.Repo) string {
|
||||
tree := BuildTree(root, repos)
|
||||
|
||||
tp := treeprint.New()
|
||||
@@ -39,7 +39,7 @@ type Node struct {
|
||||
depth int // depth is a nesting depth used when rendering a smart tree, not a depth level of a tree node.
|
||||
parent *Node
|
||||
children []*Node
|
||||
repo *git.Repo
|
||||
repo *repo.Repo
|
||||
}
|
||||
|
||||
// Root creates a new root of a tree
|
||||
@@ -83,11 +83,11 @@ func (n *Node) GetChild(val string) *Node {
|
||||
// BuildTree builds a directory tree of paths to repositories.
|
||||
// Each node represents a directory in the repo path.
|
||||
// Each leaf (final node) contains a pointer to the repo.
|
||||
func BuildTree(root string, repos []*git.Repo) *Node {
|
||||
func BuildTree(root string, repos []*repo.Repo) *Node {
|
||||
tree := Root(root)
|
||||
|
||||
for _, repo := range repos {
|
||||
path := strings.TrimPrefix(repo.Path, root)
|
||||
for _, r := range repos {
|
||||
path := strings.TrimPrefix(r.Path, root)
|
||||
path = strings.Trim(path, string(filepath.Separator))
|
||||
subs := strings.Split(path, string(filepath.Separator))
|
||||
|
||||
@@ -103,7 +103,7 @@ func BuildTree(root string, repos []*git.Repo) *Node {
|
||||
|
||||
// If that's the last fragment, it's a tree leaf and needs a *Repo attached.
|
||||
if i == len(subs)-1 {
|
||||
node.repo = repo
|
||||
node.repo = r
|
||||
}
|
||||
|
||||
continue
|
||||
|
||||
@@ -2,7 +2,7 @@ package print
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git-get/pkg/git"
|
||||
"git-get/pkg/repo"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -91,9 +91,9 @@ gitlab.com/
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
var repos []*git.Repo
|
||||
var repos []*repo.Repo
|
||||
for _, path := range test.paths {
|
||||
repos = append(repos, git.NewRepo(nil, path)) //&Repo{path: path})
|
||||
repos = append(repos, repo.New(nil, path)) //&Repo{path: path})
|
||||
}
|
||||
|
||||
printer := SmartTreePrinter{}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package git
|
||||
package repo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -35,7 +35,7 @@ type CloneOpts struct {
|
||||
IgnoreExisting bool // TODO: implement!
|
||||
}
|
||||
|
||||
func CloneRepo(opts *CloneOpts) (*Repo, error) {
|
||||
func Clone(opts *CloneOpts) (*Repo, error) {
|
||||
var progress io.Writer
|
||||
if !opts.Quiet {
|
||||
progress = os.Stdout
|
||||
@@ -76,19 +76,19 @@ func CloneRepo(opts *CloneOpts) (*Repo, error) {
|
||||
return nil, errors.Wrap(err, "Failed cloning repo")
|
||||
}
|
||||
|
||||
return NewRepo(repo, opts.Path), nil
|
||||
return New(repo, opts.Path), nil
|
||||
}
|
||||
|
||||
func OpenRepo(repoPath string) (*Repo, error) {
|
||||
func Open(repoPath string) (*Repo, error) {
|
||||
repo, err := git.PlainOpen(repoPath)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Failed opening repo")
|
||||
}
|
||||
|
||||
return NewRepo(repo, repoPath), nil
|
||||
return New(repo, repoPath), nil
|
||||
}
|
||||
|
||||
func NewRepo(repo *git.Repository, repoPath string) *Repo {
|
||||
func New(repo *git.Repository, repoPath string) *Repo {
|
||||
return &Repo{
|
||||
Repository: repo,
|
||||
Path: repoPath,
|
||||
@@ -1,4 +1,4 @@
|
||||
package git
|
||||
package repo
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
@@ -27,7 +27,7 @@ func newRepoEmpty(t *testing.T) *Repo {
|
||||
repo, err := git.PlainInit(dir, false)
|
||||
checkFatal(t, err)
|
||||
|
||||
return NewRepo(repo, dir)
|
||||
return New(repo, dir)
|
||||
}
|
||||
|
||||
func newRepoWithUntracked(t *testing.T) *Repo {
|
||||
@@ -254,7 +254,7 @@ func (r *Repo) clone(t *testing.T, branch string) *Repo {
|
||||
Quiet: true,
|
||||
}
|
||||
|
||||
repo, err := CloneRepo(cloneOpts)
|
||||
repo, err := Clone(cloneOpts)
|
||||
checkFatal(t, err)
|
||||
|
||||
return repo
|
||||
@@ -1,4 +1,4 @@
|
||||
package git
|
||||
package repo
|
||||
|
||||
import (
|
||||
"git-get/pkg/cfg"
|
||||
@@ -1,4 +1,4 @@
|
||||
package git
|
||||
package repo
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
Reference in New Issue
Block a user