Skip to content

Commit 9fce9e4

Browse files
committed
pkg/vcs: reduce cyclomatic complexity link func
1 parent a3b5ade commit 9fce9e4

File tree

1 file changed

+63
-47
lines changed

1 file changed

+63
-47
lines changed

pkg/vcs/vcs.go

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -396,63 +396,79 @@ func link(url, hash, file string, line, typ int) string {
396396
return link(url+"/fuchsia", hash, file, line, typ)
397397
}
398398
if strings.HasPrefix(url, "https://github.com/") {
399-
url = strings.TrimSuffix(url, ".git")
400-
switch typ {
401-
case 1:
402-
return url + "/tree/" + hash
403-
case 2:
404-
return url + "/commits/" + hash
405-
case 3:
406-
return url + "/blob/" + hash + "/" + file + "#L" + fmt.Sprint(line)
407-
default:
408-
return url + "/commit/" + hash
409-
}
399+
return linkGithub(url, hash, file, line, typ)
410400
}
411401
if strings.HasPrefix(url, "https://git.kernel.org/pub/scm/") ||
412402
strings.HasPrefix(url, "git://git.kernel.org/pub/scm/") {
413-
url = strings.TrimPrefix(url, "git")
414-
url = strings.TrimPrefix(url, "https")
415-
url = "https" + url
416-
switch typ {
417-
case 1:
418-
return url + "/tree/?id=" + hash
419-
case 2:
420-
return url + "/log/?id=" + hash
421-
case 3:
422-
return url + "/tree/" + file + "?id=" + hash + "#n" + fmt.Sprint(line)
423-
default:
424-
return url + "/commit/?id=" + hash
425-
}
403+
return linkKernelOrg(url, hash, file, line, typ)
426404
}
427405
for _, cgitHost := range []string{"git.kernel.dk", "git.breakpoint.cc"} {
428406
if strings.HasPrefix(url, "https://"+cgitHost) ||
429407
strings.HasPrefix(url, "git://"+cgitHost) {
430-
url = strings.TrimPrefix(strings.TrimPrefix(url, "git://"), "https://")
431-
url = strings.TrimPrefix(url, cgitHost)
432-
url = "https://" + cgitHost + "/cgit" + url
433-
switch typ {
434-
case 1:
435-
return url + "/tree/?id=" + hash
436-
case 2:
437-
return url + "/log/?id=" + hash
438-
case 3:
439-
return url + "/tree/" + file + "?id=" + hash + "#n" + fmt.Sprint(line)
440-
default:
441-
return url + "/commit/?id=" + hash
442-
}
408+
return linkCgit(cgitHost, url, hash, file, line, typ)
443409
}
444410
}
445411
if strings.HasPrefix(url, "https://") && strings.Contains(url, ".googlesource.com") {
446-
switch typ {
447-
case 1:
448-
return url + "/+/" + hash + "/"
449-
case 2:
450-
return url + "/+log/" + hash
451-
case 3:
452-
return url + "/+/" + hash + "/" + file + "#" + fmt.Sprint(line)
453-
default:
454-
return url + "/+/" + hash + "^!"
455-
}
412+
return linkGoogleSource(url, hash, file, line, typ)
456413
}
457414
return ""
458415
}
416+
417+
func linkGithub(url, hash, file string, line, typ int) string {
418+
url = strings.TrimSuffix(url, ".git")
419+
switch typ {
420+
case 1:
421+
return url + "/tree/" + hash
422+
case 2:
423+
return url + "/commits/" + hash
424+
case 3:
425+
return url + "/blob/" + hash + "/" + file + "#L" + fmt.Sprint(line)
426+
default:
427+
return url + "/commit/" + hash
428+
}
429+
}
430+
431+
func linkKernelOrg(url, hash, file string, line, typ int) string {
432+
url = strings.TrimPrefix(url, "git")
433+
url = strings.TrimPrefix(url, "https")
434+
url = "https" + url
435+
switch typ {
436+
case 1:
437+
return url + "/tree/?id=" + hash
438+
case 2:
439+
return url + "/log/?id=" + hash
440+
case 3:
441+
return url + "/tree/" + file + "?id=" + hash + "#n" + fmt.Sprint(line)
442+
default:
443+
return url + "/commit/?id=" + hash
444+
}
445+
}
446+
447+
func linkCgit(cgitHost, url, hash, file string, line, typ int) string {
448+
url = strings.TrimPrefix(strings.TrimPrefix(url, "git://"), "https://")
449+
url = strings.TrimPrefix(url, cgitHost)
450+
url = "https://" + cgitHost + "/cgit" + url
451+
switch typ {
452+
case 1:
453+
return url + "/tree/?id=" + hash
454+
case 2:
455+
return url + "/log/?id=" + hash
456+
case 3:
457+
return url + "/tree/" + file + "?id=" + hash + "#n" + fmt.Sprint(line)
458+
default:
459+
return url + "/commit/?id=" + hash
460+
}
461+
}
462+
463+
func linkGoogleSource(url, hash, file string, line, typ int) string {
464+
switch typ {
465+
case 1:
466+
return url + "/+/" + hash + "/"
467+
case 2:
468+
return url + "/+log/" + hash
469+
case 3:
470+
return url + "/+/" + hash + "/" + file + "#" + fmt.Sprint(line)
471+
default:
472+
return url + "/+/" + hash + "^!"
473+
}
474+
}

0 commit comments

Comments
 (0)