David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright 2020 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 | # Setup a project. |
| 7 | |
Andrew Lamb | 63abcd8 | 2021-02-09 10:53:00 -0700 | [diff] [blame^] | 8 | # Exit if any command fails. |
| 9 | set -e |
| 10 | |
David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 11 | function bail() { |
| 12 | echo "${1}" |
| 13 | exit 1 |
| 14 | } |
| 15 | |
David Burger | ba59a3e | 2020-04-14 12:08:08 -0600 | [diff] [blame] | 16 | function prompt_continue() { |
Andrew Lamb | 6d559a7 | 2021-02-01 14:55:11 -0700 | [diff] [blame] | 17 | read -r -p "${1} (y/N) " answer |
David Burger | ba59a3e | 2020-04-14 12:08:08 -0600 | [diff] [blame] | 18 | |
| 19 | if [[ "${answer^^}" != "Y" ]]; then |
| 20 | exit 0 |
| 21 | fi |
| 22 | } |
| 23 | |
David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 24 | function usage() { |
David Burger | c14e20f | 2020-09-23 09:49:53 -0600 | [diff] [blame] | 25 | echo "Usage: $0 <program> <project> [<branch>]" >&2 |
| 26 | echo " optionally pass branch to sync the project" >&2 |
| 27 | echo " from the local manifest at the given branch." >&2 |
David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 28 | exit 1 |
| 29 | } |
| 30 | |
Andrew Lamb | 9fa6f4a | 2021-02-01 15:26:33 -0700 | [diff] [blame] | 31 | # Clone a repo and create a symlink a local manifest. |
| 32 | # |
| 33 | # Args: |
| 34 | # $1: URL to clone. |
| 35 | # $2: Path to clone to. |
| 36 | # $3: Path to symlink to. |
| 37 | function clone_manifest() { |
| 38 | [[ $# -eq 3 ]] || die "${FUNCNAME[0]}: takes three arguments" |
David Burger | 7b40838 | 2020-04-03 14:01:28 -0600 | [diff] [blame] | 39 | |
Andrew Lamb | 9fa6f4a | 2021-02-01 15:26:33 -0700 | [diff] [blame] | 40 | local clone_url=$1 |
| 41 | local clone_src=$2 |
| 42 | local symlink=$3 |
| 43 | |
| 44 | if [[ -d "${clone_src}" ]]; then |
| 45 | # If ${clone_src} is already present the user is likely running |
| 46 | # a second time when their first run failed. Users would do this |
| 47 | # when they found they didn't have adequate permissions on a first |
| 48 | # run. In this case we wipe the artifacts from the previous run and |
| 49 | # try again. |
| 50 | prompt_continue " |
David Burger | ba59a3e | 2020-04-14 12:08:08 -0600 | [diff] [blame] | 51 | ${clone_src} appears to already exist. If you are |
| 52 | attempting to recover from a previous failed setup_project.sh attempt |
| 53 | this will attempt to fix it by removing it and resyncing. If you have |
| 54 | unsaved changes in ${clone_src} they will be lost. |
| 55 | Do you want to continue with the removal and resync?" |
| 56 | |
Andrew Lamb | 9fa6f4a | 2021-02-01 15:26:33 -0700 | [diff] [blame] | 57 | echo "Found existing ${clone_src} checkout, removing." |
| 58 | rm -rf "${clone_src}" |
| 59 | rm -f "${symlink}" |
| 60 | fi |
| 61 | |
| 62 | # We only need the local_manifest.xml but have to clone to get it. |
| 63 | # Removing the rest of what we clone before we do the sync prevents |
| 64 | # a confusing error message from being shown to the user. The |
| 65 | # --force-sync below actually causes it to not be a problem, but we'd |
| 66 | # rather avoid the user having to interpret the error. |
| 67 | if [[ -z "${branch}" ]]; then |
| 68 | git clone "${clone_url}" "${clone_src}" |
| 69 | else |
| 70 | git clone "${clone_url}" "${clone_src}" --branch "${branch}" |
| 71 | fi |
| 72 | |
| 73 | find "${clone_src}" -mindepth 1 ! -name local_manifest.xml -exec rm -rf {} + |
| 74 | |
| 75 | if [[ ! -d "${local_manifests_dir}" ]]; then |
| 76 | mkdir -p "${local_manifests_dir}" |
| 77 | fi |
| 78 | |
| 79 | local_manifest="${clone_src}/local_manifest.xml" |
| 80 | if [[ ! -e "${local_manifest}" ]]; then |
| 81 | return 1 |
| 82 | fi |
| 83 | |
| 84 | ln -sr "${local_manifest}" "${symlink}" |
| 85 | } |
| 86 | |
Andrew Lamb | 63abcd8 | 2021-02-09 10:53:00 -0700 | [diff] [blame^] | 87 | function main() { |
| 88 | if [[ $# -lt 2 ]]; then |
| 89 | usage |
| 90 | fi |
Andrew Lamb | 9fa6f4a | 2021-02-01 15:26:33 -0700 | [diff] [blame] | 91 | |
Andrew Lamb | 63abcd8 | 2021-02-09 10:53:00 -0700 | [diff] [blame^] | 92 | readonly program="${1}" |
| 93 | readonly project="${2}" |
| 94 | readonly branch="${3}" |
Andrew Lamb | 9fa6f4a | 2021-02-01 15:26:33 -0700 | [diff] [blame] | 95 | |
Andrew Lamb | 63abcd8 | 2021-02-09 10:53:00 -0700 | [diff] [blame^] | 96 | prompt_continue " |
| 97 | If you are a googler and are working with an internal checkout you do |
| 98 | not need to run this script as you already have a full repo checkout. |
| 99 | Do you want to continue running this script?" |
David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 100 | |
Andrew Lamb | 63abcd8 | 2021-02-09 10:53:00 -0700 | [diff] [blame^] | 101 | # Move to this script's directory. |
| 102 | cd "$(dirname "$0")" |
David Burger | c14e20f | 2020-09-23 09:49:53 -0600 | [diff] [blame] | 103 | |
Andrew Lamb | 63abcd8 | 2021-02-09 10:53:00 -0700 | [diff] [blame^] | 104 | readonly local_manifests_dir="../../.repo/local_manifests" |
| 105 | |
| 106 | # Clone local manifests from the program and project. Program local manifest |
| 107 | # is optional, project local manifest is required. |
| 108 | # |
| 109 | # Note that the symlinks include "_[program|project].xml" because the project |
| 110 | # name may be the same as the program name. |
| 111 | readonly prog_url="https://chrome-internal.googlesource.com/chromeos/program/${program}" |
| 112 | readonly prog_src="../../src/program/${program}" |
| 113 | readonly prog_symlink="${local_manifests_dir}/${program}_program.xml" |
| 114 | |
| 115 | readonly proj_url="https://chrome-internal.googlesource.com/chromeos/project/${program}/${project}" |
| 116 | readonly proj_src="../../src/project/${program}/${project}" |
| 117 | readonly proj_symlink="${local_manifests_dir}/${project}_project.xml" |
| 118 | |
| 119 | if ! clone_manifest "${prog_url}" "${prog_src}" "${prog_symlink}"; then |
| 120 | echo "No program local manifest found in ${prog_url}, continuing." |
| 121 | fi |
| 122 | |
| 123 | if ! clone_manifest "${proj_url}" "${proj_src}" "${proj_symlink}"; then |
| 124 | bail "Expected project local manifest in ${proj_url} does not exist, " \ |
| 125 | "exiting." |
| 126 | fi |
| 127 | |
| 128 | repo sync --force-sync -j48 |
| 129 | } |
| 130 | |
| 131 | main "$@" |