1 ! Copyright (C) 2015 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs assocs.extras calendar
4 calendar.format checksums checksums.sha combinators
5 combinators.smart compression.zlib constructors grouping io
6 io.binary io.directories io.encodings.binary io.encodings.string
7 io.encodings.utf8 io.files io.files.info io.pathnames
8 io.streams.byte-array io.streams.peek kernel math math.bitwise
9 math.parser namespaces random sequences sequences.extras
10 splitting splitting.monotonic strings ;
13 ERROR: byte-expected offset ;
15 read1 [ tell-input byte-expected ] unless* ;
17 ERROR: separator-expected expected-one-of got ;
19 : read-until* ( separators -- data )
20 dup read-until [ nip ] [ separator-expected ] if ;
22 : find-git-directory ( path -- path' )
23 [ ".git" tail? ] find-up-to-root ; inline
25 ERROR: not-a-git-directory path ;
27 : current-git-directory ( -- path )
28 current-directory get find-git-directory [
29 current-directory get not-a-git-directory
32 : make-git-path ( str -- path )
33 current-git-directory prepend-path ;
35 : make-refs-path ( str -- path )
36 [ "refs/" make-git-path ] dip append-path ;
38 : make-object-path ( str -- path )
39 [ "objects/" make-git-path ] dip 2 cut append-path append-path ;
41 : make-idx-path ( sha -- path )
42 "objects/pack/pack-" ".idx" surround make-git-path ;
44 : make-pack-path ( sha -- path )
45 "objects/pack/pack-" ".pack" surround make-git-path ;
47 : git-binary-contents ( str -- contents )
48 make-git-path binary file-contents ;
50 : git-utf8-contents ( str -- contents )
51 make-git-path utf8 file-contents ;
53 : git-lines ( str -- contents )
54 make-git-path utf8 file-lines ;
56 ERROR: expected-one-line lines ;
58 : git-line ( str -- contents )
59 git-lines dup length 1 =
60 [ first ] [ expected-one-line ] if ;
62 : git-unpacked-object-exists? ( hash -- ? )
63 make-object-path file-exists? ;
65 TUPLE: index-entry ctime mtime dev ino mode uid gid size sha1 flags name ;
66 CONSTRUCTOR: <index-entry> index-entry ( ctime mtime dev ino mode uid gid size sha1 flags name -- obj ) ;
68 : read-index-entry-v2 ( -- seq )
69 4 read be> 4 read be> 2array
70 4 read be> 4 read be> 2array
71 4 read be> 4 read be> 4 read be>
72 4 read be> 4 read be> 4 read be>
73 20 read bytes>hex-string
74 2 read be> { 0 } read-until drop [ utf8 decode ] [ length ] bi
75 7 + 8 mod dup zero? [ 8 swap - ] unless read drop
78 TUPLE: git-index magic version entries checksum ;
79 CONSTRUCTOR: <git-index> git-index ( magic version entries checksum -- obj ) ;
81 ERROR: unhandled-git-version n ;
83 : git-index-contents ( -- git-index )
84 "index" make-git-path binary [
88 { 2 [ [ read-index-entry-v2 ] replicate ] }
89 [ unhandled-git-version ]
91 20 read bytes>hex-string
95 : make-git-object ( str -- obj )
97 [ "blob " ] dip [ length number>string "\0" ] [ ] bi
98 ] B{ } append-outputs-as ;
100 : path>git-object ( path -- bytes )
101 binary file-contents make-git-object sha1 checksum-bytes ;
103 : git-hash-object ( str -- hash )
104 make-git-object sha1 checksum-bytes ;
106 : changed-index-by-sha1 ( -- seq )
107 git-index-contents entries>>
108 [ [ sha1>> ] [ name>> path>git-object bytes>hex-string ] bi = ] reject ;
110 : changed-index-by-mtime ( -- seq )
111 git-index-contents entries>>
114 [ name>> file-info modified>> timestamp>unix-time >integer ] bi = not
117 TUPLE: commit hash tree parents author committer gpgsig message ;
118 CONSTRUCTOR: <commit> commit ( tree parents author committer -- obj ) ;
120 TUPLE: tree hash tree parents author committer gpgsig message ;
121 CONSTRUCTOR: <tree> tree ( -- obj ) ;
123 : last2 ( seq -- penultimate ultimate ) 2 tail* first2 ;
125 : gmt-offset>duration ( string -- duration )
126 3 cut [ string>number ] bi@
127 [ hours ] [ minutes ] bi* time+ ;
129 : git-date>string ( seq -- string )
131 [ string>number unix-time>timestamp ]
132 [ gmt-offset>duration [ time+ ] [ >>gmt-offset ] bi ] bi*
133 timestamp>git-string ;
135 : commit. ( commit -- )
137 [ hash>> "commit " prepend print ]
138 [ author>> "Author: " prepend split-words 2 head* join-words print ]
139 [ author>> split-words git-date>string "Date: " prepend print ]
140 [ message>> split-lines [ " " prepend ] map join-lines nl print nl ]
143 ERROR: unknown-field name parameter ;
145 : set-git-object-field ( obj name parameter -- obj )
147 { "tree" [ >>tree ] }
148 { "parent" [ >>parents ] }
149 { "author" [ >>author ] }
150 { "committer" [ >>committer ] }
151 { "gpgsig" [ >>gpgsig ] }
152 { "message" [ >>message ] }
156 : git-string>assoc ( string -- assoc )
158 split-lines [ nip first CHAR: \s = ] monotonic-split
161 first " " split1 2array
164 [ rest [ rest ] map ] bi
165 swap prefix join-lines 2array
169 "message" swap 2array
172 : parse-new-git-object ( string class -- commit )
173 new swap git-string>assoc [ first2 set-git-object-field ] each ; inline
175 ERROR: unknown-git-object obj ;
177 : parse-object ( bytes -- git-obj )
179 { 0 } read-until 0 = drop dup " " split1 drop {
180 { "blob" [ "unimplemented blob parsing" throw ] }
183 [ "commit" assert= ] [ string>number read ] bi*
184 commit parse-new-git-object
186 { "tree" [ tree parse-new-git-object ] }
187 [ unknown-git-object ]
191 ERROR: idx-v1-unsupported ;
193 TUPLE: idx version table triples packfile-sha1 idx-sha1 ;
194 CONSTRUCTOR: <idx> idx ( version table triples packfile-sha1 idx-sha1 -- obj ) ;
195 ! sha1, crc32, offset
197 : parse-idx-v2 ( -- idx )
199 256 4 * read 4 group [ be> ] map
201 [ [ 20 read bytes>hex-string ] replicate ]
202 [ [ 4 read ] replicate ]
203 [ [ 4 read be> ] replicate ] tri 3array flip
204 20 read bytes>hex-string
205 20 read bytes>hex-string <idx> ;
207 : parse-idx ( path -- idx )
210 { 0xff744f63 [ parse-idx-v2 ] }
211 [ idx-v1-unsupported ]
217 : read-type-length ( -- pair )
220 [ -4 shift 3 bits ] [ 4 bits ] [ ] tri
225 7 bits #bits get shift bitor
232 : read-be-length ( -- length )
233 read1* dup 0x80 mask? [
236 [ [ 1 + 7 shift ] [ 7 bits ] bi* bitor ]
241 : read-le-length ( -- length )
242 read1* dup 0x80 mask? [
245 [ 7 bits 7 shift bitor ]
250 DEFER: git-object-from-pack
252 TUPLE: insert bytes ;
253 CONSTRUCTOR: <insert> insert ( bytes -- insert ) ;
254 TUPLE: copy offset size ;
255 CONSTRUCTOR: <copy> copy ( offset size -- copy ) ;
257 : parse-delta ( -- delta/f )
263 dup 0x01 mask? [ [ read1* bitor ] 2dip ] when
264 dup 0x02 mask? [ [ read1* 8 shift bitor ] 2dip ] when
265 dup 0x04 mask? [ [ read1* 16 shift bitor ] 2dip ] when
266 dup 0x08 mask? [ [ read1* 24 shift bitor ] 2dip ] when
267 dup 0x10 mask? [ [ read1* bitor ] dip ] when
268 dup 0x20 mask? [ [ read1* 8 shift bitor ] dip ] when
269 dup 0x40 mask? [ [ read1* 16 shift bitor ] dip ] when
270 drop [ 65536 ] when-zero <copy>
276 : parse-deltas ( bytes -- deltas )
280 [ parse-delta ] loop>array 3array
283 ERROR: unknown-delta-operation op ;
285 : apply-delta ( delta -- )
287 { [ dup insert? ] [ bytes>> write ] }
288 { [ dup copy? ] [ [ offset>> seek-absolute seek-input ] [ size>> read write ] bi ] }
289 [ unknown-delta-operation ]
292 : do-deltas ( bytes delta-bytes -- bytes' )
295 _ parse-deltas third [ apply-delta ] each
300 ERROR: unsupported-packed-raw-type type ;
302 : read-packed-raw ( -- string )
303 read-type-length first2 swap {
304 { 1 [ 256 + read uncompress ] }
305 [ unsupported-packed-raw-type ]
308 SYMBOL: initial-offset
310 : read-offset-delta ( size -- obj )
311 [ read-be-length neg initial-offset get + ] dip 256 + read uncompress
312 [ seek-absolute seek-input read-packed-raw ] dip 2array ;
314 : read-sha1-delta ( size -- obj )
315 [ 20 read bytes>hex-string git-object-from-pack ] dip read uncompress 2array ;
317 ! XXX: actual length is stored in the gzip header
318 ! We add 256 instead of using it for now.
319 : read-packed ( -- obj/f )
320 tell-input initial-offset [
321 read-type-length first2 swap {
322 { 1 [ 256 + read uncompress parse-object ] }
323 { 6 [ read-offset-delta first2 do-deltas parse-object ] }
324 ! { 7 [ B read-sha1-delta ] }
325 [ number>string "unknown packed type: " prepend throw ]
329 : parse-packed-object ( sha1 offset -- obj )
330 [ make-pack-path binary ] dip '[
331 input-stream [ <peek-stream> ] change
332 _ seek-absolute seek-input read-packed
335 ! http://stackoverflow.com/questions/18010820/git-the-meaning-of-object-size-returned-by-git-verify-pack
336 TUPLE: pack magic version count objects sha1 ;
337 : parse-pack ( path -- pack )
339 input-stream [ <peek-stream> ] change
343 [ peek1 ] [ read-packed ] produce 2array
346 : git-read-idx ( sha -- obj ) make-idx-path parse-idx ;
349 ! : git-read-pack ( sha -- obj ) make-pack-path parse-pack ;
351 : parsed-idx>hash ( seq -- hash )
356 [ first3 rot [ 3array ] dip _ set-at ] with each
360 MEMO: git-parse-all-idx ( -- seq )
361 "objects/pack/" make-git-path qualified-directory-files
362 [ ".idx" tail? ] filter
366 ERROR: no-pack-for sha1 ;
368 : find-pack-for ( sha1 -- triple )
369 git-parse-all-idx ?at [ no-pack-for ] unless ;
371 : git-object-from-pack ( sha1 -- pack )
372 [ find-pack-for [ first ] [ third ] bi parse-packed-object ] keep >>hash ;
374 : git-object-contents ( hash -- contents )
375 make-object-path binary file-contents uncompress ;
377 : git-read-object ( sha -- obj )
378 dup git-unpacked-object-exists? [
379 [ git-object-contents parse-object ] keep >>hash
384 ! !: git-object-contents ( hash -- contents )
385 ! make-object-path ! binary file-contents uncompress ;
386 ! [ git-read-object ] [ git-object-from-pack ] if ;
388 : parsed-idx>hash2 ( seq -- hash )
390 [ triples>> [ [ drop f ] [ first ] bi ] [ set-at ] sequence>hashtable ]
391 [ packfile-sha1>> ] bi
392 ] [ set-at ] sequence>hashtable ; inline
394 ERROR: expected-ref got ;
396 : parse-ref-line ( string -- string' )
398 dup "ref:" = [ drop ] [ expected-ref ] if
401 : list-refs ( -- seq )
402 current-git-directory "refs/" append-path recursive-directory-files ;
404 : remote-refs-dirs ( -- seq )
405 "remotes" make-refs-path directory-files ;
407 : ref-contents ( str -- line ) make-refs-path git-line ;
408 : git-stash-ref-sha1 ( -- contents ) "stash" ref-contents ;
409 : git-ref ( ref -- sha1 ) git-line parse-ref-line ;
410 : git-head-ref ( -- sha1 ) "HEAD" git-ref ;
411 : git-log-for-ref ( ref -- log ) git-line git-read-object ;
412 : git-head-object ( -- commit ) git-head-ref git-log-for-ref ;
413 : git-config ( -- config ) "config" make-git-path ;
416 ERROR: repeated-parent-hash hash ;
421 parents>> dup string? [ random ] unless [
422 dup git-unpacked-object-exists?
423 [ git-read-object ] [ git-object-from-pack ] if