summaryrefslogtreecommitdiff
path: root/util/scripts/cross-repo-cherrypick
blob: 1f1931b35beaf4ec1fb4f8c3fcd0f606a6965e8e (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
#!/bin/sh
# cross-repo-cherrypick - rebase helper script
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#

# Adapt to your remote branch:
BRANCH="origin/master"

# When pulling in patches from another tree from a gerrit repository,
# do the following at the end of a larger cherry-pick series:
#  git remote add ...
#  git checkout -b upstreaming
#  git cherry-pick ...
#  git rebase -i --exec util/scripts/cross-repo-cherrypick master
# Alternatively, you can run util/scripts/cross-repo-cherrypick after every
# individual cherry-pick.

# use $0 --cros to add a stub BUG/BRANCH/TEST block

commit_message() {
	git log -n 1 | grep "^    " | cut -c5-
}

ORIGIN_HOST=$( commit_message |grep "^Reviewed-on: " |head -1 |cut -d/ -f3 )
case "${ORIGIN_HOST}" in
	review.coreboot.org)
		BRANCH="origin/master"
		MESSAGE_PREFIX="UPSTREAM: "
		;;
	chromium-review.googlesource.com)
		BRANCH="cros/chromeos-2016.05"
		MESSAGE_PREFIX=""
		;;
esac

# lines must be backwards due to tac(1)
SPLICE_CMD=""
if test "$1" = "--cros"; then
	if test -z "$( commit_message |egrep '^(BUG|TEST)=')"; then
		SPLICE_CMD='print "\nTEST=none\nBRANCH=none\nBUG=none\n"'
	fi
fi

CHID=$( commit_message | grep -i "^Change-Id: I" )
CID=$( git log -n1 --grep "^$CHID$" --pretty=%H $BRANCH )
GUID="$(git config user.name) <$(git config user.email)>"

# TBD: Don't add Original- to empty lines, and possibly make script more
# solid for commits with an unexpected order of meta data lines.

(printf "${MESSAGE_PREFIX}"; commit_message) | tac | awk '/^$/ {
		if (end==0) {
			print "Original-Commit-Id: '"${CID}"'\nSigned-off-by: '"${GUID}"'";
			'"${SPLICE_CMD}"'
		}
		end=1
	}; /^(BUG|BRANCH|TEST|CQ-DEPEND)=/ {
		if (end==0) {
			print "Original-Commit-Id: '"${CID}"'\nSigned-off-by: '"${GUID}"'";
			print "";
		}
		end=1
	}; {
		if (end==0)
			print "Original-" $0;
		else
			print $0;
	}' | tac | git commit --amend -F -