|
1 | 1 | (ns meuse.git |
2 | 2 | "Interacts with a git repository" |
3 | | - (:require [meuse.config :refer [config]] |
| 3 | + (:require [meuse.config :as config] |
4 | 4 | [meuse.log :as log] |
5 | 5 | [meuse.metric :as metric] |
6 | 6 | [exoscale.ex :as ex] |
7 | 7 | [mount.core :refer [defstate]] |
| 8 | + [clojure.java.io :as io] |
8 | 9 | [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))) |
10 | 16 |
|
11 | | -(defprotocol Git |
| 17 | +(defprotocol IGit |
12 | 18 | (add [this]) |
13 | 19 | (commit [this msg-header msg-body]) |
14 | 20 | (get-lock [this]) |
|
31 | 37 | :command args})))))) |
32 | 38 |
|
33 | 39 | (defrecord LocalRepository [path target lock] |
34 | | - Git |
| 40 | + IGit |
35 | 41 | (add [this] |
36 | 42 | (git-cmd path ["add" "."])) |
37 | 43 | (commit [this msg-header msg-body] |
|
43 | 49 | (pull [this] |
44 | 50 | (git-cmd path ["pull" target]))) |
45 | 51 |
|
| 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 | + |
46 | 100 | (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)))) |
0 commit comments