Merge branch 'jc/diff' into next
[debian-git.git] / receive-pack.c
blob93929b5371bc0b9578ee5d3cb21c8ae6ade8cd1b
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include "run-command.h"
5 #include <sys/wait.h>
7 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
9 static const char *unpacker[] = { "unpack-objects", NULL };
11 static int report_status = 0;
13 static char capabilities[] = "report-status";
14 static int capabilities_sent = 0;
16 static int show_ref(const char *path, const unsigned char *sha1)
18 if (capabilities_sent)
19 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
20 else
21 packet_write(1, "%s %s%c%s\n",
22 sha1_to_hex(sha1), path, 0, capabilities);
23 capabilities_sent = 1;
24 return 0;
27 static void write_head_info(void)
29 for_each_ref(show_ref);
30 if (!capabilities_sent)
31 show_ref("capabilities^{}", null_sha1);
35 struct command {
36 struct command *next;
37 const char *error_string;
38 unsigned char old_sha1[20];
39 unsigned char new_sha1[20];
40 char ref_name[FLEX_ARRAY]; /* more */
43 static struct command *commands = NULL;
45 static int is_all_zeroes(const char *hex)
47 int i;
48 for (i = 0; i < 40; i++)
49 if (*hex++ != '0')
50 return 0;
51 return 1;
54 static int verify_old_ref(const char *name, char *hex_contents)
56 int fd, ret;
57 char buffer[60];
59 if (is_all_zeroes(hex_contents))
60 return 0;
61 fd = open(name, O_RDONLY);
62 if (fd < 0)
63 return -1;
64 ret = read(fd, buffer, 40);
65 close(fd);
66 if (ret != 40)
67 return -1;
68 if (memcmp(buffer, hex_contents, 40))
69 return -1;
70 return 0;
73 static char update_hook[] = "hooks/update";
75 static int run_update_hook(const char *refname,
76 char *old_hex, char *new_hex)
78 int code;
80 if (access(update_hook, X_OK) < 0)
81 return 0;
82 code = run_command(update_hook, refname, old_hex, new_hex, NULL);
83 switch (code) {
84 case 0:
85 return 0;
86 case -ERR_RUN_COMMAND_FORK:
87 return error("hook fork failed");
88 case -ERR_RUN_COMMAND_EXEC:
89 return error("hook execute failed");
90 case -ERR_RUN_COMMAND_WAITPID:
91 return error("waitpid failed");
92 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
93 return error("waitpid is confused");
94 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
95 return error("%s died of signal", update_hook);
96 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
97 return error("%s died strangely", update_hook);
98 default:
99 error("%s exited with error code %d", update_hook, -code);
100 return -code;
104 static int update(struct command *cmd)
106 const char *name = cmd->ref_name;
107 unsigned char *old_sha1 = cmd->old_sha1;
108 unsigned char *new_sha1 = cmd->new_sha1;
109 char new_hex[60], *old_hex, *lock_name;
110 int newfd, namelen, written;
112 cmd->error_string = NULL;
113 if (!strncmp(name, "refs/", 5) && check_ref_format(name + 5)) {
114 cmd->error_string = "funny refname";
115 return error("refusing to create funny ref '%s' locally",
116 name);
119 namelen = strlen(name);
120 lock_name = xmalloc(namelen + 10);
121 memcpy(lock_name, name, namelen);
122 memcpy(lock_name + namelen, ".lock", 6);
124 strcpy(new_hex, sha1_to_hex(new_sha1));
125 old_hex = sha1_to_hex(old_sha1);
126 if (!has_sha1_file(new_sha1)) {
127 cmd->error_string = "bad pack";
128 return error("unpack should have generated %s, "
129 "but I can't find it!", new_hex);
131 safe_create_leading_directories(lock_name);
133 newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
134 if (newfd < 0) {
135 cmd->error_string = "can't lock";
136 return error("unable to create %s (%s)",
137 lock_name, strerror(errno));
140 /* Write the ref with an ending '\n' */
141 new_hex[40] = '\n';
142 new_hex[41] = 0;
143 written = write(newfd, new_hex, 41);
144 /* Remove the '\n' again */
145 new_hex[40] = 0;
147 close(newfd);
148 if (written != 41) {
149 unlink(lock_name);
150 cmd->error_string = "can't write";
151 return error("unable to write %s", lock_name);
153 if (verify_old_ref(name, old_hex) < 0) {
154 unlink(lock_name);
155 cmd->error_string = "raced";
156 return error("%s changed during push", name);
158 if (run_update_hook(name, old_hex, new_hex)) {
159 unlink(lock_name);
160 cmd->error_string = "hook declined";
161 return error("hook declined to update %s", name);
163 else if (rename(lock_name, name) < 0) {
164 unlink(lock_name);
165 cmd->error_string = "can't rename";
166 return error("unable to replace %s", name);
168 else {
169 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
170 return 0;
174 static char update_post_hook[] = "hooks/post-update";
176 static void run_update_post_hook(struct command *cmd)
178 struct command *cmd_p;
179 int argc;
180 const char **argv;
182 if (access(update_post_hook, X_OK) < 0)
183 return;
184 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
185 if (cmd_p->error_string)
186 continue;
187 argc++;
189 argv = xmalloc(sizeof(*argv) * (1 + argc));
190 argv[0] = update_post_hook;
192 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
193 char *p;
194 if (cmd_p->error_string)
195 continue;
196 p = xmalloc(strlen(cmd_p->ref_name) + 1);
197 strcpy(p, cmd_p->ref_name);
198 argv[argc] = p;
199 argc++;
201 argv[argc] = NULL;
202 run_command_v_opt(argc, argv, RUN_COMMAND_NO_STDIO);
206 * This gets called after(if) we've successfully
207 * unpacked the data payload.
209 static void execute_commands(void)
211 struct command *cmd = commands;
213 while (cmd) {
214 update(cmd);
215 cmd = cmd->next;
217 run_update_post_hook(commands);
220 static void read_head_info(void)
222 struct command **p = &commands;
223 for (;;) {
224 static char line[1000];
225 unsigned char old_sha1[20], new_sha1[20];
226 struct command *cmd;
227 char *refname;
228 int len, reflen;
230 len = packet_read_line(0, line, sizeof(line));
231 if (!len)
232 break;
233 if (line[len-1] == '\n')
234 line[--len] = 0;
235 if (len < 83 ||
236 line[40] != ' ' ||
237 line[81] != ' ' ||
238 get_sha1_hex(line, old_sha1) ||
239 get_sha1_hex(line + 41, new_sha1))
240 die("protocol error: expected old/new/ref, got '%s'",
241 line);
243 refname = line + 82;
244 reflen = strlen(refname);
245 if (reflen + 82 < len) {
246 if (strstr(refname + reflen + 1, "report-status"))
247 report_status = 1;
249 cmd = xmalloc(sizeof(struct command) + len - 80);
250 memcpy(cmd->old_sha1, old_sha1, 20);
251 memcpy(cmd->new_sha1, new_sha1, 20);
252 memcpy(cmd->ref_name, line + 82, len - 81);
253 cmd->error_string = "n/a (unpacker error)";
254 cmd->next = NULL;
255 *p = cmd;
256 p = &cmd->next;
260 static const char *unpack(int *error_code)
262 int code = run_command_v_opt(1, unpacker, RUN_GIT_CMD);
264 *error_code = 0;
265 switch (code) {
266 case 0:
267 return NULL;
268 case -ERR_RUN_COMMAND_FORK:
269 return "unpack fork failed";
270 case -ERR_RUN_COMMAND_EXEC:
271 return "unpack execute failed";
272 case -ERR_RUN_COMMAND_WAITPID:
273 return "waitpid failed";
274 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
275 return "waitpid is confused";
276 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
277 return "unpacker died of signal";
278 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
279 return "unpacker died strangely";
280 default:
281 *error_code = -code;
282 return "unpacker exited with error code";
286 static void report(const char *unpack_status)
288 struct command *cmd;
289 packet_write(1, "unpack %s\n",
290 unpack_status ? unpack_status : "ok");
291 for (cmd = commands; cmd; cmd = cmd->next) {
292 if (!cmd->error_string)
293 packet_write(1, "ok %s\n",
294 cmd->ref_name);
295 else
296 packet_write(1, "ng %s %s\n",
297 cmd->ref_name, cmd->error_string);
299 packet_flush(1);
302 int main(int argc, char **argv)
304 int i;
305 char *dir = NULL;
307 argv++;
308 for (i = 1; i < argc; i++) {
309 char *arg = *argv++;
311 if (*arg == '-') {
312 /* Do flag handling here */
313 usage(receive_pack_usage);
315 if (dir)
316 usage(receive_pack_usage);
317 dir = arg;
319 if (!dir)
320 usage(receive_pack_usage);
322 if(!enter_repo(dir, 0))
323 die("'%s': unable to chdir or not a git archive", dir);
325 write_head_info();
327 /* EOF */
328 packet_flush(1);
330 read_head_info();
331 if (commands) {
332 int code;
333 const char *unpack_status = unpack(&code);
334 if (!unpack_status)
335 execute_commands();
336 if (report_status)
337 report(unpack_status);
339 return 0;