Skip to content

Commit a87a6f3

Browse files
phillipwoodgitster
authored andcommitted
commit: move post-rewrite code to libgit
Move run_rewrite_hook() from bulitin/commit.c to sequencer.c so it can be shared with other commands and add a new function commit_post_rewrite() based on the code in builtin/commit.c that encapsulates rewriting notes and running the post-rewrite hook. Once the sequencer learns how to create commits without forking 'git commit' these functions will be used when squashing commits. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0505d60 commit a87a6f3

File tree

3 files changed

+50
-41
lines changed

3 files changed

+50
-41
lines changed

builtin/commit.c

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
#include "gpg-interface.h"
3232
#include "column.h"
3333
#include "sequencer.h"
34-
#include "notes-utils.h"
3534
#include "mailmap.h"
36-
#include "sigchain.h"
3735

3836
static const char * const builtin_commit_usage[] = {
3937
N_("git commit [<options>] [--] <pathspec>..."),
@@ -1478,37 +1476,6 @@ static int git_commit_config(const char *k, const char *v, void *cb)
14781476
return git_status_config(k, v, s);
14791477
}
14801478

1481-
static int run_rewrite_hook(const struct object_id *oldoid,
1482-
const struct object_id *newoid)
1483-
{
1484-
struct child_process proc = CHILD_PROCESS_INIT;
1485-
const char *argv[3];
1486-
int code;
1487-
struct strbuf sb = STRBUF_INIT;
1488-
1489-
argv[0] = find_hook("post-rewrite");
1490-
if (!argv[0])
1491-
return 0;
1492-
1493-
argv[1] = "amend";
1494-
argv[2] = NULL;
1495-
1496-
proc.argv = argv;
1497-
proc.in = -1;
1498-
proc.stdout_to_stderr = 1;
1499-
1500-
code = start_command(&proc);
1501-
if (code)
1502-
return code;
1503-
strbuf_addf(&sb, "%s %s\n", oid_to_hex(oldoid), oid_to_hex(newoid));
1504-
sigchain_push(SIGPIPE, SIG_IGN);
1505-
write_in_full(proc.in, sb.buf, sb.len);
1506-
close(proc.in);
1507-
strbuf_release(&sb);
1508-
sigchain_pop(SIGPIPE);
1509-
return finish_command(&proc);
1510-
}
1511-
15121479
int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...)
15131480
{
15141481
struct argv_array hook_env = ARGV_ARRAY_INIT;
@@ -1739,14 +1706,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
17391706
rerere(0);
17401707
run_commit_hook(use_editor, get_index_file(), "post-commit", NULL);
17411708
if (amend && !no_post_rewrite) {
1742-
struct notes_rewrite_cfg *cfg;
1743-
cfg = init_copy_notes_for_rewrite("amend");
1744-
if (cfg) {
1745-
/* we are amending, so current_head is not NULL */
1746-
copy_note_for_rewrite(cfg, &current_head->object.oid, &oid);
1747-
finish_copy_notes_for_rewrite(cfg, "Notes added by 'git commit --amend'");
1748-
}
1749-
run_rewrite_hook(&current_head->object.oid, &oid);
1709+
commit_post_rewrite(current_head, &oid);
17501710
}
17511711
if (!quiet)
17521712
print_summary(prefix, &oid, !current_head);

sequencer.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "log-tree.h"
2222
#include "wt-status.h"
2323
#include "hashmap.h"
24+
#include "notes-utils.h"
25+
#include "sigchain.h"
2426

2527
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
2628

@@ -789,6 +791,51 @@ int update_head_with_reflog(const struct commit *old_head,
789791
return ret;
790792
}
791793

794+
static int run_rewrite_hook(const struct object_id *oldoid,
795+
const struct object_id *newoid)
796+
{
797+
struct child_process proc = CHILD_PROCESS_INIT;
798+
const char *argv[3];
799+
int code;
800+
struct strbuf sb = STRBUF_INIT;
801+
802+
argv[0] = find_hook("post-rewrite");
803+
if (!argv[0])
804+
return 0;
805+
806+
argv[1] = "amend";
807+
argv[2] = NULL;
808+
809+
proc.argv = argv;
810+
proc.in = -1;
811+
proc.stdout_to_stderr = 1;
812+
813+
code = start_command(&proc);
814+
if (code)
815+
return code;
816+
strbuf_addf(&sb, "%s %s\n", oid_to_hex(oldoid), oid_to_hex(newoid));
817+
sigchain_push(SIGPIPE, SIG_IGN);
818+
write_in_full(proc.in, sb.buf, sb.len);
819+
close(proc.in);
820+
strbuf_release(&sb);
821+
sigchain_pop(SIGPIPE);
822+
return finish_command(&proc);
823+
}
824+
825+
void commit_post_rewrite(const struct commit *old_head,
826+
const struct object_id *new_head)
827+
{
828+
struct notes_rewrite_cfg *cfg;
829+
830+
cfg = init_copy_notes_for_rewrite("amend");
831+
if (cfg) {
832+
/* we are amending, so old_head is not NULL */
833+
copy_note_for_rewrite(cfg, &old_head->object.oid, new_head);
834+
finish_copy_notes_for_rewrite(cfg, "Notes added by 'git commit --amend'");
835+
}
836+
run_rewrite_hook(&old_head->object.oid, new_head);
837+
}
838+
792839
static int is_original_commit_empty(struct commit *commit)
793840
{
794841
const struct object_id *ptree_oid;

sequencer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,6 @@ int update_head_with_reflog(const struct commit *old_head,
7373
const struct object_id *new_head,
7474
const char *action, const struct strbuf *msg,
7575
struct strbuf *err);
76+
void commit_post_rewrite(const struct commit *current_head,
77+
const struct object_id *new_head);
7678
#endif

0 commit comments

Comments
 (0)