aboutsummaryrefslogtreecommitdiffstats
path: root/syz-cluster/pkg/triage/git.go
blob: fb23a90b3dc38792fd0f0284a91941fe0f8915f4 (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
// Copyright 2024 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 triage

import (
	"fmt"
	"os"

	"github.com/google/syzkaller/pkg/debugtracer"
	"github.com/google/syzkaller/pkg/vcs"
	"github.com/google/syzkaller/syz-cluster/pkg/api"
)

type GitTreeOps struct {
	*vcs.Git
}

func NewGitTreeOps(dir string, sandbox bool) (*GitTreeOps, error) {
	ops := &GitTreeOps{
		Git: &vcs.Git{
			Dir: dir,
			// TODO: why doesn't sandbox=true work normally under go tests?
			Sandbox: sandbox,
			Env:     os.Environ(),
		},
	}
	err := ops.Reset()
	return ops, err
}

func (ops *GitTreeOps) HeadCommit(tree *api.Tree) (*vcs.Commit, error) {
	// See kernel-disk/cron.yaml.
	return ops.Git.Commit(tree.Name + "/" + tree.Branch)
}

func (ops *GitTreeOps) Commit(treeName, commitOrBranch string) (*vcs.Commit, error) {
	// See kernel-disk/cron.yaml.
	if vcs.CheckCommitHash(commitOrBranch) {
		return ops.Git.Commit(commitOrBranch)
	}
	return ops.Git.Commit(treeName + "/" + commitOrBranch)
}

func (ops *GitTreeOps) ApplySeries(commit string, patches [][]byte) error {
	ops.Reset()
	_, err := ops.Run("reset", "--hard", commit)
	if err != nil {
		return err
	}
	for i, patch := range patches {
		err := ops.Apply(patch)
		if err != nil {
			return fmt.Errorf("failed to apply patch %d: %w", i, err)
		}
	}
	return nil
}

func (ops *GitTreeOps) BaseForDiff(patch []byte, tracer debugtracer.DebugTracer) (*vcs.BaseCommit, error) {
	list, err := ops.Git.BaseForDiff(patch, tracer)
	if len(list) == 0 || err != nil {
		return nil, err
	}
	return list[0], nil
}