2022-09-02 02:58:36 +02:00
|
|
|
package git_commands
|
|
|
|
|
2022-09-11 07:36:47 +02:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io/fs"
|
|
|
|
"log"
|
|
|
|
"os"
|
2023-07-17 06:38:08 +02:00
|
|
|
"path/filepath"
|
2023-07-17 07:22:14 +02:00
|
|
|
"strings"
|
2023-07-17 05:40:26 +02:00
|
|
|
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2022-09-11 07:36:47 +02:00
|
|
|
)
|
|
|
|
|
2022-09-02 02:58:36 +02:00
|
|
|
type WorktreeCommands struct {
|
|
|
|
*GitCommon
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWorktreeCommands(gitCommon *GitCommon) *WorktreeCommands {
|
|
|
|
return &WorktreeCommands{
|
|
|
|
GitCommon: gitCommon,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-16 11:39:53 +02:00
|
|
|
type NewWorktreeOpts struct {
|
|
|
|
// required. The path of the new worktree.
|
|
|
|
Path string
|
|
|
|
// required. The base branch/ref.
|
|
|
|
Base string
|
2022-09-03 06:38:16 +02:00
|
|
|
|
2023-07-16 11:39:53 +02:00
|
|
|
// 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()
|
2022-09-03 06:38:16 +02:00
|
|
|
}
|
|
|
|
|
2022-09-02 02:58:36 +02:00
|
|
|
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()
|
|
|
|
}
|
2022-09-11 07:36:47 +02:00
|
|
|
|
2023-07-16 09:52:07 +02:00
|
|
|
func (self *WorktreeCommands) Detach(worktreePath string) error {
|
2023-07-24 07:29:23 +02:00
|
|
|
cmdArgs := NewGitCmd("checkout").Arg("--detach").GitDir(filepath.Join(worktreePath, ".git")).ToArgv()
|
2023-07-16 09:52:07 +02:00
|
|
|
|
2023-07-24 07:29:23 +02:00
|
|
|
return self.cmd.New(cmdArgs).Run()
|
2023-07-16 09:52:07 +02:00
|
|
|
}
|
|
|
|
|
2023-07-16 11:39:53 +02:00
|
|
|
func (self *WorktreeCommands) IsCurrentWorktree(path string) bool {
|
|
|
|
return IsCurrentWorktree(path)
|
2023-07-16 06:23:31 +02:00
|
|
|
}
|
|
|
|
|
2023-07-16 11:39:53 +02:00
|
|
|
func IsCurrentWorktree(path string) bool {
|
2022-09-11 07:36:47 +02:00
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
2023-07-17 11:55:29 +02:00
|
|
|
return false
|
2022-09-11 07:36:47 +02:00
|
|
|
}
|
|
|
|
|
2023-07-16 11:39:53 +02:00
|
|
|
return EqualPath(pwd, path)
|
2022-09-11 07:36:47 +02:00
|
|
|
}
|
|
|
|
|
2023-07-16 11:39:53 +02:00
|
|
|
func (self *WorktreeCommands) IsWorktreePathMissing(path string) bool {
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
2022-09-11 07:36:47 +02:00
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
return true
|
|
|
|
}
|
2023-07-17 11:55:29 +02:00
|
|
|
self.Log.Errorf("failed to check if worktree path `%s` exists\n%v", path, err)
|
|
|
|
return false
|
2022-09-11 07:36:47 +02:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2023-07-16 06:23:31 +02:00
|
|
|
|
|
|
|
// checks if two paths are equal
|
|
|
|
// TODO: support relative paths
|
|
|
|
func EqualPath(a string, b string) bool {
|
|
|
|
return a == b
|
|
|
|
}
|
2023-07-17 05:40:26 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2023-07-17 06:38:08 +02:00
|
|
|
|
2023-07-17 14:03:51 +02:00
|
|
|
// If in a non-bare repo, this returns the path of the main worktree
|
|
|
|
// TODO: see if this works with a bare repo.
|
2023-07-17 07:22:14 +02:00
|
|
|
func GetCurrentRepoPath() string {
|
2023-07-17 06:38:08 +02:00
|
|
|
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 {
|
2023-07-17 11:55:29 +02:00
|
|
|
// fallback
|
|
|
|
return currentPath()
|
2023-07-17 06:38:08 +02:00
|
|
|
}
|
|
|
|
|
2023-07-17 07:22:14 +02:00
|
|
|
if gitFileInfo.IsDir() {
|
|
|
|
// must be in the main worktree
|
|
|
|
return currentPath()
|
|
|
|
}
|
2023-07-17 06:38:08 +02:00
|
|
|
|
2023-07-17 09:29:24 +02:00
|
|
|
// either in a submodule, a worktree, or a bare repo
|
2023-07-17 14:03:51 +02:00
|
|
|
worktreeGitPath, ok := LinkedWorktreeGitPath(pwd)
|
2023-07-17 07:22:14 +02:00
|
|
|
if !ok {
|
|
|
|
// fallback
|
|
|
|
return currentPath()
|
2023-07-17 06:38:08 +02:00
|
|
|
}
|
|
|
|
|
2023-07-17 09:29:24 +02:00
|
|
|
// 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))
|
2023-07-17 07:22:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetCurrentRepoName() string {
|
|
|
|
return filepath.Base(GetCurrentRepoPath())
|
2023-07-17 06:38:08 +02:00
|
|
|
}
|
|
|
|
|
2023-07-17 07:22:14 +02:00
|
|
|
func currentPath() string {
|
2023-07-17 06:38:08 +02:00
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err.Error())
|
|
|
|
}
|
2023-07-17 07:22:14 +02:00
|
|
|
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
|
2023-07-17 09:06:39 +02:00
|
|
|
|
|
|
|
// ensure the directory exists
|
|
|
|
_, err := os.Stat(worktreePath)
|
|
|
|
if err != nil {
|
2023-07-17 09:29:24 +02:00
|
|
|
return result
|
2023-07-17 09:06:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = filepath.Walk(worktreePath, func(path string, info fs.FileInfo, err error) error {
|
2023-07-17 07:22:14 +02:00
|
|
|
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 {
|
2023-07-17 09:29:24 +02:00
|
|
|
return result
|
2023-07-17 07:22:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2023-07-17 06:38:08 +02:00
|
|
|
}
|