aboutsummaryrefslogtreecommitdiff
path: root/android_merge_from_upstream.sh
blob: a391979d5eba6d28d5ec258f81e5f6318147a450 (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
#!/bin/bash -eu
# Copyright 2019 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# This is a script crafted to make our Android friends' lives easier: when run
# on their copy of toolchain-utils, this script will do all of the necessary
# merging/branch creation/etc. to make keeping things up-to-date trivial.
#
# For example,
# https://android-review.googlesource.com/c/platform/external/toolchain-utils/+/1132504/1

local_branch_name="merge_with_upstream"
local_upstream="aosp/master"  # nocheck
remote="aosp"
remote_branch="${remote}/upstream-main"  # nocheck

my_dir="$(dirname "$(readlink -m "$0")")"
cd "${my_dir}"

ensure_head_is_upstream_main() {
  local current_rev main_rev
  current_rev="$(git rev-parse HEAD)"
  main_rev="$(git rev-parse "${local_upstream}")"
  if [[ "${current_rev}" != "${main_rev}" ]]; then
    echo "Please checkout ${local_upstream} and rerun this" >&2
    exit
  fi
}

ensure_no_local_branch_present() {
  if ! git rev-parse "${local_branch_name}" >& /dev/null; then
    return 0
  fi

  echo -n "${local_branch_name} is a valid branch already. Delete? [y/N] " >&2

  local line
  read -r line
  if [[ "${line}" != y* && "${line}" != Y* ]]; then
    echo "Aborted" >&2
    exit 1
  fi

  # If we're *on* that branch, deleting it is difficult. Always detach.
  git checkout --detach || return
  git branch -D "${local_branch_name}"
}

get_merge_commit_list() {
  local merge_base
  merge_base="$(git merge-base HEAD "${remote_branch}")"
  git log --oneline "${merge_base}..${remote_branch}"
}

ensure_head_is_upstream_main
ensure_no_local_branch_present

echo "Ensuring repository is up-to-date..."
git fetch "${remote}"
repo start "${local_branch_name}"

commit_list="$(get_merge_commit_list)"
num_commits="$(wc -l <<< "${commit_list}")"

# Disable shellcheck for the sed substitution warning.
# shellcheck disable=SC2001
commit_message="Merging ${num_commits} commit(s) from Chromium's toolchain-utils

Merged commit digest:
$(sed 's/^/  /' <<< "${commit_list}")
"

git merge "${remote_branch}" -m "${commit_message}"
echo 'NOTE: When you try to "repo upload", repo might show a scary warning'
echo 'about the number of changes are being uploaded. That should be fine,'
echo 'since repo will only create CLs for commits not known to our remote.'