3 #include "run-command.h"
4 #include "notes-utils.h"
6 void add_rewritten(struct rewritten *list, unsigned char *from, unsigned char *to)
8 struct rewritten_item *item;
9 ALLOC_GROW(list->items, list->nr + 1, list->alloc);
10 item = &list->items[list->nr];
11 hashcpy(item->from, from);
12 hashcpy(item->to, to);
16 int store_rewritten(struct rewritten *list, const char *file)
18 static struct lock_file lock;
19 struct strbuf buf = STRBUF_INIT;
22 fd = hold_lock_file_for_update(&lock, file, LOCK_DIE_ON_ERROR);
23 for (i = 0; i < list->nr; i++) {
24 struct rewritten_item *item = &list->items[i];
25 strbuf_addf(&buf, "%s %s\n", sha1_to_hex(item->from), sha1_to_hex(item->to));
27 if (write_in_full(fd, buf.buf, buf.len) < 0) {
28 error(_("Could not write to %s"), file);
32 if (commit_lock_file(&lock) < 0) {
33 error(_("Error wrapping up %s."), file);
42 void load_rewritten(struct rewritten *list, const char *file)
44 struct strbuf buf = STRBUF_INIT;
48 fd = open(file, O_RDONLY);
51 if (strbuf_read(&buf, fd, 0) < 0) {
58 for (p = buf.buf; *p;) {
59 unsigned char from[20];
61 char *eol = strchrnul(p, '\n');
65 if (get_sha1_hex(p, from))
67 if (get_sha1_hex(p + 41, to))
69 add_rewritten(list, from, to);
70 p = *eol ? eol + 1 : eol;
75 int run_rewrite_hook(struct rewritten *list, const char *name)
77 struct strbuf buf = STRBUF_INIT;
78 struct child_process proc;
82 argv[0] = find_hook("post-rewrite");
89 memset(&proc, 0, sizeof(proc));
92 proc.stdout_to_stderr = 1;
94 code = start_command(&proc);
97 for (i = 0; i < list->nr; i++) {
98 struct rewritten_item *item = &list->items[i];
99 strbuf_addf(&buf, "%s %s\n", sha1_to_hex(item->from), sha1_to_hex(item->to));
101 write_in_full(proc.in, buf.buf, buf.len);
103 return finish_command(&proc);
106 void copy_rewrite_notes(struct rewritten *list, const char *name, const char *msg)
108 struct notes_rewrite_cfg *cfg;
111 cfg = init_copy_notes_for_rewrite(name);
115 for (i = 0; i < list->nr; i++) {
116 struct rewritten_item *item = &list->items[i];
117 copy_note_for_rewrite(cfg, item->from, item->to);
120 finish_copy_notes_for_rewrite(cfg, msg);