diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2023-08-07 22:08:12 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2023-08-07 22:40:53 +1000 |
commit | 595e28d335c9874eb56f7de61a85586567cd8f8c (patch) | |
tree | 7eae43fd38f1888bd01c126c33f78fbc4b445cec /pkg/commands | |
parent | 0551f29de99d1d9fe2640aaa71ce2adf76896f25 (diff) |
Support bare worktrees where worktree does not have its own .git file
This was on oversight on my part: I assumed that the --work-tree arg was
always intended for use with linked worktrees which have a .git file
pointing back to the repo.
I'm honestly confused now: seems like there are three kinds of worktrees:
* the main worktree of a non-bare repo
* a linked worktree (with its own gitdir in the repo's worktrees/ dir)
* a random folder which you specify as a worktree with the --work-tree arg
I'm pretty sure the --work-tree arg is only intended to be used with this
third kind or workree
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/git_commands/repo_paths.go | 22 | ||||
-rw-r--r-- | pkg/commands/git_commands/worktree_loader.go | 2 |
2 files changed, 19 insertions, 5 deletions
diff --git a/pkg/commands/git_commands/repo_paths.go b/pkg/commands/git_commands/repo_paths.go index 46fcb5d10..6921c21b6 100644 --- a/pkg/commands/git_commands/repo_paths.go +++ b/pkg/commands/git_commands/repo_paths.go @@ -91,10 +91,24 @@ func getRepoPathsAux( if err != nil { return nil, errors.Errorf("failed to get repo git dir path: %v", err) } - worktreeGitDirPath, err := worktreeGitDirPath(fs, currentPath) - if err != nil { - return nil, errors.Errorf("failed to get worktree git dir path: %v", err) + + var worktreeGitDirPath string + if env.GetWorkTreeEnv() != "" { + // This env is set when you pass --work-tree to lazygit. In that case, + // we're not dealing with a linked work-tree, we're dealing with a 'specified' + // worktree (for lack of a better term). In this case, the worktree has no + // .git file and it just contains a bunch of files: it has no idea it's + // pointed to by a bare repo. As such it does not have its own git dir within + // the bare repo's git dir. Instead, we just use the bare repo's git dir. + worktreeGitDirPath = repoGitDirPath + } else { + var err error + worktreeGitDirPath, err = getWorktreeGitDirPath(fs, currentPath) + if err != nil { + return nil, errors.Errorf("failed to get worktree git dir path: %v", err) + } } + repoName := path.Base(repoPath) return &RepoPaths{ @@ -110,7 +124,7 @@ func getRepoPathsAux( // Returns the path of the git-dir for the worktree. For linked worktrees, the worktree has // a .git file that points to the git-dir (which itself lives in the git-dir // of the repo) -func worktreeGitDirPath(fs afero.Fs, worktreePath string) (string, error) { +func getWorktreeGitDirPath(fs afero.Fs, worktreePath string) (string, error) { // if .git is a file, we're in a linked worktree, otherwise we're in // the main worktree dotGitPath := path.Join(worktreePath, ".git") diff --git a/pkg/commands/git_commands/worktree_loader.go b/pkg/commands/git_commands/worktree_loader.go index 687e9680a..0d5f01e34 100644 --- a/pkg/commands/git_commands/worktree_loader.go +++ b/pkg/commands/git_commands/worktree_loader.go @@ -58,7 +58,7 @@ func (self *WorktreeLoader) GetWorktrees() ([]*models.Worktree, error) { isPathMissing := self.pathExists(path) var gitDir string - gitDir, err := worktreeGitDirPath(self.Fs, path) + gitDir, err := getWorktreeGitDirPath(self.Fs, path) if err != nil { self.Log.Warnf("Could not find git dir for worktree %s: %v", path, err) } |