diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2018-08-13 20:26:02 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2018-08-13 20:26:02 +1000 |
commit | 97cff656121270e9c790432e28622d92ab7b0f1a (patch) | |
tree | 7425013e94dc0a19699b48bde6bb20c6f5b86c8a /pkg/git | |
parent | f9c39ad64bddd1577636c0ce5606eda44bc704ef (diff) |
progress on refactor
Diffstat (limited to 'pkg/git')
-rw-r--r-- | pkg/git/branch.go | 31 | ||||
-rw-r--r-- | pkg/git/branch_list_builder.go | 63 |
2 files changed, 38 insertions, 56 deletions
diff --git a/pkg/git/branch.go b/pkg/git/branch.go deleted file mode 100644 index 78a52bbc8..000000000 --- a/pkg/git/branch.go +++ /dev/null @@ -1,31 +0,0 @@ -package git - -import ( - "strings" - - "github.com/fatih/color" -) - -// GetDisplayString returns the dispaly string of branch -// func (b *Branch) GetDisplayString() string { -// return gui.withPadding(b.Recency, 4) + gui.coloredString(b.Name, b.getColor()) -// } - -// GetColor branch color -func (b *Branch) GetColor() color.Attribute { - switch b.getType() { - case "feature": - return color.FgGreen - case "bugfix": - return color.FgYellow - case "hotfix": - return color.FgRed - default: - return color.FgWhite - } -} - -// expected to return feature/bugfix/hotfix or blank string -func (b *Branch) getType() string { - return strings.Split(b.Name, "/")[0] -} diff --git a/pkg/git/branch_list_builder.go b/pkg/git/branch_list_builder.go index 2f80dba32..faa073119 100644 --- a/pkg/git/branch_list_builder.go +++ b/pkg/git/branch_list_builder.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/jesseduffield/lazygit/pkg/commands" + "github.com/jesseduffield/lazygit/pkg/utils" "github.com/Sirupsen/logrus" @@ -19,59 +20,61 @@ import ( // our safe branches, then add the remaining safe branches, ensuring uniqueness // along the way +// BranchListBuilder returns a list of Branch objects for the current repo type BranchListBuilder struct { - Log *logrus.Log + Log *logrus.Logger GitCommand *commands.GitCommand } -func NewBranchListBuilder(log *logrus.Logger, gitCommand *GitCommand) (*BranchListBuilder, error) { - return nil, &BranchListBuilder{ - Log: log, - GitCommand: gitCommand - } +// NewBranchListBuilder builds a new branch list builder +func NewBranchListBuilder(log *logrus.Logger, gitCommand *commands.GitCommand) (*BranchListBuilder, error) { + return &BranchListBuilder{ + Log: log, + GitCommand: gitCommand, + }, nil } -func (b *branchListBuilder) ObtainCurrentBranch() Branch { +func (b *BranchListBuilder) obtainCurrentBranch() commands.Branch { // I used go-git for this, but that breaks if you've just done a git init, // even though you're on 'master' - branchName, _ := runDirectCommand("git symbolic-ref --short HEAD") - return Branch{Name: strings.TrimSpace(branchName), Recency: " *"} + branchName, _ := b.GitCommand.OSCommand.RunDirectCommand("git symbolic-ref --short HEAD") + return commands.Branch{Name: strings.TrimSpace(branchName), Recency: " *"} } -func (*branchListBuilder) ObtainReflogBranches() []Branch { - branches := make([]Branch, 0) - rawString, err := runDirectCommand("git reflog -n100 --pretty='%cr|%gs' --grep-reflog='checkout: moving' HEAD") +func (b *BranchListBuilder) obtainReflogBranches() []commands.Branch { + branches := make([]commands.Branch, 0) + rawString, err := b.GitCommand.OSCommand.RunDirectCommand("git reflog -n100 --pretty='%cr|%gs' --grep-reflog='checkout: moving' HEAD") if err != nil { return branches } - branchLines := splitLines(rawString) + branchLines := utils.SplitLines(rawString) for _, line := range branchLines { timeNumber, timeUnit, branchName := branchInfoFromLine(line) timeUnit = abbreviatedTimeUnit(timeUnit) - branch := Branch{Name: branchName, Recency: timeNumber + timeUnit} + branch := commands.Branch{Name: branchName, Recency: timeNumber + timeUnit} branches = append(branches, branch) } return branches } -func (b *branchListBuilder) obtainSafeBranches() []Branch { - branches := make([]Branch, 0) +func (b *BranchListBuilder) obtainSafeBranches() []commands.Branch { + branches := make([]commands.Branch, 0) - bIter, err := r.Branches() + bIter, err := b.GitCommand.Repo.Branches() if err != nil { panic(err) } err = bIter.ForEach(func(b *plumbing.Reference) error { name := b.Name().Short() - branches = append(branches, Branch{Name: name}) + branches = append(branches, commands.Branch{Name: name}) return nil }) return branches } -func (b *branchListBuilder) appendNewBranches(finalBranches, newBranches, existingBranches []Branch, included bool) []Branch { +func (b *BranchListBuilder) appendNewBranches(finalBranches, newBranches, existingBranches []commands.Branch, included bool) []commands.Branch { for _, newBranch := range newBranches { if included == branchIncluded(newBranch.Name, existingBranches) { finalBranches = append(finalBranches, newBranch) @@ -80,7 +83,7 @@ func (b *branchListBuilder) appendNewBranches(finalBranches, newBranches, existi return finalBranches } -func sanitisedReflogName(reflogBranch Branch, safeBranches []Branch) string { +func sanitisedReflogName(reflogBranch commands.Branch, safeBranches []commands.Branch) string { for _, safeBranch := range safeBranches { if strings.ToLower(safeBranch.Name) == strings.ToLower(reflogBranch.Name) { return safeBranch.Name @@ -89,15 +92,16 @@ func sanitisedReflogName(reflogBranch Branch, safeBranches []Branch) string { return reflogBranch.Name } -func (b *branchListBuilder) build() []Branch { - branches := make([]Branch, 0) +// Build the list of branches for the current repo +func (b *BranchListBuilder) Build() []commands.Branch { + branches := make([]commands.Branch, 0) head := b.obtainCurrentBranch() safeBranches := b.obtainSafeBranches() if len(safeBranches) == 0 { return append(branches, head) } reflogBranches := b.obtainReflogBranches() - reflogBranches = uniqueByName(append([]Branch{head}, reflogBranches...)) + reflogBranches = uniqueByName(append([]commands.Branch{head}, reflogBranches...)) for i, reflogBranch := range reflogBranches { reflogBranches[i].Name = sanitisedReflogName(reflogBranch, safeBranches) } @@ -108,8 +112,17 @@ func (b *branchListBuilder) build() []Branch { return branches } -func uniqueByName(branches []Branch) []Branch { - finalBranches := make([]Branch, 0) +func branchIncluded(branchName string, branches []commands.Branch) bool { + for _, existingBranch := range branches { + if strings.ToLower(existingBranch.Name) == strings.ToLower(branchName) { + return true + } + } + return false +} + +func uniqueByName(branches []commands.Branch) []commands.Branch { + finalBranches := make([]commands.Branch, 0) for _, branch := range branches { if branchIncluded(branch.Name, finalBranches) { continue |