-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pom.clj
51 lines (46 loc) · 1.7 KB
/
pom.clj
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
51
(ns build-edn.pom
(:require
[clojure.java.io :as io]
[clojure.string :as str]))
(defn- remote-origin-url-regexps
[domain]
(let [domain (str/replace domain "." "\\.")]
[(re-pattern (format "(?:\\w+@)?%s:([^/]+)/([^/]+)\\.git" domain))
(re-pattern (format "\\w+://(?:\\w+@)?%s/([^/]+)/([^/]+?)(?:\\.git)?" domain))]))
(defn- git-remote-origin-url
[git-config-path]
(try
(let [url (some->> (slurp git-config-path)
(str/split-lines)
(drop-while #(not= "[remote \"origin\"]" (str/trim %)))
(drop-while #(not (re-find #"url\s*=\s" %)))
(first))]
(some-> url
(str/split #"\s*=\s*" 2)
(second)))
(catch Exception _ nil)))
(defn- extract-user-and-repository
[domain remote-origin-url]
(some->> domain
(remote-origin-url-regexps)
(some #(re-matches % remote-origin-url))
(rest)))
(defn- scm-map
[domain [user repo]]
{:connection (format "scm:git:git://%s/%s/%s.git" domain user repo)
:developerConnection (format "scm:git:ssh://git@%s/%s/%s.git" domain user repo)
:url (format "https://%s/%s/%s" domain user repo)})
(defn generate-scm-from-git-dir
([]
(generate-scm-from-git-dir ".git"))
([git-dir]
(let [git-config-path (io/file git-dir "config")
url (git-remote-origin-url git-config-path)
domain (when url
(condp #(str/includes? %2 %1) url
"github.com" "github.com"
"gitlab.com" "gitlab.com"
nil))
user-repo (when domain (extract-user-and-repository domain url))]
(when user-repo
(scm-map domain user-repo)))))