#!/bin/bash # # Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved. # # SPDX-License-Identifier: MIT # get_repo_path() { repo_path="" for keyword in $keyline; do if [[ $keyword == $pattern* ]]; then repo_path=`echo ${keyword#*${pattern}} | sed 's/\"//g'` break; fi done } get_repo_branch() { repo_branch="" for keyword in $keyline; do if [[ $keyword == $pattern* ]]; then repo_branch=`echo ${keyword#*dest-branch=} | sed -n 's/.*"\([^"]*\)".*/\1/p'` && break fi done } cherry_pick() { if [ -d "$repo_path" ]; then pushd $repo_path > /dev/null if [ "$repo_branch" != "$GERRIT_BRANCH" ]; then echo "The branch of ${GERRIT_REFSPEC} mismatch: $GERRIT_BRANCH != ${repo_branch}" popd > /dev/null return 1 fi git fetch ssh://${GERRIT_SERVER}:${GERRIT_PORT}/${GERRIT_PROJECT} ${GERRIT_REFSPEC} git cherry-pick FETCH_HEAD if [ "$?" -ne 0 ]; then git status git log -1 echo -e "\n${FUNCNAME[0]}: Failed to apply patch!\n" popd > /dev/null return 2 else popd > /dev/null return 0 fi else echo -e "\n${FUNCNAME[0]}: No such directory! $repo_path\n" return 2 fi } apply_patch_by_gerrit_topic() { ssh -p $GERRIT_PORT $GERRIT_SERVER gerrit query --format=JSON --current-patch-set status:open topic:{$MANUAL_GERRIT_TOPIC} > "$GERRIT_QUERY_RESULT" echo -e "======== Applying Gerrit Topic: $MANUAL_GERRIT_TOPIC ========" patch_apply_count=0 changed_repositories="" change_lines=$(grep -v '"type":"stats"' "$GERRIT_QUERY_RESULT") numbers=$(echo "$change_lines" | jq -r '.number') echo $numbers change_lines=$(grep -v '"type":"stats"' "$GERRIT_QUERY_RESULT") sorted_numbers=$(echo "$change_lines" | jq -r '.number' | sort -n) echo $sorted_numbers for GERRIT_CHANGE_NUMBER in $sorted_numbers; do change=$(grep "\"number\":\"$GERRIT_CHANGE_NUMBER\"" "$GERRIT_QUERY_RESULT") GERRIT_PROJECT=$(jq -r '.project' <<< "$change") GERRIT_REFSPEC=$(jq -r '.currentPatchSet.ref' <<< "$change") GERRIT_BRANCH=$(jq -r '.branch' <<< "$change") keyline=$(grep "name=\"$GERRIT_PROJECT\"" "$MANIFEST") if [ -z "$keyline" ]; then echo "${GERRIT_REFSPEC} does not belong to ${PROJECT_NAME}, skip!" continue fi pattern="path=" get_repo_path pattern="dest-branch=" get_repo_branch cherry_pick patch_status=$? if [ "$patch_status" -eq 1 ]; then continue elif [ "$patch_status" -eq 0 ]; then patch_apply_count=$((patch_apply_count + 1)) changed_repositories="$GERRIT_PROJECT#$changed_repositories" else # apply patch failed return 255 fi done echo -e "======== Applied $patch_apply_count patch(es) for $MANUAL_GERRIT_TOPIC ========\n" return $patch_apply_count }