1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/commands/git_commands/worktree.go

69 lines
1.5 KiB
Go
Raw Normal View History

2022-09-02 02:58:36 +02:00
package git_commands
2022-09-11 07:36:47 +02:00
import (
"errors"
"fmt"
"io/fs"
"log"
"os"
"github.com/jesseduffield/lazygit/pkg/commands/models"
)
2022-09-02 02:58:36 +02:00
type WorktreeCommands struct {
*GitCommon
}
func NewWorktreeCommands(gitCommon *GitCommon) *WorktreeCommands {
return &WorktreeCommands{
GitCommon: gitCommon,
}
}
2023-07-16 05:43:20 +02:00
func (self *WorktreeCommands) New(worktreePath string, committish string) error {
cmdArgs := NewGitCmd("worktree").Arg("add", worktreePath, committish).ToArgv()
2022-09-03 06:38:16 +02:00
return self.cmd.New(cmdArgs).Run()
}
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
func (self *WorktreeCommands) Detach(worktreePath string) error {
2023-07-16 10:12:26 +02:00
cmdArgs := NewGitCmd("checkout").Arg("--detach").ToArgv()
2023-07-16 10:12:26 +02:00
return self.cmd.New(cmdArgs).SetWd(worktreePath).Run()
}
2022-09-11 07:36:47 +02:00
func (self *WorktreeCommands) IsCurrentWorktree(w *models.Worktree) bool {
2023-07-16 06:23:31 +02:00
return IsCurrentWorktree(w)
}
func IsCurrentWorktree(w *models.Worktree) bool {
2022-09-11 07:36:47 +02:00
pwd, err := os.Getwd()
if err != nil {
log.Fatalln(err.Error())
}
2023-07-16 06:23:31 +02:00
return EqualPath(pwd, w.Path)
2022-09-11 07:36:47 +02:00
}
func (self *WorktreeCommands) IsWorktreePathMissing(w *models.Worktree) bool {
if _, err := os.Stat(w.Path); err != nil {
if errors.Is(err, fs.ErrNotExist) {
return true
}
log.Fatalln(fmt.Errorf("failed to check if worktree path `%s` exists\n%w", w.Path, err).Error())
}
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
}