• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash -eu
2# Copyright 2019 The Chromium OS Authors. All rights reserved.
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"
15remote="aosp"
16remote_branch="${remote}/upstream-mirror-master"
17
18my_dir="$(dirname "$(readlink -m "$0")")"
19cd "${my_dir}"
20
21ensure_head_is_upstream_master() {
22  local current_rev master_rev
23  current_rev="$(git rev-parse HEAD)"
24  master_rev="$(git rev-parse ${local_upstream})"
25  if [[ "${current_rev}" != "${master_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.
46  local current_branch
47  current_branch="$(git branch --show-current)"
48  if [[ "${current_branch}" == "${local_branch_name}" ]]; then
49    local rev
50    rev="$(git rev-parse HEAD)"
51    # This is fine, since we assume HEAD == upstream-mirror-master anyway
52    # (e.g., the existing branch was pointless.)
53    git checkout "${rev}"
54  fi
55  git branch -D "${local_branch_name}"
56}
57
58get_merge_commit_list() {
59  local merge_base
60  merge_base="$(git merge-base HEAD ${remote_branch})"
61  git log --oneline "${merge_base}..${remote_branch}"
62}
63
64ensure_head_is_upstream_master
65ensure_no_local_branch_present
66
67echo "Ensuring repository is up-to-date..."
68git fetch "${remote}"
69repo start "${local_branch_name}"
70
71commit_list="$(get_merge_commit_list)"
72num_commits="$(wc -l <<< "${commit_list}")"
73commit_message="Merging ${num_commits} commit(s) from Chromium's toolchain-utils
74
75Merged commit digest:
76$(sed 's/^/  /' <<< "${commit_list}")
77"
78
79git merge "${remote_branch}" -m "${commit_message}"
80echo 'NOTE: When you try to `repo upload`, repo might show a scary warning'
81echo 'about the number of changes are being uploaded. That should be fine,'
82echo 'since repo will only create CLs for commits not known to our remote.'
83