Skip to content

Commit bf214d5

Browse files
committed
TDEPS-212 Support local file git repos
1 parent 270d340 commit bf214d5

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

src/main/clojure/clojure/tools/gitlibs/impl.clj

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,40 @@
4545
^File [lib]
4646
(jio/file (:gitlibs/dir @config/CONFIG) "libs" (namespace lib) (name lib)))
4747

48+
(def ^:private git-url-regex
49+
#"([a-z0-9+.-]+):\/\/(?:(?:(?:[^@]+?)@)?([^/]+?)(?::[0-9]*)?)?(/[^:]+)")
50+
51+
(def ^:private git-scp-regex
52+
#"(?:(?:[^@]+?)@)?(.+?):([^:]+)")
53+
4854
(defn- clean-url
49-
"Chop leading protocol, trailing .git, replace :'s with /"
55+
"Convert url into a safe relative path (this is not a reversible transformation)
56+
based on scheme, host, and path (drop user and port).
57+
58+
Examples:
59+
ssh://[email protected]:3333/org/repo.git => ssh/gitlab.com/org/repo
60+
[email protected]:dotted.org/dotted.repo.git => ssh/github.com/dotted.org/dotted.repo
61+
file://../foo => file/REL/_DOTDOT_/foo
62+
file:///Users/user/foo.git => file/Users/user/foo
63+
../foo => file/REL/_DOTDOT_/foo
64+
~user/foo.git => file/REL/_TILDE_user/foo
65+
66+
* https://git-scm.com/docs/git-clone#_git_urls
67+
* https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols
68+
"
5069
[url]
51-
(-> url
52-
(str/split #"://")
53-
last
54-
(str/replace #"\.git$" "")
55-
(str/replace #":" "/")))
70+
(let [[scheme host path] (cond
71+
(str/starts-with? url "file://") ["file" nil (-> url (subs 7) (str/replace #"^([^/])" "REL/$1"))]
72+
(str/includes? url "://") (let [[_ s h p] (re-matches git-url-regex url)] [s h p])
73+
(str/includes? url ":") (let [[_ h p] (re-matches git-scp-regex url)] ["ssh" h p])
74+
:local-repo ["file" nil (str/replace url #"^([^/])" "REL/$1")])
75+
clean-path (-> path
76+
(str/replace #"\.git/?$" "") ;; remove trailing .git or .git/
77+
(str/replace #"~" "_TILDE_")) ;; replace ~ with _TILDE_
78+
dir-parts (->> (concat [scheme host] (str/split clean-path #"/")) ;; split on /
79+
(remove str/blank?) ;; remove any missing path segments
80+
(map #(-> % ({"." "_DOT_", ".." "_DOTDOT_"} %))))] ;; replace . or .. segments
81+
(str/join "/" dir-parts)))
5682

5783
(defn git-dir
5884
^File [url]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
(ns clojure.tools.gitlibs.test-impl
2+
(:require
3+
[clojure.test :refer :all]
4+
[clojure.tools.gitlibs.impl :as impl]))
5+
6+
(deftest test-clean-url
7+
(are [url expected-path]
8+
(= expected-path (#'impl/clean-url url))
9+
10+
;; url formats - don't use user or port
11+
"ssh://[email protected]:3333/org/repo.git" "ssh/gitlab.com/org/repo"
12+
"ssh://[email protected]/org/repo.git" "ssh/gitlab.org.net/org/repo"
13+
"ssh://[email protected]/~user/repo.git/" "ssh/host.xz/_TILDE_user/repo"
14+
"https://github.com/org/repo.git" "https/github.com/org/repo"
15+
"git://host.xz/path/to/repo.git/" "git/host.xz/path/to/repo"
16+
17+
;; scp style url (most common github ssh url format)
18+
"[email protected]:org/repo.git" "ssh/github.com/org/repo"
19+
"[email protected]:dotted.org/dotted.repo.git" "ssh/github.com/dotted.org/dotted.repo"
20+
"host.xz:~user/path/to/repo.git/" "ssh/host.xz/_TILDE_user/path/to/repo"
21+
22+
;; file scheme
23+
"file:///Users/me/code/repo.git" "file/Users/me/code/repo"
24+
"file://../foo.git" "file/REL/_DOTDOT_/foo"
25+
"file://~/path/repo.git" "file/REL/_TILDE_/path/repo"
26+
27+
;; file repos - handle relative vs absolute, handle . .. ~
28+
"/Users/me/code/repo.git" "file/Users/me/code/repo"
29+
"../foo.git" "file/REL/_DOTDOT_/foo"
30+
"./foo.git" "file/REL/_DOT_/foo"
31+
"~user/foo.git" "file/REL/_TILDE_user/foo"
32+
33+
;; git - unknown transport with url rewrite in gitconfig (unusual, but do something useful)
34+
"work:repo.git" "ssh/work/repo"))
35+
36+

src/test/clojure/clojure/tools/test_gitlibs.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
(deftest test-procure
2222
(let [wt1 (gl/procure repo-url 'org.clojure/spec.alpha "739c1af")
2323
wt2 (gl/procure repo-url 'org.clojure/spec.alpha "6a56327")]
24-
(is (.exists (jio/file (gl/cache-dir) "_repos" "github.com" "clojure" "spec.alpha")))
24+
(is (.exists (jio/file (gl/cache-dir) "_repos" "https" "github.com" "clojure" "spec.alpha")))
2525
(is (= wt1 (.getAbsolutePath (jio/file (gl/cache-dir) "libs" "org.clojure" "spec.alpha" "739c1af56dae621aedf1bb282025a0d676eff713"))))
2626
(is (= wt2 (.getAbsolutePath (jio/file (gl/cache-dir) "libs" "org.clojure" "spec.alpha" "6a56327446c909db0d11ecf93a3c3d659b739be9"))))))
2727

0 commit comments

Comments
 (0)