Martin Roth | 7737fbc | 2020-02-27 12:12:18 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright 2020 The Chromium OS 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 | # Download a local_manifest file from a private overlay into the |
| 7 | # .repo/local_manifests directory |
| 8 | |
| 9 | set -e |
| 10 | |
| 11 | readonly OVERLAY="$1" |
| 12 | readonly MANIFEST="$2" |
| 13 | readonly LOCAL_MANIFEST_DIR=".repo/local_manifests" |
| 14 | readonly LOCAL_MANIFEST_FILE="${MANIFEST:-local_manifest.xml}" |
| 15 | readonly PRIVATE_OVERLAY="chromeos/overlays/overlay-${OVERLAY}-private" |
| 16 | readonly URL="https://chrome-internal.googlesource.com/${PRIVATE_OVERLAY}" |
| 17 | ROOTDIR="" |
| 18 | |
| 19 | usage() { |
| 20 | local exitval=$1 |
| 21 | echo " Usage:" |
| 22 | echo " $0 <overlay> [local_manifest_file]" |
| 23 | echo " Example: $0 zork" |
| 24 | exit "${exitval}" |
| 25 | } |
| 26 | |
| 27 | # Find the .repo directory in any directory above the current |
| 28 | find_repo_dir() { |
| 29 | local path="${PWD}" |
| 30 | while [[ -n ${path} && ! -e "${path}/.repo" ]]; do |
| 31 | path=${path%/*} |
| 32 | done |
| 33 | ROOTDIR="${path}" |
| 34 | } |
| 35 | |
| 36 | echo_error() { |
| 37 | echo "Error: $1" >&2 |
| 38 | exit 1 |
| 39 | } |
| 40 | |
| 41 | main() { |
| 42 | if [[ -z ${OVERLAY} || "${OVERLAY}" = "--help" ]]; then |
| 43 | usage 1 |
| 44 | fi |
| 45 | |
| 46 | find_repo_dir |
| 47 | |
| 48 | if [[ ! -d "${ROOTDIR}/.repo" ]]; then |
| 49 | echo "Error: No .repo directory found." >&2 |
| 50 | echo "Run this script from inside the chroot directory structure." >&2 |
| 51 | exit 1 |
| 52 | fi |
| 53 | |
| 54 | mkdir -p "${ROOTDIR}/${LOCAL_MANIFEST_DIR}" || |
| 55 | echo_error "Could not create ${ROOTDIR}/${LOCAL_MANIFEST_DIR}" |
| 56 | cd "${ROOTDIR}/${LOCAL_MANIFEST_DIR}" || |
| 57 | echo_error "Could not cd to ${ROOTDIR}/${LOCAL_MANIFEST_DIR}" |
| 58 | local tmp_repo_dir |
| 59 | tmp_repo_dir="$(mktemp -d)" || |
| 60 | echo_error "Could not create temp directory" |
| 61 | |
| 62 | # We can have only one manifest file - either internal or not. So move any |
| 63 | # preexisting xml files to backup_file copies. In theory, there should only |
| 64 | # be one file to save, so just keep track of the last file that was |
| 65 | # preserved |
| 66 | local backup_file |
| 67 | declare -a targets=("$(find . -maxdepth 1 -name "*.xml")") |
| 68 | for oldfile in "${targets[@]}"; do |
| 69 | if [[ -n ${oldfile} && -f ${oldfile} ]]; then |
| 70 | oldfile="$(basename "${oldfile}")" |
| 71 | backup_file=$(mktemp -p . -t "old_${oldfile}.XXXX") |
| 72 | echo "Backing up existing manifest to ${backup_file}" |
| 73 | mv -f "${oldfile}" "${backup_file}" || |
| 74 | echo_error "could not back up exisiting manifest" |
| 75 | fi |
| 76 | done |
| 77 | |
| 78 | echo "Downloading local manifest for ${OVERLAY}" |
| 79 | git clone -qn "${URL}" --depth 1 -- "${tmp_repo_dir}" || |
| 80 | echo_error "clone of private overlay failed. Do you have access rights?" |
| 81 | git --git-dir="${tmp_repo_dir}/.git" checkout HEAD "${LOCAL_MANIFEST_FILE}" || |
| 82 | echo_error "checkout failed." |
| 83 | rm -rf "${tmp_repo_dir}" |
| 84 | |
| 85 | if [[ -f ${backup_file} ]] && \ |
| 86 | cmp "${LOCAL_MANIFEST_FILE}" "${backup_file}" >/dev/null; then |
| 87 | echo "No update needed. Old manifest was identical to new." |
| 88 | echo "Removing backup." |
| 89 | rm "${backup_file}" |
| 90 | exit 0 |
| 91 | fi |
| 92 | |
| 93 | if [[ -f ${LOCAL_MANIFEST_FILE} ]]; then |
| 94 | echo "${LOCAL_MANIFEST_FILE} downloaded successfully." |
| 95 | echo "Please run 'repo sync' to refresh the tree." |
| 96 | else |
| 97 | echo_error "${LOCAL_MANIFEST_FILE} not downloaded correctly." |
| 98 | fi |
| 99 | } |
| 100 | |
| 101 | main |