]> gitweb.factorcode.org Git - factor.git/blob - extra/cli/git/git.factor
Switch to https urls
[factor.git] / extra / cli / git / git.factor
1 ! Copyright (C) 2017 Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays concurrency.combinators
4 concurrency.semaphores io io.directories io.encodings.utf8
5 io.files.info io.launcher io.pathnames kernel math namespaces
6 sequences splitting system-info unicode ;
7 IN: cli.git
8
9 SYMBOL: cli-git-num-parallel
10 cli-git-num-parallel [ cpus 2 * ] initialize
11
12 : git-command>string ( desc -- string )
13     utf8 <process-reader> stream-contents [ blank? ] trim-tail ;
14
15 : git-clone-as ( uri path -- process ) [ { "git" "clone" } ] 2dip 2array append run-process ;
16 : git-clone ( uri -- process ) [ { "git" "clone" } ] dip suffix run-process ;
17 : git-pull* ( -- process ) { "git" "pull" } run-process ;
18 : git-pull ( path -- process ) [ git-pull* ] with-directory ;
19 : git-fetch-all* ( -- process ) { "git" "fetch" "--all" } run-process ;
20 : git-fetch-all ( path -- process ) [ git-fetch-all* ] with-directory ;
21 : git-fetch-tags* ( -- process ) { "git" "fetch" "--tags" } run-process ;
22 : git-fetch-tags ( path -- process ) [ git-fetch-tags* ] with-directory ;
23 : git-checkout-new-branch* ( branch -- process ) [ { "git" "checkout" "-b" } ] dip suffix run-process ;
24 : git-checkout-new-branch ( path branch -- process ) '[ _ git-checkout-new-branch* ] with-directory ;
25 : git-checkout-existing* ( branch/checksum -- process ) [ { "git" "checkout" } ] dip suffix run-process ;
26 : git-checkout-existing ( path branch/checksum -- process ) '[ _ git-checkout-existing* ] with-directory ;
27 : git-change-remote* ( remote uri -- process ) [ { "git" "remote" "set-url" } ] 2dip 2array append run-process ;
28 : git-change-remote ( path remote uri -- process ) '[ _ _ git-change-remote* ] with-directory ;
29 : git-remote-add* ( remote uri -- process ) [ { "git" "remote" "add" } ] 2dip 2array append run-process ;
30 : git-remote-add ( path remote uri -- process ) '[ _ _ git-remote-add* ] with-directory ;
31 : git-remote-get-url* ( remote -- process ) [ { "git" "remote" "get-url" } ] dip suffix run-process ;
32 : git-remote-get-url ( path remote -- process ) '[ _ git-remote-get-url* ] with-directory ;
33 : git-rev-parse* ( branch -- string ) [ { "git" "rev-parse" } ] dip suffix git-command>string ;
34 : git-rev-parse ( path branch -- string ) '[ _ git-rev-parse* ] with-directory ;
35 : git-diff-name-only* ( from to -- lines )
36     [ { "git" "diff" "--name-only" } ] 2dip 2array append process-lines ;
37 : git-diff-name-only ( path from to -- lines )
38     '[ _ _ git-diff-name-only* ] with-directory ;
39
40 : git-directory? ( directory -- ? )
41     ".git" append-path current-directory get prepend-path
42     ?file-info dup [ directory? ] when ;
43
44 : git-current-branch* ( -- name )
45     { "git" "rev-parse" "--abbrev-ref" "HEAD" } git-command>string ;
46
47 : git-current-branch ( directory -- name )
48     [ git-current-branch* ] with-directory ;
49
50 : git-directory-name ( string -- string' )
51     file-name ".git" ?tail drop ;
52
53 : sync-repository ( url -- process )
54     dup git-directory-name git-directory?
55     [ git-directory-name git-pull ] [ git-clone ] if ;
56
57 : sync-repository-as ( url path -- process )
58     dup git-directory?
59     [ nip git-pull ] [ git-clone-as ] if ;
60
61 : sync-repositories ( directory urls -- )
62     '[
63         _ cli-git-num-parallel get <semaphore> '[
64             _ [ sync-repository ] with-semaphore
65         ] parallel-each
66     ] with-ensure-directory ;
67
68 : directory-entries-without-git ( directory -- entries )
69     recursive-directory-entries
70     [ name>> "/.git/" subseq-of? ] reject ;
71