Rebase: Run post-checkout hook on checkout

Signed-off-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
Orgad Shaneh
2018-12-20 23:45:48 +02:00
parent cd63940eff
commit 93f54842f5
2 changed files with 25 additions and 1 deletions

View File

@@ -369,6 +369,7 @@ static void add_var(struct strbuf *buf, const char *name, const char *value)
#define RESET_HEAD_DETACH (1<<0)
#define RESET_HEAD_HARD (1<<1)
#define RESET_HEAD_REFS_ONLY (1<<2)
#define RESET_HEAD_RUN_HOOK (1<<3)
static int reset_head(struct object_id *oid, const char *action,
const char *switch_to_branch, unsigned flags,
@@ -377,6 +378,7 @@ static int reset_head(struct object_id *oid, const char *action,
unsigned detach_head = flags & RESET_HEAD_DETACH;
unsigned reset_hard = flags & RESET_HEAD_HARD;
unsigned refs_only = flags & RESET_HEAD_REFS_ONLY;
unsigned run_hook = flags & RESET_HEAD_RUN_HOOK;
struct object_id head_oid;
struct tree_desc desc[2] = { { NULL }, { NULL } };
struct lock_file lock = LOCK_INIT;
@@ -481,6 +483,10 @@ reset_head_refs:
ret = create_symref("HEAD", switch_to_branch,
reflog_head);
}
if (run_hook)
run_hook_le(NULL, "post-checkout",
oid_to_hex(orig ? orig : &null_oid),
oid_to_hex(oid), "1", NULL);
leave_reset_head:
strbuf_release(&msg);
@@ -1729,7 +1735,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
strbuf_addf(&msg, "%s: checkout %s",
getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
if (reset_head(&options.onto->object.oid, "checkout", NULL,
RESET_HEAD_DETACH, NULL, msg.buf))
RESET_HEAD_DETACH | RESET_HEAD_RUN_HOOK, NULL, msg.buf))
die(_("Could not detach HEAD"));
strbuf_release(&msg);

View File

@@ -9,6 +9,8 @@ test_description='Test the post-checkout hook.'
test_expect_success setup '
test_commit one &&
test_commit two &&
test_commit rebase-on-me &&
git reset --hard HEAD^ &&
test_commit three three &&
mv .git/hooks-disabled .git/hooks
'
@@ -52,6 +54,22 @@ test_expect_success 'post-checkout receives the right args when not switching br
test $old = $new && test $flag = 0
'
test_expect_success 'post-checkout is triggered on rebase' '
git checkout -b rebase-test master &&
rm -f .git/post-checkout.args &&
git rebase rebase-on-me &&
read old new flag < .git/post-checkout.args &&
test $old != $new && test $flag = 1
'
test_expect_success 'post-checkout is triggered on rebase with fast-forward' '
git checkout -b ff-rebase-test rebase-on-me^ &&
rm -f .git/post-checkout.args &&
git rebase rebase-on-me &&
read old new flag < .git/post-checkout.args &&
test $old != $new && test $flag = 1
'
if test "$(git config --bool core.filemode)" = true; then
mkdir -p templates/hooks
cat >templates/hooks/post-checkout <<'EOF'