MCPcopy
hub / github.com/dagger/container-use / Create

Method Create

repository/repository.go:185–239  ·  view source on GitHub ↗

Create creates a new environment with the given description, explanation, and optional git reference. The git reference can be HEAD (default), a SHA, a branch name, or a tag. Requires a dagger client for container operations during environment initialization.

(ctx context.Context, dag *dagger.Client, description, explanation, gitRef string)

Source from the content-addressed store, hash-verified

183// The git reference can be HEAD (default), a SHA, a branch name, or a tag.
184// Requires a dagger client for container operations during environment initialization.
185func (r *Repository) Create(ctx context.Context, dag *dagger.Client, description, explanation, gitRef string) (*environment.Environment, error) {
186 if gitRef == "" {
187 gitRef = "HEAD"
188 }
189 id := petname.Generate(2, "-")
190 worktree, err := r.initializeWorktree(ctx, id, gitRef)
191 if err != nil {
192 return nil, err
193 }
194
195 // Protect createInitialCommit to prevent concurrent writes to .git/worktrees/*/logs/HEAD
196 if err := r.lockManager.WithLock(ctx, LockTypeForkRepo, func() error {
197 return r.createInitialCommit(ctx, worktree, id, description)
198 }); err != nil {
199 return nil, fmt.Errorf("failed to create initial commit: %w", err)
200 }
201
202 worktreeHead, err := RunGitCommand(ctx, worktree, "rev-parse", "HEAD")
203 if err != nil {
204 return nil, err
205 }
206 worktreeHead = strings.TrimSpace(worktreeHead)
207
208 var baseSourceDir *dagger.Directory
209 err = r.lockManager.WithRLock(ctx, LockTypeForkRepo, func() error {
210 var err error
211 baseSourceDir, err = dag.
212 Host().
213 Directory(r.forkRepoPath, dagger.HostDirectoryOpts{NoCache: true}). // bust cache for each Create call
214 AsGit().
215 Ref(worktreeHead).
216 Tree(dagger.GitRefTreeOpts{DiscardGitDir: true}).
217 Sync(ctx) // don't bust cache when loading from state
218 return err
219 })
220 if err != nil {
221 return nil, fmt.Errorf("failed loading initial source directory: %w", err)
222 }
223
224 config := environment.DefaultConfig()
225 if err := config.Load(r.userRepoPath); err != nil {
226 return nil, err
227 }
228
229 env, err := environment.New(ctx, dag, id, description, config, baseSourceDir)
230 if err != nil {
231 return nil, err
232 }
233
234 if err := r.propagateToWorktree(ctx, env, explanation); err != nil {
235 return nil, err
236 }
237
238 return env, nil
239}
240
241// Get retrieves a full Environment with dagger client embedded for container operations.
242// Use this when you need to perform container operations like running commands, terminals, etc.

Callers 4

CreateEnvironmentMethod · 0.80
TestWeirdUserScenariosFunction · 0.80

Calls 9

initializeWorktreeMethod · 0.95
createInitialCommitMethod · 0.95
propagateToWorktreeMethod · 0.95
DefaultConfigFunction · 0.92
NewFunction · 0.92
RunGitCommandFunction · 0.85
LoadMethod · 0.80
WithLockMethod · 0.45
WithRLockMethod · 0.45

Tested by 2

TestWeirdUserScenariosFunction · 0.64