xref: /aosp_15_r20/external/toolchain-utils/android_merge_from_upstream.sh (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1#!/bin/bash -eu
2# Copyright 2019 The ChromiumOS Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5#
6# This is a script crafted to make our Android friends' lives easier: when run
7# on their copy of toolchain-utils, this script will do all of the necessary
8# merging/branch creation/etc. to make keeping things up-to-date trivial.
9#
10# For example,
11# https://android-review.googlesource.com/c/platform/external/toolchain-utils/+/1132504/1
12
13local_branch_name="merge_with_upstream"
14local_upstream="aosp/master"  # nocheck
15remote="aosp"
16remote_branch="${remote}/upstream-main"  # nocheck
17
18my_dir="$(dirname "$(readlink -m "$0")")"
19cd "${my_dir}"
20
21ensure_head_is_upstream_main() {
22  local current_rev main_rev
23  current_rev="$(git rev-parse HEAD)"
24  main_rev="$(git rev-parse "${local_upstream}")"
25  if [[ "${current_rev}" != "${main_rev}" ]]; then
26    echo "Please checkout ${local_upstream} and rerun this" >&2
27    exit
28  fi
29}
30
31ensure_no_local_branch_present() {
32  if ! git rev-parse "${local_branch_name}" >& /dev/null; then
33    return 0
34  fi
35
36  echo -n "${local_branch_name} is a valid branch already. Delete? [y/N] " >&2
37
38  local line
39  read -r line
40  if [[ "${line}" != y* && "${line}" != Y* ]]; then
41    echo "Aborted" >&2
42    exit 1
43  fi
44
45  # If we're *on* that branch, deleting it is difficult. Always detach.
46  git checkout --detach || return
47  git branch -D "${local_branch_name}"
48}
49
50get_merge_commit_list() {
51  local merge_base
52  merge_base="$(git merge-base HEAD "${remote_branch}")"
53  git log --oneline "${merge_base}..${remote_branch}"
54}
55
56ensure_head_is_upstream_main
57ensure_no_local_branch_present
58
59echo "Ensuring repository is up-to-date..."
60git fetch "${remote}"
61repo start "${local_branch_name}"
62
63commit_list="$(get_merge_commit_list)"
64num_commits="$(wc -l <<< "${commit_list}")"
65
66# Disable shellcheck for the sed substitution warning.
67# shellcheck disable=SC2001
68commit_message="Merging ${num_commits} commit(s) from Chromium's toolchain-utils
69
70Merged commit digest:
71$(sed 's/^/  /' <<< "${commit_list}")
72"
73
74git merge "${remote_branch}" -m "${commit_message}"
75echo 'NOTE: When you try to "repo upload", repo might show a scary warning'
76echo 'about the number of changes are being uploaded. That should be fine,'
77echo 'since repo will only create CLs for commits not known to our remote.'
78