From 4c5b1574f147fe2005bcd30bbb5dc106c4838b92 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Fri, 28 Jul 2023 18:27:14 +1000 Subject: Centralise logic for obtaining repo paths There are quite a few paths you might want to get e.g. the repo's path, the worktree's path, the repo's git dir path, the worktree's git dir path. I want these all obtained once and then used when needed rather than having to have IO whenever we need them. This is not so much about reducing time spent on IO as it is about not having to care about errors every time we want a path. --- .../tests/worktree/associate_branch_bisect.go | 88 +++++++++++++++++++++ .../tests/worktree/associate_branch_rebase.go | 89 ++++++++++++++++++++++ pkg/integration/tests/worktree/bare_repo.go | 43 ++++++++++- pkg/integration/tests/worktree/bisect.go | 88 --------------------- pkg/integration/tests/worktree/rebase.go | 89 ---------------------- 5 files changed, 219 insertions(+), 178 deletions(-) create mode 100644 pkg/integration/tests/worktree/associate_branch_bisect.go create mode 100644 pkg/integration/tests/worktree/associate_branch_rebase.go delete mode 100644 pkg/integration/tests/worktree/bisect.go delete mode 100644 pkg/integration/tests/worktree/rebase.go (limited to 'pkg/integration/tests/worktree') diff --git a/pkg/integration/tests/worktree/associate_branch_bisect.go b/pkg/integration/tests/worktree/associate_branch_bisect.go new file mode 100644 index 000000000..77d46e176 --- /dev/null +++ b/pkg/integration/tests/worktree/associate_branch_bisect.go @@ -0,0 +1,88 @@ +package worktree + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +// This is important because `git worktree list` will show a worktree being in a detached head state (which is true) +// when it's in the middle of a bisect, but it won't tell you about the branch it's on. +// Even so, if you attempt to check out that branch from another worktree git won't let you, so we need to +// keep track of the association ourselves. + +// not bothering to test the linked worktree here because it's the same logic as the rebase test + +var AssociateBranchBisect = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Verify that when you start a bisect in a linked worktree, Lazygit still associates the worktree with the branch", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) {}, + SetupRepo: func(shell *Shell) { + shell.NewBranch("mybranch") + shell.CreateFileAndAdd("README.md", "hello world") + shell.Commit("initial commit") + shell.EmptyCommit("commit 2") + shell.EmptyCommit("commit 3") + shell.AddWorktree("mybranch", "../linked-worktree", "newbranch") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Branches(). + Focus(). + Lines( + Contains("mybranch").IsSelected(), + Contains("newbranch (worktree)"), + ) + + // start a bisect on the main worktree + t.Views().Commits(). + Focus(). + SelectedLine(Contains("commit 3")). + Press(keys.Commits.ViewBisectOptions). + Tap(func() { + t.ExpectPopup().Menu(). + Title(Equals("Bisect")). + Select(MatchesRegexp(`Mark .* as bad`)). + Confirm() + + t.Views().Information().Content(Contains("Bisecting")) + }). + NavigateToLine(Contains("initial commit")). + Press(keys.Commits.ViewBisectOptions). + Tap(func() { + t.ExpectPopup().Menu(). + Title(Equals("Bisect")). + Select(MatchesRegexp(`Mark .* as good`)). + Confirm() + }) + + t.Views().Branches(). + Focus(). + // switch to linked worktree + NavigateToLine(Contains("newbranch")). + Press(keys.Universal.Select). + Tap(func() { + t.ExpectPopup().Confirmation(). + Title(Equals("Switch to worktree")). + Content(Equals("This branch is checked out by worktree linked-worktree. Do you want to switch to that worktree?")). + Confirm() + + t.Views().Information().Content(DoesNotContain("Bisecting")) + }). + Lines( + Contains("newbranch").IsSelected(), + Contains("mybranch (worktree)"), + ) + + // switch back to main worktree + t.Views().Branches(). + Focus(). + NavigateToLine(Contains("mybranch")). + Press(keys.Universal.Select). + Tap(func() { + t.ExpectPopup().Confirmation(). + Title(Equals("Switch to worktree")). + Content(Equals("This branch is checked out by worktree repo. Do you want to switch to that worktree?")). + Confirm() + }) + }, +}) diff --git a/pkg/integration/tests/worktree/associate_branch_rebase.go b/pkg/integration/tests/worktree/associate_branch_rebase.go new file mode 100644 index 000000000..b0b04b805 --- /dev/null +++ b/pkg/integration/tests/worktree/associate_branch_rebase.go @@ -0,0 +1,89 @@ +package worktree + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +// This is important because `git worktree list` will show a worktree being in a detached head state (which is true) +// when it's in the middle of a rebase, but it won't tell you about the branch it's on. +// Even so, if you attempt to check out that branch from another worktree git won't let you, so we need to +// keep track of the association ourselves. + +// We need different logic for associated the branch depending on whether it's a main worktree or +// linked worktree, so this test handles both. + +var AssociateBranchRebase = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Verify that when you start a rebase in a linked or main worktree, Lazygit still associates the worktree with the branch", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) {}, + SetupRepo: func(shell *Shell) { + shell.NewBranch("mybranch") + shell.CreateFileAndAdd("README.md", "hello world") + shell.Commit("initial commit") + shell.EmptyCommit("commit 2") + shell.EmptyCommit("commit 3") + shell.AddWorktree("mybranch", "../linked-worktree", "newbranch") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Branches(). + Focus(). + Lines( + Contains("mybranch").IsSelected(), + Contains("newbranch (worktree)"), + ) + + // start a rebase on the main worktree + t.Views().Commits(). + Focus(). + NavigateToLine(Contains("commit 2")). + Press(keys.Universal.Edit) + + t.Views().Information().Content(Contains("Rebasing")) + + t.Views().Branches(). + Focus(). + // switch to linked worktree + NavigateToLine(Contains("newbranch")). + Press(keys.Universal.Select). + Tap(func() { + t.ExpectPopup().Confirmation(). + Title(Equals("Switch to worktree")). + Content(Equals("This branch is checked out by worktree linked-worktree. Do you want to switch to that worktree?")). + Confirm() + + t.Views().Information().Content(DoesNotContain("Rebasing")) + }). + Lines( + Contains("newbranch").IsSelected(), + Contains("mybranch (worktree)"), + ) + + // start a rebase on the linked worktree + t.Views().Commits(). + Focus(). + NavigateToLine(Contains("commit 2")). + Press(keys.Universal.Edit) + + t.Views().Information().Content(Contains("Rebasing")) + + // switch back to main worktree + t.Views().Branches(). + Focus(). + NavigateToLine(Contains("mybranch")). + Press(keys.Universal.Select). + Tap(func() { + t.ExpectPopup().Confirmation(). + Title(Equals("Switch to worktree")). + Content(Equals("This branch is checked out by worktree repo. Do you want to switch to that worktree?")). + Confirm() + }). + Lines( + Contains("(no branch").IsSelected(), + Contains("mybranch"), + // even though the linked worktree is rebasing, we still associate it with the branch + Contains("newbranch (worktree)"), + ) + }, +}) diff --git a/pkg/integration/tests/worktree/bare_repo.go b/pkg/integration/tests/worktree/bare_repo.go index af0133227..2343b112e 100644 --- a/pkg/integration/tests/worktree/bare_repo.go +++ b/pkg/integration/tests/worktree/bare_repo.go @@ -6,7 +6,7 @@ import ( ) var BareRepo = NewIntegrationTest(NewIntegrationTestArgs{ - Description: "Open lazygit in the worktree of a bare repo", + Description: "Open lazygit in the worktree of a bare repo and do a rebase/bisect", ExtraCmdArgs: []string{}, Skip: false, SetupConfig: func(config *config.AppConfig) {}, @@ -23,6 +23,8 @@ var BareRepo = NewIntegrationTest(NewIntegrationTestArgs{ shell.NewBranch("mybranch") shell.CreateFileAndAdd("blah", "blah") shell.Commit("initial commit") + shell.EmptyCommit("commit two") + shell.EmptyCommit("commit three") shell.RunCommand([]string{"git", "clone", "--bare", ".", "../.bare"}) @@ -45,6 +47,45 @@ var BareRepo = NewIntegrationTest(NewIntegrationTestArgs{ Contains("worktree2 (worktree)"), ) + // test that a rebase works fine + // (rebase uses the git dir of the worktree so we're confirming that it points + // to the right git dir) + t.Views().Commits(). + Focus(). + Lines( + Contains("commit three").IsSelected(), + Contains("commit two"), + Contains("initial commit"), + ). + Press(keys.Commits.MoveDownCommit). + Lines( + Contains("commit two"), + Contains("commit three").IsSelected(), + Contains("initial commit"), + ). + // test that bisect works fine (same logic as above) + NavigateToLine(Contains("commit two")). + Press(keys.Commits.ViewBisectOptions). + Tap(func() { + t.ExpectPopup().Menu(). + Title(Equals("Bisect")). + Select(MatchesRegexp(`Mark .* as bad`)). + Confirm() + + t.Views().Information().Content(Contains("Bisecting")) + }). + NavigateToLine(Contains("initial commit")). + Press(keys.Commits.ViewBisectOptions). + Tap(func() { + t.ExpectPopup().Menu(). + Title(Equals("Bisect")). + Select(MatchesRegexp(`Mark .* as good`)). + Confirm() + + t.Views().Information().Content(Contains("Bisecting")) + }) + + // switch to other worktree t.Views().Worktrees(). Focus(). Lines( diff --git a/pkg/integration/tests/worktree/bisect.go b/pkg/integration/tests/worktree/bisect.go deleted file mode 100644 index 143f8114f..000000000 --- a/pkg/integration/tests/worktree/bisect.go +++ /dev/null @@ -1,88 +0,0 @@ -package worktree - -import ( - "github.com/jesseduffield/lazygit/pkg/config" - . "github.com/jesseduffield/lazygit/pkg/integration/components" -) - -// This is important because `git worktree list` will show a worktree being in a detached head state (which is true) -// when it's in the middle of a bisect, but it won't tell you about the branch it's on. -// Even so, if you attempt to check out that branch from another worktree git won't let you, so we need to -// keep track of the association ourselves. - -// not bothering to test the linked worktree here because it's the same logic as the rebase test - -var Bisect = NewIntegrationTest(NewIntegrationTestArgs{ - Description: "Verify that when you start a bisect in a linked worktree, Lazygit still associates the worktree with the branch", - ExtraCmdArgs: []string{}, - Skip: false, - SetupConfig: func(config *config.AppConfig) {}, - SetupRepo: func(shell *Shell) { - shell.NewBranch("mybranch") - shell.CreateFileAndAdd("README.md", "hello world") - shell.Commit("initial commit") - shell.EmptyCommit("commit 2") - shell.EmptyCommit("commit 3") - shell.AddWorktree("mybranch", "../linked-worktree", "newbranch") - }, - Run: func(t *TestDriver, keys config.KeybindingConfig) { - t.Views().Branches(). - Focus(). - Lines( - Contains("mybranch").IsSelected(), - Contains("newbranch (worktree)"), - ) - - // start a bisect on the main worktree - t.Views().Commits(). - Focus(). - SelectedLine(Contains("commit 3")). - Press(keys.Commits.ViewBisectOptions). - Tap(func() { - t.ExpectPopup().Menu(). - Title(Equals("Bisect")). - Select(MatchesRegexp(`Mark .* as bad`)). - Confirm() - - t.Views().Information().Content(Contains("Bisecting")) - }). - NavigateToLine(Contains("initial commit")). - Press(keys.Commits.ViewBisectOptions). - Tap(func() { - t.ExpectPopup().Menu(). - Title(Equals("Bisect")). - Select(MatchesRegexp(`Mark .* as good`)). - Confirm() - }) - - t.Views().Branches(). - Focus(). - // switch to linked worktree - NavigateToLine(Contains("newbranch")). - Press(keys.Universal.Select). - Tap(func() { - t.ExpectPopup().Confirmation(). - Title(Equals("Switch to worktree")). - Content(Equals("This branch is checked out by worktree linked-worktree. Do you want to switch to that worktree?")). - Confirm() - - t.Views().Information().Content(DoesNotContain("Bisecting")) - }). - Lines( - Contains("newbranch").IsSelected(), - Contains("mybranch (worktree)"), - ) - - // switch back to main worktree - t.Views().Branches(). - Focus(). - NavigateToLine(Contains("mybranch")). - Press(keys.Universal.Select). - Tap(func() { - t.ExpectPopup().Confirmation(). - Title(Equals("Switch to worktree")). - Content(Equals("This branch is checked out by worktree repo. Do you want to switch to that worktree?")). - Confirm() - }) - }, -}) diff --git a/pkg/integration/tests/worktree/rebase.go b/pkg/integration/tests/worktree/rebase.go deleted file mode 100644 index 8b91702b5..000000000 --- a/pkg/integration/tests/worktree/rebase.go +++ /dev/null @@ -1,89 +0,0 @@ -package worktree - -import ( - "github.com/jesseduffield/lazygit/pkg/config" - . "github.com/jesseduffield/lazygit/pkg/integration/components" -) - -// This is important because `git worktree list` will show a worktree being in a detached head state (which is true) -// when it's in the middle of a rebase, but it won't tell you about the branch it's on. -// Even so, if you attempt to check out that branch from another worktree git won't let you, so we need to -// keep track of the association ourselves. - -// We need different logic for associated the branch depending on whether it's a main worktree or -// linked worktree, so this test handles both. - -var Rebase = NewIntegrationTest(NewIntegrationTestArgs{ - Description: "Verify that when you start a rebase in a linked or main worktree, Lazygit still associates the worktree with the branch", - ExtraCmdArgs: []string{}, - Skip: false, - SetupConfig: func(config *config.AppConfig) {}, - SetupRepo: func(shell *Shell) { - shell.NewBranch("mybranch") - shell.CreateFileAndAdd("README.md", "hello world") - shell.Commit("initial commit") - shell.EmptyCommit("commit 2") - shell.EmptyCommit("commit 3") - shell.AddWorktree("mybranch", "../linked-worktree", "newbranch") - }, - Run: func(t *TestDriver, keys config.KeybindingConfig) { - t.Views().Branches(). - Focus(). - Lines( - Contains("mybranch").IsSelected(), - Contains("newbranch (worktree)"), - ) - - // start a rebase on the main worktree - t.Views().Commits(). - Focus(). - NavigateToLine(Contains("commit 2")). - Press(keys.Universal.Edit) - - t.Views().Information().Content(Contains("Rebasing")) - - t.Views().Branches(). - Focus(). - // switch to linked worktree - NavigateToLine(Contains("newbranch")). - Press(keys.Universal.Select). - Tap(func() { - t.ExpectPopup().Confirmation(). - Title(Equals("Switch to worktree")). - Content(Equals("This branch is checked out by worktree linked-worktree. Do you want to switch to that worktree?")). - Confirm() - - t.Views().Information().Content(DoesNotContain("Rebasing")) - }). - Lines( - Contains("newbranch").IsSelected(), - Contains("mybranch (worktree)"), - ) - - // start a rebase on the linked worktree - t.Views().Commits(). - Focus(). - NavigateToLine(Contains("commit 2")). - Press(keys.Universal.Edit) - - t.Views().Information().Content(Contains("Rebasing")) - - // switch back to main worktree - t.Views().Branches(). - Focus(). - NavigateToLine(Contains("mybranch")). - Press(keys.Universal.Select). - Tap(func() { - t.ExpectPopup().Confirmation(). - Title(Equals("Switch to worktree")). - Content(Equals("This branch is checked out by worktree repo. Do you want to switch to that worktree?")). - Confirm() - }). - Lines( - Contains("(no branch").IsSelected(), - Contains("mybranch"), - // even though the linked worktree is rebasing, we still associate it with the branch - Contains("newbranch (worktree)"), - ) - }, -}) -- cgit v1.2.3