Repository navigation
Expand file tree
/
Copy pathgit-ger
More file actions
executable file
·432 lines (375 loc) · 11.5 KB
/
Copy pathgit-ger
File metadata and controls
executable file
·432 lines (375 loc) · 11.5 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/bin/bash
# Where to store our private working information
DATADIR=.git/git-gerrit
usage()
{
echo "Usage: git gerrit init [-u <URL>] [-b <default branch>] [-p <project name>] " >&2
echo " git gerrit update" >&2
echo " git gerrit submit [-d] [-t topic] [-q]" >&2
echo " git gerrit new" >&2
exit 1
}
# Set working-branch-related globals based on a branch name
set_branch_globals()
{
export BRANCH="$1"
export BRANCHDIR="$(pwd)/${DATADIR}/${BRANCH}"
}
# Given a branch name, outputs a valid ref name for that branch on a remote.
# If the remote 'origin' exists, that will be used; otherwise, the first
# remote alphabetically that is not 'gerrit' will be used. If none is
# found, outputs empty string.
find_upstream()
{
local branch=$1
ref=$(
git for-each-ref --format='%(refname:lstrip=2)' --count=1 \
refs/remotes/origin/${branch}
)
if [ -z "${ref}" ]
then
ref=$(
git for-each-ref --format='%(refname:lstrip=2)' \
"refs/remotes/*/${branch}" \
| grep -v refs/remotes/gerrit | head -1
)
fi
echo "${ref}"
}
get_upstream()
{
git rev-parse --abbrev-ref @{upstream} 2> /dev/null
}
# Check that the current checked-out branch is a valid working branch
ensure_working_branch()
{
if [ ! -d "${BRANCHDIR}" ]
then
echo "Current branch '${BRANCH}' is not a git-gerrit working branch!" >&2
echo "Create a working branch with 'git gerrit start'" >&2
exit 1
fi
}
# Removes a working branch and all associated metadata, with confirmation.
# If that working branch is the current branch, switch to main/master.
close_working_branch()
{
local $branch=$1
}
gerrit_init()
{
local project
local url
while getopts "p:u:b:" OPTION
do
case $OPTION in
p)
project=$OPTARG
;;
u)
url=$OPTARG
;;
b)
default_branch=$OPTARG
;;
esac
done
# Determine base URL
if test -z "$url"
then
url=$(git config --get gerrit.url)
if test -z "$url"
then
echo -n "Gerrit URL not specified! " >&2
echo "You may use 'git gerrit init -u <gerrit URL>'" >&2
echo -n "or you may set the default with " >&2
echo "'git config --global gerrit.url <gerrit URL>'" >&2
exit 1
fi
else
shift
fi
# Determine project name
if test -z "$project"
then
project=$(basename $(git ls-remote --get-url origin))
if test "$project" = "origin"
then
echo "Cannot determine project based on origin URL - please use " >&2
echo "'get gerrit init -p <project name>'" >&2
exit 1
fi
fi
# Determine project's default branch name, which is assumed to exist
# as a local branch as well
if [ -z "$default_branch" ]
then
defref=$(find_upstream main)
if [ -z "${defref}" ]
then
defref=$(find_upstream master)
fi
if [ -z "${defref}" ]
then
echo "Neither 'main' nor 'master' branches exist - please specify" >&2
echo "default branch with 'git gerrit init -b <branch name>'" >&2
exit 1
fi
default_branch=$(basename "${defref}")
fi
# Ok, ready to make permanent changes.
# Delete gerrit remote if one already exists
if test "$(git ls-remote --get-url gerrit)" != "gerrit"
then
git remote remove gerrit
fi
# Add gerrit remote
git remote add gerrit "${url%%/}/${project}"
# Create data directory and save default branch info
mkdir -p "${DATADIR}"
echo -n "${default_branch}" > "${DATADIR}/default_branch"
}
gerrit_start()
{
local upstream=""
local notes=""
while getopts ":u:n:" OPTION
do
case $OPTION in
u) upstream=$OPTARG ;;
n) notes="$OPTARG" ;;
\?) echo "Unknown option '-$OPTION' to git gerrit start" >&2
usage ;;
esac
done
shift $(($OPTIND - 1))
# Reset global variables for new branch
set_branch_globals "$1"
shift
if [ -z "${BRANCH}" ]
then
echo "I don't know how you did this" >&2
usage
fi
if [ -e "${BRANCHDIR}/change-id" ]
then
echo "Branch '${BRANCH}' is already a git-gerrit working branch!" >&2
exit 1
fi
if git show-ref -q --heads ${BRANCH}
then
echo "Local branch '${BRANCH}' already exists!"
exit 1
fi
# Determine ref for upstream branch
local upref
if [ -n "${upstream}" ]
then
upref=$(find_upstream ${upstream})
fi
# Create branch, branchdir, etc.
mkdir -p "${BRANCHDIR}"
git checkout --quiet -b ${BRANCH} --track ${upref}
echo -n "${upref}" > "${BRANCHDIR}/upstream"
# Create a change-id file - just needs to be a valid git SHA ref for
# "something", ideally that won't collide, so just use date
echo -n "I$(date | git hash-object -t blob --stdin)" > "${BRANCHDIR}/change-id"
# Save initial working branch notes
echo -n "${notes}" > "${BRANCHDIR}/notes"
}
gerrit_update()
{
# Ensure current branch has no uncommitted changes
git diff-index --quiet HEAD || {
echo -n "Your current branch has uncommitted changes; please commit all " >&2
echo "before merging" >&2
exit
}
# Get upstream updates, and see if our change-ID has shown up
git fetch --all
local upstream=$(cat "${BRANCHDIR}/upstream")
local changeid=$(cat "${BRANCHDIR}/change-id")
if ! git log --exit-code \
--grep="^Change-Id: ${changeid}" ${upstream} "^${BRANCH}" > /dev/null
then
echo "huh"
fi
echo "Merging local $target onto working branch..."
git checkout $branch
if [ "$mergeop" = "rebase" ]
then
git rebase $target
else
git merge -m "Merge from master" --log --no-edit $target
fi
}
get_current_branchid()
{
branch=$(git rev-parse --abbrev-ref HEAD)
echo $branch | sed -e 's|/|_|g'
}
gerrit_submit()
{
# Determine target branch
target=$(get_upstream)
reffor=for
unset topicspec commitargs
while getopts "b:dt:q" OPTION
do
case $OPTION in
b)
target=$OPTARG
;;
d)
reffor=drafts
echo Submitting as DRAFT
;;
t)
topicspec=/$OPTARG
echo Submitting with topic: $OPTARG
;;
q)
commitargs="$commitargs --no-edit"
echo Will skip EDITOR for commit message
;;
esac
done
if ! test -d $DATADIR
then
echo "Please run 'git gerrit init' on this repository!" >&2
exit 1
fi
# Add hook - we could do this at "git gerrit init" time, but when using
# the "repo" tool the hooks directory gets reset on "repo sync" so this
# is safer
cat <<'EOF' > .git/hooks/prepare-commit-msg
#!/bin/sh
# Added automatically by git-gerrit
git gerrit prepare-commit-msg $1
EOF
chmod 755 .git/hooks/prepare-commit-msg
# Determine current working branch; save for prepare-commit-msg hook
branch=$(git rev-parse --abbrev-ref HEAD)
branchid=$(get_current_branchid)
echo $branchid > $DATADIR/current_branch
# Make sure working branch and target are different
if test "$target" = "$branch"
then
echo "Working branch and target are the same - nothing to do!" >&2
echo "Please make your changes on a dedicated local branch, not master." >&2
exit 1
fi
# Ensure current branch has no uncommitted changes
git diff-index --quiet HEAD || {
echo -n "Your current branch has uncommitted changes; please commit all " >&2
echo "before merging" >&2
exit 1
}
# Logic: Create a temporary branch based on the target
# branch. Perform a squash merge of the current branch onto the
# temporary branch, and then push that single commit to gerrit.
echo Merging changes on top of $target...
git checkout --quiet -B gerrit-submit $target
git merge --squash $branch || {
echo
echo "Error during merge - try 'git gerrit update' first" >&2
echo
git reset --quiet --hard
git checkout --quiet $branch
exit 1
}
# Make sure there's something to commit!
git diff-index --quiet HEAD && {
echo
echo -n "No changes were merged - make some commits on your branch " >&2
echo "before submitting!" >&2
echo
git reset --quiet --hard
git checkout --quiet $branch
exit 1
}
echo
echo "Merge successful - please provide a meaningful commit message"
echo
git commit ${commitargs}
git push gerrit HEAD:refs/${reffor}/${target}${topicspec}
# Save commit message for next time, and switch back to working branch
git log -1 --format=%B --no-color > $DATADIR/msg-$branchid
git checkout --quiet $branch > /dev/null
}
prepare_commit_msg()
{
# Path to commit message so far
MSG="$1"
shift
# Only do our special magic on the gerrit-submit branch
if test "$BRANCH" != "gerrit-submit"
then
exit
fi
# Determine working branch (set by gerrit submit)
if test -e "$DATADIR/current_branch"
then
BRANCH=$(cat $DATADIR/current_branch)
else
echo "Sorry, something went very wrong..." >&2
exit 1
fi
# Determine active Change-Id for working branch
if test -e "$DATADIR/id-$BRANCH"
then
CHANGE=$(cat "$DATADIR/id-$BRANCH")
# If this Change-Id already exists in the git log for the *current*
# branch (the target branch), then Gerrit has already merged it and we
# should choose a new ID
if git log --format=full | grep -q $CHANGE
then
CHANGE=""
fi
fi
# If no active Change-Id, generate one and save it, and populate the
# proposed commit message.
#
# If there IS an active Change-Id, re-use previous commit message
# (saved by gerrit submit), which should already include the Change-Id.
if test -z "$CHANGE"
then
CHANGE=$(gen_change_id)
echo $CHANGE > "$DATADIR/id-$BRANCH"
echo "PLEASE EDIT to provide a meaningful commit message!" > $MSG.new
echo >> $MSG.new
echo "The following commits from your working branch will be included:"\
>> $MSG.new
echo >> $MSG.new
# Filter out Change-Id lines from proposed commit message (in case user
# has standard Gerrit commit-msg hook installed as well)
grep -v '^ Change-Id:' $MSG \
| grep -v 'Squashed commit of the following:' >> $MSG.new
mv $MSG.new $MSG
# Append new Change-Id
echo >> $MSG
echo "# Do not delete the following line!" >> $MSG
echo "Change-Id: $CHANGE" >> $MSG
else
if test -e "$DATADIR/msg-$BRANCH"
then
cp $DATADIR/msg-$BRANCH $MSG
fi
fi
}
# "main"
# Always start at the top of the git directory
cd $(git rev-parse --git-dir)/..
# Initial value for branch globals based on current branch
set_branch_globals "$(git rev-parse --abbrev-ref HEAD)"
# Call subcommand based on first argument
CMD=$1
FUNC="gerrit_$CMD"
shift
if typeset -F "${FUNC}" >/dev/null
then
"${FUNC}" "$@"
else
echo "Unknown git-gerrit subcommand '${CMD}'!" >&2
usage
fi