3 #include "run-command.h"
4 #include "notes-utils.h"
7 void add_rewritten(struct rewritten *list, unsigned char *from, unsigned char *to)
9 struct rewritten_item *item;
10 ALLOC_GROW(list->items, list->nr + 1, list->alloc);
11 item = &list->items[list->nr];
12 hashcpy(item->from, from);
13 hashcpy(item->to, to);
17 int store_rewritten(struct rewritten *list, const char *file)
19 static struct lock_file lock;
20 struct strbuf buf = STRBUF_INIT;
23 fd = hold_lock_file_for_update(&lock, file, LOCK_DIE_ON_ERROR);
24 for (i = 0; i < list->nr; i++) {
25 struct rewritten_item *item = &list->items[i];
26 strbuf_addf(&buf, "%s %s\n", sha1_to_hex(item->from), sha1_to_hex(item->to));
28 if (write_in_full(fd, buf.buf, buf.len) < 0) {
29 error(_("Could not write to %s"), file);
33 if (commit_lock_file(&lock) < 0) {
34 error(_("Error wrapping up %s."), file);
43 void load_rewritten(struct rewritten *list, const char *file)
45 struct strbuf buf = STRBUF_INIT;
49 fd = open(file, O_RDONLY);
52 if (strbuf_read(&buf, fd, 0) < 0) {
59 for (p = buf.buf; *p;) {
60 unsigned char from[20];
62 char *eol = strchrnul(p, '\n');
66 if (get_sha1_hex(p, from))
68 if (get_sha1_hex(p + 41, to))
70 add_rewritten(list, from, to);
71 p = *eol ? eol + 1 : eol;
76 int run_rewrite_hook(struct rewritten *list, const char *name)
78 struct strbuf buf = STRBUF_INIT;
79 struct child_process proc;
83 argv[0] = find_hook("post-rewrite");
90 memset(&proc, 0, sizeof(proc));
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 write_in_full(proc.in, buf.buf, buf.len);
104 return finish_command(&proc);
107 void copy_rewrite_notes(struct rewritten *list, const char *name, const char *msg)
109 struct notes_rewrite_cfg *cfg;
112 cfg = init_copy_notes_for_rewrite(name);
116 for (i = 0; i < list->nr; i++) {
117 struct rewritten_item *item = &list->items[i];
118 copy_note_for_rewrite(cfg, item->from, item->to);
121 finish_copy_notes_for_rewrite(cfg, msg);