Skip to content

Commit 62a018c

Browse files
authored
Update tracking behaviour for branches created from remote branches (#3712)
### Overview | Current Behaviour | New Behaviour | |--|--| | Local branches will **always** track the remote branches they were created from. | Local branches will track the remote branches they were created from **only if their names match**. | ### Description The current behaviour when creating a new branch off of a remote branch is to always track the branch it was created from. For example, if a branch 'my_branch' is created off of the remote branch 'fix_crash_13', then 'my_branch' will be tracking the remote 'fix_crash_13' branch. It is common practice to have both the local and remote branches named the same when the local is tracking the remote one. Therefore, it is reasonable to expect that 'my_branch' should not track the remote 'fix_crash_13' branch. The new behaviour when creating a new branch off of a remote branch is to track the branch it was created from only if the branch names match. If the branch names DO NOT match then the newly created branch will not track the remote branch it was created from. For example, if a user creates a new branch 'fix_crash_13' off of the remote branch 'fix_crash_13', then the local 'fix_crash_13' branch will track the remote 'fix_crash_13' branch. However, if the user creates a new branch called 'other_branch_name' off of the remote branch 'fix_crash_13', then the local 'other_branch_name' branch will NOT track the remote 'fix_crash_13' branch. - **Please check if the PR fulfills these requirements** * [x] Cheatsheets are up-to-date (run `go generate ./...`) * [x] Code has been formatted (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#code-formatting)) * [x] Tests have been added/updated (see [here](https://github.com/jesseduffield/lazygit/blob/master/pkg/integration/README.md) for the integration test guide) * [ ] Text is internationalised (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#internationalisation)) * [ ] Docs have been updated if necessary * [x] You've read through your own file changes for silly mistakes etc
2 parents a047fba + b26ff43 commit 62a018c

File tree

5 files changed

+115
-1
lines changed

5 files changed

+115
-1
lines changed

pkg/commands/git_commands/branch.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ func (self *BranchCommands) New(name string, base string) error {
2828
return self.cmd.New(cmdArgs).Run()
2929
}
3030

31+
func (self *BranchCommands) NewWithoutTracking(name string, base string) error {
32+
cmdArgs := NewGitCmd("checkout").
33+
Arg("-b", name, base).
34+
Arg("--no-track").
35+
ToArgv()
36+
37+
return self.cmd.New(cmdArgs).Run()
38+
}
39+
3140
// CreateWithUpstream creates a new branch with a given upstream, but without
3241
// checking it out
3342
func (self *BranchCommands) CreateWithUpstream(name string, upstream string) error {

pkg/gui/controllers/helpers/refs_helper.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,12 @@ func (self *RefsHelper) NewBranch(from string, fromFormattedName string, suggest
279279
InitialContent: suggestedBranchName,
280280
HandleConfirm: func(response string) error {
281281
self.c.LogAction(self.c.Tr.Actions.CreateBranch)
282-
if err := self.c.Git().Branch.New(SanitizedBranchName(response), from); err != nil {
282+
newBranchName := SanitizedBranchName(response)
283+
newBranchFunc := self.c.Git().Branch.New
284+
if newBranchName != suggestedBranchName {
285+
newBranchFunc = self.c.Git().Branch.NewWithoutTracking
286+
}
287+
if err := newBranchFunc(newBranchName, from); err != nil {
283288
return err
284289
}
285290

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package branch
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var NewBranchFromRemoteTrackingDifferentName = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Set tracking information when creating a new branch from a remote branch",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
shell.EmptyCommit("commit")
15+
shell.NewBranch("other_branch")
16+
shell.CloneIntoRemote("origin")
17+
shell.Checkout("master")
18+
shell.RunCommand([]string{"git", "branch", "-D", "other_branch"})
19+
},
20+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
21+
t.Views().Remotes().
22+
Focus().
23+
Lines(
24+
Contains("origin").IsSelected(),
25+
).
26+
PressEnter()
27+
28+
t.Views().RemoteBranches().
29+
IsFocused().
30+
Lines(
31+
Contains("master").IsSelected(),
32+
Contains("other_branch"),
33+
).
34+
SelectNextItem().
35+
Press(keys.Universal.New)
36+
37+
t.ExpectPopup().Prompt().
38+
Title(Equals("New branch name (branch is off of 'origin/other_branch')")).
39+
Clear().
40+
Type("different_name").
41+
Confirm()
42+
43+
t.Views().Branches().
44+
Focus().
45+
Lines(
46+
Contains("different_name").DoesNotContain("✓"),
47+
Contains("master"),
48+
)
49+
},
50+
})
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package branch
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var NewBranchFromRemoteTrackingSameName = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Set tracking information when creating a new branch from a remote branch",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
shell.EmptyCommit("commit")
15+
shell.NewBranch("other_branch")
16+
shell.CloneIntoRemote("origin")
17+
shell.Checkout("master")
18+
shell.RunCommand([]string{"git", "branch", "-D", "other_branch"})
19+
},
20+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
21+
t.Views().Remotes().
22+
Focus().
23+
Lines(
24+
Contains("origin").IsSelected(),
25+
).
26+
PressEnter()
27+
28+
t.Views().RemoteBranches().
29+
IsFocused().
30+
Lines(
31+
Contains("master").IsSelected(),
32+
Contains("other_branch"),
33+
).
34+
SelectNextItem().
35+
Press(keys.Universal.New)
36+
37+
t.ExpectPopup().Prompt().
38+
Title(Equals("New branch name (branch is off of 'origin/other_branch')")).
39+
Confirm()
40+
41+
t.Views().Branches().
42+
Focus().
43+
Lines(
44+
Contains("other_branch").Contains("✓"),
45+
Contains("master"),
46+
)
47+
},
48+
})

pkg/integration/tests/test_list.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ var tests = []*components.IntegrationTest{
4242
branch.Delete,
4343
branch.DeleteRemoteBranchWithCredentialPrompt,
4444
branch.DetachedHead,
45+
branch.NewBranchFromRemoteTrackingDifferentName,
46+
branch.NewBranchFromRemoteTrackingSameName,
4547
branch.OpenPullRequestNoUpstream,
4648
branch.OpenWithCliArg,
4749
branch.Rebase,

0 commit comments

Comments
 (0)