4 #include "run-command.h"
6 #include "notes-utils.h"
8 void add_rewritten(struct rewritten *list, unsigned char *from, unsigned char *to)
10 struct rewritten_item *item;
11 ALLOC_GROW(list->items, list->nr + 1, list->alloc);
12 item = &list->items[list->nr];
13 hashcpy(item->from, from);
14 hashcpy(item->to, to);
18 int store_rewritten(struct rewritten *list, const char *file)
20 static struct lock_file lock;
21 struct strbuf buf = STRBUF_INIT;
24 fd = hold_lock_file_for_update(&lock, file, LOCK_DIE_ON_ERROR);
25 for (i = 0; i < list->nr; i++) {
26 struct rewritten_item *item = &list->items[i];
27 strbuf_addf(&buf, "%s %s\n", sha1_to_hex(item->from), sha1_to_hex(item->to));
29 if (write_in_full(fd, buf.buf, buf.len) < 0) {
30 error(_("Could not write to %s"), file);
34 if (commit_lock_file(&lock) < 0) {
35 error(_("Error wrapping up %s."), file);
44 void load_rewritten(struct rewritten *list, const char *file)
46 struct strbuf buf = STRBUF_INIT;
50 fd = open(file, O_RDONLY);
53 if (strbuf_read(&buf, fd, 0) < 0) {
60 for (p = buf.buf; *p;) {
61 unsigned char from[20];
63 char *eol = strchrnul(p, '\n');
67 if (get_sha1_hex(p, from))
69 if (get_sha1_hex(p + 41, to))
71 add_rewritten(list, from, to);
72 p = *eol ? eol + 1 : eol;
77 int run_rewrite_hook(struct rewritten *list, const char *name)
79 struct strbuf buf = STRBUF_INIT;
80 struct child_process proc = CHILD_PROCESS_INIT;
84 argv[0] = find_hook("post-rewrite");
93 proc.stdout_to_stderr = 1;
95 code = start_command(&proc);
98 for (i = 0; i < list->nr; i++) {
99 struct rewritten_item *item = &list->items[i];
100 strbuf_addf(&buf, "%s %s\n", sha1_to_hex(item->from), sha1_to_hex(item->to));
102 sigchain_push(SIGPIPE, SIG_IGN);
103 write_in_full(proc.in, buf.buf, buf.len);
105 sigchain_pop(SIGPIPE);
106 return finish_command(&proc);
109 void copy_rewrite_notes(struct rewritten *list, const char *name, const char *msg)
111 struct notes_rewrite_cfg *cfg;
114 cfg = init_copy_notes_for_rewrite(name);
118 for (i = 0; i < list->nr; i++) {
119 struct rewritten_item *item = &list->items[i];
120 copy_note_for_rewrite(cfg, item->from, item->to);
123 finish_copy_notes_for_rewrite(cfg, msg);