Skip to content

Commit 67c4eb4

Browse files
committed
feat: add keybindings
1 parent 9524300 commit 67c4eb4

File tree

3 files changed

+51
-47
lines changed

3 files changed

+51
-47
lines changed

pkg/gui/controllers/basic_commits_controller.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,7 @@ func (self *BasicCommitsController) checkout(commit *models.Commit) error {
285285
return commit.Hash == branch.CommitHash && branch.Name != self.c.Model().CheckedOutBranch
286286
})
287287

288-
if len(commitBranches) > 0 {
289-
return self.c.Helpers().Refs.CreateCheckoutMenu(commit.Hash, commitBranches)
290-
}
291-
292-
self.c.Confirm(types.ConfirmOpts{
293-
Title: self.c.Tr.CheckoutCommit,
294-
Prompt: self.c.Tr.SureCheckoutThisCommit,
295-
HandleConfirm: func() error {
296-
self.c.LogAction(self.c.Tr.Actions.CheckoutCommit)
297-
return self.c.Helpers().Refs.CheckoutRef(commit.Hash, types.CheckoutRefOptions{})
298-
},
299-
})
300-
return nil
288+
return self.c.Helpers().Refs.CreateCheckoutMenu(commit.Hash, commitBranches)
301289
}
302290

303291
func (self *BasicCommitsController) copyRange(*models.Commit) error {

pkg/gui/controllers/helpers/refs_helper.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -273,26 +273,10 @@ func (self *RefsHelper) CreateGitResetMenu(ref string) error {
273273
}
274274

275275
func (self *RefsHelper) CreateCheckoutMenu(ref string, branches []*models.Branch) error {
276-
menuItems := lo.Map(branches, func(branch *models.Branch, _ int) *types.MenuItem {
277-
return &types.MenuItem{
278-
LabelColumns: []string{branch.Name},
279-
OnPress: func() error {
280-
self.c.LogAction(self.c.Tr.Actions.CheckoutBranch)
281-
if err := self.c.Git().Branch.Checkout(branch.Name, git_commands.CheckoutOptions{}); err != nil {
282-
return err
283-
}
284-
return self.c.WithWaitingStatus(self.c.Tr.LoadingCommits, func(gocui.Task) error {
285-
return self.c.Refresh(
286-
types.RefreshOptions{Mode: types.SYNC, Scope: []types.RefreshableView{types.COMMITS}},
287-
)
288-
})
289-
},
290-
Tooltip: self.c.Tr.CheckoutBranchTooltip,
291-
}
292-
})
276+
var menuItems []*types.MenuItem
293277

294278
menuItems = append(menuItems, &types.MenuItem{
295-
LabelColumns: []string{ref},
279+
LabelColumns: []string{fmt.Sprintf("%s %s: %s", self.c.Tr.CheckoutCommit, self.c.Tr.AsDetachedHead, ref[:7])},
296280
OnPress: func() error {
297281
self.c.Confirm(types.ConfirmOpts{
298282
Title: self.c.Tr.CheckoutCommit,
@@ -305,8 +289,36 @@ func (self *RefsHelper) CreateCheckoutMenu(ref string, branches []*models.Branch
305289
return nil
306290
},
307291
Tooltip: self.c.Tr.CheckoutCommitTooltip,
292+
Key: 'd',
308293
})
309294

295+
if len(branches) > 0 {
296+
menuItems = lo.Map(branches, func(branch *models.Branch, index int) *types.MenuItem {
297+
return &types.MenuItem{
298+
LabelColumns: []string{fmt.Sprintf("%s: %s", self.c.Tr.Actions.CheckoutBranch, branch.Name)},
299+
OnPress: func() error {
300+
self.c.LogAction(self.c.Tr.Actions.CheckoutBranch)
301+
if err := self.c.Git().Branch.Checkout(branch.Name, git_commands.CheckoutOptions{}); err != nil {
302+
return err
303+
}
304+
return self.c.WithWaitingStatus(self.c.Tr.LoadingCommits, func(gocui.Task) error {
305+
return self.c.Refresh(
306+
types.RefreshOptions{Mode: types.SYNC, Scope: []types.RefreshableView{types.COMMITS}},
307+
)
308+
})
309+
},
310+
Tooltip: self.c.Tr.CheckoutBranchTooltip,
311+
Key: rune(index + 1 + '0'), // Convert 1-based index to key
312+
}
313+
})
314+
} else {
315+
menuItems = append(menuItems, &types.MenuItem{
316+
LabelColumns: []string{style.FgDefault.SetStrikethrough().Sprint(self.c.Tr.Actions.CheckoutBranch)},
317+
OnPress: func() error { return nil },
318+
DisabledReason: &types.DisabledReason{Text: self.c.Tr.NoBranchesFoundAtCommitTooltip},
319+
})
320+
}
321+
310322
return self.c.Menu(types.CreateMenuOptions{
311323
Title: self.c.Tr.Actions.CheckoutBranchOrCommit,
312324
Items: menuItems,

pkg/i18n/english.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ type TranslationSet struct {
130130
CheckoutTypeNewBranchTooltip string
131131
CheckoutTypeDetachedHead string
132132
CheckoutTypeDetachedHeadTooltip string
133+
AsDetachedHead string
133134
NewBranch string
134135
NewBranchFromStashTooltip string
135136
NoBranchesThisRepo string
@@ -524,6 +525,7 @@ type TranslationSet struct {
524525
CheckoutCommit string
525526
CheckoutCommitTooltip string
526527
CheckoutBranchTooltip string
528+
NoBranchesFoundAtCommitTooltip string
527529
SureCheckoutThisCommit string
528530
GitFlowOptions string
529531
NotAGitFlowBranch string
@@ -1123,6 +1125,7 @@ func EnglishTranslationSet() *TranslationSet {
11231125
CheckoutTypeNewBranchTooltip: "Checkout the remote branch as a local branch, tracking the remote branch.",
11241126
CheckoutTypeDetachedHead: "Detached head",
11251127
CheckoutTypeDetachedHeadTooltip: "Checkout the remote branch as a detached head, which can be useful if you just want to test the branch but not work on it yourself. You can still create a local branch from it later.",
1128+
AsDetachedHead: "as detached head",
11261129
NewBranch: "New branch",
11271130
NewBranchFromStashTooltip: "Create a new branch from the selected stash entry. This works by git checking out the commit that the stash entry was created from, creating a new branch from that commit, then applying the stash entry to the new branch as an additional commit.",
11281131
NoBranchesThisRepo: "No branches for this repo",
@@ -1516,22 +1519,23 @@ func EnglishTranslationSet() *TranslationSet {
15161519
DeleteRemoteTagPrompt: "Are you sure you want to delete the remote tag '{{.tagName}}' from '{{.upstream}}'?",
15171520
PushTagTitle: "Remote to push tag '{{.tagName}}' to:",
15181521
// Using 'push tag' rather than just 'push' to disambiguate from a global push
1519-
PushTag: "Push tag",
1520-
PushTagTooltip: "Push the selected tag to a remote. You'll be prompted to select a remote.",
1521-
NewTag: "New tag",
1522-
NewTagTooltip: "Create new tag from current commit. You'll be prompted to enter a tag name and optional description.",
1523-
CreatingTag: "Creating tag",
1524-
ForceTag: "Force Tag",
1525-
ForceTagPrompt: "The tag '{{.tagName}}' exists already. Press {{.cancelKey}} to cancel, or {{.confirmKey}} to overwrite.",
1526-
FetchRemoteTooltip: "Fetch updates from the remote repository. This retrieves new commits and branches without merging them into your local branches.",
1527-
FetchingRemoteStatus: "Fetching remote",
1528-
CheckoutCommit: "Checkout commit",
1529-
CheckoutCommitTooltip: "Checkout the selected commit as a detached HEAD.",
1530-
CheckoutBranchTooltip: "Checkout the selected branch.",
1531-
SureCheckoutThisCommit: "Are you sure you want to checkout this commit?",
1532-
GitFlowOptions: "Show git-flow options",
1533-
NotAGitFlowBranch: "This does not seem to be a git flow branch",
1534-
NewGitFlowBranchPrompt: "New {{.branchType}} name:",
1522+
PushTag: "Push tag",
1523+
PushTagTooltip: "Push the selected tag to a remote. You'll be prompted to select a remote.",
1524+
NewTag: "New tag",
1525+
NewTagTooltip: "Create new tag from current commit. You'll be prompted to enter a tag name and optional description.",
1526+
CreatingTag: "Creating tag",
1527+
ForceTag: "Force Tag",
1528+
ForceTagPrompt: "The tag '{{.tagName}}' exists already. Press {{.cancelKey}} to cancel, or {{.confirmKey}} to overwrite.",
1529+
FetchRemoteTooltip: "Fetch updates from the remote repository. This retrieves new commits and branches without merging them into your local branches.",
1530+
FetchingRemoteStatus: "Fetching remote",
1531+
CheckoutCommit: "Checkout commit",
1532+
CheckoutCommitTooltip: "Checkout the selected commit as a detached HEAD.",
1533+
CheckoutBranchTooltip: "Checkout the selected branch.",
1534+
NoBranchesFoundAtCommitTooltip: "No branches found at selected commit.",
1535+
SureCheckoutThisCommit: "Are you sure you want to checkout this commit?",
1536+
GitFlowOptions: "Show git-flow options",
1537+
NotAGitFlowBranch: "This does not seem to be a git flow branch",
1538+
NewGitFlowBranchPrompt: "New {{.branchType}} name:",
15351539

15361540
IgnoreTracked: "Ignore tracked file",
15371541
IgnoreTrackedPrompt: "Are you sure you want to ignore a tracked file?",

0 commit comments

Comments
 (0)