Compare commits

...

1 Commits

Author SHA1 Message Date
Jesse Duffield
4a07e03701 Local-ise remote branches when checking out by name
The idea here is that in the git CLI you will often want to checkout a
remote branch like origin/blah by doing `git checkout blah`. That will
automatically create a local branch named blah that tracks origin/blah.

Previously in the suggestions view when checking out a branch by name,
we would only show local branches and remote branches like origin/blah
but wouldn't show blah as an option (assuming no local branch existed
with that name). This meant users would checkout origin/blah and git
would check out that branch as a detached head which is rarely what you
actually want.

Now we give them both options. The alternative approach we could have
taken is to still show the branch as origin/blah but then ask if the
user wants to check out the branch as detached or as a local branch
tracking the remote branch. That approach is certainly more versatile,
but it's also something you can do already by going to the remote branch
directly via the remotes view. Admittedly, my approach involves less
work.
2024-03-09 13:53:14 +11:00
5 changed files with 97 additions and 3 deletions

View File

@@ -433,7 +433,7 @@ func (self *BranchesController) forceCheckout() error {
func (self *BranchesController) checkoutByName() error {
return self.c.Prompt(types.PromptOpts{
Title: self.c.Tr.BranchName + ":",
FindSuggestionsFunc: self.c.Helpers().Suggestions.GetRefsSuggestionsFunc(),
FindSuggestionsFunc: self.c.Helpers().Suggestions.GetCheckoutBranchesSuggestionsFunc(),
HandleConfirm: func(response string) error {
self.c.LogAction("Checkout branch")
return self.c.Helpers().Refs.CheckoutRef(response, types.CheckoutRefOptions{

View File

@@ -3,6 +3,7 @@ package helpers
import (
"fmt"
"os"
"strings"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/models"
@@ -25,6 +26,8 @@ import (
// finding suggestions in this file, so that it's easy to see if a function already
// exists for fetching a particular model.
var specialRefNames = []string{"HEAD", "FETCH_HEAD", "MERGE_HEAD", "ORIG_HEAD"}
type ISuggestionsHelper interface {
GetRemoteSuggestionsFunc() func(string) []*types.Suggestion
GetBranchNameSuggestionsFunc() func(string) []*types.Suggestion
@@ -168,9 +171,29 @@ func (self *SuggestionsHelper) GetRefsSuggestionsFunc() func(string) []*types.Su
remoteBranchNames := self.getRemoteBranchNames("/")
localBranchNames := self.getBranchNames()
tagNames := self.getTagNames()
additionalRefNames := []string{"HEAD", "FETCH_HEAD", "MERGE_HEAD", "ORIG_HEAD"}
refNames := append(append(append(remoteBranchNames, localBranchNames...), tagNames...), additionalRefNames...)
refNames := append(append(append(remoteBranchNames, localBranchNames...), tagNames...), specialRefNames...)
return FuzzySearchFunc(refNames)
}
func (self *SuggestionsHelper) GetCheckoutBranchesSuggestionsFunc() func(string) []*types.Suggestion {
remoteBranchNames := self.getRemoteBranchNames("/")
// We include remote branches with the remote stripped off in the list of suggestions
// so that you can check out the branch as a local branch tracking the remote branch, just
// like you can do in the git CLI. I.e. if you checkout 'origin/blah' it will be
// checked out as as a detached head, but if you checkout 'blah' it will be checked out
// as a local branch tracking 'origin/blah'.
localisedRemoteBranchNames := lo.Map(remoteBranchNames, func(branchName string, _ int) string {
// strip the remote name from the branch name
return branchName[strings.Index(branchName, "/")+1:]
})
localBranchNames := self.getBranchNames()
tagNames := self.getTagNames()
refNames := append(append(append(append(remoteBranchNames, localBranchNames...), tagNames...), specialRefNames...), localisedRemoteBranchNames...)
refNames = lo.Uniq(refNames)
return FuzzySearchFunc(refNames)
}

View File

@@ -148,6 +148,10 @@ func (self *Shell) Checkout(name string) *Shell {
return self.RunCommand([]string{"git", "checkout", name})
}
func (self *Shell) DeleteBranch(name string) *Shell {
return self.RunCommand([]string{"git", "branch", "-D", name})
}
func (self *Shell) Merge(name string) *Shell {
return self.RunCommand([]string{"git", "merge", "--commit", "--no-ff", name})
}

View File

@@ -0,0 +1,66 @@
package branch
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var CheckoutByNameRemote = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Checkout a remote branch by name, both using the full name and the local name.",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
shell.EmptyCommit("initial commit")
// create an origin/foo remote branch
shell.CloneIntoRemote("origin")
shell.NewBranch("foo")
shell.PushBranch("origin", "foo")
// delete the local version of the branch because we need to test checking it out from scratch
shell.Checkout("master")
shell.DeleteBranch("foo")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Branches().
Focus().
Lines(
Contains("master").IsSelected(),
).
// maximising window so that we can see the tracked branch
Press(keys.Universal.NextScreenMode).
Press(keys.Branches.CheckoutBranchByName).
Tap(func() {
t.ExpectPopup().Prompt().
Title(Equals("Branch name:")).
Type("foo").
SuggestionLines(
Contains("foo"),
Contains("origin/foo"),
).
ConfirmFirstSuggestion()
}).
Lines(
Contains("foo").
// we have not checked out origin/foo...
DoesNotContain("origin/foo").
// ... but we are tracking it (formatted as '<remote> <branch>')
Contains("origin foo"),
Contains("master"),
).
Press(keys.Branches.CheckoutBranchByName).
Tap(func() {
t.ExpectPopup().Prompt().
Title(Equals("Branch name:")).
Type("origin/foo").
SuggestionLines(
Contains("origin/foo"),
).
ConfirmFirstSuggestion()
}).
Lines(
Contains("HEAD detached at origin/foo"),
Contains("foo"),
Contains("master"),
)
},
})

View File

@@ -37,6 +37,7 @@ var tests = []*components.IntegrationTest{
bisect.FromOtherBranch,
bisect.Skip,
branch.CheckoutByName,
branch.CheckoutByNameRemote,
branch.CreateTag,
branch.Delete,
branch.DeleteRemoteBranchWithCredentialPrompt,