Skip to content

Commit dfa7a6c

Browse files
peffgitster
authored andcommitted
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>
1 parent f474c52 commit dfa7a6c

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

builtin-clone.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "dir.h"
2121
#include "pack-refs.h"
2222
#include "sigchain.h"
23+
#include "run-command.h"
2324

2425
/*
2526
* Overall FIXMEs:
@@ -377,6 +378,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
377378
struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
378379
struct transport *transport = NULL;
379380
char *src_ref_prefix = "refs/heads/";
381+
int err = 0;
380382

381383
struct refspec refspec;
382384

@@ -631,12 +633,15 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
631633
if (write_cache(fd, active_cache, active_nr) ||
632634
commit_locked_index(lock_file))
633635
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);
634639
}
635640

636641
strbuf_release(&reflog_msg);
637642
strbuf_release(&branch_top);
638643
strbuf_release(&key);
639644
strbuf_release(&value);
640645
junk_pid = 0;
641-
return 0;
646+
return err;
642647
}

t/t5403-post-checkout-hook.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,16 @@ test_expect_success 'post-checkout receives the right args when not switching br
7171
test $old = $new -a $flag = 0
7272
'
7373

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+
7486
test_done

0 commit comments

Comments
 (0)