-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add "Detach worktree" option when checking out a branch occupied by another worktree #5341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -475,7 +475,7 @@ func (self *BranchesController) press(selectedBranch *models.Branch) error { | |
|
|
||
| worktreeForRef, ok := self.worktreeForBranch(selectedBranch) | ||
| if ok && !worktreeForRef.IsCurrent { | ||
| return self.promptToCheckoutWorktree(worktreeForRef) | ||
| return self.promptToCheckoutWorktree(worktreeForRef, selectedBranch.Name) | ||
| } | ||
|
|
||
| self.c.LogAction(self.c.Tr.Actions.CheckoutBranch) | ||
|
|
@@ -498,16 +498,39 @@ func (self *BranchesController) worktreeForBranch(branch *models.Branch) (*model | |
| return git_commands.WorktreeForBranch(branch, self.c.Model().Worktrees) | ||
| } | ||
|
|
||
| func (self *BranchesController) promptToCheckoutWorktree(worktree *models.Worktree) error { | ||
| prompt := utils.ResolvePlaceholderString(self.c.Tr.AlreadyCheckedOutByWorktree, map[string]string{ | ||
| func (self *BranchesController) promptToCheckoutWorktree(worktree *models.Worktree, branchName string) error { | ||
| title := utils.ResolvePlaceholderString(self.c.Tr.BranchCheckedOutByWorktree, map[string]string{ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a fan of putting this text in the title. Branch names can be very long, so this might get truncated. I see that we are doing the same thing already when deleting branches, but it's not good there, either. :) I'd suggest to use a shorter title and put the longer text in a menu prompt (see CreateMenuOptions.Prompt), something like |
||
| "worktreeName": worktree.Name, | ||
| "branchName": branchName, | ||
| }) | ||
|
|
||
| return self.c.ConfirmIf(!self.c.UserConfig().Gui.SkipSwitchWorktreeOnCheckoutWarning, types.ConfirmOpts{ | ||
| Title: self.c.Tr.SwitchToWorktree, | ||
| Prompt: prompt, | ||
| HandleConfirm: func() error { | ||
| return self.c.Helpers().Worktree.Switch(worktree, context.LOCAL_BRANCHES_CONTEXT_KEY) | ||
| var detachDisabledReason *types.DisabledReason | ||
| if !worktree.IsPathMissing && !self.c.Git().Worktree.IsWorktreeClean(worktree.Path) { | ||
| detachDisabledReason = &types.DisabledReason{Text: self.c.Tr.DetachWorktreeHasUncommittedChanges} | ||
| } | ||
|
Comment on lines
+508
to
+510
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need this. Detaching a worktree when there are modified files doesn't lose data, the modifications will stay the same. And we don't guard against this when detaching a worktree in response to deleting a branch, either. |
||
|
|
||
| return self.c.Menu(types.CreateMenuOptions{ | ||
| Title: title, | ||
| Items: []*types.MenuItem{ | ||
| { | ||
| Label: self.c.Tr.SwitchToWorktree, | ||
| OnPress: func() error { | ||
| return self.c.Helpers().Worktree.Switch(worktree, context.LOCAL_BRANCHES_CONTEXT_KEY) | ||
| }, | ||
| }, | ||
| { | ||
| Label: self.c.Tr.DetachWorktree, | ||
| Tooltip: self.c.Tr.DetachWorktreeTooltip, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tooltip sounds as if detaching the worktree is the only thing that will happen. It should also mention that the branch is going to be checked out in the current worktree afterwards. |
||
| DisabledReason: detachDisabledReason, | ||
| OnPress: func() error { | ||
| return self.c.Helpers().Refs.CheckoutRef(branchName, types.CheckoutRefOptions{ | ||
| PreCheckoutCommand: func() error { | ||
| self.c.LogAction(self.c.Tr.DetachingWorktree) | ||
| return self.c.Git().Worktree.Detach(worktree.Path) | ||
| }, | ||
| }) | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a pretty long function; would be good to move it to BranchesHelper.