48
48
:db/unique :db.unique/identity
49
49
:db.install/_attribute :db.part/db }
50
50
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
+
51
66
{:db/id #db/id[:db.part/db ]
52
67
:db/ident :git/parents
53
68
:db/valueType :db.type/ref
180
195
; ;040000 tree 6b880666740300ac57361d5aee1a90488ba1305c src
181
196
; ;040000 tree 407924e4812c72c880b011b5a1e0b9cb4eb68cfa test
182
197
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
+
183
219
(defn dir
184
220
" Returns [[sha :type filename] ...]"
185
221
[tree]
236
272
~g)))
237
273
238
274
(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}]
240
276
(let [tempid? map? ; ;todo - better pred
241
277
sha->id (index->id-fn db :git/sha )
242
278
email->id (index->id-fn db :email/address )
266
302
data es))
267
303
data)]
268
304
[nodeid data]))
269
- [treeid treedata] (tx-data [tree :tree " " ])
305
+ [treeid treedata] (tx-data [tree :tree repo-name ])
270
306
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 )
272
309
:git/commit cid
273
310
:codeq/op :import }
274
311
(cond-> {:db/id cid
324
361
conn))
325
362
326
363
(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!" )))
334
381
335
382
(defn main [& [db-uri commit]]
336
383
(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)))
339
388
(println " Usage: datomic.codeq.core db-uri [commit-name]" )))
340
389
341
390
(defn -main
346
395
347
396
348
397
(comment
349
- (def uri " datomic:mem://codeq " )
398
+ (def uri " datomic:mem://git " )
350
399
; ;(def uri "datomic:free://localhost:4334/codeq")
351
400
(datomic.codeq.core/main uri " c3bd979cfe65da35253b25cb62aad4271430405c" )
352
401
(datomic.codeq.core/main uri " 20f8db11804afc8c5a1752257d5fdfcc2d131d08" )
355
404
(def conn (d/connect uri))
356
405
(def db (d/db conn))
357
406
(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)
358
409
)
0 commit comments