aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/vcs/git.go
blob: 8a51b3cfb71ce8c94f013e4a3f533d4a6fc649c8 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
// Copyright 2017 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package vcs

import (
	"bufio"
	"bytes"
	"errors"
	"fmt"
	"net/mail"
	"os"
	"os/exec"
	"regexp"
	"sort"
	"strings"
	"time"

	"github.com/google/syzkaller/pkg/debugtracer"
	"github.com/google/syzkaller/pkg/hash"
	"github.com/google/syzkaller/pkg/log"
	"github.com/google/syzkaller/pkg/osutil"
)

type gitRepo struct {
	*Git
}

func newGitRepo(dir string, ignoreCC map[string]bool, opts []RepoOpt) *gitRepo {
	git := &gitRepo{
		Git: &Git{
			Dir:      dir,
			Sandbox:  true,
			Env:      filterEnv(),
			ignoreCC: ignoreCC,
		},
	}
	for _, opt := range opts {
		switch opt {
		case OptPrecious:
			git.precious = true
		case OptDontSandbox:
			git.Sandbox = false
		}
	}
	return git
}

func filterEnv() []string {
	// We have to filter various git environment variables - if
	// these variables are set (e.g. if a test is being run as
	// part of a rebase) we're going to be acting on some other
	// repository (e.g the syzkaller tree itself) rather than the
	// intended repo.
	env := os.Environ()
	for i := 0; i < len(env); i++ {
		if strings.HasPrefix(env[i], "GIT_DIR") ||
			strings.HasPrefix(env[i], "GIT_WORK_TREE") ||
			strings.HasPrefix(env[i], "GIT_INDEX_FILE") ||
			strings.HasPrefix(env[i], "GIT_OBJECT_DIRECTORY") {
			env = append(env[:i], env[i+1:]...)
			i--
		}
	}

	return env
}

func (git *gitRepo) Poll(repo, branch string) (*Commit, error) {
	git.Reset()
	origin, err := git.Run("remote", "get-url", "origin")
	if err != nil || strings.TrimSpace(string(origin)) != repo {
		// The repo is here, but it has wrong origin (e.g. repo in config has changed), re-clone.
		if err := git.clone(repo, branch); err != nil {
			return nil, err
		}
	}
	// Use origin/branch for the case the branch was force-pushed,
	// in such case branch is not the same is origin/branch and we will
	// stuck with the local version forever (git checkout won't fail).
	if _, err := git.Run("checkout", "origin/"+branch); err != nil {
		// No such branch (e.g. branch in config has changed), re-clone.
		if err := git.clone(repo, branch); err != nil {
			return nil, err
		}
	}
	if output, err := git.Run("fetch", "--force"); err != nil {
		if git.isNetworkError(output) {
			// The clone operation will fail as well, so no sense to re-clone.
			return nil, err
		}
		if err := git.clone(repo, branch); err != nil {
			return nil, err
		}
	}
	if _, err := git.Run("checkout", "origin/"+branch); err != nil {
		return nil, err
	}
	if _, err := git.Run("submodule", "update", "--init"); err != nil {
		return nil, err
	}
	return git.Commit(HEAD)
}

func (git *gitRepo) isNetworkError(output []byte) bool {
	// The list is not exhaustive and is meant to be extended over time.
	return bytes.Contains(output, []byte("fatal: read error: Connection reset by peer"))
}

func (git *gitRepo) CheckoutBranch(repo, branch string) (*Commit, error) {
	if err := git.repair(); err != nil {
		return nil, err
	}
	repoHash := hash.String([]byte(repo))
	// Because the HEAD is detached, submodules assumes "origin" to be the default
	// remote when initializing.
	// This sets "origin" to be the current remote.
	// Ignore errors as we can double add or remove the same remote and that will fail.
	git.Run("remote", "rm", "origin")
	git.Run("remote", "add", "origin", repo)
	git.Run("remote", "add", repoHash, repo)
	_, err := git.Run("fetch", "--force", repoHash, branch)
	if err != nil {
		return nil, err
	}
	if _, err := git.Run("checkout", "FETCH_HEAD", "--force"); err != nil {
		return nil, err
	}
	if _, err := git.Run("submodule", "update", "--init"); err != nil {
		return nil, err
	}
	// If the branch checkout had to be "forced" the directory may
	// contain remaining untracked files.
	// Clean again to ensure the new branch is in a clean state.
	if err := git.repair(); err != nil {
		return nil, err
	}
	return git.Commit(HEAD)
}

func (git *gitRepo) CheckoutCommit(repo, commit string) (*Commit, error) {
	if err := git.repair(); err != nil {
		return nil, err
	}
	if err := git.fetchRemote(repo, commit); err != nil {
		return nil, err
	}
	return git.SwitchCommit(commit)
}

func (git *gitRepo) fetchRemote(repo, commit string) error {
	repoHash := hash.String([]byte(repo))
	// Ignore error as we can double add the same remote and that will fail.
	git.Run("remote", "add", repoHash, repo)
	fetchArgs := []string{"fetch", "--force", "--tags", repoHash}
	if commit != "" && gitFullHashRe.MatchString(commit) {
		// This trick only works with full commit hashes.
		fetchArgs = append(fetchArgs, commit)
	}
	_, err := git.Run(fetchArgs...)
	if err != nil {
		var verbose *osutil.VerboseError
		if errors.As(err, &verbose) &&
			bytes.Contains(verbose.Output, []byte("error: cannot lock ref")) {
			// It can happen that the fetched repo has tags names that conflict
			// with the ones already present in the repository.
			// Try to fetch more, but this time prune tags, it should help.
			// The --prune-tags option will remove all tags that are not present
			// in this remote repo, so don't do it always. Only when necessary.
			_, err = git.Run("fetch", "--force", "--tags", "--prune", "--prune-tags", repoHash)
		}
	}
	return err
}

func (git *gitRepo) SwitchCommit(commit string) (*Commit, error) {
	if !git.precious {
		git.Run("reset", "--hard")
		git.Run("clean", "-fdx")
	}
	if _, err := git.Run("checkout", commit); err != nil {
		return nil, err
	}
	if _, err := git.Run("submodule", "update", "--init"); err != nil {
		return nil, err
	}
	return git.Commit(HEAD)
}

func (git *gitRepo) clone(repo, branch string) error {
	if git.precious {
		return fmt.Errorf("won't reinit precious repo")
	}
	if err := git.initRepo(nil); err != nil {
		return err
	}
	if _, err := git.Run("remote", "add", "origin", repo); err != nil {
		return err
	}
	if _, err := git.Run("fetch", "origin", branch); err != nil {
		return err
	}
	return nil
}

func (git *gitRepo) repair() error {
	if err := git.Reset(); err != nil {
		return git.initRepo(err)
	}
	return nil
}

func (git *gitRepo) initRepo(reason error) error {
	if reason != nil {
		log.Logf(1, "git: initializing repo at %v: %v", git.Dir, reason)
	}
	if err := os.RemoveAll(git.Dir); err != nil {
		return fmt.Errorf("failed to remove repo dir: %w", err)
	}
	if err := osutil.MkdirAll(git.Dir); err != nil {
		return fmt.Errorf("failed to create repo dir: %w", err)
	}
	if git.Sandbox {
		if err := osutil.SandboxChown(git.Dir); err != nil {
			return err
		}
	}
	if _, err := git.Run("init"); err != nil {
		return err
	}
	return nil
}

func (git *gitRepo) Contains(commit string) (bool, error) {
	return git.containedIn(HEAD, commit)
}

const gitDateFormat = "Mon Jan 2 15:04:05 2006 -0700"

func gitParseCommit(output, user, domain []byte, ignoreCC map[string]bool) (*Commit, error) {
	lines := bytes.Split(output, []byte{'\n'})
	if len(lines) < 8 || len(lines[0]) != 40 {
		return nil, fmt.Errorf("unexpected git log output: %q", output)
	}
	date, err := time.Parse(gitDateFormat, string(lines[4]))
	if err != nil {
		return nil, fmt.Errorf("failed to parse date in git log output: %w\n%q", err, output)
	}
	commitDate, err := time.Parse(gitDateFormat, string(lines[6]))
	if err != nil {
		return nil, fmt.Errorf("failed to parse date in git log output: %w\n%q", err, output)
	}
	recipients := make(map[string]bool)
	recipients[strings.ToLower(string(lines[2]))] = true
	var tags []string
	// Use summary line + all description lines.
	for _, line := range append([][]byte{lines[1]}, lines[7:]...) {
		if user != nil {
			userPos := bytes.Index(line, user)
			if userPos != -1 {
				domainPos := bytes.Index(line[userPos+len(user)+1:], domain)
				if domainPos != -1 {
					startPos := userPos + len(user)
					endPos := userPos + len(user) + domainPos + 1
					tag := string(line[startPos:endPos])
					present := false
					for _, tag1 := range tags {
						if tag1 == tag {
							present = true
							break
						}
					}
					if !present {
						tags = append(tags, tag)
					}
				}
			}
		}
		for _, re := range ccRes {
			matches := re.FindSubmatchIndex(line)
			if matches == nil {
				continue
			}
			addr, err := mail.ParseAddress(string(line[matches[2]:matches[3]]))
			if err != nil {
				break
			}
			email := strings.ToLower(addr.Address)
			if ignoreCC[email] {
				continue
			}
			recipients[email] = true
			break
		}
	}
	sortedRecipients := make(Recipients, 0, len(recipients))
	for addr := range recipients {
		sortedRecipients = append(sortedRecipients, RecipientInfo{mail.Address{Address: addr}, To})
	}
	sort.Sort(sortedRecipients)
	parents := strings.Split(string(lines[5]), " ")
	com := &Commit{
		Hash:       string(lines[0]),
		Title:      string(lines[1]),
		Author:     string(lines[2]),
		AuthorName: string(lines[3]),
		Parents:    parents,
		Recipients: sortedRecipients,
		Tags:       tags,
		Date:       date,
		CommitDate: commitDate,
	}
	return com, nil
}

func (git *gitRepo) GetCommitByTitle(title string) (*Commit, error) {
	commits, _, err := git.GetCommitsByTitles([]string{title})
	if err != nil || len(commits) == 0 {
		return nil, err
	}
	return commits[0], nil
}

const (
	fetchCommitsMaxAgeInYears = 5
)

func (git *gitRepo) GetCommitsByTitles(titles []string) ([]*Commit, []string, error) {
	var greps []string
	m := make(map[string]string)
	for _, title := range titles {
		canonical := CanonicalizeCommit(title)
		greps = append(greps, canonical)
		m[canonical] = title
	}
	since := time.Now().Add(-time.Hour * 24 * 365 * fetchCommitsMaxAgeInYears).Format("01-02-2006")
	commits, err := git.fetchCommits(since, HEAD, "", "", greps, true)
	if err != nil {
		return nil, nil, err
	}
	var results []*Commit
	for _, com := range commits {
		canonical := CanonicalizeCommit(com.Title)
		if orig := m[canonical]; orig != "" {
			delete(m, canonical)
			results = append(results, com)
			com.Title = orig
		}
	}
	var missing []string
	for _, orig := range m {
		missing = append(missing, orig)
	}
	return results, missing, nil
}

func (git *gitRepo) LatestCommits(afterCommit string, afterDate time.Time) ([]CommitShort, error) {
	args := []string{"log", "--pretty=format:%h:%cd"}
	if !afterDate.IsZero() {
		args = append(args, "--since", afterDate.Format(time.RFC3339))
	}
	if afterCommit != "" {
		args = append(args, afterCommit+"..")
	}
	output, err := git.Run(args...)
	if err != nil {
		return nil, err
	}
	if len(output) == 0 {
		return nil, nil
	}
	var ret []CommitShort
	for _, line := range strings.Split(string(output), "\n") {
		hash, date, _ := strings.Cut(line, ":")
		commitDate, err := time.Parse(gitDateFormat, date)
		if err != nil {
			return nil, fmt.Errorf("failed to parse date in %q: %w", line, err)
		}
		ret = append(ret, CommitShort{Hash: hash, CommitDate: commitDate})
	}
	return ret, nil
}

func (git *gitRepo) ExtractFixTagsFromCommits(baseCommit, email string) ([]*Commit, error) {
	user, domain, err := splitEmail(email)
	if err != nil {
		return nil, fmt.Errorf("failed to parse email %q: %w", email, err)
	}
	grep := user + "+.*" + domain
	since := time.Now().Add(-time.Hour * 24 * 365 * fetchCommitsMaxAgeInYears).Format("01-02-2006")
	return git.fetchCommits(since, baseCommit, user, domain, []string{grep}, false)
}

func splitEmail(email string) (user, domain string, err error) {
	addr, err := mail.ParseAddress(email)
	if err != nil {
		return "", "", err
	}
	at := strings.IndexByte(addr.Address, '@')
	if at == -1 {
		return "", "", fmt.Errorf("no @ in email address")
	}
	user = addr.Address[:at]
	domain = addr.Address[at:]
	if plus := strings.IndexByte(user, '+'); plus != -1 {
		user = user[:plus]
	}
	return
}

func (git *gitRepo) Bisect(bad, good string, dt debugtracer.DebugTracer, pred func() (BisectResult,
	error)) ([]*Commit, error) {
	git.Reset()
	firstBad, err := git.Commit(bad)
	if err != nil {
		return nil, err
	}
	output, err := git.Run("bisect", "start", bad, good)
	if err != nil {
		return nil, err
	}
	defer git.Reset()
	dt.Log("# git bisect start %v %v\n%s", bad, good, output)
	current, err := git.Commit(HEAD)
	if err != nil {
		return nil, err
	}
	var bisectTerms = [...]string{
		BisectBad:  "bad",
		BisectGood: "good",
		BisectSkip: "skip",
	}
	for {
		res, err := pred()
		// Linux EnvForCommit may cherry-pick some fixes, reset these before the next step.
		git.Run("reset", "--hard")
		if err != nil {
			return nil, err
		}
		if res == BisectBad {
			firstBad = current
		}
		output, err = git.Run("bisect", bisectTerms[res])
		dt.Log("# git bisect %v %v\n%s", bisectTerms[res], current.Hash, output)
		if err != nil {
			if bytes.Contains(output, []byte("There are only 'skip'ped commits left to test")) {
				return git.bisectInconclusive(output)
			}
			return nil, err
		}
		next, err := git.Commit(HEAD)
		if err != nil {
			return nil, err
		}
		if current.Hash == next.Hash {
			return []*Commit{firstBad}, nil
		}
		current = next
	}
}

var gitFullHashRe = regexp.MustCompile("[a-f0-9]{40}")

func (git *gitRepo) bisectInconclusive(output []byte) ([]*Commit, error) {
	// For inconclusive bisection git prints the following message:
	//
	//	There are only 'skip'ped commits left to test.
	//	The first bad commit could be any of:
	//	1f43f400a2cbb02f3d34de8fe30075c070254816
	//	4d96e13ee9cd1f7f801e8c7f4b12f09d1da4a5d8
	//	5cd856a5ef9aa189df757c322be34ad735a5b17f
	//	We cannot bisect more!
	//
	// For conclusive bisection:
	//
	//	7c3850adbcccc2c6c9e7ab23a7dcbc4926ee5b96 is the first bad commit
	var commits []*Commit
	for _, hash := range gitFullHashRe.FindAll(output, -1) {
		com, err := git.Commit(string(hash))
		if err != nil {
			return nil, err
		}
		commits = append(commits, com)
	}
	return commits, nil
}

func (git *gitRepo) ReleaseTag(commit string) (string, error) {
	tags, err := git.previousReleaseTags(commit, true, true, true)
	if err != nil {
		return "", err
	}
	if len(tags) == 0 {
		return "", fmt.Errorf("no release tags found for commit %v", commit)
	}
	return tags[0], nil
}

func (git *gitRepo) previousReleaseTags(commit string, self, onlyTop, includeRC bool) ([]string, error) {
	var tags []string
	if self {
		output, err := git.Run("tag", "--list", "--points-at", commit, "--merged", commit, "v*.*")
		if err != nil {
			return nil, err
		}
		tags = gitParseReleaseTags(output, includeRC)
		if onlyTop && len(tags) != 0 {
			return tags, nil
		}
	}
	output, err := git.Run("tag", "--no-contains", commit, "--merged", commit, "v*.*")
	if err != nil {
		return nil, err
	}
	tags1 := gitParseReleaseTags(output, includeRC)
	tags = append(tags, tags1...)
	if len(tags) == 0 {
		return nil, fmt.Errorf("no release tags found for commit %v", commit)
	}
	return tags, nil
}

func (git *gitRepo) IsRelease(commit string) (bool, error) {
	tags1, err := git.previousReleaseTags(commit, true, false, false)
	if err != nil {
		return false, err
	}
	tags2, err := git.previousReleaseTags(commit, false, false, false)
	if err != nil {
		return false, err
	}
	return len(tags1) != len(tags2), nil
}

func (git *gitRepo) Object(name, commit string) ([]byte, error) {
	return git.Run("show", fmt.Sprintf("%s:%s", commit, name))
}

func (git *gitRepo) MergeBases(firstCommit, secondCommit string) ([]*Commit, error) {
	output, err := git.Run("merge-base", firstCommit, secondCommit)
	if err != nil {
		return nil, err
	}
	ret := []*Commit{}
	for _, hash := range strings.Fields(string(output)) {
		commit, err := git.Commit(hash)
		if err != nil {
			return nil, err
		}
		ret = append(ret, commit)
	}
	return ret, nil
}

// CommitExists relies on 'git cat-file -e'.
// If object exists its exit status is 0.
// If object doesn't exist its exit status is 1 (not documented).
// Otherwise, the exit status is 128 (not documented).
func (git *gitRepo) CommitExists(commit string) (bool, error) {
	_, err := git.Run("cat-file", "-e", commit)
	var vErr *osutil.VerboseError
	if errors.As(err, &vErr) && vErr.ExitCode == 1 {
		return false, nil
	}
	if err != nil {
		return false, err
	}
	return true, nil
}

func (git *gitRepo) PushCommit(repo, commit string) error {
	tagName := "tag-" + commit // assign tag to guarantee remote persistence
	git.Run("tag", tagName)    // ignore errors on re-tagging
	if _, err := git.Run("push", repo, "tag", tagName); err != nil {
		return fmt.Errorf("git push %s tag %s: %w", repo, tagName, err)
	}
	return nil
}

var (
	fileNameRe = regexp.MustCompile(`^diff.* b\/([^\s]+)$`)
	indexRe    = regexp.MustCompile(`^index (\w+)\.\.(?:\w+)(?:\s\d+)?$`)
)

type ModifiedFile struct {
	Name     string
	LeftHash string
}

// ParseGitDiff extracts the files modified in the git patch.
func ParseGitDiff(patch []byte) []ModifiedFile {
	var ret []ModifiedFile
	scanner := bufio.NewScanner(bytes.NewReader(patch))
	for scanner.Scan() {
		line := scanner.Text()
		indexMatch := indexRe.FindStringSubmatch(line)
		if indexMatch != nil && len(ret) > 0 {
			ret[len(ret)-1].LeftHash = indexMatch[1]
		} else {
			fileNameMatch := fileNameRe.FindStringSubmatch(line)
			if fileNameMatch != nil {
				ret = append(ret, ModifiedFile{Name: fileNameMatch[1]})
			}
		}
	}
	return ret
}

type Git struct {
	Dir      string
	Sandbox  bool
	Env      []string
	precious bool
	ignoreCC map[string]bool
}

func (git Git) Run(args ...string) ([]byte, error) {
	cmd, err := git.command(args...)
	if err != nil {
		return nil, err
	}
	return osutil.Run(3*time.Hour, cmd)
}

func (git Git) command(args ...string) (*exec.Cmd, error) {
	cmd := osutil.Command("git", args...)
	cmd.Dir = git.Dir
	cmd.Env = git.Env
	if git.Sandbox {
		if err := osutil.Sandbox(cmd, true, false); err != nil {
			return nil, err
		}
	}
	return cmd, nil
}

// Apply invokes git apply for a series of git patches.
// It is different from Patch() in that it normally handles raw patch emails.
func (git Git) Apply(patch []byte) error {
	cmd, err := git.command("apply", "-")
	if err != nil {
		return err
	}
	stdin, err := cmd.StdinPipe()
	if err != nil {
		return err
	}
	go func() {
		stdin.Write(patch)
		stdin.Close()
	}()
	_, err = osutil.Run(3*time.Hour, cmd)
	return err
}

// Reset resets the git repo to a known clean state.
func (git Git) Reset() error {
	if git.precious {
		return nil
	}
	git.Run("reset", "--hard", "--recurse-submodules")
	git.Run("clean", "-xfdf")
	git.Run("submodule", "foreach", "--recursive", "git", "clean", "-xfdf")
	git.Run("bisect", "reset")
	_, err := git.Run("reset", "--hard", "--recurse-submodules")
	return err
}

// Commit extracts the information about the particular git commit.
func (git Git) Commit(hash string) (*Commit, error) {
	const patchSeparator = "---===syzkaller-patch-separator===---"
	output, err := git.Run("log", "--format=%H%n%s%n%ae%n%an%n%ad%n%P%n%cd%n%b"+patchSeparator,
		"-n", "1", "-p", "-U0", hash)
	if err != nil {
		return nil, err
	}
	pos := bytes.Index(output, []byte(patchSeparator))
	if pos == -1 {
		return nil, fmt.Errorf("git log output does not contain patch separator")
	}
	commit, err := gitParseCommit(output[:pos], nil, nil, git.ignoreCC)
	if err != nil {
		return nil, err
	}
	commit.Patch = output[pos+len(patchSeparator):]
	for len(commit.Patch) != 0 && commit.Patch[0] == '\n' {
		commit.Patch = commit.Patch[1:]
	}
	return commit, nil
}

func (git Git) fetchCommits(since, base, user, domain string, greps []string, fixedStrings bool) ([]*Commit, error) {
	const commitSeparator = "---===syzkaller-commit-separator===---"
	args := []string{"log", "--since", since, "--format=%H%n%s%n%ae%n%an%n%ad%n%P%n%cd%n%b%n" + commitSeparator}
	if fixedStrings {
		args = append(args, "--fixed-strings")
	}
	for _, grep := range greps {
		args = append(args, "--grep", grep)
	}
	args = append(args, base)
	cmd := exec.Command("git", args...)
	cmd.Dir = git.Dir
	cmd.Env = filterEnv()
	if git.Sandbox {
		if err := osutil.Sandbox(cmd, true, false); err != nil {
			return nil, err
		}
	}
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		return nil, err
	}
	if err := cmd.Start(); err != nil {
		return nil, err
	}
	defer cmd.Wait()
	defer cmd.Process.Kill()
	var (
		s           = bufio.NewScanner(stdout)
		buf         = new(bytes.Buffer)
		separator   = []byte(commitSeparator)
		commits     []*Commit
		userBytes   []byte
		domainBytes []byte
	)
	if user != "" {
		userBytes = []byte(user + "+")
		domainBytes = []byte(domain)
	}
	for s.Scan() {
		ln := s.Bytes()
		if !bytes.Equal(ln, separator) {
			buf.Write(ln)
			buf.WriteByte('\n')
			continue
		}
		com, err := gitParseCommit(buf.Bytes(), userBytes, domainBytes, git.ignoreCC)
		if err != nil {
			return nil, err
		}
		if user == "" || len(com.Tags) != 0 {
			commits = append(commits, com)
		}
		buf.Reset()
	}
	return commits, s.Err()
}

type BaseCommit struct {
	*Commit
	Branches []string
}

// BaseForDiff returns a list of commits that could have been the base commit
// for the specified git patch.
// The returned list is minimized to only contain the commits that are represented in different
// subsets of branches.
func (git Git) BaseForDiff(diff []byte, tracer debugtracer.DebugTracer) ([]*BaseCommit, error) {
	// We can't just query git log with --find-object=HASH because that will only return
	// the revisions where the hashed content was introduced or removed, while what we actually
	// want is the latest revision(s) where the content modified in the diff is still in place.

	// So we build a set of potential commits of interest:
	// 1) Tips of the branches.
	// 2) Parents of the commit that in any way mention the modified blob hashes.
	// Then these commits are verified.

	args := []string{
		"log",
		"--all",
		"--no-renames",
		// Note that we cannot accelerate it by specifying "--since"
		"-n", "100",
		`--format=%H:%P`,
	}
	var fileNames []string
	nameToHash := map[string]string{}
	ignoreFiles := map[string]struct{}{}
	for _, file := range ParseGitDiff(diff) {
		if strings.Trim(file.LeftHash, "0") == "" {
			// Newly created files are not of any help here.
			ignoreFiles[file.Name] = struct{}{}
			continue
		}
		if _, ignore := ignoreFiles[file.Name]; ignore {
			continue
		}
		if ok, err := git.verifyHash(file.LeftHash); err != nil {
			return nil, fmt.Errorf("hash verification failed: %w", err)
		} else if !ok {
			// The object is not known in this repository, so we won't find the exact base commit.
			tracer.Log("unknown object %s, stopping base commit search", file.LeftHash)
			return nil, nil
		}
		if _, ok := nameToHash[file.Name]; !ok {
			// If the diff is actually a concatenation of several diffs, we only
			// want to remember the first left side hash for each file.
			fileNames = append(fileNames, file.Name)
			nameToHash[file.Name] = file.LeftHash
		}
		args = append(args, "--find-object="+file.LeftHash)
	}
	tracer.Log("extracted %d left blob hashes", len(nameToHash))
	if len(nameToHash) == 0 {
		return nil, nil
	}
	output, err := git.Run(args...)
	if err != nil {
		return nil, err
	}
	commitBranches := map[string]map[string]struct{}{}
	record := func(commit string, branch string) {
		if commitBranches[commit] == nil {
			commitBranches[commit] = map[string]struct{}{}
		}
		commitBranches[commit][branch] = struct{}{}
	}

	s := bufio.NewScanner(bytes.NewReader(output))
	for s.Scan() {
		// TODO: we can further reduce the search space by adding "--raw" to args
		// and only considering the commits that introduce the blobs from the diff.
		commit, parents, _ := strings.Cut(s.Text(), ":")
		// Focus on the first parent.
		candidate, _, _ := strings.Cut(parents, " ")
		if candidate == "" {
			// For the first commit, there's no parent.
			candidate = commit
		}
		// Only focus on branches that are still alive.
		const cutOffDays = 60
		list, err := git.branchesThatContain(candidate, time.Now().Add(-time.Hour*24*cutOffDays))
		if err != nil {
			return nil, fmt.Errorf("failed to query branches: %w", err)
		}
		for _, info := range list {
			record(candidate, info.Branch)
			record(info.Commit, info.Branch)
		}
	}
	var ret []*BaseCommit
	for commit, branches := range commitBranches {
		tracer.Log("considering %q [%q]", commit, branches)
		fileHashes, err := git.fileHashes(commit, fileNames)
		if err != nil {
			return nil, fmt.Errorf("failed to extract hashes for %s: %w", commit, err)
		}
		var noMatch []string
		for _, name := range fileNames {
			if !strings.HasPrefix(fileHashes[name], nameToHash[name]) {
				noMatch = append(noMatch, name)
			}
		}
		if len(noMatch) != 0 {
			tracer.Log("hashes don't match for %q", noMatch)
			continue
		}
		var branchList []string
		for branch := range branches {
			branchList = append(branchList, branch)
		}
		sort.Strings(branchList)
		info, err := git.Commit(commit)
		if err != nil {
			return nil, fmt.Errorf("failed to extract commit info: %w", err)
		}
		tracer.Log("hashes match, commit date is %v, branches %v", info.CommitDate, branchList)
		ret = append(ret, &BaseCommit{Commit: info, Branches: branchList})
	}
	return git.minimizeBaseCommits(ret)
}

func (git Git) minimizeBaseCommits(list []*BaseCommit) ([]*BaseCommit, error) {
	// We want to preserve commits that are present in different subsets of branches.
	// Then, we want to sort them topologically and break ties by date.
	lastCommit := map[string]*BaseCommit{}
	for _, item := range list {
		key := strings.Join(item.Branches, ":")
		prev, ok := lastCommit[key]
		if !ok {
			lastCommit[key] = item
			continue
		}
		isNewer, err := git.containedIn(item.Hash, prev.Hash)
		if err != nil {
			return nil, fmt.Errorf("topological sort step failed: %w", err)
		}
		if isNewer {
			lastCommit[key] = item
		}
	}
	var filtered []*BaseCommit
	for _, item := range lastCommit {
		filtered = append(filtered, item)
	}
	sort.Slice(filtered, func(i, j int) bool {
		return filtered[i].CommitDate.After(filtered[j].CommitDate)
	})
	return filtered, nil
}

// fileHashes returns the blob SHA hashes for a particular list of files on a particular commit.
func (git Git) fileHashes(commit string, files []string) (map[string]string, error) {
	output, err := git.Run(append([]string{"ls-tree", commit}, files...)...)
	if err != nil {
		return nil, err
	}
	ret := map[string]string{}
	s := bufio.NewScanner(bytes.NewReader(output))
	for s.Scan() {
		line := s.Text()
		fields := strings.Fields(line)
		if len(fields) != 4 {
			return nil, fmt.Errorf("invalid output: %q", line)
		}
		ret[fields[3]] = fields[2]
	}
	return ret, nil
}

type branchCommit struct {
	Branch string
	Commit string
}

func (git Git) branchesThatContain(commit string, since time.Time) ([]branchCommit, error) {
	output, err := git.Run(
		"branch", "-a",
		"--contains", commit,
		`--format=%(committerdate);%(objectname);%(refname:short)`,
	)
	if err != nil {
		return nil, err
	}
	var ret []branchCommit
	s := bufio.NewScanner(bytes.NewReader(output))
	for s.Scan() {
		dateString, branchInfo, _ := strings.Cut(s.Text(), ";")
		commit, branch, _ := strings.Cut(branchInfo, ";")
		date, err := time.Parse(gitDateFormat, dateString)
		if err != nil {
			return nil, fmt.Errorf("failed to parse git date: %w\n%q", err, dateString)
		}
		if date.Before(since) {
			continue
		}
		ret = append(ret, branchCommit{Branch: branch, Commit: commit})
	}
	return ret, nil
}

func (git Git) verifyHash(hash string) (bool, error) {
	_, err := git.Run("rev-parse", "--quiet", "--verify", hash)
	if err != nil {
		var verboseErr *osutil.VerboseError
		if errors.As(err, &verboseErr) && verboseErr.ExitCode == 1 {
			return false, nil
		}
		return false, err
	}
	return true, nil
}

func (git Git) containedIn(parent, commit string) (bool, error) {
	_, err := git.Run("merge-base", "--is-ancestor", commit, parent)
	return err == nil, nil
}

func (git Git) Diff(commitA, commitB string) ([]byte, error) {
	return git.Run("diff", commitA+".."+commitB)
}