blob: 7f87448c773eb6bd75e8c415a39f53f258d411ef [file] [log] [blame]
David Burger234efd72020-02-26 17:34:47 -07001#!/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
David Burger234efd72020-02-26 17:34:47 -07008function bail() {
9 echo "${1}"
10 exit 1
11}
12
David Burgerba59a3e2020-04-14 12:08:08 -060013function prompt_continue() {
14 read -p "${1} (y/N) " answer
15
16 if [[ "${answer^^}" != "Y" ]]; then
17 exit 0
18 fi
19}
20
David Burger234efd72020-02-26 17:34:47 -070021function usage() {
David Burger7b408382020-04-03 14:01:28 -060022 echo "Usage: $0 <program> [<project>]" >&2
23 echo " for setups with a single repo for the program with" >&2
24 echo " projects in subdirectories, only the program argument" >&2
25 echo " is provided." >&2
David Burger234efd72020-02-26 17:34:47 -070026 exit 1
27}
28
David Burgerba59a3e2020-04-14 12:08:08 -060029# Exit if any command fails.
30set -e
David Burger234efd72020-02-26 17:34:47 -070031
David Burgerba59a3e2020-04-14 12:08:08 -060032prompt_continue "
33If you are a googler and are working with an internal checkout you do
34not need to run this script as you already have a full repo checkout.
35Do you want to continue running this script?"
Sean McAllister01931d12020-03-12 10:34:40 -060036
37# Move to this script's directory.
38cd "$(dirname "$0")"
39
David Burger573a0502020-03-31 13:54:21 -060040readonly program="${1}"
David Burger7b408382020-04-03 14:01:28 -060041# Will be empty if this is a single program repository project.
David Burger573a0502020-03-31 13:54:21 -060042readonly project="${2}"
David Burger234efd72020-02-26 17:34:47 -070043
David Burger573a0502020-03-31 13:54:21 -060044readonly local_manifests_dir="../../.repo/local_manifests"
David Burger234efd72020-02-26 17:34:47 -070045
David Burger7b408382020-04-03 14:01:28 -060046if [[ -z "${project}" ]]; then
47 readonly clone_url="https://chrome-internal.googlesource.com/chromeos/program/${program}"
48 readonly clone_src="../../src/program/${program}"
49 readonly symlink="${local_manifests_dir}/${program}.xml"
50else
51 readonly clone_url="https://chrome-internal.googlesource.com/chromeos/project/${program}/${project}"
52 readonly clone_src="../../src/project/${program}/${project}"
53 readonly symlink="${local_manifests_dir}/${project}.xml"
54fi
55
56if [[ -d "${clone_src}" ]]; then
57 # If ${clone_src} is already present the user is likely running
David Burger573a0502020-03-31 13:54:21 -060058 # a second time when their first run failed. Users would do this
59 # when they found they didn't have adequate permissions on a first
60 # run. In this case we wipe the artifacts from the previous run and
61 # try again.
David Burgerba59a3e2020-04-14 12:08:08 -060062 prompt_continue "
63${clone_src} appears to already exist. If you are
64attempting to recover from a previous failed setup_project.sh attempt
65this will attempt to fix it by removing it and resyncing. If you have
66unsaved changes in ${clone_src} they will be lost.
67Do you want to continue with the removal and resync?"
68
David Burger7b408382020-04-03 14:01:28 -060069 echo "Founding existing ${clone_src} checkout, removing."
70 rm -rf "${clone_src}"
David Burger573a0502020-03-31 13:54:21 -060071 rm -f "${symlink}"
David Burger234efd72020-02-26 17:34:47 -070072fi
73
David Burger99263c12020-04-04 09:43:10 -060074# We only need the local_manifest.xml but have to clone to get it.
75# Removing the rest of what we clone before we do the sync prevents
76# a confusing error message from being shown to the user. The
77# --force-sync below actually causes it to not be a problem, but we'd
78# rather avoid the user having to interpret the error.
David Burger7b408382020-04-03 14:01:28 -060079git clone "${clone_url}" "${clone_src}"
David Burger99263c12020-04-04 09:43:10 -060080find "${clone_src}" -mindepth 1 ! -name local_manifest.xml -exec rm -rf {} +
David Burger234efd72020-02-26 17:34:47 -070081
David Burger234efd72020-02-26 17:34:47 -070082if [[ ! -d "${local_manifests_dir}" ]]; then
83 mkdir -p "${local_manifests_dir}"
84fi
85
David Burger7b408382020-04-03 14:01:28 -060086local_manifest="${clone_src}/local_manifest.xml"
David Burger234efd72020-02-26 17:34:47 -070087if [[ ! -e "${local_manifest}" ]]; then
88 bail "Expected local manifest ${local_manifest} does not exist, exiting."
89fi
90
91ln -sr "${local_manifest}" "${symlink}"
92
93repo sync --force-sync -j48