aboutsummaryrefslogtreecommitdiff
path: root/bin/codereview
blob: ffa0e3cd907eb12e641bbb622e679de9b801f314 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/local/plan9/bin/rc

git=git
show=false
fn gitshow {
	echo '%' git $*
	git $*
}
if(! ~ $#* 0 && ~ $1 -v) {
	git=gitshow
	show=true
	shift
}
	
if(~ $#* 0) {
	echo 'usage: codereview <command> <args>' >[1=2]
	exit usage
}

if(~ $#PLAN9 0) {
	PLAN9=/usr/local/plan9
}
if(! test -d $PLAN9/lib/git) {
	echo 'codereview: cannot find $PLAN9/lib/git' >[1=2]
	exit git
}

if(! test -e $PLAN9/.git/hooks/commit-msg) {
	if($show) {
		echo '% ln -s ../../lib/git/commit-msg.hook $PLAN9/.git/hooks/commit-msg'
	}
	ln -s ../../lib/git/commit-msg.hook $PLAN9/.git/hooks/commit-msg
}

switch($1) {
case help
	9 man 1 codereview

case pending
	shift
	if(! ~ $#* 0) {
		echo 'usage: codereview pending' >[1=2]
		exit usage
	}
	$git branch -v

case create
	shift
	if(~ $#* 0) {
		echo 'usage: codereview create branchname' >[1=2]
		exit usage
	}
	branch=$1
	shift
	if(! git branch -l | 9 grep '\* master$' >/dev/null) {
		echo 'codereview: create not on master branch; use codereview commit' >[1=2]
		exit master
	}
	if($show) {
		echo '% git branch '$branch' && git commit || git branch -d '$branch >[1=2]
	}
	git branch $branch && git checkout $branch && git commit -a $* || git branch -d $branch

case commit
	shift
	if(git branch -l | 9 grep '\* master$' >/dev/null) {
		echo 'codereview: commit on master branch; use codereview create <branchname>' >[1=2]
		exit master
	}
	if(~ `{git merge-base HEAD HEAD} `{git merge-base HEAD master}) {
		# first commit on branch, somehow.
		$git commit $*
		exit $status
	}
	$git commit --amend -a $*
	exit $status

case upload
	if(git branch -l | 9 grep '\* master$' >/dev/null) {
		echo 'codereview: upload on master branch' >[1=2]
		exit master
	}
	if(~ `{git merge-base HEAD HEAD} `{git merge-base HEAD master}) {
		# no commit on branch
		echo 'codereview: no commits yet on this feature branch' >[1=2]
		exit commit
	}
	if(! 9 grep 'machine plan9port-review.googlesource.com' $HOME/.netrc >/dev/null >[2=1]) {
		echo 'codereview: warning: cannot find plan9port-review in netrc' >[1=2]
	}
	if(! git status | 9 grep 'nothing to commit, working directory clean' >/dev/null) {
		echo 'codereview: warning: local changes not yet committed' >[1=2]
		git status
	}
	$git push https://plan9port-review.googlesource.com/plan9 HEAD:refs/for/master >[2=1] | 9 sed 's/.*
//'

case sync
	shift
	if(! ~ $#* 0) {
		echo 'usage: codereview sync' >[1=2]
		exit usage
	}
	$git fetch -q
	
	branch=`{git branch -l | 9 sed -n 's/^\* //p'}
	if(~ $branch master) {
		$git merge -q --ff-only origin/master
		exit $status
	}

	if(~ `{git merge-base HEAD HEAD} `{git merge-base HEAD master}) {
		# no commit on branch
		git merge -q --ff-only origin/master
		exit $status
	}

	# Exactly this commit in master. Fast-forward from master and delete this branch.
	if(git branch -r --contains HEAD | 9 grep '^ *origin/master$' >/dev/null) {
		$git checkout -q master
		$git merge -q --ff-only origin/master
		$git branch -q -d $branch
		exit $status
	}
	
	changeid=`{git log -n 1 | 9 sed -n 's/^    Change-Id: //p'}
	if(~ $#changeid 0) {
		echo 'codereview: cannot find change id'
		exit changeid
	}

	if(git log --grep 'Change-Id: '$changeid origin/master | 9 grep . >/dev/null) {
		# Something like this got submitted.
		$git checkout -q master
		$git merge -q --ff-only origin/master
		echo 'Change submitted but perhaps not identical to your copy.' >[1=2]
		echo 'To remove old copy:' git branch -d $branch >[1=2]
		exit 1
	}
	
	if(git branch -r --contains master | 9 grep '^ *origin/master$' >/dev/null)
		$git branch -f master origin/master
	$git rebase -q origin/master

case *
	echo 'codereview: unrecognized command '$1 >[1=2]
	exit usage
}