#!/bin/bash # # Copyright (c) 2020-2022 Wind River Systems, Inc. # # SPDX-License-Identifier: Apache-2.0 # # # A collection of utilities relating to 'repo' # REPO_UTILS_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}" )" )" source ${REPO_UTILS_DIR}/utils.sh # # Get the root dir of a repo managed repository # repo_root [] # repo_root () { local query_dir="${1:-${PWD}}" local work_dir if [ ! -d "${query_dir}" ]; then echo_stderr "not a valid directory: ${query_dir}" return 1 fi if [ "${query_dir:0:1}" != "/" ]; then query_dir=$(readlink -f ${query_dir}) if [ $? -ne 0 ]; then return 1 fi fi work_dir="${query_dir}" while true; do if [ -d "$work_dir/.repo/manifests" ]; then echo $work_dir return 0 fi if [ "${work_dir}" == "/" ]; then break fi work_dir="$(dirname "${work_dir}")" done echo_stderr "directory is not controlled by repo: ${query_dir}" return 1 } # # Get the active manifest file of a repo managed repository # repo_manifest [] # REPO_MANIFEST_FILE= repo_set_manifest_file() { REPO_MANIFEST_FILE="$1" } repo_manifest () { local query_dir="${1:-${PWD}}" local root_dir="" local repo_manifest="" root_dir="$(repo_root "${query_dir}")" if [ $? -ne 0 ]; then return 1 fi if [[ -n "$REPO_MANIFEST_FILE" ]] ; then if [[ "$REPO_MANIFEST_FILE" =~ ^/ ]] ; then echo "$REPO_MANIFEST_FILE" else echo "${root_dir}/.repo/manifests/$REPO_MANIFEST_FILE" fi return 0 fi repo_manifest="${root_dir}/.repo/manifest.xml" # Depending on repo version, ${repo_manifest} is either a symlink to # the real manifest, or a wrapper manifest that includes the real manifest if [ -L "${repo_manifest}" ]; then readlink -f "${repo_manifest}" else grep " # # Extract the revision of a project within the manifest. # The default revision is supplied in the absence # of an explicit project revision. # # manifest = Path to manifest. # project-name = name of project. # manifest_get_revision_of_project () { local manifest="${1}" local project="${2}" local default_revision="" local revision="" default_revision=$(manifest_get_default_revision "${manifest}") revision=$(grep ' # # Extract the default revision of the manifest, if any. # # manifest = Path to manifest. # manifest_get_default_revision () { local manifest="${1}" grep ']*\).*#\1#' \ -e 's#/$##' } # # manifest_set_revision # # old_manifest = Path to original manifest. # new_manifest = Path to modified manifest. It will not overwrite an # existing file. # revision = A branch, tag ,or sha. Branch and SHA can be used # directly, but repo requires that a tag be in the form # "refs/tags/". # lock_down = 0,1 or 2. If 2, set a revision on all other non-listed # projects to equal the SHA of the current git head. # If 1, similar to 2, but only if the project doesn't have # some other form of revision specified. # project-list = A comma seperated list of projects. Listed projects # will have their revision set to the provided revision # value. # excluded_project-list = A comma seperated list of projects. Listed # projects will not be subject to lock-down. # manifest_set_revision () { local old_manifest="${1}" local new_manifest="${2}" local revision="${3}" local lock_down="${4}" local set_default="${5}" local projects="${6//,/ }" local ld_exclude_projects="${7//,/ }" local old_default_revision="" local repo_root_dir="" local line="" local FOUND=0 local path="" local project="" local rev="" repo_root_dir=$(repo_root) if [ $? -ne 0 ]; then echo_stderr "Current directory is not managed by repo." return 1 fi if [ ! -f "${old_manifest}" ]; then echo_stderr "Old manifest file is missing '${old_manifest}'." return 1 fi if [ -f "${new_manifest}" ]; then echo_stderr "New manifest file already present '${new_manifest}'." return 1 fi mkdir -p "$(dirname "${new_manifest}")" if [ $? -ne 0 ]; then echo_stderr "Failed to create directory '$(dirname "${new_manifest}")'" return 1 fi old_default_revision=$(manifest_get_default_revision "${old_manifest}") if [ ${set_default} -eq 1 ] && [ "${old_default_revision}" == "" ]; then # We only know how to alter an existing default revision, not set a # new one, so continue without setting a default. set_default=0 fi while IFS= read -r line; do echo "${line}" | grep -q '