From 9fce9e44cb18ac5d0a14e0ca8968b630324ff110 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 29 Dec 2025 12:52:25 +0100 Subject: pkg/vcs: reduce cyclomatic complexity link func --- pkg/vcs/vcs.go | 110 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 63 insertions(+), 47 deletions(-) (limited to 'pkg/vcs') diff --git a/pkg/vcs/vcs.go b/pkg/vcs/vcs.go index b7197a7d6..95167d2e9 100644 --- a/pkg/vcs/vcs.go +++ b/pkg/vcs/vcs.go @@ -396,63 +396,79 @@ func link(url, hash, file string, line, typ int) string { return link(url+"/fuchsia", hash, file, line, typ) } if strings.HasPrefix(url, "https://github.com/") { - url = strings.TrimSuffix(url, ".git") - switch typ { - case 1: - 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 - } + return linkGithub(url, hash, file, line, typ) } if strings.HasPrefix(url, "https://git.kernel.org/pub/scm/") || strings.HasPrefix(url, "git://git.kernel.org/pub/scm/") { - url = strings.TrimPrefix(url, "git") - url = strings.TrimPrefix(url, "https") - url = "https" + url - switch typ { - case 1: - 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 - } + return linkKernelOrg(url, hash, file, line, typ) } for _, cgitHost := range []string{"git.kernel.dk", "git.breakpoint.cc"} { if strings.HasPrefix(url, "https://"+cgitHost) || strings.HasPrefix(url, "git://"+cgitHost) { - url = strings.TrimPrefix(strings.TrimPrefix(url, "git://"), "https://") - url = strings.TrimPrefix(url, cgitHost) - url = "https://" + cgitHost + "/cgit" + url - switch typ { - case 1: - 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 - } + return linkCgit(cgitHost, url, hash, file, line, typ) } } if strings.HasPrefix(url, "https://") && strings.Contains(url, ".googlesource.com") { - switch typ { - case 1: - return url + "/+/" + hash + "/" - case 2: - return url + "/+log/" + hash - case 3: - return url + "/+/" + hash + "/" + file + "#" + fmt.Sprint(line) - default: - return url + "/+/" + hash + "^!" - } + return linkGoogleSource(url, hash, file, line, typ) } return "" } + +func linkGithub(url, hash, file string, line, typ int) string { + url = strings.TrimSuffix(url, ".git") + switch typ { + case 1: + 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 + } +} + +func linkKernelOrg(url, hash, file string, line, typ int) string { + url = strings.TrimPrefix(url, "git") + url = strings.TrimPrefix(url, "https") + url = "https" + url + switch typ { + case 1: + 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 + } +} + +func linkCgit(cgitHost, url, hash, file string, line, typ int) string { + url = strings.TrimPrefix(strings.TrimPrefix(url, "git://"), "https://") + url = strings.TrimPrefix(url, cgitHost) + url = "https://" + cgitHost + "/cgit" + url + switch typ { + case 1: + 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 + } +} + +func linkGoogleSource(url, hash, file string, line, typ int) string { + switch typ { + case 1: + return url + "/+/" + hash + "/" + case 2: + return url + "/+log/" + hash + case 3: + return url + "/+/" + hash + "/" + file + "#" + fmt.Sprint(line) + default: + return url + "/+/" + hash + "^!" + } +} -- cgit mrf-deployment