mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-21 00:30:00 +02:00
.devcontainer
.github
.vscode
cmd
demo
docs
pkg
app
cheatsheet
commands
git_commands
bisect.go
bisect_info.go
blame.go
branch.go
branch_loader.go
branch_loader_test.go
branch_test.go
commit.go
commit_file_loader.go
commit_file_loader_test.go
commit_loader.go
commit_loader_test.go
commit_test.go
common.go
config.go
custom.go
deps_test.go
diff.go
file.go
file_loader.go
file_loader_test.go
file_test.go
flow.go
flow_test.go
git_command_builder.go
git_command_builder_test.go
main_branches.go
patch.go
rebase.go
rebase_test.go
reflog_commit_loader.go
reflog_commit_loader_test.go
remote.go
remote_loader.go
repo_paths.go
repo_paths_test.go
stash.go
stash_loader.go
stash_loader_test.go
stash_test.go
status.go
submodule.go
sync.go
sync_test.go
tag.go
tag_loader.go
tag_loader_test.go
version.go
version_test.go
working_tree.go
working_tree_test.go
worktree.go
worktree_loader.go
worktree_loader_test.go
git_config
hosting_service
models
oscommands
patch
testdata
git.go
git_cmd_obj_builder.go
git_cmd_obj_runner.go
common
config
constants
env
fakes
gui
i18n
integration
jsonschema
logs
snake
tasks
theme
updates
utils
schema
scripts
test
vendor
.codespellrc
.editorconfig
.gitattributes
.gitignore
.golangci.yml
.goreleaser.yml
CODE-OF-CONDUCT.md
CONTRIBUTING.md
Dockerfile
LICENSE
Makefile
README.md
VISION.md
go.mod
go.sum
main.go
There are quite a few paths you might want to get e.g. the repo's path, the worktree's path, the repo's git dir path, the worktree's git dir path. I want these all obtained once and then used when needed rather than having to have IO whenever we need them. This is not so much about reducing time spent on IO as it is about not having to care about errors every time we want a path.
75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
package git_commands
|
|
|
|
import (
|
|
"path/filepath"
|
|
|
|
"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").GitDir(filepath.Join(worktreePath, ".git")).ToArgv()
|
|
|
|
return self.cmd.New(cmdArgs).Run()
|
|
}
|
|
|
|
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 !worktree.IsCurrent
|
|
}
|