diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2023-07-24 15:29:23 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2023-07-30 18:35:23 +1000 |
commit | b93b9dae888683bb36da141981842a85e8010ff2 (patch) | |
tree | a4027264e225ca24beb983acda504cacda54025e /pkg/commands | |
parent | 277142fc4b9db9d722b648efb29b6fa905b5fb36 (diff) |
Add worktree tests for removing/detaching
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/git_commands/git_command_builder.go | 18 | ||||
-rw-r--r-- | pkg/commands/git_commands/git_command_builder_test.go | 2 | ||||
-rw-r--r-- | pkg/commands/git_commands/submodule.go | 2 | ||||
-rw-r--r-- | pkg/commands/git_commands/worktree.go | 4 |
4 files changed, 18 insertions, 8 deletions
diff --git a/pkg/commands/git_commands/git_command_builder.go b/pkg/commands/git_commands/git_command_builder.go index 05d788b4c..78e0236a3 100644 --- a/pkg/commands/git_commands/git_command_builder.go +++ b/pkg/commands/git_commands/git_command_builder.go @@ -42,20 +42,30 @@ func (self *GitCommandBuilder) Config(value string) *GitCommandBuilder { return self } -func (self *GitCommandBuilder) RepoPath(value string) *GitCommandBuilder { +// the -C arg will make git do a `cd` to the directory before doing anything else +func (self *GitCommandBuilder) Dir(path string) *GitCommandBuilder { // repo path comes before the command - self.args = append([]string{"-C", value}, self.args...) + self.args = append([]string{"-C", path}, self.args...) return self } -func (self *GitCommandBuilder) WorktreePath(path string) *GitCommandBuilder { - // worktree path comes before the command +// Note, you may prefer to use the Dir method instead of this one +func (self *GitCommandBuilder) Worktree(path string) *GitCommandBuilder { + // worktree arg comes before the command self.args = append([]string{"--work-tree", path}, self.args...) return self } +// Note, you may prefer to use the Dir method instead of this one +func (self *GitCommandBuilder) GitDir(path string) *GitCommandBuilder { + // git dir arg comes before the command + self.args = append([]string{"--git-dir", path}, self.args...) + + return self +} + func (self *GitCommandBuilder) ToArgv() []string { return append([]string{"git"}, self.args...) } diff --git a/pkg/commands/git_commands/git_command_builder_test.go b/pkg/commands/git_commands/git_command_builder_test.go index becee087c..69d41854c 100644 --- a/pkg/commands/git_commands/git_command_builder_test.go +++ b/pkg/commands/git_commands/git_command_builder_test.go @@ -45,7 +45,7 @@ func TestGitCommandBuilder(t *testing.T) { expected: []string{"git", "-c", "user.email=bar", "-c", "user.name=foo", "push"}, }, { - input: NewGitCmd("push").RepoPath("a/b/c").ToArgv(), + input: NewGitCmd("push").Dir("a/b/c").ToArgv(), expected: []string{"git", "-C", "a/b/c", "push"}, }, } diff --git a/pkg/commands/git_commands/submodule.go b/pkg/commands/git_commands/submodule.go index ca3a23ef5..3d8602b9a 100644 --- a/pkg/commands/git_commands/submodule.go +++ b/pkg/commands/git_commands/submodule.go @@ -82,7 +82,7 @@ func (self *SubmoduleCommands) Stash(submodule *models.SubmoduleConfig) error { } cmdArgs := NewGitCmd("stash"). - RepoPath(submodule.Path). + Dir(submodule.Path). Arg("--include-untracked"). ToArgv() diff --git a/pkg/commands/git_commands/worktree.go b/pkg/commands/git_commands/worktree.go index 65ab152a7..e35b3a715 100644 --- a/pkg/commands/git_commands/worktree.go +++ b/pkg/commands/git_commands/worktree.go @@ -54,9 +54,9 @@ func (self *WorktreeCommands) Delete(worktreePath string, force bool) error { } func (self *WorktreeCommands) Detach(worktreePath string) error { - cmdArgs := NewGitCmd("checkout").Arg("--detach").ToArgv() + cmdArgs := NewGitCmd("checkout").Arg("--detach").GitDir(filepath.Join(worktreePath, ".git")).ToArgv() - return self.cmd.New(cmdArgs).SetWd(worktreePath).Run() + return self.cmd.New(cmdArgs).Run() } func (self *WorktreeCommands) IsCurrentWorktree(path string) bool { |