rebase: cherry-pick: add copyright
[git] / rewrite.c
1 #include "cache.h"
2 #include "rewrite.h"
3 #include "run-command.h"
4 #include "notes-utils.h"
5 #include "builtin.h"
6
7 void add_rewritten(struct rewritten *list, unsigned char *from, unsigned char *to)
8 {
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);
14         list->nr++;
15 }
16
17 int store_rewritten(struct rewritten *list, const char *file)
18 {
19         static struct lock_file lock;
20         struct strbuf buf = STRBUF_INIT;
21         int fd, i, ret = 0;
22
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));
27         }
28         if (write_in_full(fd, buf.buf, buf.len) < 0) {
29                 error(_("Could not write to %s"), file);
30                 ret = 1;
31                 goto leave;
32         }
33         if (commit_lock_file(&lock) < 0) {
34                 error(_("Error wrapping up %s."), file);
35                 ret = 1;
36                 goto leave;
37         }
38 leave:
39         strbuf_release(&buf);
40         return ret;
41 }
42
43 void load_rewritten(struct rewritten *list, const char *file)
44 {
45         struct strbuf buf = STRBUF_INIT;
46         char *p;
47         int fd;
48
49         fd = open(file, O_RDONLY);
50         if (fd < 0)
51                 return;
52         if (strbuf_read(&buf, fd, 0) < 0) {
53                 close(fd);
54                 strbuf_release(&buf);
55                 return;
56         }
57         close(fd);
58
59         for (p = buf.buf; *p;) {
60                 unsigned char from[20];
61                 unsigned char to[20];
62                 char *eol = strchrnul(p, '\n');
63                 if (eol - p != 81)
64                         /* wrong size */
65                         break;
66                 if (get_sha1_hex(p, from))
67                         break;
68                 if (get_sha1_hex(p + 41, to))
69                         break;
70                 add_rewritten(list, from, to);
71                 p = *eol ? eol + 1 : eol;
72         }
73         strbuf_release(&buf);
74 }
75
76 int run_rewrite_hook(struct rewritten *list, const char *name)
77 {
78         struct strbuf buf = STRBUF_INIT;
79         struct child_process proc;
80         const char *argv[3];
81         int code, i;
82
83         argv[0] = find_hook("post-rewrite");
84         if (!argv[0])
85                 return 0;
86
87         argv[1] = name;
88         argv[2] = NULL;
89
90         memset(&proc, 0, sizeof(proc));
91         proc.argv = argv;
92         proc.in = -1;
93         proc.stdout_to_stderr = 1;
94
95         code = start_command(&proc);
96         if (code)
97                 return code;
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));
101         }
102         write_in_full(proc.in, buf.buf, buf.len);
103         close(proc.in);
104         return finish_command(&proc);
105 }
106
107 void copy_rewrite_notes(struct rewritten *list, const char *name, const char *msg)
108 {
109         struct notes_rewrite_cfg *cfg;
110         int i;
111
112         cfg = init_copy_notes_for_rewrite(name);
113         if (!cfg)
114                 return;
115
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);
119         }
120
121         finish_copy_notes_for_rewrite(cfg, msg);
122 }