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