Skip to content

Commit 54a6123

Browse files
committed
Add new object-type api method
1 parent a5a70c9 commit 54a6123

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Changelog
22
===========
33

4+
* next
5+
* Add new `object-type` api - takes rev, returns object type
46
* 2.2.156 on Apr 7, 2021
57
* `tags` api should fetch to ensure all tags are returned
68
* 2.2.152 on Apr 3, 2021

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@
4747
[url rev]
4848
(first (resolve-all url [rev])))
4949

50+
(defn object-type
51+
"Takes a git url and rev, and returns the object type, one of :tag :tree
52+
:commit or :blob, or nil if not known or ambiguous."
53+
[url rev]
54+
(let [git-dir (impl/ensure-git-dir url)]
55+
(if-let [type (impl/git-type git-dir rev)]
56+
type
57+
(do
58+
(impl/git-fetch (jio/file git-dir))
59+
(impl/git-type git-dir rev)))))
60+
5061
(defn procure
5162
"Procure a working tree at rev for the git url representing the library lib,
5263
returns the directory path. lib is a qualified symbol where the qualifier is a

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@
102102
(when (zero? exit)
103103
(str/trimr out))))
104104

105+
(defn git-type
106+
[git-dir rev]
107+
(let [{:keys [exit out]} (run-git "--git-dir" git-dir "cat-file" "-t" rev)]
108+
(when (zero? exit)
109+
(keyword (str/trimr out)))))
110+
105111
;; git merge-base --is-ancestor <maybe-ancestor-commit> <descendant-commit>
106112
(defn- ancestor?
107113
[git-dir x y]

0 commit comments

Comments
 (0)