gitApplyPatchFromFile streams the patch to avoid loading it entirely into memory.
(ctx context.Context, dir string, patch *File)
| 1278 | |
| 1279 | // gitApplyPatchFromFile streams the patch to avoid loading it entirely into memory. |
| 1280 | func 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 | |
| 1333 | func initGitRepo(ctx context.Context, dir string) error { |
| 1334 | if err := runGit(ctx, dir, "init"); err != nil { |