aboutsummaryrefslogtreecommitdiffstats
path: root/tools/syz-base-commit/main.go
blob: f6ed1d045777e60966ac90949f89cc075ca6a956 (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
// Copyright 2020 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.

// syz-base-commit is a tool for debugging the blob-based base commit detection functionality.

package main

import (
	"flag"
	"log"
	"os"

	"github.com/google/syzkaller/pkg/debugtracer"
	"github.com/google/syzkaller/pkg/tool"
	"github.com/google/syzkaller/pkg/vcs"
)

var (
	flagRepo = flag.String("sourcedir", "", "path to the Linux kernel repository")
)

func main() {
	defer tool.Init()()
	args := flag.Args()
	if *flagRepo == "" || len(args) != 1 {
		tool.Failf("expected format: syz-base-commit --sourcedir ./linux-repo some-patch.diff")
	}
	log.Printf("note: the tool runs much faster after a `git commit-graph write --reachable`")
	diff, err := os.ReadFile(args[0])
	if err != nil {
		tool.Fail(err)
	}
	git := &vcs.Git{
		Dir: *flagRepo,
	}
	commits, err := git.BaseForDiff(diff, &debugtracer.GenericTracer{
		TraceWriter: os.Stderr,
	})
	if err != nil {
		tool.Fail(err)
	}
	if len(commits) == 0 {
		log.Printf("no suitable commits found!")
		os.Exit(0)
	}
	log.Printf("found %d candidates:", len(commits))
	for _, commit := range commits {
		log.Printf("%+v %v", commit.Commit, commit.Branches)
	}
}