-
Notifications
You must be signed in to change notification settings - Fork 4
/
porcelain.lisp
180 lines (153 loc) · 6.13 KB
/
porcelain.lisp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
(in-package :fwoar.cl-git)
(defvar *git-repository*)
(setf (documentation '*git-repository* 'variable)
"The git repository path for porcelain commands to operate on.")
(defvar *git-encoding* :utf-8
"The encoding to use when parsing git objects")
(defun co.fwoar.git:repository ()
*git-repository*)
(defun co.fwoar.git:in-repository (root)
(setf *git-repository*
(ensure-repository
(truename root))))
(defmacro co.fwoar.git:with-repository ((root) &body body)
`(let ((*git-repository* (ensure-repository ,root)))
,@body))
(defun co.fwoar.git:show-repository ()
*git-repository*)
(defun in-git-package (symbol)
(intern (symbol-name symbol)
:co.fwoar.git))
(defun handle-list (_1)
(case (in-git-package (car _1))
(co.fwoar.git::unwrap `(uiop:nest (car)
(mapcar ,@(cdr _1))))
(t (cons (in-git-package (car _1))
(cdr _1)))))
(defun co.fwoar.git::resolve-refish (it)
(flet ((hash-p (it)
(and (> (length it) 32)
(every (serapeum:op
(digit-char-p _1 16))
it))))
(cond
((block is-branch
(mapc (fw.lu:destructuring-lambda ((name hash))
(when (equal it name)
(return-from is-branch
(ensure-ref hash))))
(branches *git-repository*))
nil))
((hash-p it) (ensure-ref it)))))
(defmacro co.fwoar.git:git (&rest commands)
`(uiop:nest ,@(reverse
(funcall (data-lens:<>1
(data-lens:over (serapeum:op
(typecase _1
(string `(identity ,_1))
(list (handle-list _1)))))
(data-lens:transform-head (serapeum:op
(etypecase _1
(string `(co.fwoar.git::resolve-refish ,_1))
(t _1)))))
commands))))
(defun co.fwoar.git::ensure-ref (it)
(ensure-ref it))
(defun co.fwoar.git::decode (it)
(babel:octets-to-string it :encoding *git-encoding*))
(defun co.fwoar.git::<<= (fun &rest args)
(apply #'mapcan fun args))
(defmacro co.fwoar.git::map (fun list)
(alexandria:once-only (list)
(alexandria:with-gensyms (it)
`(mapcar ,(if (consp fun)
`(lambda (,it)
(,(in-git-package (car fun))
,@(cdr fun)
,it))
`',(in-git-package fun))
,list))))
(defmacro co.fwoar.git::juxt (&rest args)
(let ((funs (butlast args))
(arg (car (last args))))
(alexandria:once-only (arg)
`(list ,@(mapcar (lambda (f)
`(,@(alexandria:ensure-list f) ,arg))
funs)))))
(defmacro co.fwoar.git::pipe (&rest funs)
(let ((funs (reverse (butlast funs)))
(var (car (last funs))))
`(uiop:nest ,@(mapcar (lambda (it)
(if (consp it)
`(,(in-git-package (car it)) ,@(cdr it))
`(,(in-git-package it))))
funs)
,var)))
(defun co.fwoar.git::filter (fun &rest args)
(apply #'remove-if-not fun args))
(defun co.fwoar.git::object (thing)
(extract-object thing))
(defun co.fwoar.git:show (object)
(extract-object object))
(defun co.fwoar.git:contents (object)
(co.fwoar.git:show object))
(defun co.fwoar.git:component (&rest args)
(let ((component-list (butlast args))
(target (car (last args))))
(fwoar.cl-git::component component-list target)))
(defun co.fwoar.git:tree (commit-object)
(component :tree
commit-object))
(defun co.fwoar.git::filter-tree (name-pattern tree)
#+lispworks
(declare (notinline serapeum:string-prefix-p))
(let* ((tree-entries (component :entries tree))
(scanner (cl-ppcre:create-scanner name-pattern)))
(remove-if-not (serapeum:op
(cl-ppcre:scan scanner _))
tree-entries
:key #'te-name)))
(defun co.fwoar.git:branch (&optional (branch nil branch-p))
#+lispworks
(declare (notinline serapeum:assocadr))
(let* ((branches (branches *git-repository*))
(branch-hash (if branch-p
(serapeum:assocadr (etypecase branch
(string branch)
(keyword (string-downcase branch)))
branches
:test 'equal)
(or (serapeum:assocadr "master" branches :test 'equal)
(serapeum:assocadr "main" branches :test 'equal)))))
(if branch-hash
(ref *git-repository*
branch-hash)
(error "branch ~s not found" branch))))
(defun co.fwoar.git:branches ()
(branches *git-repository*))
(defun co.fwoar.git::parents (commit)
(mapcar 'ensure-ref
(component :parents commit)))
(defun co.fwoar.git:commit-parents (commit)
(co.fwoar.git::parents commit))
(defun co.fwoar.git:rev-list (ref-id &optional (limit nil limit-p))
"Return the commits reachable from the ref."
(when limit-p
(rotatef ref-id limit))
(let ((seen (make-hash-table)))
(labels ((iterate (queue accum &optional (count 0))
(if (or (when limit-p
(= limit count))
(null queue))
accum
(destructuring-bind (next . rest) queue
(let ((parents (co.fwoar.git::parents next)))
(iterate (append rest parents)
(if (gethash next seen)
accum
(progn
(setf (gethash next seen) t)
(cons next accum)))
(1+ count)))))))
(iterate (list (ensure-ref ref-id))
()))))