MCPcopy Index your code
hub / github.com/dagger/dagger / gitApplyPatchFromFile

Function gitApplyPatchFromFile

core/changeset.go:1280–1331  ·  view source on GitHub ↗

gitApplyPatchFromFile streams the patch to avoid loading it entirely into memory.

(ctx context.Context, dir string, patch *File)

Source from the content-addressed store, hash-verified

1278
1279// gitApplyPatchFromFile streams the patch to avoid loading it entirely into memory.
1280func gitApplyPatchFromFile(ctx context.Context, dir string, patch *File) error {
1281 if patch == nil {
1282 return nil
1283 }
1284
1285 patchRef, _ := patch.Snapshot.Peek()
1286 if patchRef == nil {
1287 return fmt.Errorf("evaluate patch ref: nil")
1288 }
1289 patchPathSelector, _ := patch.File.Peek()
1290
1291 return MountRef(ctx, patchRef, func(patchMount string, _ *mount.Mount) error {
1292 patchPath, err := containerdfs.RootPath(patchMount, patchPathSelector)
1293 if err != nil {
1294 return err
1295 }
1296
1297 // Check if patch file is empty
1298 info, err := os.Stat(patchPath)
1299 if err != nil {
1300 return fmt.Errorf("stat patch file: %w", err)
1301 }
1302 if info.Size() == 0 {
1303 return nil
1304 }
1305
1306 tempPatch := filepath.Join(dir, ".dagger-patch")
1307 srcFile, err := os.Open(patchPath)
1308 if err != nil {
1309 return fmt.Errorf("open patch file: %w", err)
1310 }
1311 defer srcFile.Close()
1312
1313 dstFile, err := os.Create(tempPatch)
1314 if err != nil {
1315 return fmt.Errorf("create temp patch file: %w", err)
1316 }
1317
1318 if _, err := io.Copy(dstFile, srcFile); err != nil {
1319 dstFile.Close()
1320 os.Remove(tempPatch)
1321 return fmt.Errorf("copy patch file: %w", err)
1322 }
1323 if err := dstFile.Close(); err != nil {
1324 os.Remove(tempPatch)
1325 return fmt.Errorf("close temp patch file: %w", err)
1326 }
1327
1328 defer os.Remove(tempPatch)
1329 return runGit(ctx, dir, "apply", "--allow-empty", tempPatch)
1330 }, mountRefAsReadOnly)
1331}
1332
1333func initGitRepo(ctx context.Context, dir string) error {
1334 if err := runGit(ctx, dir, "init"); err != nil {

Callers 1

Calls 10

runGitFunction · 0.85
PeekMethod · 0.80
MountRefFunction · 0.70
StatMethod · 0.65
SizeMethod · 0.65
OpenMethod · 0.65
CloseMethod · 0.65
RemoveMethod · 0.65
CreateMethod · 0.45
CopyMethod · 0.45

Tested by

no test coverage detected