Add display_error_message to reduce duplication.
authorStephen Haberman <stephen@exigencecorp.com>
Tue, 30 Sep 2008 03:24:45 +0000 (22:24 -0500)
committerStephen Haberman <stephen@exigencecorp.com>
Tue, 30 Sep 2008 03:24:45 +0000 (22:24 -0500)
server/functions
server/update-ensure-follows
server/update-ensure-tag-in-branch
server/update-ensure-ticket-reference
server/update-prefer-rebase
server/update-prefer-underscores
server/update-stable
tests/t2000-server-update-ensure-ticket-reference.sh
tests/t2001-server-update-ensure-ticket-reference-merges.sh
tests/t2600-server-update-prefer-underscores.sh

index 4557d76..5040ef7 100644 (file)
@@ -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
+}
 
index 2d5ef08..2cf6338 100644 (file)
@@ -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
index bab1eef..3c01543 100644 (file)
@@ -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
 
index 33acbc7..3dc9c29 100644 (file)
@@ -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
index fab594d..464cb7e 100644 (file)
@@ -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
index 9f9cd29..db728dc 100644 (file)
@@ -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
index b935dae..a8768c0 100644 (file)
 # 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
index d96fa92..3d966ab 100644 (file)
@@ -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"
 '
 
index 71d694e..1db3550 100644 (file)
@@ -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 &&
index 91271ec..f05a48c 100644 (file)
@@ -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