MCPcopy Create free account
hub / github.com/celer-pkg/celer / ApplyPatch

Function ApplyPatch

pkgs/git/control.go:382–460  ·  view source on GitHub ↗

ApplyPatch apply git patch.

(nameVersion, repoDir, patchFile string)

Source from the content-addressed store, hash-verified

380
381// ApplyPatch apply git patch.
382func ApplyPatch(nameVersion, repoDir, patchFile string) error {
383 if !fileio.PathExists(repoDir) {
384 return errors.ErrDirNotExist
385 }
386 if !fileio.PathExists(filepath.Join(repoDir, ".git")) {
387 return errors.ErrNotGitDir
388 }
389
390 patchFileName := filepath.Base(patchFile)
391
392 // Check if patched already.
393 recordFilePath := filepath.Join(repoDir, ".patched")
394 if pathExists(recordFilePath) {
395 bytes, err := os.ReadFile(recordFilePath)
396 if err != nil {
397 return fmt.Errorf("failed to read .patched for '%s' -> %w", nameVersion, err)
398 }
399
400 lines := strings.Split(string(bytes), "\n")
401 if slices.Contains(lines, patchFileName) {
402 return nil
403 }
404 }
405
406 // Read the first few lines of the file to check for Git patch features.
407 file, err := os.Open(patchFile)
408 if err != nil {
409 return err
410 }
411 defer file.Close()
412
413 var gitBatch bool
414 scanner := bufio.NewScanner(file)
415 for range 10 {
416 if !scanner.Scan() {
417 break
418 }
419 line := scanner.Text()
420
421 // If you find Git patch features such as "From " or "Subject: "
422 if strings.HasPrefix(line, "diff --git ") {
423 gitBatch = true
424 break
425 }
426 }
427 if err := scanner.Err(); err != nil {
428 return err
429 }
430
431 if gitBatch {
432 title := fmt.Sprintf("[apply patch: %s]", nameVersion)
433 args := []string{"apply", "--ignore-space-change", "--ignore-whitespace", "-v", patchFile}
434 executor := cmd.NewExecutor(title, "git", args...)
435 executor.SetWorkDir(repoDir)
436 if output, err := executor.ExecuteOutputLive(); err != nil {
437 return fmt.Errorf("failed to apply patch for '%s' -> %s -> %w", nameVersion, output, err)
438 }
439 } else {

Callers 1

ApplyPatchesMethod · 0.92

Calls 6

PathExistsFunction · 0.92
NewExecutorFunction · 0.92
SprintfMethod · 0.80
SetWorkDirMethod · 0.80
ExecuteOutputLiveMethod · 0.80
pathExistsFunction · 0.70

Tested by

no test coverage detected