From 44248d9ab0818dfca6a5c1f5ee2ad5b0d45d4998 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Tue, 29 Sep 2020 18:34:01 +1000 Subject: [PATCH] pull branch model out into models package --- pkg/commands/branch_list_builder.go | 21 +++++++++++---------- pkg/commands/git.go | 3 ++- pkg/commands/pull_request.go | 3 ++- pkg/commands/pull_request_test.go | 12 ++++++------ pkg/gui/branches_panel.go | 9 +++++---- pkg/gui/custom_commands.go | 5 +++-- pkg/gui/gui.go | 3 ++- pkg/gui/presentation/branches.go | 6 +++--- pkg/{commands => models}/branch.go | 2 +- 9 files changed, 35 insertions(+), 29 deletions(-) rename pkg/{commands => models}/branch.go (96%) 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 { diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index be67def2d..dae81f7fa 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -6,11 +6,12 @@ import ( "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands" + "github.com/jesseduffield/lazygit/pkg/models" ) // list panel functions -func (gui *Gui) getSelectedBranch() *commands.Branch { +func (gui *Gui) getSelectedBranch() *models.Branch { if len(gui.State.Branches) == 0 { return nil } @@ -209,7 +210,7 @@ func (gui *Gui) handleCheckoutByName(g *gocui.Gui, v *gocui.View) error { }) } -func (gui *Gui) getCheckedOutBranch() *commands.Branch { +func (gui *Gui) getCheckedOutBranch() *models.Branch { if len(gui.State.Branches) == 0 { return nil } @@ -247,7 +248,7 @@ func (gui *Gui) deleteBranch(force bool) error { return gui.deleteNamedBranch(selectedBranch, force) } -func (gui *Gui) deleteNamedBranch(selectedBranch *commands.Branch, force bool) error { +func (gui *Gui) deleteNamedBranch(selectedBranch *models.Branch, force bool) error { title := gui.Tr.SLocalize("DeleteBranch") var messageID string if force { @@ -446,7 +447,7 @@ func (gui *Gui) handleRenameBranch(g *gocui.Gui, v *gocui.View) error { }) } -func (gui *Gui) currentBranch() *commands.Branch { +func (gui *Gui) currentBranch() *models.Branch { if len(gui.State.Branches) == 0 { return nil } diff --git a/pkg/gui/custom_commands.go b/pkg/gui/custom_commands.go index d13c2d918..847163f81 100644 --- a/pkg/gui/custom_commands.go +++ b/pkg/gui/custom_commands.go @@ -9,6 +9,7 @@ import ( "github.com/fatih/color" "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands" + "github.com/jesseduffield/lazygit/pkg/models" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -17,13 +18,13 @@ type CustomCommandObjects struct { SelectedReflogCommit *commands.Commit SelectedSubCommit *commands.Commit SelectedFile *commands.File - SelectedLocalBranch *commands.Branch + SelectedLocalBranch *models.Branch SelectedRemoteBranch *commands.RemoteBranch SelectedRemote *commands.Remote SelectedTag *commands.Tag SelectedStashEntry *commands.StashEntry SelectedCommitFile *commands.CommitFile - CheckedOutBranch *commands.Branch + CheckedOutBranch *models.Branch PromptResponses []string } diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index d35fe59be..c1d52bee3 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -25,6 +25,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/patch" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/i18n" + "github.com/jesseduffield/lazygit/pkg/models" "github.com/jesseduffield/lazygit/pkg/tasks" "github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/updates" @@ -273,7 +274,7 @@ type Modes struct { type guiState struct { Files []*commands.File SubmoduleConfigs []*commands.SubmoduleConfig - Branches []*commands.Branch + Branches []*models.Branch Commits []*commands.Commit StashEntries []*commands.StashEntry CommitFiles []*commands.CommitFile diff --git a/pkg/gui/presentation/branches.go b/pkg/gui/presentation/branches.go index 3e5c2d625..17107698d 100644 --- a/pkg/gui/presentation/branches.go +++ b/pkg/gui/presentation/branches.go @@ -5,12 +5,12 @@ import ( "strings" "github.com/fatih/color" - "github.com/jesseduffield/lazygit/pkg/commands" + "github.com/jesseduffield/lazygit/pkg/models" "github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/utils" ) -func GetBranchListDisplayStrings(branches []*commands.Branch, fullDescription bool, diffName string) [][]string { +func GetBranchListDisplayStrings(branches []*models.Branch, fullDescription bool, diffName string) [][]string { lines := make([][]string, len(branches)) for i := range branches { @@ -22,7 +22,7 @@ func GetBranchListDisplayStrings(branches []*commands.Branch, fullDescription bo } // getBranchDisplayStrings returns the display string of branch -func getBranchDisplayStrings(b *commands.Branch, fullDescription bool, diffed bool) []string { +func getBranchDisplayStrings(b *models.Branch, fullDescription bool, diffed bool) []string { displayName := b.Name if b.DisplayName != "" { displayName = b.DisplayName diff --git a/pkg/commands/branch.go b/pkg/models/branch.go similarity index 96% rename from pkg/commands/branch.go rename to pkg/models/branch.go index 321101755..c95e335f9 100644 --- a/pkg/commands/branch.go +++ b/pkg/models/branch.go @@ -1,4 +1,4 @@ -package commands +package models // Branch : A git branch // duplicating this for now