Code

clone: run post-checkout hook when checking out
authorJeff King <peff@peff.net>
Tue, 3 Mar 2009 05:37:51 +0000 (00:37 -0500)
committerJunio C Hamano <gitster@pobox.com>
Tue, 3 Mar 2009 07:50:44 +0000 (23:50 -0800)
The mental model for clone is that the branch is "checked
out" (and it even says this in Documentation/git-clone.txt:
"...creates and checks out an initial branch"). Therefore it
is reasonable for users to expect that any post-checkout
hook would be run.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin-clone.c
t/t5403-post-checkout-hook.sh

index c338910b1c76f3c994e4a22c9c0a1da38843e050..a6d89bc625bb4bcab85b5a143e9f670c3012b98e 100644 (file)
@@ -20,6 +20,7 @@
 #include "dir.h"
 #include "pack-refs.h"
 #include "sigchain.h"
+#include "run-command.h"
 
 /*
  * Overall FIXMEs:
@@ -377,6 +378,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
        struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
        struct transport *transport = NULL;
        char *src_ref_prefix = "refs/heads/";
+       int err = 0;
 
        struct refspec refspec;
 
@@ -631,6 +633,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
                if (write_cache(fd, active_cache, active_nr) ||
                    commit_locked_index(lock_file))
                        die("unable to write new index file");
+
+               err |= run_hook(NULL, "post-checkout", sha1_to_hex(null_sha1),
+                               sha1_to_hex(remote_head->old_sha1), "1", NULL);
        }
 
        strbuf_release(&reflog_msg);
@@ -638,5 +643,5 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
        strbuf_release(&key);
        strbuf_release(&value);
        junk_pid = 0;
-       return 0;
+       return err;
 }
index 9b2e1a94c5fb5aa094853a169cbdf2f7dc56e152..4fdb418550ee0f3c93e80f8c3df41094c0ada00c 100755 (executable)
@@ -71,4 +71,16 @@ test_expect_success 'post-checkout receives the right args when not switching br
         test $old = $new -a $flag = 0
 '
 
+mkdir -p templates/hooks
+cat >templates/hooks/post-checkout <<'EOF'
+#!/bin/sh
+echo $@ > $GIT_DIR/post-checkout.args
+EOF
+chmod +x templates/hooks/post-checkout
+
+test_expect_success 'post-checkout hook is triggered by clone' '
+       git clone --template=templates . clone3 &&
+       test -f clone3/.git/post-checkout.args
+'
+
 test_done