git-module

git clone git://git.lin.moe/fork/git-module.git

 1package git
 2
 3import "context"
 4
 5// WorktreeAddOptions contains optional arguments for adding a worktree.
 6//
 7// Docs: https://git-scm.com/docs/git-worktree#Documentation/git-worktree.txt-add
 8type WorktreeAddOptions struct {
 9	// The new branch name to create and checkout in the worktree.
10	Branch string
11	CommandOptions
12}
13
14// WorktreeAdd creates a new worktree at the given path linked to this
15// repository. The commitIsh determines the HEAD of the new worktree.
16func (r *Repository) WorktreeAdd(ctx context.Context, path, commitIsh string, opts ...WorktreeAddOptions) error {
17	var opt WorktreeAddOptions
18	if len(opts) > 0 {
19		opt = opts[0]
20	}
21
22	args := []string{"worktree", "add"}
23	if opt.Branch != "" {
24		args = append(args, "-b", opt.Branch)
25	}
26	args = append(args, "--end-of-options", path, commitIsh)
27
28	_, err := exec(ctx, r.path, args, opt.Envs)
29	return err
30}
31
32// WorktreeRemoveOptions contains optional arguments for removing a worktree.
33//
34// Docs: https://git-scm.com/docs/git-worktree#Documentation/git-worktree.txt-remove
35type WorktreeRemoveOptions struct {
36	// Indicates whether to force removal even if the worktree is dirty.
37	Force bool
38	CommandOptions
39}
40
41// WorktreeRemove removes a worktree at the given path.
42func (r *Repository) WorktreeRemove(ctx context.Context, path string, opts ...WorktreeRemoveOptions) error {
43	var opt WorktreeRemoveOptions
44	if len(opts) > 0 {
45		opt = opts[0]
46	}
47
48	args := []string{"worktree", "remove"}
49	if opt.Force {
50		args = append(args, "--force")
51	}
52	args = append(args, "--end-of-options", path)
53
54	_, err := exec(ctx, r.path, args, opt.Envs)
55	return err
56}