diff --git a/hack/lib/util.sh b/hack/lib/util.sh index 68b5c6bd65..47445dabb7 100755 --- a/hack/lib/util.sh +++ b/hack/lib/util.sh @@ -496,25 +496,42 @@ kube::util::ensure_single_dir_gopath() { fi } -# Checks whether there are any files matching pattern $2 changed between the -# current branch and upstream branch named by $1. -# Returns 1 (false) if there are no changes, 0 (true) if there are changes -# detected. -kube::util::has_changes_against_upstream_branch() { +# Find the base commit using: +# $PULL_BASE_SHA if set (from Prow) +# current ref from the remote upstream branch +kube::util::base_ref() { local -r git_branch=$1 - local -r pattern=$2 - local -r not_pattern=${3:-totallyimpossiblepattern} - local full_branch + + if [[ -n ${PULL_BASE_SHA:-} ]]; then + echo "${PULL_BASE_SHA}" + return + fi full_branch="$(kube::util::git_upstream_remote_name)/${git_branch}" - echo "Checking for '${pattern}' changes against '${full_branch}'" + # make sure the branch is valid, otherwise the check will pass erroneously. if ! git describe "${full_branch}" >/dev/null; then # abort! exit 1 fi + + echo "${full_branch}" +} + +# Checks whether there are any files matching pattern $2 changed between the +# current branch and upstream branch named by $1. +# Returns 1 (false) if there are no changes +# 0 (true) if there are changes detected. +kube::util::has_changes() { + local -r git_branch=$1 + local -r pattern=$2 + local -r not_pattern=${3:-totallyimpossiblepattern} + + local base_ref=$(kube::util::base_ref "${git_branch}") + echo "Checking for '${pattern}' changes against '${base_ref}'" + # notice this uses ... to find the first shared ancestor - if git diff --name-only "${full_branch}...HEAD" | grep -v -E "${not_pattern}" | grep "${pattern}" > /dev/null; then + if git diff --name-only "${base_ref}...HEAD" | grep -v -E "${not_pattern}" | grep "${pattern}" > /dev/null; then return 0 fi # also check for pending changes diff --git a/hack/verify-godep-licenses.sh b/hack/verify-godep-licenses.sh index a4eda566db..8e49d6f96e 100755 --- a/hack/verify-godep-licenses.sh +++ b/hack/verify-godep-licenses.sh @@ -23,8 +23,8 @@ source "${KUBE_ROOT}/hack/lib/init.sh" readonly branch=${1:-${KUBE_VERIFY_GIT_BRANCH:-master}} if ! [[ ${KUBE_FORCE_VERIFY_CHECKS:-} =~ ^[yY]$ ]] && \ - ! kube::util::has_changes_against_upstream_branch "${branch}" 'Godeps/' && \ - ! kube::util::has_changes_against_upstream_branch "${branch}" 'vendor/'; then + ! kube::util::has_changes "${branch}" 'Godeps/' && \ + ! kube::util::has_changes "${branch}" 'vendor/'; then exit 0 fi diff --git a/hack/verify-godeps.sh b/hack/verify-godeps.sh index 8fda624ca7..8760c7649d 100755 --- a/hack/verify-godeps.sh +++ b/hack/verify-godeps.sh @@ -23,10 +23,10 @@ source "${KUBE_ROOT}/hack/lib/init.sh" readonly branch=${1:-${KUBE_VERIFY_GIT_BRANCH:-master}} if ! [[ ${KUBE_FORCE_VERIFY_CHECKS:-} =~ ^[yY]$ ]] && \ - ! kube::util::has_changes_against_upstream_branch "${branch}" 'Godeps/' && \ - ! kube::util::has_changes_against_upstream_branch "${branch}" 'vendor/' && \ - ! kube::util::has_changes_against_upstream_branch "${branch}" 'hack/lib/' && \ - ! kube::util::has_changes_against_upstream_branch "${branch}" 'hack/.*godep'; then + ! kube::util::has_changes "${branch}" 'Godeps/' && \ + ! kube::util::has_changes "${branch}" 'vendor/' && \ + ! kube::util::has_changes "${branch}" 'hack/lib/' && \ + ! kube::util::has_changes "${branch}" 'hack/.*godep'; then exit 0 fi diff --git a/hack/verify-readonly-packages.sh b/hack/verify-readonly-packages.sh index 87864a61a3..014984c904 100755 --- a/hack/verify-readonly-packages.sh +++ b/hack/verify-readonly-packages.sh @@ -46,7 +46,7 @@ find_files() { IFS=$'\n' conflicts=($(find_files | sed 's|/.readonly||' | while read dir; do dir=${dir#./} - if kube::util::has_changes_against_upstream_branch "${branch}" "^${dir}/[^/]*\$" '/\.readonly$|/BUILD$|/zz_generated|/\.generated\.|\.proto$|\.pb\.go$' >/dev/null; then + if kube::util::has_changes "${branch}" "^${dir}/[^/]*\$" '/\.readonly$|/BUILD$|/zz_generated|/\.generated\.|\.proto$|\.pb\.go$' >/dev/null; then echo "${dir}" fi done)) diff --git a/hack/verify-staging-godeps.sh b/hack/verify-staging-godeps.sh index e984d164a1..e5564d879f 100755 --- a/hack/verify-staging-godeps.sh +++ b/hack/verify-staging-godeps.sh @@ -23,12 +23,12 @@ source "${KUBE_ROOT}/hack/lib/init.sh" readonly branch=${1:-${KUBE_VERIFY_GIT_BRANCH:-master}} if ! [[ ${KUBE_FORCE_VERIFY_CHECKS:-} =~ ^[yY]$ ]] && \ - ! kube::util::has_changes_against_upstream_branch "${branch}" 'staging/' && \ - ! kube::util::has_changes_against_upstream_branch "${branch}" 'build/' && \ - ! kube::util::has_changes_against_upstream_branch "${branch}" 'Godeps/' && \ - ! kube::util::has_changes_against_upstream_branch "${branch}" 'vendor/' && \ - ! kube::util::has_changes_against_upstream_branch "${branch}" 'hack/lib/' && \ - ! kube::util::has_changes_against_upstream_branch "${branch}" 'hack/.*godep'; then + ! kube::util::has_changes "${branch}" 'staging/' && \ + ! kube::util::has_changes "${branch}" 'build/' && \ + ! kube::util::has_changes "${branch}" 'Godeps/' && \ + ! kube::util::has_changes "${branch}" 'vendor/' && \ + ! kube::util::has_changes "${branch}" 'hack/lib/' && \ + ! kube::util::has_changes "${branch}" 'hack/.*godep'; then exit 0 fi