summaryrefslogtreecommitdiffstats
path: root/pkg/commands/git_commands/worktree.go
blob: 65ab152a7c8e795d2739edd40c3689c568c55ce1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package git_commands

import (
	"errors"
	"io/fs"
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/jesseduffield/lazygit/pkg/commands/models"
)

type WorktreeCommands struct {
	*GitCommon
}

func NewWorktreeCommands(gitCommon *GitCommon) *WorktreeCommands {
	return &WorktreeCommands{
		GitCommon: gitCommon,
	}
}

type NewWorktreeOpts struct {
	// required. The path of the new worktree.
	Path string
	// required. The base branch/ref.
	Base string

	// if true, ends up with a detached head
	Detach bool

	// optional. if empty, and if detach is false, we will checkout the base
	Branch string
}

func (self *WorktreeCommands) New(opts NewWorktreeOpts) error {
	if opts.Detach && opts.Branch != "" {
		panic("cannot specify branch when detaching")
	}

	cmdArgs := NewGitCmd("worktree").Arg("add").
		ArgIf(opts.Detach, "--detach").
		ArgIf(opts.Branch != "", "-b", opts.Branch).
		Arg(opts.Path, opts.Base)

	return self.cmd.New(cmdArgs.ToArgv()).Run()
}

func (self *WorktreeCommands) Delete(worktreePath string, force bool) error {
	cmdArgs := NewGitCmd("worktree").Arg("remove").ArgIf(force, "-f").Arg(worktreePath).ToArgv()

	return self.cmd.New(cmdArgs).Run()
}

func (self *WorktreeCommands) Detach(worktreePath string) error {
	cmdArgs := NewGitCmd("checkout").Arg("--detach").ToArgv()

	return self.cmd.New(cmdArgs).SetWd(worktreePath).Run()
}

func (self *WorktreeCommands) IsCurrentWorktree(path string) bool {
	return IsCurrentWorktree(path)
}

func IsCurrentWorktree(path string) bool {
	pwd, err := os.Getwd()
	if err != nil {
		return false
	}

	return EqualPath(pwd, path)
}

func (self *WorktreeCommands) IsWorktreePathMissing(path string) bool {
	if _, err := os.Stat(path); err != nil {
		if errors.Is(err, fs.ErrNotExist) {
			return true
		}
		self.Log.Errorf("failed to check if worktree path `%s` exists\n%v", path, err)
		return false
	}
	return false
}

// checks if two paths are equal
// TODO: support relative paths
func EqualPath(a string, b string) bool {
	return a == b
}

func WorktreeForBranch(branch *models.Branch, worktrees []*models.Worktree) (*models.Worktree, bool) {
	for _, worktree := range worktrees {
		if worktree.Branch == branch.Name {
			return worktree, true
		}
	}

	return nil, false
}

func CheckedOutByOtherWorktree(branch *models.Branch, worktrees []*models.Worktree) bool {
	worktree, ok := WorktreeForBranch(branch, worktrees)
	if !ok {
		return false
	}

	return !IsCurrentWorktree(worktree.Path)
}

// If in a non-bare repo, this returns the path of the main worktree
// TODO: see if this works with a bare repo.
func GetCurrentRepoPath() string {
	pwd, err := os.Getwd()
	if err != nil {
		log.Fatalln(err.Error())
	}

	// check if .git is a file or a directory
	gitPath := filepath.Join(pwd, ".git")
	gitFileInfo, err := os.Stat(gitPath)
	if err != nil {
		// fallback
		return currentPath()
	}

	if gitFileInfo.IsDir() {
		// must be in the main worktree
		return currentPath()
	}

	// either in a submodule, a worktree, or a bare repo
	worktreeGitPath, ok := LinkedWorktreeGitPath(pwd)
	if !ok {
		// fallback
		return currentPath()
	}

	// confirm whether the next directory up is the 'worktrees' directory
	parent := filepath.Dir(worktreeGitPath)
	if filepath.Base(parent) != "worktrees" {
		// fallback
		return currentPath()
	}

	// now we just jump up two more directories to get the repo name
	return filepath.Dir(filepath.Dir(parent))
}

func GetCurrentRepoName() string {
	return filepath.Base(GetCurrentRepoPath())
}

func currentPath() string {
	pwd, err := os.Getwd()
	if err != nil {
		log.Fatalln(err.Error())
	}
	return pwd
}

func linkedWortkreePaths() []string {
	// first we need to get the repo dir
	repoPath := GetCurrentRepoPath()
	result := []string{}
	worktreePath := filepath.Join(repoPath, ".git", "worktrees")
	// for each directory in this path we're going to cat the `gitdir` file and append its contents to our result

	// ensure the directory exists
	_, err := os.Stat(worktreePath)
	if err != nil {
		return result
	}

	err = filepath.Walk(worktreePath, func(path string, info fs.FileInfo, err error) error {
		if info.IsDir() {
			gitDirPath := filepath.Join(path, "gitdir")
			gitDirBytes, err := os.ReadFile(gitDirPath)
			if err != nil {
				// ignoring error
				return nil
			}
			trimmedGitDir := strings.TrimSpace(string(gitDirBytes))
			// removing the .git part
			worktreeDir := filepath.Dir(trimmedGitDir)
			result = append(result, worktreeDir)
		}
		return nil
	})
	if err != nil {
		return result
	}

	return result
}