-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.go
More file actions
88 lines (78 loc) · 2.62 KB
/
Copy pathgenerate.go
File metadata and controls
88 lines (78 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package patchapply
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
)
// GeneratePatch runs `git format-patch --stdout` in repoDir for the given
// commit range and returns the raw format-patch email bytes.
//
// commitRange can be any revision range accepted by git (e.g. "HEAD~1",
// "HEAD~3..HEAD", "origin/main..HEAD", a single commit hash, etc.).
// Pass "HEAD" to format the latest commit.
//
// The function shells out to git; the working directory must be a git
// repository. It does not modify the repository in any way.
func GeneratePatch(repoDir, commitRange string) ([]byte, error) {
if repoDir == "" {
return nil, fmt.Errorf("repoDir is required")
}
if commitRange == "" {
return nil, fmt.Errorf("commitRange is required")
}
// Validate that git is available.
if _, err := exec.LookPath("git"); err != nil {
return nil, fmt.Errorf("git not found in PATH: %w", err)
}
args := []string{"format-patch", "--stdout"}
// Handle ranges that contain ".." or multiple revs by passing as-is.
// For a single commit, add "-1" to format just that commit.
if !strings.Contains(commitRange, "..") && !strings.Contains(commitRange, " ") {
args = append(args, "-1", commitRange)
} else {
args = append(args, commitRange)
}
cmd := exec.Command("git", args...)
cmd.Dir = repoDir
cmd.Stderr = os.Stderr
var stdout bytes.Buffer
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("git format-patch failed: %w", err)
}
if stdout.Len() == 0 {
return nil, fmt.Errorf("git format-patch produced no output for range %q", commitRange)
}
return stdout.Bytes(), nil
}
// GeneratePatchSeries runs `git format-patch --stdout` for a range and returns
// the raw mbox bytes containing all patches in the series.
//
// Unlike GeneratePatch, this always treats the input as a range and does not
// add the "-1" flag, so multi-commit ranges produce a single mbox with all
// patches in order.
func GeneratePatchSeries(repoDir, commitRange string) ([]byte, error) {
if repoDir == "" {
return nil, fmt.Errorf("repoDir is required")
}
if commitRange == "" {
return nil, fmt.Errorf("commitRange is required")
}
if _, err := exec.LookPath("git"); err != nil {
return nil, fmt.Errorf("git not found in PATH: %w", err)
}
cmd := exec.Command("git", "format-patch", "--stdout", commitRange)
cmd.Dir = repoDir
cmd.Stderr = os.Stderr
var stdout bytes.Buffer
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("git format-patch failed: %w", err)
}
if stdout.Len() == 0 {
return nil, fmt.Errorf("git format-patch produced no output for range %q", commitRange)
}
return stdout.Bytes(), nil
}