Skip to content

Commit 442ede9

Browse files
authored
Offer to force-delete a worktree if it contains submodules (#4959)
For a worktree that contains submodules, trying to delete it would show an error instead of offering to force-remove it. Fixes #4125.
2 parents 5812466 + 3415ed9 commit 442ede9

File tree

6 files changed

+52
-4
lines changed

6 files changed

+52
-4
lines changed

pkg/gui/controllers/helpers/worktree_helper.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ func (self *WorktreeHelper) Remove(worktree *models.Worktree, force bool) error
189189
self.c.LogAction(self.c.Tr.RemoveWorktree)
190190
if err := self.c.Git().Worktree.Delete(worktree.Path, force); err != nil {
191191
errMessage := err.Error()
192-
if !strings.Contains(errMessage, "--force") {
192+
if !strings.Contains(errMessage, "--force") &&
193+
!strings.Contains(errMessage, "fatal: working trees containing submodules cannot be moved or removed") {
193194
return err
194195
}
195196

pkg/i18n/english.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1933,7 +1933,7 @@ func EnglishTranslationSet() *TranslationSet {
19331933
RemoveWorktree: "Remove worktree",
19341934
RemoveWorktreeTitle: "Remove worktree",
19351935
RemoveWorktreePrompt: "Are you sure you want to remove worktree '{{.worktreeName}}'?",
1936-
ForceRemoveWorktreePrompt: "'{{.worktreeName}}' contains modified or untracked files (to be honest, it could contain both). Are you sure you want to remove it?",
1936+
ForceRemoveWorktreePrompt: "'{{.worktreeName}}' contains modified or untracked files, or submodules (or all of these). Are you sure you want to remove it?",
19371937
RemovingWorktree: "Deleting worktree",
19381938
DetachWorktree: "Detach worktree",
19391939
DetachingWorktree: "Detaching worktree",

pkg/integration/tests/test_list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ var tests = []*components.IntegrationTest{
455455
worktree.FastForwardWorktreeBranch,
456456
worktree.FastForwardWorktreeBranchShouldNotPolluteCurrentWorktree,
457457
worktree.ForceRemoveWorktree,
458+
worktree.ForceRemoveWorktreeWithSubmodules,
458459
worktree.RemoveWorktreeFromBranch,
459460
worktree.ResetWindowTabs,
460461
worktree.SymlinkIntoRepoSubdir,

pkg/integration/tests/worktree/force_remove_worktree.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var ForceRemoveWorktree = NewIntegrationTest(NewIntegrationTestArgs{
3636

3737
t.ExpectPopup().Confirmation().
3838
Title(Equals("Remove worktree")).
39-
Content(Equals("'linked-worktree' contains modified or untracked files (to be honest, it could contain both). Are you sure you want to remove it?")).
39+
Content(Equals("'linked-worktree' contains modified or untracked files, or submodules (or all of these). Are you sure you want to remove it?")).
4040
Confirm()
4141
}).
4242
Lines(
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package worktree
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var ForceRemoveWorktreeWithSubmodules = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Force remove a worktree that contains submodules",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
shell.NewBranch("mybranch")
15+
shell.CreateFileAndAdd("README.md", "hello world")
16+
shell.Commit("initial commit")
17+
shell.CloneIntoSubmodule("submodule", "submodule")
18+
shell.Commit("Add submodule")
19+
shell.AddWorktree("mybranch", "../linked-worktree", "newbranch")
20+
shell.RunCommand([]string{"git", "-C", "../linked-worktree", "submodule", "update", "--init"})
21+
},
22+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
23+
t.Views().Worktrees().
24+
Focus().
25+
Lines(
26+
Contains("repo (main)").IsSelected(),
27+
Contains("linked-worktree"),
28+
).
29+
NavigateToLine(Contains("linked-worktree")).
30+
Press(keys.Universal.Remove).
31+
Tap(func() {
32+
t.ExpectPopup().Confirmation().
33+
Title(Equals("Remove worktree")).
34+
Content(Equals("Are you sure you want to remove worktree 'linked-worktree'?")).
35+
Confirm()
36+
37+
t.ExpectPopup().Confirmation().
38+
Title(Equals("Remove worktree")).
39+
Content(Equals("'linked-worktree' contains modified or untracked files, or submodules (or all of these). Are you sure you want to remove it?")).
40+
Confirm()
41+
}).
42+
Lines(
43+
Contains("repo (main)").IsSelected(),
44+
)
45+
},
46+
})

pkg/integration/tests/worktree/remove_worktree_from_branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var RemoveWorktreeFromBranch = NewIntegrationTest(NewIntegrationTestArgs{
4848

4949
t.ExpectPopup().Confirmation().
5050
Title(Equals("Remove worktree")).
51-
Content(Equals("'linked-worktree' contains modified or untracked files (to be honest, it could contain both). Are you sure you want to remove it?")).
51+
Content(Equals("'linked-worktree' contains modified or untracked files, or submodules (or all of these). Are you sure you want to remove it?")).
5252
Confirm()
5353
}).
5454
Lines(

0 commit comments

Comments
 (0)