1BEGIN {
2	# No approval required to add a new gitlab instance here
3	gitlab_hosts["code.videolan.org"] = 1
4	gitlab_hosts["foss.heptapod.net"] = 1
5	gitlab_hosts["framagit.org"] = 1
6	gitlab_hosts["gitlab.com"] = 1
7	gitlab_hosts["gitlab.common-lisp.net"] = 1
8	gitlab_hosts["gitlab.cs.fau.de"] = 1
9	gitlab_hosts["gitlab.dune-project.org"] = 1
10	gitlab_hosts["gitlab.freedesktop.org"] = 1
11	gitlab_hosts["gitlab.gnome.org"] = 1
12	gitlab_hosts["gitlab.howett.net"] = 1
13	gitlab_hosts["gitlab.inria.fr"] = 1
14	gitlab_hosts["gitlab.isc.org"] = 1
15	gitlab_hosts["gitlab.linphone.org"] = 1
16	gitlab_hosts["gitlab.mathematik.uni-stuttgart.de"] = 1
17	gitlab_hosts["gitlab.mn.tu-dresden.de"] = 1
18	gitlab_hosts["gitlab.mpcdf.mpg.de"] = 1
19	gitlab_hosts["gitlab.redox-os.org"] = 1
20	gitlab_hosts["gitlab.torproject.org"] = 1
21	gitlab_hosts["gitlab.xfce.org"] = 1
22	gitlab_hosts["invent.kde.org"] = 1
23	gitlab_hosts["salsa.debian.org"] = 1
24	gitlab_hosts["source.puri.sm"] = 1
25}
26
27function warn(fmt, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) {
28	printf("WARNING: " fmt "\n", a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) >"/dev/stderr"
29}
30
31function commit_from_git_url(url) {
32	if (url["query", "tag"]) {
33		return url["query", "tag"]
34	} else {
35		return url["fragment"]
36	}
37}
38
39function split_git_url(info, git_url,		url, path, account, project, commit, i, dir_ver, host, tag, fragment) {
40	delete info
41	split_url(url, git_url)
42	url["scheme"] = tolower(url["scheme"])
43	url["host"] = tolower(url["host"])
44	if (url["scheme"] ~ /^git(\+https?)?$/) {
45		if (url["host"] == "github.com") {
46			split(url["path"], path, "/")
47			account = path[2]
48			project = path[3]
49			sub(/\.[gG][iI][tT]$/, "", project)
50			commit = commit_from_git_url(url)
51
52			delete url
53			url["scheme"] = "https"
54			url["host"] = "codeload.github.com"
55			url["path"] = sprintf("/%s/%s/tar.gz/%s", account, project, commit)
56			url["query"] = "dummy"
57			url["query", "dummy"] = "/"
58			info["site"] = join_url(url)
59
60			info["filename"] = sprintf("%s-%s-%s_GH0.tar.gz", account, project, commit)
61
62			# Per bsd.sites.mk:
63			# "GitHub silently converts tags starting with v to not have v in the filename
64			# and extraction directory.  It also replaces + with -."
65			dir_ver = commit
66			sub(/^[vV]/, "", dir_ver)
67			gsub(/\+/, "-", dir_ver)
68			gsub(/--/, "-", dir_ver)
69			info["dir"] = sprintf("%s-%s", project, dir_ver)
70
71			return 1
72		} else if (gitlab_hosts[url["host"]]) {
73			split(url["path"], path, "/")
74			account = path[2]
75			for (i = 3; i < length(path); i++) {
76				account = account "/" path[i]
77			}
78			project = path[i]
79			sub(/\.[gG][iI][tT]$/, "", project)
80			commit = commit_from_git_url(url)
81			fragment = url["fragment"]
82			tag = url["query", "tag"]
83
84			host = url["host"]
85			delete url
86			url["scheme"] = "https"
87			url["host"] = host
88			url["path"] = sprintf("/%s/%s/-/archive/%s.tar.gz", account, project, commit)
89			url["query"] = "dummy"
90			url["query", "dummy"] = "/"
91			info["site"] = join_url(url)
92
93			gsub(/\//, "-", account)
94			info["filename"] = sprintf("%s-%s-%s_GL0.tar.gz", account, project, commit)
95
96			# c.f. https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=266724
97			if (tag) {
98				info["dir"] = sprintf("%s-%s-%s", project, tag, fragment)
99			} else {
100				info["dir"] = sprintf("%s-%s", project, commit)
101			}
102
103			return 1
104		}
105	}
106
107	warn("Do not know how to handle %s", git_url)
108	warn("If it points to a GitLab instance try adding the hostname to gitlab_hosts[] at the top of cargo-crates-git-common.awk")
109	return 0
110}
111