summaryrefslogtreecommitdiffstats
path: root/scripts/create-pull-request
blob: a9cf6f94f1461cd84137de24c69b476d33607d2d (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
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
#!/bin/bash
ODIR=pull-$$
RELATIVE_TO="master"
COMMIT_ID="HEAD"
PREFIX="PATCH"
RFC=0

usage() {
CMD=$(basename $0)
cat <<EOM
Usage: $CMD [-h] [-o output_dir] [-m msg_body_file] [-s subject] [-r relative_to] [-i commit_id] -u remote -b branch
  -b branch           Branch name in the specified remote
  -c                  Create an RFC (Request for Comment) patch series
  -h                  Display this help message
  -i commit_id        Ending commit (default: HEAD)
  -m msg_body_file    The file containing a blurb to be inserted into the summary email
  -o output_dir       Specify the output directory for the messages (default: pull-PID)
  -p prefix           Use [prefix N/M] instead of [PATCH N/M] as the subject prefix
  -r relative_to      Starting commit (default: master)
  -s subject          The subject to be inserted into the summary email
  -u remote           The git remote where the branch is located

 Examples:
   $CMD -u contrib -b nitin/basic
   $CMD -u contrib -r distro/master -i nitin/distro -b nitin/distro
   $CMD -u contrib -r master -i misc -b nitin/misc -o pull-misc
   $CMD -u contrib -p "RFC PATCH" -b nitin/experimental
EOM
}

# Parse and validate arguments
while getopts "b:chi:m:o:p:r:s:u:" OPT; do
	case $OPT in
	b)
		BRANCH="$OPTARG"
		;;
	c)
		RFC=1
		;;
	h)
		usage
		exit 0
		;;
	i)
		COMMIT_ID="$OPTARG"
		;;
	m)
		BODY="$OPTARG"
		if [ ! -e "$BODY" ]; then
			echo "ERROR: Body file does not exist"
			exit 1
		fi
		;;
	o)
		ODIR="$OPTARG"
		;;
	p)
		PREFIX="$OPTARG"
		;;
	r)
		RELATIVE_TO="$OPTARG"
		;;
	s)
		SUBJECT="$OPTARG"
		;;
	u)
		REMOTE="$OPTARG"
		REMOTE_URL=$(git config remote.$REMOTE.url)
		if [ $? -ne 0 ]; then
			echo "ERROR: git config failed to find a url for '$REMOTE'"
			exit 1
		fi

		# Rewrite known private URLs to public URLs
		# Determine the repository name for use in the WEB_URL later
		case "$REMOTE_URL" in
		ssh://git@git.pokylinux.org*)
			REMOTE_REPO=$(echo $REMOTE_URL | sed "s#.*/\(.*\)#\1#")
			REMOTE_URL=${REMOTE_URL/'ssh://git@'/'git://'}
			;;
		ssh://git@git.yoctoproject.org*)
			REMOTE_REPO=$(echo $REMOTE_URL | sed "s#.*/\(.*\)#\1#")
			REMOTE_URL=${REMOTE_URL/"ssh://git@"/"git://"}
			;;
		git@github.com:*)
			REMOTE_REPO=$(echo $REMOTE_URL | sed 's#.*:\(.*\)\(\.git\)$#\1#')
			REMOTE_URL=${REMOTE_URL/"git@github.com:"/"git://github.com/"}
			;;
		esac
		# The .git suffix is optional in the URL, drop in for the REPO
		REMOTE_REPO=${REMOTE_REPO%.git}
		;;
	esac
done

if [ -z "$BRANCH" ] || [ -z "$REMOTE_URL" ]; then
	usage
	exit 1
fi

if [ $RFC -eq 1 ]; then
	PREFIX="RFC $PREFIX"
fi


# Set WEB_URL from known remotes
case "$REMOTE_URL" in
	*git.yoctoproject.org*)
		WEB_URL="http://git.yoctoproject.org/cgit.cgi/$REMOTE_REPO/log/?h=$BRANCH"
		;;
	*git.pokylinux.org*)
		WEB_URL="http://git.pokylinux.org/cgit.cgi/$REMOTE_REPO/log/?h=$BRANCH"
		;;
	*github.com*)
		WEB_URL="https://github.com/$REMOTE_REPO/tree/$BRANCH"
		;;
esac

# Perform a sanity test on the web URL. Issue a warning if it is not
# accessible, but do not abort as users may want to run offline.
if [ -n "$WEB_URL" ]; then
	wget --no-check-certificate -q $WEB_URL -O /dev/null
	if [ $? -ne 0 ]; then
		echo "WARNING: Branch '$BRANCH' was not found on the contrib git tree."
		echo "         Please check your remote and branch parameter before sending."
		echo ""
	fi
fi

if [ -e $ODIR ]; then
	echo "ERROR: output directory $ODIR exists."
	exit 1
fi
mkdir $ODIR


# Generate the patches and cover letter
git format-patch -M --subject-prefix="$PREFIX" -n -o $ODIR --thread=shallow --cover-letter $RELATIVE_TO..$COMMIT_ID > /dev/null


# Customize the cover letter
CL="$ODIR/0000-cover-letter.patch"
PM="$ODIR/pull-msg"
git request-pull $RELATIVE_TO $REMOTE_URL $COMMIT_ID >> "$PM"
if [ $? -ne 0 ]; then
	echo "ERROR: git request-pull reported an error"
	exit 1
fi

# The cover letter already has a diffstat, remove it from the pull-msg
# before inserting it.
sed -n "0,\#$REMOTE_URL# p" "$PM" | sed -i "/BLURB HERE/ r /dev/stdin" "$CL"
rm "$PM"

# If this is an RFC, make that clear in the cover letter
if [ $RFC -eq 1 ]; then
(cat <<EOM
Please review the following changes for suitability for inclusion. If you have
any objections or suggestions for improvement, please respond to the patches. If
you agree with the changes, please provide your Acked-by.

EOM
) | sed -i "/BLURB HERE/ r /dev/stdin" "$CL"
fi

# Insert the WEB_URL if there is one
if [ -n "$WEB_URL" ]; then
	echo "  $WEB_URL" | sed -i "\#$REMOTE_URL# r /dev/stdin" "$CL"
fi


# If the user specified a message body, insert it into the cover letter and
# remove the BLURB token.
if [ -n "$BODY" ]; then
	sed -i "/BLURB HERE/ r $BODY" "$CL"
	sed -i "/BLURB HERE/ d" "$CL"
fi

# If the user specified a subject, replace the SUBJECT token with it.
if [ -n "$SUBJECT" ]; then
	sed -i -e "s/\*\*\* SUBJECT HERE \*\*\*/$SUBJECT/" "$CL"
fi


# Generate report for user
cat <<EOM
The following patches have been prepared:
$(for PATCH in $(ls $ODIR/*); do echo "    $PATCH"; done)

Review their content, especially the summary mail:
    $CL

When you are satisfied, you can send them with:
    send-pull-request -a -p $ODIR
EOM