From e605600d9e45f1b88ecc5c68c1ec4e24ceea6bd5 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Thu, 11 Jun 2020 10:42:13 -0700 Subject: [PATCH] Check cherry-pick hashes in pep8 tox target NOTE(elod.illes): This is a combination of 2 commits: the cherry-pick hash checker script and a fix for the script. 1. Check cherry-pick hashes in pep8 tox target This adds a tools/ script that checks any cherry-picked hashes on the current commit (or a provided commit) to make sure that all the hashes exist on at least master or stable/.* branches. This should help avoid accidentally merging stable backports where one of the hashes along the line has changed due to conflicts. 2. Fix cherry-pick check for merge patch Cherry-pick check script validates the proposed patch's commit message. If a patch is not on top of the given branch then Zuul rebases it to the top and the patch becomes a merge patch. In this case the script validates the merge patch's commit message instead of the original patch's commit message and fails. This fix selects the parent of the patch if it is a merge patch. (cherry picked from commit c7c48c6f52c9159767b60a4576ba37726156a5f7) (cherry picked from commit 02f213b831d8e1d4a1d8ebb18d1260571fe20b84) (cherry picked from commit 7a5111ba2943014b6fd53a5fe7adcd9bc445315e) Change-Id: I4afaa0808b75cc31a8dd14663912c162281a1a42 (cherry picked from commit aebc829c4e0d39a160eaaa5ad949c1256c8179e6) (cherry picked from commit 5cacfaab82853241022d3a2a0734f82dae59a34b) (cherry picked from commit d307b964ce380f2fa57debc6c4c8346ac8736afe) (cherry picked from commit c3dd9f86f13a86c901443054de5ad3ab57953901) (cherry picked from commit f1ab10b8252bab4123c55a9fc10583710f347cef) --- tools/check-cherry-picks.sh | 42 +++++++++++++++++++++++++++++++++++++ tox.ini | 1 + 2 files changed, 43 insertions(+) create mode 100755 tools/check-cherry-picks.sh diff --git a/tools/check-cherry-picks.sh b/tools/check-cherry-picks.sh new file mode 100755 index 000000000000..32627e59b605 --- /dev/null +++ b/tools/check-cherry-picks.sh @@ -0,0 +1,42 @@ +#!/bin/sh +# +# A tool to check the cherry-pick hashes from the current git commit message +# to verify that they're all on either master or stable/ branches +# + +commit_hash="" + +# Check if the patch is a merge patch by counting the number of parents. +# If the patch has 2 parents, then the 2nd parent is the patch we want +# to validate. +parent_number=$(git show --format='%P' --quiet | awk '{print NF}') +if [ $parent_number -eq 2 ]; then + commit_hash=$(git show --format='%P' --quiet | awk '{print $NF}') +fi + +hashes=$(git show --format='%b' --quiet $commit_hash | sed -nr 's/^.cherry picked from commit (.*).$/\1/p') +checked=0 +branches+="" +for hash in $hashes; do + branch=$(git branch -a --contains "$hash" 2>/dev/null| grep -oE '(master|stable/[a-z]+)') + if [ $? -ne 0 ]; then + echo "Cherry pick hash $hash not on any master or stable branches" + exit 1 + fi + branches+=" $branch" + checked=$(($checked + 1)) +done + +if [ $checked -eq 0 ]; then + if ! grep -q '^defaultbranch=stable/' .gitreview; then + echo "Checked $checked cherry-pick hashes: OK" + exit 0 + else + if ! git show --format='%B' --quiet | grep -qi 'stable.*only'; then + echo 'Stable branch requires either cherry-pick -x headers or [stable-only] tag!' + exit 1 + fi + fi +else + echo Checked $checked cherry-pick hashes on branches: $(echo $branches | tr ' ' '\n' | sort | uniq) +fi diff --git a/tox.ini b/tox.ini index 140f4ce4f94e..983baeb0edad 100644 --- a/tox.ini +++ b/tox.ini @@ -55,6 +55,7 @@ commands = bash -c "! find doc/ -type f -name *.json | xargs grep -U -n $'\r'" # Check that all included JSON files are valid JSON bash -c '! find doc/ -type f -name *.json | xargs -t -n1 python -m json.tool 2>&1 > /dev/null | grep -B1 -v ^python' + bash tools/check-cherry-picks.sh [testenv:fast8] # This is a subset of the full pep8 check which