From 7b1a6c5d42a7a2f3d3a11cdf91607886c1c2c978 Mon Sep 17 00:00:00 2001 From: Stephen Haberman Date: Mon, 29 Sep 2008 22:24:45 -0500 Subject: [PATCH] Add display_error_message to reduce duplication. --- server/functions | 12 +++++ server/update-ensure-follows | 10 ++-- server/update-ensure-tag-in-branch | 8 ++-- server/update-ensure-ticket-reference | 9 ++-- server/update-prefer-rebase | 12 ++--- server/update-prefer-underscores | 9 ++-- server/update-stable | 48 ++++--------------- ...0-server-update-ensure-ticket-reference.sh | 6 +-- ...r-update-ensure-ticket-reference-merges.sh | 2 +- .../t2600-server-update-prefer-underscores.sh | 2 +- 10 files changed, 44 insertions(+), 74 deletions(-) diff --git a/server/functions b/server/functions index 4557d76..5040ef7 100644 --- a/server/functions +++ b/server/functions @@ -206,4 +206,16 @@ function with_lock_unlock_if_held() { fi } +function display_error_message() { + echo "----------------------------------------------------" >&2 + echo "" >&2 + echo "" >&2 + for ((i=1;i<=$#;i+=1)); do + eval message="$"$i + echo "$message" >&2 + done + echo "" >&2 + echo "" >&2 + echo "----------------------------------------------------" >&2 +} diff --git a/server/update-ensure-follows b/server/update-ensure-follows index 2d5ef08..2cf6338 100644 --- a/server/update-ensure-follows +++ b/server/update-ensure-follows @@ -9,6 +9,8 @@ # in it. # +. $(dirname $0)/functions + # Command line refname="$1" oldrev="$2" @@ -44,15 +46,11 @@ follows=($follows) count=${#follows[@]} for ((i = 0 ; i < count ; i++)) do follow="${follows[$i]}" - git rev-parse "$follow" + git rev-parse --verify --quiet "$follow" if [ $? -eq 0 ] ; then missing_commits=$(git log ^$newrev $follow --pretty=oneline | wc -l) if [ $missing_commits -ne 0 ] ; then - echo "----------------------------------------------------" - echo - echo "You need to merge $follow into $short_refname" - echo - echo "----------------------------------------------------" + display_error_message "You need to merge $follow into $short_refname" exit 1 fi fi diff --git a/server/update-ensure-tag-in-branch b/server/update-ensure-tag-in-branch index bab1eef..3c01543 100644 --- a/server/update-ensure-tag-in-branch +++ b/server/update-ensure-tag-in-branch @@ -3,6 +3,8 @@ # Tags should only be allowed to point to commits that are within a branch. # +. $(dirname $0)/functions + refname="$1" oldrev="$2" newrev="$3" @@ -24,11 +26,7 @@ fi contains=$(git branch --contains "$newrev" | wc -l) if [ $contains -eq 0 ] ; then - echo "----------------------------------------------------" - echo - echo "The tag $short_refname is not included in any branch." - echo - echo "----------------------------------------------------" + display_error_message "The tag $short_refname is not included in any branch." exit 1 fi diff --git a/server/update-ensure-ticket-reference b/server/update-ensure-ticket-reference index 33acbc7..3dc9c29 100644 --- a/server/update-ensure-ticket-reference +++ b/server/update-ensure-ticket-reference @@ -1,5 +1,7 @@ #!/bin/sh +. $(dirname $0)/functions + # Command line refname="$1" oldrev="$2" @@ -15,7 +17,6 @@ case "$refname" in ;; esac - excused=" $(git config hooks.update-ensure-ticket-reference.excused) " if [[ $excused =~ " $short_refname " ]] ; then exit 0 @@ -29,11 +30,7 @@ fi | while read commit ; do # Have log dump the "subject line, new line, body" of each commit message for grepping git log -n 1 '--pretty=format:%s%n%b' "$commit" | grep -i '\(\(re\|refs\|qa\) #[0-9]\+\)\|\(no ticket\)' > /dev/null if [ $? -ne 0 ] ; then - echo "----------------------------------------------------" >&2 - echo "" >&2 - echo "Commit $commit does not reference a ticket" >&2 - echo "" >&2 - echo "----------------------------------------------------" >&2 + display_error_message "Commit $commit does not reference a ticket" exit 1 fi done diff --git a/server/update-prefer-rebase b/server/update-prefer-rebase index fab594d..464cb7e 100644 --- a/server/update-prefer-rebase +++ b/server/update-prefer-rebase @@ -36,6 +36,8 @@ # \ # c -- d <-- topic +. $(dirname $0)/functions + refname="$1" oldrev="$2" newrev="$3" @@ -63,14 +65,8 @@ git rev-parse --not --branches | git rev-list --stdin $oldrev..$newrev | while r all_commits=$(git rev-list --first-parent $baserev..$parent | wc -l) new_commits=$(git rev-parse --not --branches | git rev-list --stdin $baserev..$parent | wc -l) if [ $all_commits -eq $new_commits ] ; then - # echo "parent=$parent" - # echo "all_commits=$all_commits" - # echo "new_commits=$new_commits" - echo "----------------------------------------------------" - echo - echo "It looks like you should rebase instead of merging $commit" - echo - echo "----------------------------------------------------" + # echo "parent=$parent" # echo "all_commits=$all_commits" # echo "new_commits=$new_commits" + display_error_message "It looks like you should rebase instead of merging $commit" exit 1 fi done diff --git a/server/update-prefer-underscores b/server/update-prefer-underscores index 9f9cd29..db728dc 100644 --- a/server/update-prefer-underscores +++ b/server/update-prefer-underscores @@ -4,6 +4,8 @@ # that ratetables could still sneak when, but we'll give # people the benefit of the doubt. +. $(dirname $0)/functions + # Command line refname="$1" oldrev="$2" @@ -11,12 +13,7 @@ newrev="$3" if expr "$oldrev" : '0*$' >/dev/null ; then if expr "$refname" : '.*[A-Z].*' ; then - echo "----------------------------------------------------" - echo - echo "Please prefer underscored branch names (e.g. rate_tables)" - echo "instead of camel-casing branch names (e.g. rateTables)." - echo - echo "----------------------------------------------------" + display_error_message "Please use underscored branch names (e.g. rate_tables)" exit 1 fi fi diff --git a/server/update-stable b/server/update-stable index b935dae..a8768c0 100644 --- a/server/update-stable +++ b/server/update-stable @@ -44,13 +44,12 @@ # would not have appeared in QA. # +. $(dirname $0)/functions + refname="$1" oldrev="$2" newrev="$3" -. $(dirname $0)/functions -set_change_type - case "$refname" in refs/heads/*) short_refname=${refname##refs/heads/} @@ -60,6 +59,7 @@ case "$refname" in ;; esac +set_change_type if [ "$change_type" == "delete" ] ; then exit 0 fi @@ -67,11 +67,7 @@ fi if [ "$change_type" == "create" -a "$short_refname" != "stable" ] ; then not_on_stable=$(git rev-list stable..$newrev) if [ "$not_on_stable" == "" ] ; then - echo "----------------------------------------------------" - echo - echo "Creating a branch must include new commits" - echo - echo "----------------------------------------------------" + display_error_message "Creating a branch must include new commits" exit 1 fi fi @@ -102,53 +98,33 @@ if [ "$short_refname" == "stable" ] ; then # = all new commits on stable count=$(git rev-parse --not --branches | git rev-list --stdin $oldrev..$newrev | wc -l) if [ "$count" -ne "1" ] ; then - echo "----------------------------------------------------" - echo - echo "Moving stable must entail a single commit" - echo - echo "----------------------------------------------------" + display_error_message "Moving stable must entail a single commit" exit 1 fi number_of_parents=$(git rev-list --no-walk --parents $newrev | sed 's/ /\n/g' | grep -v $newrev | wc -l) if [ "$number_of_parents" -ne "2" ] ; then - echo "----------------------------------------------------" - echo - echo "Moving stable must entail a merge commit" - echo - echo "----------------------------------------------------" + display_error_message "Moving stable must entail a merge commit" exit 1 fi first_parent=$(git rev-list --no-walk --parents $newrev | sed 's/ /\n/g' | grep -v $newrev | head --lines=1) if [ "$first_parent" != "$oldrev" ] ; then - echo "----------------------------------------------------" - echo - echo "Moving stable must have the previous stable as the first parent" - echo - echo "----------------------------------------------------" + display_error_message "Moving stable must have the previous stable as the first parent" exit 1 fi second_parent=$(git rev-list --no-walk --parents $newrev | sed 's/ /\n/g' | grep -v $newrev | tail --lines=1) changed_lines=$(git diff $second_parent..$newrev | wc -l) if [ "$changed_lines" -ne "0" ] ; then - echo "----------------------------------------------------" - echo - echo "Moving stable must not result in any changes from $second_parent" - echo - echo "----------------------------------------------------" + display_error_message "Moving stable must not result in any changes from $second_parent" exit 1 fi else # Check if candidate/topic is already in stable git branch --contains "$oldrev" | grep stable >/dev/null if [ $? -eq 0 ] ; then - echo "----------------------------------------------------" - echo - echo "$short_refname has been merged into stable" - echo - echo "----------------------------------------------------" + display_error_message "$short_refname has been merged into stable" exit 1 fi @@ -164,11 +140,7 @@ else # Check if topic is already in candidates candidate=$(git branch --contains "$oldrev" | grep -oP "candidate.*" --max-count=1) if [ $? -eq 0 ] ; then - echo "----------------------------------------------------" - echo - echo "$short_refname has been merged into $candidate" - echo - echo "----------------------------------------------------" + display_error_message "$short_refname has been merged into $candidate" exit 1 fi fi diff --git a/tests/t2000-server-update-ensure-ticket-reference.sh b/tests/t2000-server-update-ensure-ticket-reference.sh index d96fa92..3d966ab 100644 --- a/tests/t2000-server-update-ensure-ticket-reference.sh +++ b/tests/t2000-server-update-ensure-ticket-reference.sh @@ -16,13 +16,13 @@ test_expect_success 'setup' ' ' # setup the update hook -install_server_hook 'update-ensure-ticket-reference' 'update' +install_update_hook 'update-ensure-ticket-reference' test_expect_success 'reject with bad message' ' echo $test_name >a && git commit -a -m "$test_name" && head=$(git rev-parse HEAD) && - ! git push >push.out 2>push.err && + ! git push 2>push.err && cat push.err | grep "Commit $head does not reference a ticket" ' @@ -54,7 +54,7 @@ test_expect_success 'reject with bad message in second of three' ' echo "$test_name third" >a && git commit -a -m "$test_name third re #3222" && - ! git push >push.out 2>push.err && + ! git push 2>push.err && cat push.err | grep "Commit $head does not reference a ticket" ' diff --git a/tests/t2001-server-update-ensure-ticket-reference-merges.sh b/tests/t2001-server-update-ensure-ticket-reference-merges.sh index 71d694e..1db3550 100644 --- a/tests/t2001-server-update-ensure-ticket-reference-merges.sh +++ b/tests/t2001-server-update-ensure-ticket-reference-merges.sh @@ -16,7 +16,7 @@ test_expect_success 'setup' ' ' # setup the hook -install_server_hook 'update-ensure-ticket-reference' 'update' +install_update_hook 'update-ensure-ticket-reference' test_expect_success 'accept merge with merge message' ' git checkout -b topic1 master && diff --git a/tests/t2600-server-update-prefer-underscores.sh b/tests/t2600-server-update-prefer-underscores.sh index 91271ec..f05a48c 100644 --- a/tests/t2600-server-update-prefer-underscores.sh +++ b/tests/t2600-server-update-prefer-underscores.sh @@ -32,7 +32,7 @@ test_expect_success 'pushing topicTopic fails' ' echo "$test_name" >a && git commit -a -m "$test_name on topicTopic" && ! git push origin topicTopic 2>push.err && - cat push.err | grep "Please prefer underscored branch names" + cat push.err | grep "Please use underscored branch names" ' test_done -- 2.20.1