MCPcopy Index your code
hub / github.com/dagger/container-use / initializeWorktree

Method initializeWorktree

repository/git.go:121–161  ·  view source on GitHub ↗

initializeWorktree initializes a new worktree for environment creation. It pushes the specified gitRef to create a new branch with the given id, then creates a worktree from that branch.

(ctx context.Context, id, gitRef string)

Source from the content-addressed store, hash-verified

119// initializeWorktree initializes a new worktree for environment creation.
120// It pushes the specified gitRef to create a new branch with the given id, then creates a worktree from that branch.
121func (r *Repository) initializeWorktree(ctx context.Context, id, gitRef string) (string, error) {
122 if gitRef == "" {
123 gitRef = "HEAD"
124 }
125
126 worktreePath, err := r.WorktreePath(id)
127 if err != nil {
128 return "", err
129 }
130
131 slog.Info("Initializing new worktree", "repository", r.userRepoPath, "environment-id", id, "from-ref", gitRef)
132
133 return worktreePath, r.lockManager.WithLock(ctx, LockTypeForkRepo, func() error {
134 resolvedRef, err := RunGitCommand(ctx, r.userRepoPath, "rev-parse", gitRef)
135 if err != nil {
136 return err
137 }
138 resolvedRef = strings.TrimSpace(resolvedRef)
139
140 _, err = RunGitCommand(ctx, r.userRepoPath, "push", containerUseRemote, fmt.Sprintf("%s:refs/heads/%s", resolvedRef, id))
141 if err != nil {
142 // Retry once on failure
143 _, err = RunGitCommand(ctx, r.userRepoPath, "push", containerUseRemote, fmt.Sprintf("%s:refs/heads/%s", resolvedRef, id))
144 if err != nil {
145 return err
146 }
147 }
148
149 _, err = RunGitCommand(ctx, r.forkRepoPath, "worktree", "add", worktreePath, id)
150 if err != nil {
151 return err
152 }
153
154 _, err = RunGitCommand(ctx, r.userRepoPath, "fetch", containerUseRemote, id)
155 if err != nil {
156 return err
157 }
158
159 return nil
160 })
161}
162
163// getWorktree gets or recreates a worktree for an existing environment.
164// It assumes the environment branch already exists in the forkRepo and will fail if it doesn't.

Callers 1

CreateMethod · 0.95

Calls 4

WorktreePathMethod · 0.95
RunGitCommandFunction · 0.85
InfoMethod · 0.80
WithLockMethod · 0.45

Tested by

no test coverage detected