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