diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-09-29 18:34:01 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-09-29 20:48:49 +1000 |
commit | 44248d9ab0818dfca6a5c1f5ee2ad5b0d45d4998 (patch) | |
tree | 832829129b6cc3690fc51e2c254d8b71d0de4657 /pkg/commands | |
parent | c87b2c02fa2c82dd07a77335df6c7d2e7817a012 (diff) |
pull branch model out into models package
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/branch.go | 26 | ||||
-rw-r--r-- | pkg/commands/branch_list_builder.go | 21 | ||||
-rw-r--r-- | pkg/commands/git.go | 3 | ||||
-rw-r--r-- | pkg/commands/pull_request.go | 3 | ||||
-rw-r--r-- | pkg/commands/pull_request_test.go | 12 |
5 files changed, 21 insertions, 44 deletions
diff --git a/pkg/commands/branch.go b/pkg/commands/branch.go deleted file mode 100644 index 321101755..000000000 --- a/pkg/commands/branch.go +++ /dev/null @@ -1,26 +0,0 @@ -package commands - -// Branch : A git branch -// duplicating this for now -type Branch struct { - Name string - // the displayname is something like '(HEAD detached at 123asdf)', whereas in that case the name would be '123asdf' - DisplayName string - Recency string - Pushables string - Pullables string - UpstreamName string - Head bool -} - -func (b *Branch) RefName() string { - return b.Name -} - -func (b *Branch) ID() string { - return b.RefName() -} - -func (b *Branch) Description() string { - return b.RefName() -} diff --git a/pkg/commands/branch_list_builder.go b/pkg/commands/branch_list_builder.go index 25947d7b7..99a27929a 100644 --- a/pkg/commands/branch_list_builder.go +++ b/pkg/commands/branch_list_builder.go @@ -4,6 +4,7 @@ import ( "regexp" "strings" + "github.com/jesseduffield/lazygit/pkg/models" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/sirupsen/logrus" ) @@ -35,7 +36,7 @@ func NewBranchListBuilder(log *logrus.Entry, gitCommand *GitCommand, reflogCommi }, nil } -func (b *BranchListBuilder) obtainBranches() []*Branch { +func (b *BranchListBuilder) obtainBranches() []*models.Branch { cmdStr := `git for-each-ref --sort=-committerdate --format="%(HEAD)|%(refname:short)|%(upstream:short)|%(upstream:track)" refs/heads` output, err := b.GitCommand.OSCommand.RunCommandWithOutput(cmdStr) if err != nil { @@ -44,7 +45,7 @@ func (b *BranchListBuilder) obtainBranches() []*Branch { trimmedOutput := strings.TrimSpace(output) outputLines := strings.Split(trimmedOutput, "\n") - branches := make([]*Branch, 0, len(outputLines)) + branches := make([]*models.Branch, 0, len(outputLines)) for _, line := range outputLines { if line == "" { continue @@ -53,7 +54,7 @@ func (b *BranchListBuilder) obtainBranches() []*Branch { split := strings.Split(line, SEPARATION_CHAR) name := strings.TrimPrefix(split[1], "heads/") - branch := &Branch{ + branch := &models.Branch{ Name: name, Pullables: "?", Pushables: "?", @@ -92,13 +93,13 @@ func (b *BranchListBuilder) obtainBranches() []*Branch { } // Build the list of branches for the current repo -func (b *BranchListBuilder) Build() []*Branch { +func (b *BranchListBuilder) Build() []*models.Branch { branches := b.obtainBranches() reflogBranches := b.obtainReflogBranches() // loop through reflog branches. If there is a match, merge them, then remove it from the branches and keep it in the reflog branches - branchesWithRecency := make([]*Branch, 0) + branchesWithRecency := make([]*models.Branch, 0) outer: for _, reflogBranch := range reflogBranches { for j, branch := range branches { @@ -122,7 +123,7 @@ outer: foundHead = true branch.Recency = " *" branches = append(branches[0:i], branches[i+1:]...) - branches = append([]*Branch{branch}, branches...) + branches = append([]*models.Branch{branch}, branches...) break } } @@ -131,24 +132,24 @@ outer: if err != nil { panic(err) } - branches = append([]*Branch{{Name: currentBranchName, DisplayName: currentBranchDisplayName, Head: true, Recency: " *"}}, branches...) + branches = append([]*models.Branch{{Name: currentBranchName, DisplayName: currentBranchDisplayName, Head: true, Recency: " *"}}, branches...) } return branches } // TODO: only look at the new reflog commits, and otherwise store the recencies in // int form against the branch to recalculate the time ago -func (b *BranchListBuilder) obtainReflogBranches() []*Branch { +func (b *BranchListBuilder) obtainReflogBranches() []*models.Branch { foundBranchesMap := map[string]bool{} re := regexp.MustCompile(`checkout: moving from ([\S]+) to ([\S]+)`) - reflogBranches := make([]*Branch, 0, len(b.ReflogCommits)) + reflogBranches := make([]*models.Branch, 0, len(b.ReflogCommits)) for _, commit := range b.ReflogCommits { if match := re.FindStringSubmatch(commit.Name); len(match) == 3 { recency := utils.UnixToTimeAgo(commit.UnixTimestamp) for _, branchName := range match[1:] { if !foundBranchesMap[branchName] { foundBranchesMap[branchName] = true - reflogBranches = append(reflogBranches, &Branch{ + reflogBranches = append(reflogBranches, &models.Branch{ Recency: recency, Name: branchName, }) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index c2c617b68..dcd77f526 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -20,6 +20,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/env" "github.com/jesseduffield/lazygit/pkg/i18n" + "github.com/jesseduffield/lazygit/pkg/models" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/sirupsen/logrus" gitconfig "github.com/tcnksm/go-gitconfig" @@ -813,7 +814,7 @@ func (c *GitCommand) GetRemoteURL() string { } // CheckRemoteBranchExists Returns remote branch -func (c *GitCommand) CheckRemoteBranchExists(branch *Branch) bool { +func (c *GitCommand) CheckRemoteBranchExists(branch *models.Branch) bool { _, err := c.OSCommand.RunCommandWithOutput( "git show-ref --verify -- refs/remotes/origin/%s", branch.Name, diff --git a/pkg/commands/pull_request.go b/pkg/commands/pull_request.go index fff0978ef..934b3ff57 100644 --- a/pkg/commands/pull_request.go +++ b/pkg/commands/pull_request.go @@ -6,6 +6,7 @@ import ( "github.com/go-errors/errors" "github.com/jesseduffield/lazygit/pkg/config" + "github.com/jesseduffield/lazygit/pkg/models" ) // Service is a service that repository is on (Github, Bitbucket, ...) @@ -89,7 +90,7 @@ func NewPullRequest(gitCommand *GitCommand) *PullRequest { } // Create opens link to new pull request in browser -func (pr *PullRequest) Create(branch *Branch) error { +func (pr *PullRequest) Create(branch *models.Branch) error { branchExistsOnRemote := pr.GitCommand.CheckRemoteBranchExists(branch) if !branchExistsOnRemote { diff --git a/pkg/commands/pull_request_test.go b/pkg/commands/pull_request_test.go index 88a012bf2..c75551592 100644 --- a/pkg/commands/pull_request_test.go +++ b/pkg/commands/pull_request_test.go @@ -46,7 +46,7 @@ func TestGetRepoInfoFromURL(t *testing.T) { func TestCreatePullRequest(t *testing.T) { type scenario struct { testName string - branch *Branch + branch *models.Branch command func(string, ...string) *exec.Cmd test func(err error) } @@ -54,7 +54,7 @@ func TestCreatePullRequest(t *testing.T) { scenarios := []scenario{ { "Opens a link to new pull request on bitbucket", - &Branch{ + &models.Branch{ Name: "feature/profile-page", }, func(cmd string, args ...string) *exec.Cmd { @@ -73,7 +73,7 @@ func TestCreatePullRequest(t *testing.T) { }, { "Opens a link to new pull request on bitbucket with http remote url", - &Branch{ + &models.Branch{ Name: "feature/events", }, func(cmd string, args ...string) *exec.Cmd { @@ -92,7 +92,7 @@ func TestCreatePullRequest(t *testing.T) { }, { "Opens a link to new pull request on github", - &Branch{ + &models.Branch{ Name: "feature/sum-operation", }, func(cmd string, args ...string) *exec.Cmd { @@ -111,7 +111,7 @@ func TestCreatePullRequest(t *testing.T) { }, { "Opens a link to new pull request on gitlab", - &Branch{ + &models.Branch{ Name: "feature/ui", }, func(cmd string, args ...string) *exec.Cmd { @@ -130,7 +130,7 @@ func TestCreatePullRequest(t *testing.T) { }, { "Throws an error if git service is unsupported", - &Branch{ + &models.Branch{ Name: "feature/divide-operation", }, func(cmd string, args ...string) *exec.Cmd { |