blob: d58113eba2e3a0cb2223dfb8c77ab6f6b09c1b39 [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
Andrew Lamb63abcd82021-02-09 10:53:00 -07008# Exit if any command fails.
9set -e
10
David Burger234efd72020-02-26 17:34:47 -070011function bail() {
12 echo "${1}"
13 exit 1
14}
15
David Burgerba59a3e2020-04-14 12:08:08 -060016function prompt_continue() {
Andrew Lamb6d559a72021-02-01 14:55:11 -070017 read -r -p "${1} (y/N) " answer
David Burgerba59a3e2020-04-14 12:08:08 -060018
19 if [[ "${answer^^}" != "Y" ]]; then
20 exit 0
21 fi
22}
23
David Burger234efd72020-02-26 17:34:47 -070024function usage() {
David Burgerc14e20f2020-09-23 09:49:53 -060025 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 Burger234efd72020-02-26 17:34:47 -070028 exit 1
29}
30
Andrew Lamb9fa6f4a2021-02-01 15:26:33 -070031# 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.
37function clone_manifest() {
38 [[ $# -eq 3 ]] || die "${FUNCNAME[0]}: takes three arguments"
David Burger7b408382020-04-03 14:01:28 -060039
Andrew Lamb9fa6f4a2021-02-01 15:26:33 -070040 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 Burgerba59a3e2020-04-14 12:08:08 -060051${clone_src} appears to already exist. If you are
52attempting to recover from a previous failed setup_project.sh attempt
53this will attempt to fix it by removing it and resyncing. If you have
54unsaved changes in ${clone_src} they will be lost.
55Do you want to continue with the removal and resync?"
56
Andrew Lamb9fa6f4a2021-02-01 15:26:33 -070057 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 Lamb63abcd82021-02-09 10:53:00 -070087function main() {
88 if [[ $# -lt 2 ]]; then
89 usage
90 fi
Andrew Lamb9fa6f4a2021-02-01 15:26:33 -070091
Andrew Lamb63abcd82021-02-09 10:53:00 -070092 readonly program="${1}"
93 readonly project="${2}"
94 readonly branch="${3}"
Andrew Lamb9fa6f4a2021-02-01 15:26:33 -070095
Andrew Lamb63abcd82021-02-09 10:53:00 -070096 prompt_continue "
97If you are a googler and are working with an internal checkout you do
98not need to run this script as you already have a full repo checkout.
99Do you want to continue running this script?"
David Burger234efd72020-02-26 17:34:47 -0700100
Andrew Lamb63abcd82021-02-09 10:53:00 -0700101 # Move to this script's directory.
102 cd "$(dirname "$0")"
David Burgerc14e20f2020-09-23 09:49:53 -0600103
Andrew Lamb63abcd82021-02-09 10:53:00 -0700104 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
131main "$@"