-
Notifications
You must be signed in to change notification settings - Fork 10
/
git.clj
51 lines (43 loc) · 1.85 KB
/
git.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
(ns leiningen.polylith.git
(:require [clojure.pprint :as pp]
[clojure.string :as str]
[leiningen.polylith.cmd.shared :as shared])
(:import (java.io FileNotFoundException)))
(defn git-bookmarks [ws-path]
(try
(read-string (slurp (str ws-path "/.polylith/git.edn")))
(catch FileNotFoundException _ {})))
(defn current-sha1 [ws-path]
(if-let [current-sha1 (first (str/split (shared/sh "git" "rev-parse" "HEAD" :dir ws-path) #"\n"))]
current-sha1
(shared/throw-polylith-exception "Workspace does not have a git repository or a valid first commit.")))
(defn diff [ws-path hash-1 hash-2]
(let [changed-files (if hash-1
(shared/sh "git" "diff" "--name-only" hash-1 hash-2 :dir ws-path)
(shared/sh "git" "ls-tree" "-r" "HEAD" "--name-only" :dir ws-path))]
(str/split changed-files #"\n")))
(defn last-success-sha1 [ws-path]
(:last-success (git-bookmarks ws-path)))
(defn set-bookmark! [ws-path bookmark]
(println "set" bookmark "in .polylith/git.edn")
(let [bookmarks (assoc (git-bookmarks ws-path)
bookmark (current-sha1 ws-path))
file (str ws-path "/.polylith/git.edn")]
(pp/pprint bookmarks (clojure.java.io/writer file))))
(defn bookmark->sha1 [ws-path bookmark]
(let [bookmarks (git-bookmarks ws-path)
bookmark (keyword bookmark)
sha1 (bookmarks bookmark)]
sha1))
(defn valid-sha1? [ws-path arg]
(try
(str/starts-with? (shared/sh "git" "cat-file" "-t" arg :dir ws-path) "commit")
(catch Exception _
false)))
(defn parse-git-args [ws-path [bookmark-or-sha1]]
(let [sha1 (if bookmark-or-sha1
(if (valid-sha1? ws-path bookmark-or-sha1)
bookmark-or-sha1
(bookmark->sha1 ws-path bookmark-or-sha1))
(last-success-sha1 ws-path))]
sha1))