Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1962,8 +1962,9 @@ int cmd_commit(int argc,

repo_rerere(the_repository, 0);
run_auto_maintenance(the_repository, quiet);
run_commit_hook(use_editor, repo_get_index_file(the_repository),
NULL, "post-commit", NULL);
run_post_commit_hook(the_repository, use_editor,
repo_get_index_file(the_repository),
parents, &oid);
if (amend && !no_post_rewrite) {
commit_post_rewrite(the_repository, current_head, &oid);
}
Expand Down
52 changes: 51 additions & 1 deletion sequencer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,56 @@ void commit_post_rewrite(struct repository *r,
run_rewrite_hook(&old_head->object.oid, new_head);
}

static int commit_parents_equal(const struct commit_list *a,
const struct commit_list *b)
{
while (a && b) {
if (!oideq(&a->item->object.oid, &b->item->object.oid))
return 0;
a = a->next;
b = b->next;
}
return !a && !b;
}

void run_post_commit_hook(struct repository *r, int use_editor,
const char *index_file,
const struct commit_list *parents,
struct object_id *oid)
{
struct ref_store *refs = get_main_ref_store(r);
char *orig_head;
const char *new_head;
struct object_id head_oid;
struct commit *head_commit;
int invoked_hook = 0;

orig_head = refs_resolve_refdup(refs, "HEAD", RESOLVE_REF_READING,
NULL, NULL);
run_commit_hook(use_editor, index_file, &invoked_hook,
"post-commit", NULL);
if (!invoked_hook || !orig_head)
goto out;

new_head = refs_resolve_ref_unsafe(refs, "HEAD", RESOLVE_REF_READING,
&head_oid, NULL);
if (!new_head || strcmp(new_head, orig_head) || oideq(&head_oid, oid))
goto out;

if (odb_read_object_info(r->objects, &head_oid, NULL) != OBJ_COMMIT)
goto out;

head_commit = lookup_commit(r, &head_oid);
if (!head_commit || repo_parse_commit(r, head_commit))
goto out;

if (commit_parents_equal(parents, head_commit->parents))
oidcpy(oid, &head_oid);

out:
free(orig_head);
}

static int run_prepare_commit_msg_hook(struct repository *r,
struct strbuf *msg,
const char *commit)
Expand Down Expand Up @@ -1724,7 +1774,7 @@ static int try_to_commit(struct repository *r,
goto out;
}

run_commit_hook(0, r->index_file, NULL, "post-commit", NULL);
run_post_commit_hook(r, 0, r->index_file, parents, oid);
if (flags & AMEND_MSG)
commit_post_rewrite(r, current_head, oid);

Expand Down
5 changes: 5 additions & 0 deletions sequencer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "wt-status.h"

struct commit;
struct commit_list;
struct index_state;
struct repository;

Expand Down Expand Up @@ -258,6 +259,10 @@ int update_head_with_reflog(const struct commit *old_head,
void commit_post_rewrite(struct repository *r,
const struct commit *current_head,
const struct object_id *new_head);
void run_post_commit_hook(struct repository *r, int use_editor,
const char *index_file,
const struct commit_list *parents,
struct object_id *oid);

void create_autostash(struct repository *r, const char *path);
void create_autostash_ref(struct repository *r, const char *refname,
Expand Down
25 changes: 25 additions & 0 deletions t/t5407-post-rewrite-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,29 @@ test_expect_success 'rebase with commits that become empty' '
verify_hook_input
'

test_expect_success 'git commit --amend with post-commit rewriting HEAD' '
git checkout main &&
git reset --hard D &&
clear_hook_input &&
test_config notes.rewriteRef refs/notes/commits &&
git notes add -f -m "carry-me" HEAD &&
test_hook post-commit <<-\EOF &&
old=$(git rev-parse HEAD) &&
git cat-file commit "$old" >"$old.raw" &&
awk '\''!done && /^$/ { print "extra-header amend"; done = 1 } { print }'\'' \
"$old.raw" >"$old.new" &&
new=$(git hash-object -t commit -w "$old.new") &&
rm -f "$old.raw" "$old.new" &&
git update-ref HEAD "$new" "$old"
EOF
oldsha=$(git rev-parse HEAD^0) &&
git commit --amend -m "D amended with hook" &&
echo amend >expected.args &&
echo "$oldsha $(git rev-parse HEAD^0)" >expected.data &&
verify_hook_input &&
echo "carry-me" >expected.note &&
git notes show HEAD >actual.note &&
test_cmp expected.note actual.note
'

test_done
137 changes: 137 additions & 0 deletions t/t7524-commit-summary.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,141 @@ test_expect_success 'commit summary ignores rewrites' '
test_cmp diffstat.total actual.total
'

install_rewrite_post_commit_hook () {
test_hook "$@" post-commit <<-\EOF
old=$(git rev-parse HEAD) &&
git cat-file commit "$old" >"$old.raw" &&
awk '!done && /^$/ { print "extra-header post-commit"; done = 1 } { print }' \
"$old.raw" >"$old.new" &&
new=$(git hash-object -t commit -w "$old.new") &&
rm -f "$old.raw" "$old.new" &&
git update-ref HEAD "$new" "$old"
EOF
}

test_expect_success 'commit summary reflects HEAD rewritten by post-commit hook' '
git reset --hard initial &&
install_rewrite_post_commit_hook &&
echo changed >file &&
git add file &&
git commit -m rewritten >actual &&
short=$(git rev-parse --short HEAD) &&
test_grep "^\[[^ ]* $short\] rewritten$" actual
'

test_expect_success 'commit summary reflects detached HEAD rewritten by post-commit hook' '
orig_branch=$(git symbolic-ref HEAD) &&
test_when_finished "git checkout -f ${orig_branch#refs/heads/}" &&
git checkout --detach initial &&
install_rewrite_post_commit_hook &&
echo detached >file &&
git add file &&
git commit -m "detached rewritten" >actual &&
short=$(git rev-parse --short HEAD) &&
test_grep "^\[detached HEAD $short\] detached rewritten$" actual
'

test_expect_success 'commit summary reflects root commit rewritten by post-commit hook' '
test_when_finished "rm -rf root-repo" &&
git init root-repo &&
install_rewrite_post_commit_hook -C root-repo &&
echo hello >root-repo/file &&
git -C root-repo add file &&
git -C root-repo commit -m root >actual &&
short=$(git -C root-repo rev-parse --short HEAD) &&
test_grep "^\[[^ ]* (root-commit) $short\] root$" actual
'

test_expect_success 'commit summary reflects repacked HEAD rewritten by post-commit hook' '
git reset --hard initial &&
test_hook post-commit <<-\EOF &&
old=$(git rev-parse HEAD) &&
git cat-file commit "$old" >"$old.raw" &&
awk '\''!done && /^$/ { print "extra-header repacked"; done = 1 } { print }'\'' \
"$old.raw" >"$old.new" &&
new=$(git hash-object -t commit -w "$old.new") &&
rm -f "$old.raw" "$old.new" &&
git update-ref HEAD "$new" "$old" &&
git repack -ad -q
EOF
echo repacked >file &&
git add file &&
git commit -m repacked >actual &&
short=$(git rev-parse --short HEAD) &&
test_grep "^\[[^ ]* $short\] repacked$" actual
'

test_expect_success 'commit summary reflects cherry-pick rewritten by post-commit hook' '
git reset --hard initial &&
echo pickme >file &&
git add file &&
git commit -m pickme &&
pick=$(git rev-parse HEAD) &&
git reset --hard initial &&
install_rewrite_post_commit_hook &&
git cherry-pick "$pick" >actual &&
short=$(git rev-parse --short HEAD) &&
test_grep "^\[[^ ]* $short\] pickme$" actual
'

test_expect_success 'commit summary ignores child commit added by post-commit hook' '
git reset --hard initial &&
test_hook post-commit <<-\EOF &&
old=$(git rev-parse HEAD) &&
tree=$(git rev-parse HEAD^{tree}) &&
child=$(git commit-tree "$tree" -p "$old" -m child) &&
git update-ref HEAD "$child" "$old"
EOF
echo child-test >file &&
git add file &&
git commit -m parent >actual &&
parent_short=$(git rev-parse --short HEAD^) &&
test_grep "^\[[^ ]* $parent_short\] parent$" actual
'

test_expect_success 'commit summary ignores branch switch in post-commit hook' '
git reset --hard initial &&
orig_branch=$(git symbolic-ref HEAD) &&
test_when_finished "git symbolic-ref HEAD $orig_branch" &&
git branch -f other initial &&
test_hook post-commit <<-\EOF &&
git symbolic-ref HEAD refs/heads/other
EOF
echo branch-switch >file &&
git add file &&
git commit -m switched >actual &&
orig_short=$(git rev-parse --short "$orig_branch") &&
test_grep "^\[[^ ]* $orig_short\] switched$" actual
'

test_expect_success 'commit summary reflects merge commit rewritten by post-commit hook' '
git reset --hard initial &&
git branch -f side initial &&
git checkout side &&
echo side >side-file &&
git add side-file &&
git commit -m side &&
git checkout - &&
echo main >main-file &&
git add main-file &&
git commit -m main &&
install_rewrite_post_commit_hook &&
git merge --no-commit --no-ff side &&
git commit -m "merge rewritten" >actual &&
short=$(git rev-parse --short HEAD) &&
test_grep "^\[[^ ]* $short\] merge rewritten$" actual
'

test_expect_success 'commit summary reflects rewritten HEAD in linked worktree' '
git reset --hard initial &&
test_when_finished "git worktree remove --force linked-wt" &&
git worktree add -b wt-branch linked-wt initial &&
install_rewrite_post_commit_hook &&
echo wt >linked-wt/file &&
git -C linked-wt add file &&
git -C linked-wt commit -m "wt rewritten" >actual &&
short=$(git -C linked-wt rev-parse --short HEAD) &&
test_grep "^\[wt-branch $short\] wt rewritten$" actual
'

test_done
Loading