This repository was archived by the owner on Jun 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgit_utils.clj
84 lines (66 loc) · 2.6 KB
/
git_utils.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(ns lambdacd-git.git-utils
(:require [me.raynes.conch :refer [let-programs]]
[clojure.java.io :as io]
[clojure.string :as s])
(:import (java.nio.file Files)
(java.nio.file.attribute FileAttribute)
(java.util Date)))
(defn git [git-handle & args]
(let [theargs (concat args [{:dir (:dir git-handle)}])]
(let-programs [git "/usr/bin/git"]
(apply git theargs))))
(defn no-file-attributes []
(into-array FileAttribute []))
(defn- create-temp-dir []
(str (Files/createTempDirectory "lambdacd-git" (no-file-attributes))))
(defn git-init []
(let [dir (create-temp-dir)
git-handle {:dir dir
:remote (str "file://" dir)
:commits []
:commits-by-msg {}
:staged-file-content nil}]
(git git-handle "init")
git-handle))
(defn git-add-file [git-handle file-name file-content]
(spit (io/file (:dir git-handle) file-name) file-content)
(git git-handle "add" "-A")
(assoc git-handle :staged-file-content file-content))
(defn git-commit [git-handle msg]
(git git-handle "commit" "-m" msg "--allow-empty")
(let [new-hash (s/trim (git git-handle "rev-parse" "HEAD"))
commit-desc {:hash new-hash :file-content (:staged-file-content git-handle)}]
(-> git-handle
(update :commits #(conj % commit-desc))
(update :commits-by-msg #(assoc % msg commit-desc))
(assoc :staged-file-content nil))))
(defn git-checkout-b [git-handle new-branch]
(git git-handle "checkout" "-b" new-branch)
git-handle)
(defn git-checkout [git-handle branch]
(git git-handle "checkout" branch)
git-handle)
(defn git-tag [git-handle tag]
(git git-handle "tag" tag)
git-handle)
(defn git-tag-list [git-handle commit]
(git git-handle "tag" "-l" "--points-at" commit))
(defn git-user-name [git-handle]
(s/trim (git git-handle "config" "--get" "user.name")))
(defn git-user-email [git-handle]
(s/trim (git git-handle "config" "--get" "user.email")))
(defn commit-by-msg [git-handle msg]
(or
(get-in git-handle [:commits-by-msg msg :hash])
(throw (Exception. (str "no hash found for " msg)))))
(defn commit-timestamp-iso [git-handle hash]
(git git-handle "show" "--pretty=format:%cd" "--date=iso" hash))
(defn commit-timestamp-date [git-handle hash]
(let [timestamp (git git-handle "show" "--pretty=format:%ct" hash)]
(-> timestamp
(s/trim)
(Integer/parseInt)
(* 1000)
(Date.))))
(defn get-last-commit-msg [git-handle]
(git git-handle "log" "--pretty=%B" "-1"))