diff --git a/pkg/commands/git_commands/branch.go b/pkg/commands/git_commands/branch.go index 79e418d7f..bb065605c 100644 --- a/pkg/commands/git_commands/branch.go +++ b/pkg/commands/git_commands/branch.go @@ -28,6 +28,15 @@ func (self *BranchCommands) New(name string, base string) error { return self.cmd.New(cmdArgs).Run() } +func (self *BranchCommands) NewWithoutTracking(name string, base string) error { + cmdArgs := NewGitCmd("checkout"). + Arg("-b", name, base). + Arg("--no-track"). + ToArgv() + + return self.cmd.New(cmdArgs).Run() +} + // CreateWithUpstream creates a new branch with a given upstream, but without // checking it out func (self *BranchCommands) CreateWithUpstream(name string, upstream string) error { diff --git a/pkg/gui/controllers/helpers/refs_helper.go b/pkg/gui/controllers/helpers/refs_helper.go index 08c6e173a..095e9848b 100644 --- a/pkg/gui/controllers/helpers/refs_helper.go +++ b/pkg/gui/controllers/helpers/refs_helper.go @@ -279,7 +279,12 @@ func (self *RefsHelper) NewBranch(from string, fromFormattedName string, suggest InitialContent: suggestedBranchName, HandleConfirm: func(response string) error { self.c.LogAction(self.c.Tr.Actions.CreateBranch) - if err := self.c.Git().Branch.New(SanitizedBranchName(response), from); err != nil { + newBranchName := SanitizedBranchName(response) + newBranchFunc := self.c.Git().Branch.New + if newBranchName != suggestedBranchName { + newBranchFunc = self.c.Git().Branch.NewWithoutTracking + } + if err := newBranchFunc(newBranchName, from); err != nil { return err } diff --git a/pkg/integration/tests/branch/new_branch_from_remote_tracking_different_name.go b/pkg/integration/tests/branch/new_branch_from_remote_tracking_different_name.go new file mode 100644 index 000000000..75cc28280 --- /dev/null +++ b/pkg/integration/tests/branch/new_branch_from_remote_tracking_different_name.go @@ -0,0 +1,50 @@ +package branch + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var NewBranchFromRemoteTrackingDifferentName = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Set tracking information when creating a new branch from a remote branch", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) {}, + SetupRepo: func(shell *Shell) { + shell.EmptyCommit("commit") + shell.NewBranch("other_branch") + shell.CloneIntoRemote("origin") + shell.Checkout("master") + shell.RunCommand([]string{"git", "branch", "-D", "other_branch"}) + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Remotes(). + Focus(). + Lines( + Contains("origin").IsSelected(), + ). + PressEnter() + + t.Views().RemoteBranches(). + IsFocused(). + Lines( + Contains("master").IsSelected(), + Contains("other_branch"), + ). + SelectNextItem(). + Press(keys.Universal.New) + + t.ExpectPopup().Prompt(). + Title(Equals("New branch name (branch is off of 'origin/other_branch')")). + Clear(). + Type("different_name"). + Confirm() + + t.Views().Branches(). + Focus(). + Lines( + Contains("different_name").DoesNotContain("✓"), + Contains("master"), + ) + }, +}) diff --git a/pkg/integration/tests/branch/new_branch_from_remote_tracking_same_name.go b/pkg/integration/tests/branch/new_branch_from_remote_tracking_same_name.go new file mode 100644 index 000000000..753fd32fb --- /dev/null +++ b/pkg/integration/tests/branch/new_branch_from_remote_tracking_same_name.go @@ -0,0 +1,48 @@ +package branch + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var NewBranchFromRemoteTrackingSameName = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Set tracking information when creating a new branch from a remote branch", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) {}, + SetupRepo: func(shell *Shell) { + shell.EmptyCommit("commit") + shell.NewBranch("other_branch") + shell.CloneIntoRemote("origin") + shell.Checkout("master") + shell.RunCommand([]string{"git", "branch", "-D", "other_branch"}) + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Remotes(). + Focus(). + Lines( + Contains("origin").IsSelected(), + ). + PressEnter() + + t.Views().RemoteBranches(). + IsFocused(). + Lines( + Contains("master").IsSelected(), + Contains("other_branch"), + ). + SelectNextItem(). + Press(keys.Universal.New) + + t.ExpectPopup().Prompt(). + Title(Equals("New branch name (branch is off of 'origin/other_branch')")). + Confirm() + + t.Views().Branches(). + Focus(). + Lines( + Contains("other_branch").Contains("✓"), + Contains("master"), + ) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index 22072be97..ee547e950 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -42,6 +42,8 @@ var tests = []*components.IntegrationTest{ branch.Delete, branch.DeleteRemoteBranchWithCredentialPrompt, branch.DetachedHead, + branch.NewBranchFromRemoteTrackingDifferentName, + branch.NewBranchFromRemoteTrackingSameName, branch.OpenPullRequestNoUpstream, branch.OpenWithCliArg, branch.Rebase,