-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathrepo_diff.go
More file actions
136 lines (118 loc) · 3.64 KB
/
repo_diff.go
File metadata and controls
136 lines (118 loc) · 3.64 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package git
import (
"context"
"fmt"
"io"
)
// DiffOptions contains optional arguments for parsing diff.
//
// Docs: https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---full-index
type DiffOptions struct {
// The commit ID to used for computing diff between a range of commits (base,
// revision]. When not set, only computes diff for a single commit at revision.
Base string
CommandOptions
}
// Diff returns a parsed diff object between given commits of the repository.
func (r *Repository) Diff(ctx context.Context, rev string, maxFiles, maxFileLines, maxLineChars int, opts ...DiffOptions) (*Diff, error) {
var opt DiffOptions
if len(opts) > 0 {
opt = opts[0]
}
commit, err := r.CatFileCommit(ctx, rev)
if err != nil {
return nil, err
}
var args []string
if opt.Base == "" {
// First commit of repository
if commit.ParentsCount() == 0 {
args = []string{"show", "--full-index", "--end-of-options", rev}
} else {
c, err := commit.Parent(ctx, 0)
if err != nil {
return nil, err
}
args = []string{"diff", "--full-index", "-M", c.ID.String(), "--end-of-options", rev}
}
} else {
args = []string{"diff", "--full-index", "-M", opt.Base, "--end-of-options", rev}
}
stdout, w := io.Pipe()
done := make(chan SteamParseDiffResult)
go StreamParseDiff(stdout, done, maxFiles, maxFileLines, maxLineChars)
err = pipe(ctx, r.path, args, opt.Envs, w)
_ = w.Close() // Close writer to exit parsing goroutine
if err != nil {
return nil, err
}
result := <-done
return result.Diff, result.Err
}
// RawDiffFormat is the format of a raw diff.
type RawDiffFormat string
const (
RawDiffNormal RawDiffFormat = "diff"
RawDiffPatch RawDiffFormat = "patch"
)
// RawDiffOptions contains optional arguments for dumping a raw diff.
//
// Docs: https://git-scm.com/docs/git-format-patch
type RawDiffOptions struct {
CommandOptions
}
// RawDiff dumps diff of repository in given revision directly to given
// io.Writer.
func (r *Repository) RawDiff(ctx context.Context, rev string, diffType RawDiffFormat, w io.Writer, opts ...RawDiffOptions) error {
var opt RawDiffOptions
if len(opts) > 0 {
opt = opts[0]
}
commit, err := r.CatFileCommit(ctx, rev) //nolint
if err != nil {
return err
}
var args []string
switch diffType {
case RawDiffNormal:
if commit.ParentsCount() == 0 {
args = []string{"show", "--full-index", "--end-of-options", rev}
} else {
c, err := commit.Parent(ctx, 0)
if err != nil {
return err
}
args = []string{"diff", "--full-index", "-M", c.ID.String(), "--end-of-options", rev}
}
case RawDiffPatch:
if commit.ParentsCount() == 0 {
args = []string{"format-patch", "--full-index", "--no-signoff", "--no-signature", "--stdout", "--root", "--end-of-options", rev}
} else {
c, err := commit.Parent(ctx, 0)
if err != nil {
return err
}
args = []string{"format-patch", "--full-index", "--no-signoff", "--no-signature", "--stdout", "--end-of-options", rev + "..." + c.ID.String()}
}
default:
return fmt.Errorf("invalid diffType: %s", diffType)
}
if err = pipe(ctx, r.path, args, opt.Envs, w); err != nil {
return err
}
return nil
}
// DiffBinaryOptions contains optional arguments for producing binary patch.
type DiffBinaryOptions struct {
CommandOptions
}
// DiffBinary returns binary patch between base and head revisions that could be
// used for git-apply.
func (r *Repository) DiffBinary(ctx context.Context, base, head string, opts ...DiffBinaryOptions) ([]byte, error) {
var opt DiffBinaryOptions
if len(opts) > 0 {
opt = opts[0]
}
args := []string{"diff", "--full-index", "--binary", "--end-of-options", base, head}
return exec(ctx, r.path, args, opt.Envs)
}