diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2022-05-20 17:30:29 +0200 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2022-05-20 20:06:53 +0200 |
| commit | f94bdf2730bcf53b45244e98aedb1a7a7711d49f (patch) | |
| tree | 82146d09ca3322485a45c71e042b6368b42f7041 /pkg/vcs | |
| parent | bd37ad7ed09f3176a3793d63ec111a2b35ea9fc2 (diff) | |
pkg/vcs: add FileLink
Add a function that produces a link to a source file:line
for the given repository/commit.
Update #652
Diffstat (limited to 'pkg/vcs')
| -rw-r--r-- | pkg/vcs/vcs.go | 22 | ||||
| -rw-r--r-- | pkg/vcs/vcs_test.go | 39 |
2 files changed, 56 insertions, 5 deletions
diff --git a/pkg/vcs/vcs.go b/pkg/vcs/vcs.go index 8f9ff3acb..2355cac69 100644 --- a/pkg/vcs/vcs.go +++ b/pkg/vcs/vcs.go @@ -307,25 +307,29 @@ const SyzkallerRepo = "https://github.com/google/syzkaller" const HEAD = "HEAD" func CommitLink(url, hash string) string { - return link(url, hash, 0) + return link(url, hash, "", 0, 0) } func TreeLink(url, hash string) string { - return link(url, hash, 1) + return link(url, hash, "", 0, 1) } func LogLink(url, hash string) string { - return link(url, hash, 2) + return link(url, hash, "", 0, 2) } -func link(url, hash string, typ int) string { +func FileLink(url, hash, file string, line int) string { + return link(url, hash, file, line, 3) +} + +func link(url, hash, file string, line, typ int) string { if url == "" || hash == "" { return "" } switch url { case "https://fuchsia.googlesource.com": // We collect hashes from the fuchsia repo. - return link(url+"/fuchsia", hash, typ) + return link(url+"/fuchsia", hash, file, line, typ) } if strings.HasPrefix(url, "https://github.com/") { url = strings.TrimSuffix(url, ".git") @@ -334,6 +338,8 @@ func link(url, hash string, typ int) string { return url + "/tree/" + hash case 2: return url + "/commits/" + hash + case 3: + return url + "/blob/" + hash + "/" + file + "#L" + fmt.Sprint(line) default: return url + "/commit/" + hash } @@ -348,6 +354,8 @@ func link(url, hash string, typ int) string { return url + "/tree/?id=" + hash case 2: return url + "/log/?id=" + hash + case 3: + return url + "/tree/" + file + "?id=" + hash + "#n" + fmt.Sprint(line) default: return url + "/commit/?id=" + hash } @@ -363,6 +371,8 @@ func link(url, hash string, typ int) string { return url + "/tree/?id=" + hash case 2: return url + "/log/?id=" + hash + case 3: + return url + "/tree/" + file + "?id=" + hash + "#n" + fmt.Sprint(line) default: return url + "/commit/?id=" + hash } @@ -374,6 +384,8 @@ func link(url, hash string, typ int) string { return url + "/+/" + hash + "/" case 2: return url + "/+log/" + hash + case 3: + return url + "/+/" + hash + "/" + file + "#" + fmt.Sprint(line) default: return url + "/+/" + hash + "^!" } diff --git a/pkg/vcs/vcs_test.go b/pkg/vcs/vcs_test.go index 54593c2b4..2cee5e12d 100644 --- a/pkg/vcs/vcs_test.go +++ b/pkg/vcs/vcs_test.go @@ -192,6 +192,45 @@ func TestCommitLink(t *testing.T) { } } +func TestFileLink(t *testing.T) { + type Test struct { + URL string + Hash string + File string + Line int + FileLink string + } + tests := []Test{ + { + "https://github.com/google/syzkaller", + "76dd003f1b102b791d8b342a1f92a6486ff56a1e", + "Makefile", + 42, + "https://github.com/google/syzkaller/blob/76dd003f1b102b791d8b342a1f92a6486ff56a1e/Makefile#L42", + }, + { + "https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git", + "8fe28cb58bcb", + "Makefile", + 42, + "https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Makefile?id=8fe28cb58bcb#n42", + }, + { + "https://android.googlesource.com/kernel/common", + "d0c3914ffbe4c00f0a131bae83f811d5606699bc", + "Makefile", + 42, + "https://android.googlesource.com/kernel/common/+/d0c3914ffbe4c00f0a131bae83f811d5606699bc/Makefile#42", + }, + } + for _, test := range tests { + link := FileLink(test.URL, test.Hash, test.File, test.Line) + if link != test.FileLink { + t.Errorf("Test: %+v\ngot: %v", test, link) + } + } +} + func TestParse(t *testing.T) { // nolint: lll test1 := []byte(`Foo (Maintainer) Bar <a@email.com> (maintainer:KERNEL) |
