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