blob: afbc395f73d2ac0e528e8555a793ece2e4b00bea [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() {
Andrew Lambdfe7c9f2021-02-09 13:20:40 -070012 RED='\033[38;5;9m'
13 NC='\033[0m' # No Color
14 echo -e "${RED}${1}${NC}"
David Burger234efd72020-02-26 17:34:47 -070015 exit 1
16}
17
David Burgerba59a3e2020-04-14 12:08:08 -060018function prompt_continue() {
Andrew Lamb6d559a72021-02-01 14:55:11 -070019 read -r -p "${1} (y/N) " answer
David Burgerba59a3e2020-04-14 12:08:08 -060020
21 if [[ "${answer^^}" != "Y" ]]; then
22 exit 0
23 fi
24}
25
Andrew Lamb9fa6f4a2021-02-01 15:26:33 -070026# Clone a repo and create a symlink a local manifest.
27#
28# Args:
29# $1: URL to clone.
30# $2: Path to clone to.
31# $3: Path to symlink to.
32function clone_manifest() {
33 [[ $# -eq 3 ]] || die "${FUNCNAME[0]}: takes three arguments"
David Burger7b408382020-04-03 14:01:28 -060034
Andrew Lamb9fa6f4a2021-02-01 15:26:33 -070035 local clone_url=$1
36 local clone_src=$2
37 local symlink=$3
38
39 if [[ -d "${clone_src}" ]]; then
40 # If ${clone_src} is already present the user is likely running
41 # a second time when their first run failed. Users would do this
42 # when they found they didn't have adequate permissions on a first
43 # run. In this case we wipe the artifacts from the previous run and
44 # try again.
45 prompt_continue "
David Burgerba59a3e2020-04-14 12:08:08 -060046${clone_src} appears to already exist. If you are
47attempting to recover from a previous failed setup_project.sh attempt
48this will attempt to fix it by removing it and resyncing. If you have
49unsaved changes in ${clone_src} they will be lost.
50Do you want to continue with the removal and resync?"
51
Andrew Lamb9fa6f4a2021-02-01 15:26:33 -070052 echo "Found existing ${clone_src} checkout, removing."
53 rm -rf "${clone_src}"
54 rm -f "${symlink}"
55 fi
56
57 # We only need the local_manifest.xml but have to clone to get it.
58 # Removing the rest of what we clone before we do the sync prevents
59 # a confusing error message from being shown to the user. The
60 # --force-sync below actually causes it to not be a problem, but we'd
61 # rather avoid the user having to interpret the error.
62 if [[ -z "${branch}" ]]; then
63 git clone "${clone_url}" "${clone_src}"
64 else
65 git clone "${clone_url}" "${clone_src}" --branch "${branch}"
66 fi
67
68 find "${clone_src}" -mindepth 1 ! -name local_manifest.xml -exec rm -rf {} +
69
70 if [[ ! -d "${local_manifests_dir}" ]]; then
71 mkdir -p "${local_manifests_dir}"
72 fi
73
74 local_manifest="${clone_src}/local_manifest.xml"
75 if [[ ! -e "${local_manifest}" ]]; then
76 return 1
77 fi
78
79 ln -sr "${local_manifest}" "${symlink}"
80}
81
Andrew Lamb64df3302021-02-10 17:42:02 -070082function usage() {
83 echo "Usage: $0 [options] <program> <project>
84
85Options:
86 -br, --branch Sync the project from the local manifest at the given branch.
Andrew Lambe1fa0d72021-02-10 17:52:50 -070087 --chipset Name of the chipset overlay to sync a local manifest from.
Andrew Lamb64df3302021-02-10 17:42:02 -070088 -h, --help This help output.
89 " >&2
90
91 exit 1
92}
93
Andrew Lamb63abcd82021-02-09 10:53:00 -070094function main() {
Andrew Lamb64df3302021-02-10 17:42:02 -070095 while [[ $# -ne 0 ]]; do
96 case $1 in
97 -br|--branch)
98 readonly branch=$2
99 shift
100 ;;
Andrew Lambe1fa0d72021-02-10 17:52:50 -0700101 --chipset)
102 readonly chipset=$2
103 shift
104 ;;
Andrew Lamb64df3302021-02-10 17:42:02 -0700105 -h|--help)
106 usage
107 ;;
108 *)
109 ARGS+=( "$1" )
110 ;;
111 esac
112 shift
113 done
114 set -- "${ARGS[@]}" "$@"
115
116 if [[ $# -ne 2 ]]; then
Andrew Lamb63abcd82021-02-09 10:53:00 -0700117 usage
118 fi
Andrew Lamb9fa6f4a2021-02-01 15:26:33 -0700119
Andrew Lamb63abcd82021-02-09 10:53:00 -0700120 readonly program="${1}"
121 readonly project="${2}"
Andrew Lamb9fa6f4a2021-02-01 15:26:33 -0700122
Andrew Lamb63abcd82021-02-09 10:53:00 -0700123 prompt_continue "
124If you are a googler and are working with an internal checkout you do
125not need to run this script as you already have a full repo checkout.
126Do you want to continue running this script?"
David Burger234efd72020-02-26 17:34:47 -0700127
Andrew Lamb63abcd82021-02-09 10:53:00 -0700128 # Move to this script's directory.
129 cd "$(dirname "$0")"
David Burgerc14e20f2020-09-23 09:49:53 -0600130
Andrew Lamb63abcd82021-02-09 10:53:00 -0700131 readonly local_manifests_dir="../../.repo/local_manifests"
132
133 # Clone local manifests from the program and project. Program local manifest
134 # is optional, project local manifest is required.
135 #
136 # Note that the symlinks include "_[program|project].xml" because the project
137 # name may be the same as the program name.
138 readonly prog_url="https://chrome-internal.googlesource.com/chromeos/program/${program}"
139 readonly prog_src="../../src/program/${program}"
140 readonly prog_symlink="${local_manifests_dir}/${program}_program.xml"
141
Andrew Lamb63abcd82021-02-09 10:53:00 -0700142 if ! clone_manifest "${prog_url}" "${prog_src}" "${prog_symlink}"; then
143 echo "No program local manifest found in ${prog_url}, continuing."
144 fi
145
Andrew Lambe1fa0d72021-02-10 17:52:50 -0700146 readonly proj_url="https://chrome-internal.googlesource.com/chromeos/project/${program}/${project}"
147 readonly proj_src="../../src/project/${program}/${project}"
148 readonly proj_symlink="${local_manifests_dir}/${project}_project.xml"
149
Andrew Lamb63abcd82021-02-09 10:53:00 -0700150 if ! clone_manifest "${proj_url}" "${proj_src}" "${proj_symlink}"; then
151 bail "Expected project local manifest in ${proj_url} does not exist, " \
152 "exiting."
153 fi
154
Andrew Lambe1fa0d72021-02-10 17:52:50 -0700155 if [[ -n "${chipset}" ]]; then
156 readonly chip_url="https://chrome-internal.googlesource.com/chromeos/overlays/chipset-${chipset}-private"
157 readonly chip_src="../../private-overlays/chipset-${chipset}-private"
158 readonly chip_symlink="${local_manifests_dir}/${chipset}_chipset.xml"
159 if ! clone_manifest "${chip_url}" "${chip_src}" "${chip_symlink}"; then
160 bail "Expected chipset local manifest in ${chip_url} does not exist, " \
161 "exiting."
162 fi
163 fi
164
165
Andrew Lamb805e7462021-02-09 10:54:39 -0700166 echo "Local manifest setup complete, sync new projects with:
167
168repo sync --force-sync -j48"
Andrew Lamb63abcd82021-02-09 10:53:00 -0700169}
170
171main "$@"