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