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 <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Nov 17, 2017 at 11:34 UTC a87a6f3c98ea80740fa31d2559b78f75f8138132
3 files changed +50 -41
builtin/commit.c
+1 -41
@@ -31,9 +31,7 @@
31 #include "gpg-interface.h"
32 #include "column.h"
33 #include "sequencer.h"
34 -#include "notes-utils.h"
34 #include "mailmap.h"
36 -#include "sigchain.h"
35
36 static const char * const builtin_commit_usage[] = {
37 N_("git commit [<options>] [--] <pathspec>..."),
@@ -1478,37 +1476,6 @@ static int git_commit_config(const char *k, const char *v, void *cb)
1476 return git_status_config(k, v, s);
1477 }
1478
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 -
1479 int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...)
1480 {
1481 struct argv_array hook_env = ARGV_ARRAY_INIT;
@@ -1739,14 +1706,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1706 rerere(0);
1707 run_commit_hook(use_editor, get_index_file(), "post-commit", NULL);
1708 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);
1710 }
1711 if (!quiet)
1712 print_summary(prefix, &oid, !current_head);
sequencer.c
+47
@@ -21,6 +21,8 @@
21 #include "log-tree.h"
22 #include "wt-status.h"
23 #include "hashmap.h"
24 +#include "notes-utils.h"
25 +#include "sigchain.h"
26
27 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
28
@@ -789,6 +791,51 @@ int update_head_with_reflog(const struct commit *old_head,
791 return ret;
792 }
793
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 +
839 static int is_original_commit_empty(struct commit *commit)
840 {
841 const struct object_id *ptree_oid;
sequencer.h
+2
@@ -73,4 +73,6 @@ int update_head_with_reflog(const struct commit *old_head,
73 const struct object_id *new_head,
74 const char *action, const struct strbuf *msg,
75 struct strbuf *err);
76 +void commit_post_rewrite(const struct commit *current_head,
77 + const struct object_id *new_head);
78 #endif