Skip to content

Commit d3304f6

Browse files
committed
Don't read git files if they are unreadable.
Also print warnings if the file is not readable. Fixes #1585.
1 parent 8498d8b commit d3304f6

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

leiningen-core/src/leiningen/core/utils.clj

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
(URL. (str "http://" url)))))
2525

2626
(defn read-file
27-
"Read the contents of file if it exists."
27+
"Returns the first Clojure form in a file if it exists."
2828
[file]
2929
(if (.exists file)
3030
(try (read-string (slurp file))
3131
(catch Exception e
32-
(binding [*out* *err*]
32+
(binding [*out* *err*] ;; TODO: use main/warn for this in 3.0
3333
(println "Error reading"
3434
(.getName file)
3535
"from"
@@ -55,7 +55,7 @@
5555
[".clj" (str clojure.lang.RT/LOADER_SUFFIX ".class")]))
5656

5757
(defn error [& args]
58-
(binding [*out* *err*]
58+
(binding [*out* *err*] ;; TODO: use main/warn for this in 3.0
5959
(apply println "Error:" args)))
6060

6161
(defn require-resolve
@@ -116,30 +116,48 @@
116116

117117
;; # Git
118118

119+
;; This is very similar to the read-file function above. The only differences
120+
;; are the error messages and the transformations done on the content.
121+
(defn- git-file-contents
122+
"Returns the (trimmed) contents by the given git path, or nil if it is
123+
inacessible or nonexisting. If it exists and is not readable, a warning is
124+
printed."
125+
[git-dir ref-path]
126+
(let [ref (io/file git-dir ref-path)]
127+
(println (.toString ref))
128+
(if (.canRead ref)
129+
(.trim (slurp ref))
130+
(do
131+
(when (.exists ref)
132+
(binding [*out* *err*] ;; TODO: use main/warn for this in 3.0
133+
(println "Warning: Contents of git file"
134+
(str ".git/" ref-path) "is not readable.")
135+
(println "(Check that you have the right permissions to read"
136+
"the .git repo)")))
137+
nil))))
138+
119139
(defn ^:internal resolve-git-dir [project]
120140
(let [alternate-git-root (io/file (get-in project [:scm :dir]))
121141
git-dir-file (io/file (or alternate-git-root (:root project)) ".git")]
122-
(if (.isFile git-dir-file)
142+
(if (and (.isFile git-dir-file) (.canRead git-dir-file))
123143
(io/file (second (re-find #"gitdir: (\S+)" (slurp (str git-dir-file)))))
124144
git-dir-file)))
125145

126146
(defn- read-git-ref
127147
"Reads the commit SHA1 for a git ref path, or nil if no commit exist."
128148
[git-dir ref-path]
129-
(let [ref (io/file git-dir ref-path)]
130-
(if (.exists ref)
131-
(.trim (slurp ref))
132-
nil)))
149+
(git-file-contents git-dir ref-path))
133150

134151
(defn- read-git-head-file
135152
"Reads the current value of HEAD by attempting to read .git/HEAD, returning
136153
the SHA1 or nil if none exists."
137154
[git-dir]
138-
(let [head (.trim (slurp (str (io/file git-dir "HEAD"))))]
139-
(if-let [ref-path (second (re-find #"ref: (\S+)" head))]
140-
(read-git-ref git-dir ref-path))))
155+
(some->> (git-file-contents git-dir "HEAD")
156+
(re-find #"ref: (\S+)")
157+
(second)
158+
(read-git-ref git-dir)))
141159

142-
;; TODO: de-dupe with pom namespace
160+
;; TODO: de-dupe with pom namespace (3.0?)
143161

144162
(defn ^:internal read-git-head
145163
"Reads the value of HEAD and returns a commit SHA1, or nil if no commit

0 commit comments

Comments
 (0)