clone: run post-checkout hook when checking out
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>
Jeff King committed
Mar 3, 2009 at 00:37 UTC
dfa7a6c579d75037b9dd5c3654a36d31c60f203c
2 files changed
+18
-1
builtin-clone.c
+6
-1
@@ -20,6 +20,7 @@
20
#include "dir.h"
21
#include "pack-refs.h"
22
#include "sigchain.h"
23
+#include "run-command.h"
24
25
/*
26
* Overall FIXMEs:
@@ -377,6 +378,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
378
struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
379
struct transport *transport = NULL;
380
char *src_ref_prefix = "refs/heads/";
381
+ int err = 0;
382
383
struct refspec refspec;
384
@@ -631,6 +633,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
633
if (write_cache(fd, active_cache, active_nr) ||
634
commit_locked_index(lock_file))
635
die("unable to write new index file");
636
+
637
+ err |= run_hook(NULL, "post-checkout", sha1_to_hex(null_sha1),
638
+ sha1_to_hex(remote_head->old_sha1), "1", NULL);
639
}
640
641
strbuf_release(&reflog_msg);
@@ -638,5 +643,5 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
643
strbuf_release(&key);
644
strbuf_release(&value);
645
junk_pid = 0;
641
- return 0;
646
+ return err;
647
}
t/t5403-post-checkout-hook.sh
+12
@@ -71,4 +71,16 @@ test_expect_success 'post-checkout receives the right args when not switching br
71
test $old = $new -a $flag = 0
72
'
73
74
+mkdir -p templates/hooks
75
+cat >templates/hooks/post-checkout <<'EOF'
76
+#!/bin/sh
77
+echo $@ > $GIT_DIR/post-checkout.args
78
+EOF
79
+chmod +x templates/hooks/post-checkout
80
+
81
+test_expect_success 'post-checkout hook is triggered by clone' '
82
+ git clone --template=templates . clone3 &&
83
+ test -f clone3/.git/post-checkout.args
84
+'
85
+
86
test_done