Skip to content

Commit 6cf1230

Browse files
committed
support repos, associate commits with repo, root tree is named by repo
1 parent 2fcd551 commit 6cf1230

File tree

1 file changed

+64
-13
lines changed

1 file changed

+64
-13
lines changed

src/datomic/codeq/core.clj

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@
4848
:db/unique :db.unique/identity
4949
:db.install/_attribute :db.part/db}
5050

51+
{:db/id #db/id[:db.part/db]
52+
:db/ident :git/commits
53+
:db/valueType :db.type/ref
54+
:db/cardinality :db.cardinality/many
55+
:db/doc "Associate repo with these git commits"
56+
:db.install/_attribute :db.part/db}
57+
58+
{:db/id #db/id[:db.part/db]
59+
:db/ident :git/repo
60+
:db/valueType :db.type/string
61+
:db/cardinality :db.cardinality/one
62+
:db/doc "A git repo uri"
63+
:db/unique :db.unique/identity
64+
:db.install/_attribute :db.part/db}
65+
5166
{:db/id #db/id[:db.part/db]
5267
:db/ident :git/parents
5368
:db/valueType :db.type/ref
@@ -180,6 +195,27 @@
180195
;;040000 tree 6b880666740300ac57361d5aee1a90488ba1305c src
181196
;;040000 tree 407924e4812c72c880b011b5a1e0b9cb4eb68cfa test
182197

198+
;; example git remote origin
199+
;;RichMacPro:codeq rich$ git remote show -n origin
200+
;;* remote origin
201+
;; Fetch URL: https://github.com/Datomic/codeq.git
202+
;; Push URL: https://github.com/Datomic/codeq.git
203+
;; HEAD branch: (not queried)
204+
205+
(defn get-repo-uri
206+
"returns [uri name]"
207+
[]
208+
(with-open [s (exec-stream (str "git remote show -n origin"))]
209+
(let [es (line-seq s)
210+
^String line (second es)
211+
uri (subs line (inc (.lastIndexOf line " ")))
212+
noff (.lastIndexOf uri "/")
213+
noff (if (not (pos? noff)) (.lastIndexOf uri ":") noff)
214+
name (subs uri (inc noff))
215+
_ (assert (and (pos? (count name)) (.endsWith name ".git")) "Can't find remote origin")
216+
name (subs name 0 (.indexOf name "."))]
217+
[uri name])))
218+
183219
(defn dir
184220
"Returns [[sha :type filename] ...]"
185221
[tree]
@@ -236,7 +272,7 @@
236272
~g)))
237273

238274
(defn commit-tx-data
239-
[db {:keys [sha msg tree parents author authored committer committed] :as commit}]
275+
[db repo repo-name {:keys [sha msg tree parents author authored committer committed] :as commit}]
240276
(let [tempid? map? ;;todo - better pred
241277
sha->id (index->id-fn db :git/sha)
242278
email->id (index->id-fn db :email/address)
@@ -266,9 +302,10 @@
266302
data es))
267303
data)]
268304
[nodeid data]))
269-
[treeid treedata] (tx-data [tree :tree ""])
305+
[treeid treedata] (tx-data [tree :tree repo-name])
270306
tx (into treedata
271-
[{:db/id (d/tempid :db.part/tx)
307+
[[:db/add repo :git/commits cid]
308+
{:db/id (d/tempid :db.part/tx)
272309
:git/commit cid
273310
:codeq/op :import}
274311
(cond-> {:db/id cid
@@ -324,18 +361,30 @@
324361
conn))
325362

326363
(defn import-git
327-
[conn commits]
328-
(doseq [commit commits]
329-
(let [db (d/db conn)]
330-
(println "Importing commit:" (:sha commit))
331-
(d/transact conn (commit-tx-data db commit))))
332-
(d/request-index conn)
333-
(println "Import complete!"))
364+
[conn repo-uri repo-name commits]
365+
;;todo - add already existing commits to new repo if it includes them
366+
(println "Importing repo:" repo-uri "as:" repo-name)
367+
(let [db (d/db conn)
368+
repo
369+
(or (ffirst (d/q '[:find ?e :in $ ?uri :where [?e :git/repo ?uri]] db repo-uri))
370+
(let [temp (d/tempid :db.part/user)
371+
tx-ret @(d/transact conn [[:db/add temp :git/repo repo-uri]])
372+
repo (d/resolve-tempid (d/db conn) (:tempids tx-ret) temp)]
373+
(println "Adding repo" repo-uri)
374+
repo))]
375+
(doseq [commit commits]
376+
(let [db (d/db conn)]
377+
(println "Importing commit:" (:sha commit))
378+
(d/transact conn (commit-tx-data db repo repo-name commit))))
379+
(d/request-index conn)
380+
(println "Import complete!")))
334381

335382
(defn main [& [db-uri commit]]
336383
(if db-uri
337-
(let [conn (ensure-db db-uri)]
338-
(import-git conn (unimported-commits (d/db conn) commit)))
384+
(let [conn (ensure-db db-uri)
385+
[repo-uri repo-name] (get-repo-uri)]
386+
;(prn repo-uri)
387+
(import-git conn repo-uri repo-name (unimported-commits (d/db conn) commit)))
339388
(println "Usage: datomic.codeq.core db-uri [commit-name]")))
340389

341390
(defn -main
@@ -346,7 +395,7 @@
346395

347396

348397
(comment
349-
(def uri "datomic:mem://codeq")
398+
(def uri "datomic:mem://git")
350399
;;(def uri "datomic:free://localhost:4334/codeq")
351400
(datomic.codeq.core/main uri "c3bd979cfe65da35253b25cb62aad4271430405c")
352401
(datomic.codeq.core/main uri "20f8db11804afc8c5a1752257d5fdfcc2d131d08")
@@ -355,4 +404,6 @@
355404
(def conn (d/connect uri))
356405
(def db (d/db conn))
357406
(seq (d/datoms db :aevt :file/name))
407+
(seq (d/datoms db :aevt :git/tree))
408+
(d/q '[:find ?e :where [?f :file/name "core.clj"] [?n :git/filename ?f] [?n :git/object ?e]] db)
358409
)

0 commit comments

Comments
 (0)