Blob


1 #!/usr/local/plan9/bin/rc
3 git=git
4 show=false
5 fn gitshow {
6 echo '%' git $*
7 git $*
8 }
9 if(! ~ $#* 0 && ~ $1 -v) {
10 git=gitshow
11 show=true
12 shift
13 }
15 if(~ $#* 0) {
16 echo 'usage: codereview <command> <args>' >[1=2]
17 exit usage
18 }
20 if(~ $#PLAN9 0) {
21 PLAN9=/usr/local/plan9
22 }
23 if(! test -d $PLAN9/lib/git) {
24 echo 'codereview: cannot find $PLAN9/lib/git' >[1=2]
25 exit git
26 }
28 if(! test -e $PLAN9/.git/hooks/commit-msg) {
29 if($show) {
30 echo '% ln -s ../../lib/git/commit-msg.hook $PLAN9/.git/hooks/commit-msg'
31 }
32 ln -s ../../lib/git/commit-msg.hook $PLAN9/.git/hooks/commit-msg
33 }
35 switch($1) {
36 case help
37 9 man 1 codereview
39 case pending
40 shift
41 if(! ~ $#* 0) {
42 echo 'usage: codereview pending' >[1=2]
43 exit usage
44 }
45 $git branch --list
47 case create
48 shift
49 if(! ~ $#* 1) {
50 echo 'usage: codereview create branchname' >[1=2]
51 exit usage
52 }
53 branch=$1
54 if(! git branch -l | 9 grep '\* master$') {
55 echo 'codereview: create not on master branch; use codereview commit' >[1=2]
56 exit master
57 }
58 if($show) {
59 echo '% git branch '$branch' && git commit || git branch -d '$branch >[1=2]
60 }
61 git branch $branch && git commit $* || git branch -d $branch
63 case commit
64 shift
65 if(git branch -l | 9 grep '\* master$') {
66 echo 'codereview: commit on master branch; use codereview create <branchname>' >[1=2]
67 exit master
68 }
69 if(~ `{git merge-base HEAD HEAD} `{git merge-base HEAD master}) {
70 # first commit on branch, somehow.
71 $git commit $*
72 exit $status
73 }
74 $git commit --amend $*
75 exit $status
77 case upload
78 if(git branch -l | 9 grep '\* master$') {
79 echo 'codereview: upload on master branch' >[1=2]
80 exit master
81 }
82 if(~ `{git merge-base HEAD HEAD} `{git merge-base HEAD master}) {
83 # no commit on branch
84 echo 'codereview: no commits yet on this feature branch' >[1=2]
85 exit commit
86 }
87 if(! 9 grep 'machine plan9port-review.googlesource.com' $HOME/.netrc >/dev/null >[2=1]) {
88 echo 'codereview: warning: cannot find plan9port-review in netrc' >[1=2]
89 }
90 $git push https://plan9port-review.googlesource.com/plan9 HEAD:refs/for/master >[2=1] | 9 sed 's/.* //'
92 case sync
93 shift
94 if(! ~ $#* 0) {
95 echo 'usage: codereview sync' >[1=2]
96 exit usage
97 }
98 $git fetch -q
100 branch=`{git branch -l | 9 sed -n 's/^\* //p'}
101 if(~ $branch master) {
102 $git merge -q --ff-only origin/master
103 exit $status
106 if(~ `{git merge-base HEAD HEAD} `{git merge-base HEAD master}) {
107 # no commit on branch
108 git merge -q --ff-only origin/master
109 exit $status
112 # Exactly this commit in master. Fast-forward from master and delete this branch.
113 if(git branch -r --contains HEAD | 9 grep '^ *origin/master$') {
114 $git checkout -q master
115 $git merge -q --ff-only origin/master
116 $git branch -q -d $branch
117 exit $status
120 changeid=`{git log -n 1 | 9 sed -n 's/^ Change-Id: //p'}
121 if(~ $#changeid 0) {
122 echo 'codereview: cannot find change id'
123 exit changeid
126 if(git log --grep 'Change-Id: '$changeid origin/master | 9 grep .) {
127 # Something like this got submitted.
128 $git checkout -q master
129 $git merge -q --ff-only origin/master
130 echo 'Change submitted but perhaps not identical to your copy.' >[1=2]
131 echo 'To remove old copy:' git branch -d $branch >[1=2]
132 exit 1
135 if(git branch -r --contains master | 9 grep '^ *origin/master$')
136 $git branch -f master origin/master
137 $git rebase -q origin/master
139 case *
140 echo 'codereview: unrecognized command '$1 >[1=2]
141 exit usage