]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/git/commit
git/commit: remove trailing 'subst -g'
[plan9front.git] / sys / src / cmd / git / commit
1 #!/bin/rc -e
2 rfork ne
3 . /sys/lib/git/common.rc
4
5 fn whoami{
6         name=`{git/conf user.name}
7         email=`{git/conf user.email}
8         if(test -f /adm/keys.who){
9                 if(~ $name '')
10                         name=`{awk -F'|' '$1=="'$user'" {x=$3} END{print x}' </adm/keys.who}
11                 if(~ $email '')
12                         email=`{awk -F'|' '$1=="'$user'" {x=$5} END{print x}' </adm/keys.who}
13         }
14         if(~ $name '')
15                 name=glenda
16         if(~ $email '')
17                 email=glenda@9front.local
18 }
19
20 fn findbranch{
21         branch=`{git/branch}
22         if(test -e $gitfs/branch/$branch/tree){
23                 refpath=.git/refs/$branch
24                 initial=false
25         }
26         if not if(test -e $gitfs/object/$branch/tree){
27                 refpath=.git/HEAD
28                 initial=false
29         }
30         if not if(! test -e $gitfs/HEAD/tree){
31                 refpath=.git/refs/$branch
32                 initial=true
33         }
34         if not
35                 die 'invalid branch:' $branch
36 }
37
38 # Remove commentary lines.
39 # Remove leading and trailing empty lines.
40 # Combine consecutive empty lines between paragraphs.
41 # Remove trailing spaces from lines.
42 # Ensure there's trailing newline.
43 fn cleanmsg{
44         awk '
45         /^[     ]*#/ {next}
46         /^[     ]*$/ {empty = 1; next}
47
48         wet && empty {printf "\n"}
49         {wet = 1; empty = 0}
50         {sub(/[         ]+$/, ""); print $0}
51         '
52 }
53
54 fn editmsg{
55         if(! test -s $msgfile.tmp){
56                 >$msgfile.tmp {
57                         echo '# Author:' $name '<'$email'>'
58                         echo '#'
59                         for(p in $parents)
60                                 echo '# parent:' $p
61                         git/walk -fAMR $files | subst '^' '# '
62                         echo '#'
63                         echo '# Commit message:'
64                 }
65                 edit=1
66         }
67         if(! ~ $#edit 0){
68                 giteditor=`{git/conf core.editor}
69                 if(~ $#editor 0)
70                         editor=$giteditor
71                 if(~ $#editor 0)
72                         editor=hold
73                 $editor $msgfile.tmp
74         }
75         cleanmsg < $msgfile.tmp > $msgfile
76         if(! test -s $msgfile)
77                 die 'empty commit message'
78 }
79
80 fn parents{
81         if(! ~ $#revise 0)
82                 parents=`{cat $gitfs/HEAD/parent}
83         if not if(test -f .git/index9/merge-parents)
84                 parents=`{cat .git/index9/merge-parents | sort | uniq}
85         if not if(~ $initial true)
86                 parents=()
87         if not
88                 parents=`{git/query $branch}
89 }
90
91 fn commit{
92         msg=`''{cat $msgfile}
93         if(! ~ $#parents 0)
94                 pflags='-p'^$parents
95         hash=`{git/save -n $"name -e $"email  -m $"msg $pflags $files || die $status}
96         rm -f .git/index9/merge-parents
97 }
98
99 fn update{
100         mkdir -p `{basename -d $refpath}
101         # Paranoia: let's not mangle the repo.
102         if(~ $#hash 0)
103                 die 'botched commit'
104         echo $branch: $hash
105         echo $hash > $refpath
106         for(f in $files){
107                 if(test -e .git/index9/removed/$f || ! test -e $f){
108                         rm -f .git/index9/removed/$f
109                         rm -f .git/index9/tracked/$f
110                 }
111                 if not{
112                         mkdir -p `{basename -d $f}
113                         walk -eq $f > .git/index9/tracked/$f
114                 }
115         }
116 }
117
118 fn sigexit{
119         rm -f $msgfile $msgfile.tmp
120 }
121
122 gitup
123
124 flagfmt='m:msg message, r:revise, e:edit'; args='[file ...]'
125 eval `''{aux/getflags $*} || exec aux/usage
126
127 msgfile=/tmp/git-msg.$pid
128 if(~ $#msg 1)
129         echo $msg >$msgfile.tmp
130 if not if(~ $#revise 1){
131         msg=1
132         echo revising commit `{cat $gitfs/HEAD/hash}
133         cat $gitfs/HEAD/msg >$msgfile.tmp
134 }
135
136 files=()
137 if(! ~ $#* 0)
138         files=`$nl{git/walk -c `$nl{cleanname -d $gitrel $*}}
139 if(~ $status '' || ~ $#files 0 && ! test -f .git/index9/merge-parents && ~ $#revise 0)
140         die 'nothing to commit' $status
141 @{
142         flag e +
143         whoami
144         findbranch
145         parents
146         editmsg
147         commit
148         update
149 } || die 'could not commit:' $status
150 exit ''