xref: /aosp_15_r20/external/coreboot/util/scripts/cross-repo-cherrypick (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1#!/usr/bin/env sh
2# cross-repo-cherrypick - rebase helper script
3#
4# SPDX-License-Identifier: GPL-2.0-only
5
6# Adapt to your remote branch:
7BRANCH="origin/main"
8
9# When pulling in patches from another tree from a gerrit repository,
10# do the following at the end of a larger cherry-pick series:
11#  git remote add ...
12#  git checkout -b upstreaming
13#  git cherry-pick ...
14#  git rebase -i --exec util/scripts/cross-repo-cherrypick master
15# Alternatively, you can run util/scripts/cross-repo-cherrypick after every
16# individual cherry-pick.
17
18# use $0 --cros to add a stub BUG/BRANCH/TEST block
19
20commit_message() {
21	git log -n 1 | grep "^    " | cut -c5-
22}
23
24ORIGIN_HOST=$( commit_message |grep "^Reviewed-on: " |head -1 |cut -d/ -f3 )
25case "${ORIGIN_HOST}" in
26	review.coreboot.org)
27		BRANCH="origin/main"
28		MESSAGE_PREFIX="UPSTREAM: "
29		;;
30	chromium-review.googlesource.com)
31		BRANCH="cros/main"
32		MESSAGE_PREFIX=""
33		;;
34esac
35
36# lines must be backwards due to tac(1)
37SPLICE_CMD=""
38if test "$1" = "--cros"; then
39	if test -z "$(commit_message | grep -E '^(BUG|TEST)=')"; then
40		SPLICE_CMD='print "\nTEST=none\nBRANCH=none\nBUG=none\n"'
41	fi
42fi
43
44CHID=$( commit_message | grep -i "^Change-Id: I" )
45CID=$( git log -n1 --grep "^$CHID$" --pretty=%H $BRANCH )
46GUID="$(git config user.name) <$(git config user.email)>"
47
48# TBD: Don't add Original- to empty lines, and possibly make script more
49# solid for commits with an unexpected order of meta data lines.
50
51(printf "${MESSAGE_PREFIX}"; commit_message) | tac | awk '/^$/ {
52		if (end==0) {
53			print "GitOrigin-RevId: '"${CID}"'\nSigned-off-by: '"${GUID}"'";
54			'"${SPLICE_CMD}"'
55		}
56		end=1
57	}; /^(BUG|BRANCH|TEST)=/ {
58		if (end==0) {
59			print "GitOrigin-RevId: '"${CID}"'\nSigned-off-by: '"${GUID}"'";
60			print "";
61		}
62		end=1
63	}; /^Cq-Depend:/ {
64		print $0;
65		next
66	}; /^Change-Id:/ {
67		print $0;
68		next
69	}; {
70		if (end==0)
71			print "Original-" $0;
72		else
73			print $0;
74	}' | tac | git commit --amend -F -
75