Skip to content

Commit db3109b

Browse files
authored
Merge pull request #19 from mcorbin/jgit
Add JGit to manage the crate index
2 parents 7b6ef01 + 12d60ee commit db3109b

5 files changed

Lines changed: 94 additions & 11 deletions

File tree

dev/resources/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ logging:
1414
com.zaxxer.hikari.pool.HikariPool: info
1515
org.apache.http: error
1616
io.netty.buffer.PoolThreadCache: error
17+
org.eclipse.jgit.internal.storage.file.FileSnapshot: info
1718
metadata:
19+
type: "shell"
1820
path: "/home/mathieu/prog/rust/testregistry"
1921
target: "origin/master"
2022
url: "https://github.com/mcorbin/testregistry"

project.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
[org.clojure/java.jdbc "0.7.11"]
3333
[seancorfield/next.jdbc "1.0.409"]
3434
[org.clojure/tools.logging "1.0.0"]
35+
[org.eclipse.jgit/org.eclipse.jgit "5.7.0.202003110725-r"]
3536
[org.postgresql/postgresql "42.2.11"]
3637
[ragtime "0.8.0"]
3738
[ring/ring-core "1.8.0"]

src/meuse/git.clj

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
(ns meuse.git
22
"Interacts with a git repository"
3-
(:require [meuse.config :refer [config]]
3+
(:require [meuse.config :as config]
44
[meuse.log :as log]
55
[meuse.metric :as metric]
66
[exoscale.ex :as ex]
77
[mount.core :refer [defstate]]
8+
[clojure.java.io :as io]
89
[clojure.java.shell :as shell]
9-
[clojure.string :as string]))
10+
[clojure.string :as string])
11+
(:import (org.eclipse.jgit.api Git)
12+
(org.eclipse.jgit.storage.file FileRepositoryBuilder)
13+
(org.eclipse.jgit.transport CredentialsProvider
14+
RefSpec
15+
UsernamePasswordCredentialsProvider)))
1016

11-
(defprotocol Git
17+
(defprotocol IGit
1218
(add [this])
1319
(commit [this msg-header msg-body])
1420
(get-lock [this])
@@ -31,7 +37,7 @@
3137
:command args}))))))
3238

3339
(defrecord LocalRepository [path target lock]
34-
Git
40+
IGit
3541
(add [this]
3642
(git-cmd path ["add" "."]))
3743
(commit [this msg-header msg-body]
@@ -43,6 +49,56 @@
4349
(pull [this]
4450
(git-cmd path ["pull" target])))
4551

52+
(defrecord JGitFileRepository [target
53+
lock
54+
^Git git
55+
^CredentialsProvider credentials]
56+
IGit
57+
(add [this]
58+
(doto (.add git)
59+
(.addFilepattern ".")
60+
(.call)))
61+
(commit [this msg-header msg-body]
62+
(doto (.commit git)
63+
(.setMessage (str msg-header "\n\n" msg-body))
64+
(.call)))
65+
(get-lock [this]
66+
lock)
67+
(pull [this]
68+
(let [[remote branch] (string/split target #"/")]
69+
(doto (.pull git)
70+
(.setCredentialsProvider credentials)
71+
(.setRemote remote)
72+
(.setRemoteBranchName branch)
73+
(.call))))
74+
(push [this]
75+
(let [[remote branch] (string/split target #"/")
76+
ref-spec (RefSpec. branch)]
77+
(doto (.push git)
78+
(.setCredentialsProvider credentials)
79+
(.setRemote remote)
80+
(.setRefSpecs (into-array [ref-spec]))
81+
(.call)))))
82+
83+
(defn build-jgit
84+
[config]
85+
(map->JGitFileRepository
86+
{:credentials (UsernamePasswordCredentialsProvider.
87+
(:username config)
88+
(:password config))
89+
:git (Git/open (io/file (:path config)))
90+
:lock (java.lang.Object.)
91+
:target (:target config)}))
92+
93+
(defn build-local-git
94+
[config]
95+
(map->LocalRepository
96+
{:path (:path config)
97+
:lock (java.lang.Object.)
98+
:target (:target config)}))
99+
46100
(defstate git
47-
:start (map->LocalRepository (merge (:metadata config)
48-
{:lock (java.lang.Object.)})))
101+
:start (condp = (get-in config/config [:metadata :type])
102+
"jgit" (build-jgit (:metadata config/config))
103+
"shell" (build-local-git (:metadata config/config))
104+
(build-local-git (:metadata config/config))))

src/meuse/spec.clj

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,36 @@
9898
:http/cert
9999
:http/cacert]))
100100

101+
(defmulti metadata :type)
102+
101103
(s/def :metadata/path ::directory)
102104
(s/def :metadata/target ::non-empty-string)
103105
(s/def :metadata/url ::non-empty-string)
104-
(s/def :metadata/metadata (s/keys :req-un [:metadata/path
105-
:metadata/target
106-
:metadata/url]))
106+
107+
(defmethod metadata "shell"
108+
[_]
109+
(s/keys :req-un [:metadata/path
110+
:metadata/target
111+
:metadata/url]))
112+
113+
(s/def :metadata/username ::non-empty-string)
114+
(s/def :metadata/password ::non-empty-string)
115+
116+
(defmethod metadata "jgit"
117+
[_]
118+
(s/keys :req-un [:metadata/path
119+
:metadata/target
120+
:metadata/url
121+
:metadata/username
122+
:metadata/password]))
123+
124+
(defmethod metadata :default
125+
[_]
126+
(s/keys :req-un [:metadata/path
127+
:metadata/target
128+
:metadata/url]))
129+
130+
(s/def :metadata/metadata (s/multi-spec metadata :type))
107131

108132
(s/def :crate/path ::directory)
109133

test/meuse/helpers/git.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(ns meuse.helpers.git
2-
(:require [meuse.git :refer [Git]]))
2+
(:require [meuse.git :refer [IGit]]))
33

44
(defrecord GitMock [state lock]
5-
Git
5+
IGit
66
(add [this]
77
(swap! state conj {:cmd "add"}))
88
(commit [this msg-header msg-body]

0 commit comments

Comments
 (0)