]> gitweb.factorcode.org Git - factor.git/blob - extra/mason/git/git.factor
Fix comments to be ! not #!.
[factor.git] / extra / mason / git / git.factor
1 ! Copyright (C) 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors combinators.short-circuit continuations
4 debugger io io.directories io.directories.hierarchy
5 io.encodings.utf8 io.files io.launcher io.sockets
6 io.streams.string kernel mason.common mason.email sequences
7 splitting ;
8 IN: mason.git
9
10 : git-id ( -- id )
11     { "git" "show" } utf8 [ lines ] with-process-reader
12     first " " split second ;
13
14 <PRIVATE
15
16 : git-clone-cmd ( -- cmd )
17     {
18         "git"
19         "clone"
20         "git://factorcode.org/git/factor.git"
21     } ;
22
23 : git-clone ( -- )
24     ! Must be run from builds-dir
25     "Cloning initial repository" print-timestamp
26     git-clone-cmd try-output-process ;
27
28 : git-pull-cmd ( -- cmd )
29     {
30         "git"
31         "pull"
32         "git://factorcode.org/git/factor.git"
33         "master"
34     } ;
35
36 : repo-corrupted-body ( error -- string )
37     [
38         "Corrupted repository on " write host-name write " will be re-cloned." print
39         "Error while pulling was:" print
40         nl
41         error.
42     ] with-string-writer ;
43
44 : git-repo-corrupted ( error -- )
45     repo-corrupted-body "corrupted repo" email-fatal
46     "factor" delete-tree
47     git-clone ;
48
49 : git-pull-failed ( error -- )
50     dup output-process-error? [
51         dup output>> "not uptodate. Cannot merge." swap subseq?
52         [ git-repo-corrupted ]
53         [ rethrow ]
54         if
55     ] [ rethrow ] if ;
56
57 : git-status-cmd ( -- cmd )
58     { "git" "status" } ;
59
60 : git-status-failed ( error -- )
61     ! Exit code 1 means there's nothing to commit.
62     dup { [ process-failed? ] [ code>> 1 = ] } 1&&
63     [ drop ] [ rethrow ] if ;
64
65 : git-status ( -- seq )
66     [
67         git-status-cmd utf8 [ lines ] with-process-reader*
68         { 0 1 } member? [ drop ] [ process-failed ] if
69         [ "#\t" head? ] filter
70     ] [ git-status-failed { } ] recover ;
71
72 : check-repository ( -- seq )
73     "factor" [ git-status ] with-directory ;
74
75 : repo-dirty-body ( error -- string )
76     [
77         "Dirty repository on " write host-name write " will be re-cloned." print
78         "Modified and untracked files:" print nl
79         [ print ] each
80     ] with-string-writer ;
81
82 : git-repo-dirty ( files -- )
83     repo-dirty-body "dirty repo" email-fatal
84     "factor" delete-tree
85     git-clone ;
86
87 PRIVATE>
88
89 : git-clone-or-pull ( -- id )
90     ! Must be run from builds-dir.
91     "factor" exists? [
92         check-repository [
93             "factor" [
94                 [ git-pull-cmd short-running-process ]
95                 [ git-pull-failed ]
96                 recover
97             ] with-directory
98         ] [ git-repo-dirty ] if-empty
99     ] [ git-clone ] if
100     "factor" [ git-id ] with-directory ;