-
Notifications
You must be signed in to change notification settings - Fork 7
/
git-applypr
executable file
·89 lines (77 loc) · 1.95 KB
/
git-applypr
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
#!/bin/bash
# SPDX-License-Identifier: LGPL-2.1-or-later
set -euo pipefail
OPT_REMOTE=
OPT_LINK=
OPT_SIGN=
OPT_EDIT=--no-edit
OPT_INTERACTIVE=
usage() {
echo "
Usage: $(basename $0) [OPTIONS] <PR> [trailers ...]
Fetch a pull-request from github
OPTIONS
-l Add link to the PR in the commit message using applypr.link-base from config
-s Add Signed-off-by by the current user
-r <remote> Use <remote>, otherwise uses fetchpr.remote from config
-e Edit commit message
-i Use --iterative for git-rebase when applying
" 1>&$1;
exit $(($1 - 1));
}
setup_link_base() {
OPT_LINK=$(git config --get --default="" applypr.link-base)
if [ -z "$OPT_LINK" ]; then
echo "Option -l requires applypr.link-base from config" >&2
usage 2
fi
# remove trailing / if it exists
OPT_LINK="${OPT_LINK%/}"
}
setup_sign() {
OPT_SIGN="Signed-off-by: $(git config --get user.name) <$(git config --get user.email)>"
}
setup_remote() {
if [[ "${1+defined}" == defined ]]; then
OPT_REMOTE=$1
else
OPT_REMOTE="$(git config --get fetchpr.remote)"
fi
}
setup_edit() {
OPT_EDIT=--edit
}
args=0
while getopts "lsr:eih" o; do
case "${o}" in
l) setup_link_base; args=$[$args + 1];;
s) setup_sign; args=$[$args + 1];;
r) setup_remote $OPTARG; args=$[$args + 2] ;;
e) setup_edit; args=$[$args + 1];;
i) OPT_INTERACTIVE=--interactive; args=$[$args + 1];;
h) usage 1;;
\?) usage 2;;
esac
done
shift $args
setup_remote
branch=$(git rev-parse --abbrev-ref HEAD)
pr=$1
shift
trailers=()
trailers+=("$@")
if [[ -n "$OPT_LINK" ]]; then
trailers+=("Link: $OPT_LINK/pull/$pr")
fi
if [[ -n "$OPT_SIGN" ]]; then
trailers+=("$OPT_SIGN")
fi
args=("${trailers[@]/#/--trailer \'}")
args=("${args[@]/%/\'}")
git fetchpr -r $OPT_REMOTE $pr
git rebase $OPT_INTERACTIVE \
-x "git -c trailer.where=end -c trailer.if-exists=addIfDifferent commit --amend $OPT_EDIT ${args[*]}" \
--onto HEAD HEAD FETCH_HEAD
if [[ "$branch" != "HEAD" ]]; then
git checkout -B $branch HEAD
fi