internal/gitutil/merge.go

da232bfde4952959944a074ccec455f38eab3b66
gitbay/internal/gitutil/merge.go history · blame · raw

367 lines · 13932 bytes

  1package gitutil
  2
  3import (
  4	"bytes"
  5	"fmt"
  6	"os"
  7	"os/exec"
  8	"strings"
  9
 10	"gitbay.org/gitbay/internal/toolpath"
 11)
 12
 13// FetchInto copies srcRef from srcDir into dstDir as dstRef, forcing the
 14// update. Objects are copied, not shared — the destination owns everything
 15// afterward, which is what keeps MRs alive when their fork is deleted.
 16func FetchInto(dstDir, srcDir, srcRef, dstRef string) error {
 17	cmd := exec.Command(toolpath.Look("git"), "-C", dstDir, "fetch", "--quiet", "--no-write-fetch-head",
 18		srcDir, "+"+srcRef+":"+dstRef)
 19	if out, err := cmd.CombinedOutput(); err != nil {
 20		return fmt.Errorf("fetch %s from %s: %v\n%s", srcRef, srcDir, err, out)
 21	}
 22	return nil
 23}
 24
 25// UpdateRefCAS points ref at newSHA only if it currently points at oldSHA
 26// (empty oldSHA = must not exist). This is the compare-and-swap that makes
 27// merges safe against concurrent pushes.
 28func UpdateRefCAS(dir, ref, newSHA, oldSHA string) error {
 29	args := []string{"-C", dir, "update-ref", ref, newSHA}
 30	if oldSHA != "" {
 31		args = append(args, oldSHA)
 32	}
 33	cmd := exec.Command(toolpath.Look("git"), args...)
 34	if out, err := cmd.CombinedOutput(); err != nil {
 35		return fmt.Errorf("update-ref %s: %v\n%s", ref, err, out)
 36	}
 37	return nil
 38}
 39
 40func DeleteRef(dir, ref string) error {
 41	cmd := exec.Command(toolpath.Look("git"), "-C", dir, "update-ref", "-d", ref)
 42	if out, err := cmd.CombinedOutput(); err != nil {
 43		return fmt.Errorf("delete-ref %s: %v\n%s", ref, err, out)
 44	}
 45	return nil
 46}
 47
 48// RefExists reports whether ref is present, whatever it points at. Unlike
 49// ResolveRef it does not need the object to exist, so a ref left dangling
 50// by an interrupted prune still reads as present and gets deleted.
 51func RefExists(dir, ref string) bool {
 52	return exec.Command(toolpath.Look("git"), "-C", dir, "show-ref", "--verify", "--quiet", ref).Run() == nil
 53}
 54
 55// PruneNow repacks the repository and drops every unreachable object at
 56// once, instead of after git's two-week grace. For when a ref was deleted
 57// so that what it pointed at stops being fetchable by sha. Without the
 58// grace, a push whose objects have left quarantine but whose ref is not
 59// yet written can lose them; the window is milliseconds, and the one
 60// caller is an explicit admin command, not a timer.
 61func PruneNow(dir string) error {
 62	cmd := exec.Command(toolpath.Look("git"), "-C", dir, "gc", "--quiet", "--prune=now")
 63	if out, err := cmd.CombinedOutput(); err != nil {
 64		return fmt.Errorf("gc --prune=now: %v\n%s", err, out)
 65	}
 66	return nil
 67}
 68
 69// RevListRange returns commits in old..new, newest first.
 70func RevListRange(dir, old, new string) ([]string, error) {
 71	cmd := exec.Command(toolpath.Look("git"), "-C", dir, "rev-list", "--end-of-options", new, "^"+old)
 72	out, err := cmd.Output()
 73	if err != nil {
 74		return nil, fmt.Errorf("rev-list %s..%s: %w", old, new, err)
 75	}
 76	var shas []string
 77	for _, l := range strings.Split(strings.TrimSpace(string(out)), "\n") {
 78		if l != "" {
 79			shas = append(shas, l)
 80		}
 81	}
 82	return shas, nil
 83}
 84
 85// MergeTree performs a real merge of ours and theirs, returning the merged
 86// tree id. conflict=true means the merge cannot be done automatically.
 87func MergeTree(dir, ours, theirs string) (tree string, conflict bool, err error) {
 88	cmd := exec.Command(toolpath.Look("git"), "-C", dir, "merge-tree", "--write-tree", "--end-of-options", ours, theirs)
 89	out, runErr := cmd.Output()
 90	tree = strings.TrimSpace(strings.SplitN(string(out), "\n", 2)[0])
 91	if runErr != nil {
 92		if ee, ok := runErr.(*exec.ExitError); ok && ee.ExitCode() == 1 {
 93			return "", true, nil // conflicted merge
 94		}
 95		return "", false, fmt.Errorf("merge-tree: %w", runErr)
 96	}
 97	return tree, false, nil
 98}
 99
100// CommitTree creates a merge commit with the given parents, authored and
101// committed by the merging user. There is no server signing key by design.
102func CommitTree(dir, tree string, parents []string, name, email, message string) (string, error) {
103	args := []string{"-C", dir, "commit-tree", tree, "-m", message}
104	for _, p := range parents {
105		args = append(args, "-p", p)
106	}
107	cmd := exec.Command(toolpath.Look("git"), args...)
108	cmd.Env = append(os.Environ(),
109		"GIT_AUTHOR_NAME="+name, "GIT_AUTHOR_EMAIL="+email,
110		"GIT_COMMITTER_NAME="+name, "GIT_COMMITTER_EMAIL="+email,
111	)
112	out, err := cmd.Output()
113	if err != nil {
114		return "", fmt.Errorf("commit-tree: %w", err)
115	}
116	return strings.TrimSpace(string(out)), nil
117}
118
119// Diff returns the patch for old..new (three-dot semantics are the caller's
120// job: pass the merge base as old), cut at limit bytes on a line boundary;
121// truncated says whether it was cut, so the caller can say so instead of
122// rendering a hunk that ends mid-line (#117).
123func Diff(dir, old, new string, limit int64) (patch string, truncated bool, err error) {
124	cmd := exec.Command(toolpath.Look("git"), "-C", dir, "diff", "--stat", "--patch", "--end-of-options", old, new)
125	out, err := cmd.Output()
126	if err != nil {
127		return "", false, fmt.Errorf("diff: %w", err)
128	}
129	out, truncated = cutAtLine(out, limit)
130	return string(out), truncated, nil
131}
132
133// cutAtLine keeps at most limit bytes, ending on the last newline before
134// the limit.
135func cutAtLine(out []byte, limit int64) ([]byte, bool) {
136	if int64(len(out)) <= limit {
137		return out, false
138	}
139	cut := out[:limit]
140	if i := bytes.LastIndexByte(cut, '\n'); i >= 0 {
141		cut = cut[:i+1]
142	}
143	return cut, true
144}
145
146// PatchID identifies the change between old and new independently of the
147// commits carrying it: git patch-id --stable over the whole-range diff.
148// Two revisions with the same PatchID propose the same change, whatever
149// was rebased underneath. "" when the range has no diff.
150func PatchID(dir, old, new string) (string, error) {
151	diff := exec.Command(toolpath.Look("git"), "-C", dir, "diff", "--end-of-options", old, new)
152	pid := exec.Command(toolpath.Look("git"), "-C", dir, "patch-id", "--stable")
153	pipe, err := diff.StdoutPipe()
154	if err != nil {
155		return "", err
156	}
157	pid.Stdin = pipe
158	if err := diff.Start(); err != nil {
159		return "", fmt.Errorf("diff: %w", err)
160	}
161	out, err := pid.Output()
162	if werr := diff.Wait(); werr != nil {
163		return "", fmt.Errorf("diff: %w", werr)
164	}
165	if err != nil {
166		return "", fmt.Errorf("patch-id: %w", err)
167	}
168	fields := strings.Fields(string(out))
169	if len(fields) == 0 {
170		return "", nil
171	}
172	return fields[0], nil
173}
174
175// MergeBase returns the best common ancestor, or an error if none exists.
176func MergeBase(dir, a, b string) (string, error) {
177	cmd := exec.Command(toolpath.Look("git"), "-C", dir, "merge-base", "--end-of-options", a, b)
178	out, err := cmd.Output()
179	if err != nil {
180		return "", fmt.Errorf("no common history between %s and %s", a, b)
181	}
182	return strings.TrimSpace(string(out)), nil
183}
184
185// CommitFileChange writes content at path on branch as a new commit and
186// advances the branch with compare-and-swap. Used by web edits; hooks do not
187// run, so callers enforce policy themselves.
188func CommitFileChange(dir, branch, path string, content []byte, name, email, message string) (string, error) {
189	branchRef := "refs/heads/" + branch
190	parent, err := ResolveRef(dir, branchRef)
191	if err != nil {
192		return "", fmt.Errorf("branch %s: %w", branch, err)
193	}
194
195	// Hash the new blob.
196	hb := exec.Command(toolpath.Look("git"), "-C", dir, "hash-object", "-w", "--stdin")
197	hb.Stdin = strings.NewReader(string(content))
198	out, err := hb.Output()
199	if err != nil {
200		return "", fmt.Errorf("hash-object: %w", err)
201	}
202	blob := strings.TrimSpace(string(out))
203
204	// Stage the parent tree in a temporary index, splice the blob in, and
205	// write the new tree.
206	idx, err := os.CreateTemp("", "gitbay-index-*")
207	if err != nil {
208		return "", err
209	}
210	idx.Close()
211	defer os.Remove(idx.Name())
212	env := append(os.Environ(), "GIT_INDEX_FILE="+idx.Name())
213
214	rt := exec.Command(toolpath.Look("git"), "-C", dir, "read-tree", parent+"^{tree}")
215	rt.Env = env
216	if out, err := rt.CombinedOutput(); err != nil {
217		return "", fmt.Errorf("read-tree: %v\n%s", err, out)
218	}
219	ui := exec.Command(toolpath.Look("git"), "-C", dir, "update-index", "--add", "--cacheinfo", "100644,"+blob+","+path)
220	ui.Env = env
221	if out, err := ui.CombinedOutput(); err != nil {
222		return "", fmt.Errorf("update-index: %v\n%s", err, out)
223	}
224	wt := exec.Command(toolpath.Look("git"), "-C", dir, "write-tree")
225	wt.Env = env
226	out, err = wt.Output()
227	if err != nil {
228		return "", fmt.Errorf("write-tree: %w", err)
229	}
230	tree := strings.TrimSpace(string(out))
231
232	sha, err := CommitTree(dir, tree, []string{parent}, name, email, message)
233	if err != nil {
234		return "", err
235	}
236	if err := UpdateRefCAS(dir, branchRef, sha, parent); err != nil {
237		return "", fmt.Errorf("branch moved during edit; reload and retry: %w", err)
238	}
239	return sha, nil
240}
241
242// CommitParents returns the parent SHAs of a commit.
243func CommitParents(dir, sha string) ([]string, error) {
244	out, err := exec.Command(toolpath.Look("git"), "-C", dir, "rev-list", "--parents", "-n1", "--end-of-options", sha).Output()
245	if err != nil {
246		return nil, fmt.Errorf("rev-list --parents %s: %w", sha, err)
247	}
248	fields := strings.Fields(string(out))
249	if len(fields) < 1 {
250		return nil, fmt.Errorf("no output for %s", sha)
251	}
252	return fields[1:], nil
253}
254
255// AuthorIdent returns a commit's author name, email, and ISO date.
256func AuthorIdent(dir, sha string) (name, email, date string, err error) {
257	out, err := exec.Command(toolpath.Look("git"), "-C", dir, "log", "-1", "--format=%an%x1f%ae%x1f%aI", "--end-of-options", sha).Output()
258	if err != nil {
259		return "", "", "", fmt.Errorf("log %s: %w", sha, err)
260	}
261	parts := strings.SplitN(strings.TrimSpace(string(out)), "\x1f", 3)
262	if len(parts) != 3 {
263		return "", "", "", fmt.Errorf("bad ident for %s", sha)
264	}
265	return parts[0], parts[1], parts[2], nil
266}
267
268// CommitMessage returns a commit's full message.
269func CommitMessage(dir, sha string) (string, error) {
270	out, err := exec.Command(toolpath.Look("git"), "-C", dir, "log", "-1", "--format=%B", "--end-of-options", sha).Output()
271	if err != nil {
272		return "", fmt.Errorf("log %s: %w", sha, err)
273	}
274	return strings.TrimRight(string(out), "\n"), nil
275}
276
277// MergeTreeOnto replays commit's changes (relative to base) onto onto,
278// returning the resulting tree. conflict=true when it cannot apply cleanly.
279func MergeTreeOnto(dir, base, onto, commit string) (tree string, conflict bool, err error) {
280	cmd := exec.Command(toolpath.Look("git"), "-C", dir, "merge-tree", "--write-tree", "--merge-base="+base, "--end-of-options", onto, commit)
281	out, runErr := cmd.Output()
282	tree = strings.TrimSpace(strings.SplitN(string(out), "\n", 2)[0])
283	if runErr != nil {
284		if ee, ok := runErr.(*exec.ExitError); ok && ee.ExitCode() == 1 {
285			return "", true, nil
286		}
287		return "", false, fmt.Errorf("merge-tree: %w", runErr)
288	}
289	return tree, false, nil
290}
291
292// CommitTreeIdent creates a commit with distinct author and committer
293// identities. Empty authorDate means now.
294func CommitTreeIdent(dir, tree string, parents []string,
295	authorName, authorEmail, authorDate, committerName, committerEmail, message string) (string, error) {
296	args := []string{"-C", dir, "commit-tree", tree, "-m", message}
297	for _, p := range parents {
298		args = append(args, "-p", p)
299	}
300	cmd := exec.Command(toolpath.Look("git"), args...)
301	env := append(os.Environ(),
302		"GIT_AUTHOR_NAME="+authorName, "GIT_AUTHOR_EMAIL="+authorEmail,
303		"GIT_COMMITTER_NAME="+committerName, "GIT_COMMITTER_EMAIL="+committerEmail,
304	)
305	if authorDate != "" {
306		env = append(env, "GIT_AUTHOR_DATE="+authorDate)
307	}
308	cmd.Env = env
309	out, err := cmd.Output()
310	if err != nil {
311		return "", fmt.Errorf("commit-tree: %w", err)
312	}
313	return strings.TrimSpace(string(out)), nil
314}
315
316// ResolveTree returns the tree id of a commit.
317func ResolveTree(dir, sha string) (string, error) {
318	out, err := exec.Command(toolpath.Look("git"), "-C", dir, "rev-parse", "--verify", "--end-of-options", sha+"^{tree}").Output()
319	if err != nil {
320		return "", fmt.Errorf("rev-parse %s^{tree}: %w", sha, err)
321	}
322	return strings.TrimSpace(string(out)), nil
323}
324
325// DiffFiles lists the paths changed between old and new.
326func DiffFiles(dir, old, new string) ([]string, error) {
327	out, err := exec.Command(toolpath.Look("git"), "-C", dir, "diff", "--name-only", "--end-of-options", old, new).Output()
328	if err != nil {
329		return nil, fmt.Errorf("diff --name-only: %w", err)
330	}
331	var files []string
332	for _, l := range strings.Split(strings.TrimSpace(string(out)), "\n") {
333		if l != "" {
334			files = append(files, l)
335		}
336	}
337	return files, nil
338}
339
340// RangeDiff compares two revisions of the same work: what the commits
341// between oldBase and oldHead became between newBase and newHead. This is
342// what answers "what changed since I reviewed this", which a plain diff
343// of the two heads cannot — that shows the whole branch again, rebases
344// and all.
345//
346// Each side carries its own base, because the target moves: comparing
347// both revisions against today's base would attribute every commit that
348// landed on the target in between to the author of this merge request.
349//
350// --creation-factor is raised from git's default of 60. That default is
351// tuned for comparing two independently developed patch series, where
352// refusing to pair is the safe answer. Here the two sides are known to be
353// revisions of one branch, and the commonest revision of all — a commit
354// that adds a file, with one line inside it changed — is not paired at
355// 60: git reports the commit as deleted and a different one added, which
356// tells a reviewer nothing. It pairs at 80, and two genuinely unrelated
357// commits are still left unpaired there; both measured.
358func RangeDiff(dir, oldBase, oldHead, newBase, newHead string, limit int64) (patch string, truncated bool, err error) {
359	cmd := exec.Command(toolpath.Look("git"), "-C", dir, "range-diff", "--creation-factor=80", "--end-of-options",
360		oldBase+".."+oldHead, newBase+".."+newHead)
361	out, err := cmd.Output()
362	if err != nil {
363		return "", false, fmt.Errorf("range-diff: %w", err)
364	}
365	out, truncated = cutAtLine(out, limit)
366	return string(out), truncated, nil
367}