Upgrade the client post-checkout hook to automatically reset the merge attribute...
authorStephen Haberman <stephen@exigencecorp.com>
Fri, 3 Oct 2008 07:00:03 +0000 (02:00 -0500)
committerStephen Haberman <stephen@exigencecorp.com>
Fri, 10 Oct 2008 17:07:15 +0000 (12:07 -0500)
client/post-checkout-rebase
tests/t1100-client-post-commit-rebase.sh
tests/test-lib.sh

index 883fa3f..423fb66 100644 (file)
@@ -1,18 +1,26 @@
 #!/bin/sh
 
-# The hook is given three parameters: the ref of the previous HEAD, the ref of
-# the new HEAD (which may or may not have changed), and a flag indicating
-# whether the checkout was a branch checkout (changing branches, flag=1) or a
-# file checkout (retrieving a file from the index, flag=0).
+# $1 = previous hash
+# $2 = new hash (could be the same)
+# $3 = flag (0=retreiving from index, 1=new checkout)
 
-branch=$(git symbolic-ref HEAD 2>/dev/null)
+branch=$(git symbolic-ref --quiet HEAD)
 if [[ $? -ne 0 ]] ; then
        exit 0
 fi
 
 branch=${branch/refs\/heads\//}
 
-git config --list | grep "branch.${branch}.rebase" > /dev/null
+rebase=$(git config "branch.${branch}.rebase")
 if [ $? -ne 0 ] ; then
-       git config --add "branch.${branch}.rebase" true
+       git config "branch.${branch}.rebase" "true"
 fi
+
+merge=$(git config "branch.${branch}.merge")
+remote=$(git config "branch.${branch}.remote")
+if [ "$branch" != "stable" -a "$remote" == "origin" -a "$merge" == "refs/heads/stable" ] ; then
+       git config "branch.${branch}.merge" "refs/heads/${branch}"
+fi
+
+exit 0
+
index af83b20..f47364c 100644 (file)
@@ -7,11 +7,15 @@ test_description='client checkout auto-set branch rebase=true'
 test_expect_success 'setup' '
        echo "setup" >file &&
        git add file &&
-       git commit -m "setup"
+       git commit -m "setup" &&
+       git clone . ./server &&
+       git remote add origin ./server &&
+       git config branch.master.remote origin &&
+       git config branch.master.merge refs/heads/master
 '
 
 # setup the post-checkout hook
-install_client_hook 'post-checkout-rebase' 'post-checkout'
+install_post_checkout_hook 'post-checkout-rebase'
 
 test_expect_success 'sets rebase on new topic branch' '
        ! git config --list | grep branch.master.rebase &&
@@ -19,5 +23,19 @@ test_expect_success 'sets rebase on new topic branch' '
        git config --list | grep branch.topic.rebase=true
 '
 
+test_expect_success 'checking out remote branch does nothing' '
+       git push origin topic:topic2 &&
+       git fetch &&
+       git checkout origin/topic2 &&
+       ! git config --list | grep "branch..rebase"
+'
+
+test_expect_success 'cloning stable sets up the correct merge' '
+       git push origin topic:stable &&
+       git fetch &&
+       git checkout -b topic3 origin/stable &&
+       test "refs/heads/topic3" = "$(git config branch.topic3.merge)"
+'
+
 test_done
 
index 16099b7..01d0cee 100644 (file)
@@ -453,10 +453,24 @@ install_client_hook () {
     chmod +x "$TRASH_HOOKS/$2"
 }
 
+install_post_checkout_hook () {
+       mkdir -p ".git/hooks"
+       hook=".git/hooks/post-checkout"
+
+       echo "#!/bin/sh" >$hook
+       for ((i=1;i<=$#;i+=1)); do
+               eval script_name="$"$i
+               echo "../../client/$script_name \$1 \$2 \$3 &&" >>$hook
+       done
+       echo "echo >/dev/null" >>$hook
+
+       chmod +x $hook
+}
+
 install_server_hook () {
-    mkdir -p "server/.git/hooks"
-    cp "../../server/$1" "server/.git/hooks/$2"
-    chmod +x "server/.git/hooks/$2"
+       mkdir -p "server/.git/hooks"
+       cp "../../server/$1" "server/.git/hooks/$2"
+       chmod +x "server/.git/hooks/$2"
 }
 
 install_update_hook () {