4 #include "run-command.h"
8 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
10 static const char *unpacker[] = { "unpack-objects", NULL };
11 static const char *keep_packer[] = {
12 "index-pack", "--stdin", "--fix-thin", NULL
15 static int report_status;
18 static char capabilities[] = "report-status keep-pack";
19 static int capabilities_sent;
21 static int show_ref(const char *path, const unsigned char *sha1)
23 if (capabilities_sent)
24 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
26 packet_write(1, "%s %s%c%s\n",
27 sha1_to_hex(sha1), path, 0, capabilities);
28 capabilities_sent = 1;
32 static void write_head_info(void)
34 for_each_ref(show_ref);
35 if (!capabilities_sent)
36 show_ref("capabilities^{}", null_sha1);
42 const char *error_string;
43 unsigned char old_sha1[20];
44 unsigned char new_sha1[20];
45 char ref_name[FLEX_ARRAY]; /* more */
48 static struct command *commands;
50 static int is_all_zeroes(const char *hex)
53 for (i = 0; i < 40; i++)
59 static int verify_old_ref(const char *name, char *hex_contents)
64 if (is_all_zeroes(hex_contents))
66 fd = open(name, O_RDONLY);
69 ret = read(fd, buffer, 40);
73 if (memcmp(buffer, hex_contents, 40))
78 static char update_hook[] = "hooks/update";
80 static int run_update_hook(const char *refname,
81 char *old_hex, char *new_hex)
85 if (access(update_hook, X_OK) < 0)
87 code = run_command(update_hook, refname, old_hex, new_hex, NULL);
91 case -ERR_RUN_COMMAND_FORK:
92 return error("hook fork failed");
93 case -ERR_RUN_COMMAND_EXEC:
94 return error("hook execute failed");
95 case -ERR_RUN_COMMAND_WAITPID:
96 return error("waitpid failed");
97 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
98 return error("waitpid is confused");
99 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
100 return error("%s died of signal", update_hook);
101 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
102 return error("%s died strangely", update_hook);
104 error("%s exited with error code %d", update_hook, -code);
109 static int update(struct command *cmd)
111 const char *name = cmd->ref_name;
112 unsigned char *old_sha1 = cmd->old_sha1;
113 unsigned char *new_sha1 = cmd->new_sha1;
114 char new_hex[60], *old_hex, *lock_name;
115 int newfd, namelen, written;
117 cmd->error_string = NULL;
118 if (!strncmp(name, "refs/", 5) && check_ref_format(name + 5)) {
119 cmd->error_string = "funny refname";
120 return error("refusing to create funny ref '%s' locally",
124 namelen = strlen(name);
125 lock_name = xmalloc(namelen + 10);
126 memcpy(lock_name, name, namelen);
127 memcpy(lock_name + namelen, ".lock", 6);
129 strcpy(new_hex, sha1_to_hex(new_sha1));
130 old_hex = sha1_to_hex(old_sha1);
131 if (!has_sha1_file(new_sha1)) {
132 cmd->error_string = "bad pack";
133 return error("unpack should have generated %s, "
134 "but I can't find it!", new_hex);
136 if (deny_non_fast_forwards && !is_null_sha1(old_sha1)) {
137 struct commit *old_commit, *new_commit;
138 struct commit_list *bases, *ent;
140 old_commit = (struct commit *)parse_object(old_sha1);
141 new_commit = (struct commit *)parse_object(new_sha1);
142 bases = get_merge_bases(old_commit, new_commit, 1);
143 for (ent = bases; ent; ent = ent->next)
144 if (!hashcmp(old_sha1, ent->item->object.sha1))
146 free_commit_list(bases);
148 return error("denying non-fast forward;"
149 " you should pull first");
151 safe_create_leading_directories(lock_name);
153 newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
155 cmd->error_string = "can't lock";
156 return error("unable to create %s (%s)",
157 lock_name, strerror(errno));
160 /* Write the ref with an ending '\n' */
163 written = write(newfd, new_hex, 41);
164 /* Remove the '\n' again */
170 cmd->error_string = "can't write";
171 return error("unable to write %s", lock_name);
173 if (verify_old_ref(name, old_hex) < 0) {
175 cmd->error_string = "raced";
176 return error("%s changed during push", name);
178 if (run_update_hook(name, old_hex, new_hex)) {
180 cmd->error_string = "hook declined";
181 return error("hook declined to update %s", name);
183 else if (rename(lock_name, name) < 0) {
185 cmd->error_string = "can't rename";
186 return error("unable to replace %s", name);
189 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
194 static char update_post_hook[] = "hooks/post-update";
196 static void run_update_post_hook(struct command *cmd)
198 struct command *cmd_p;
202 if (access(update_post_hook, X_OK) < 0)
204 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
205 if (cmd_p->error_string)
209 argv = xmalloc(sizeof(*argv) * (1 + argc));
210 argv[0] = update_post_hook;
212 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
214 if (cmd_p->error_string)
216 p = xmalloc(strlen(cmd_p->ref_name) + 1);
217 strcpy(p, cmd_p->ref_name);
222 run_command_v_opt(argc, argv, RUN_COMMAND_NO_STDIO);
226 * This gets called after(if) we've successfully
227 * unpacked the data payload.
229 static void execute_commands(void)
231 struct command *cmd = commands;
237 run_update_post_hook(commands);
240 static void read_head_info(void)
242 struct command **p = &commands;
244 static char line[1000];
245 unsigned char old_sha1[20], new_sha1[20];
250 len = packet_read_line(0, line, sizeof(line));
253 if (line[len-1] == '\n')
258 get_sha1_hex(line, old_sha1) ||
259 get_sha1_hex(line + 41, new_sha1))
260 die("protocol error: expected old/new/ref, got '%s'",
264 reflen = strlen(refname);
265 if (reflen + 82 < len) {
266 if (strstr(refname + reflen + 1, "report-status"))
268 if (strstr(refname + reflen + 1, "keep-pack"))
271 cmd = xmalloc(sizeof(struct command) + len - 80);
272 hashcpy(cmd->old_sha1, old_sha1);
273 hashcpy(cmd->new_sha1, new_sha1);
274 memcpy(cmd->ref_name, line + 82, len - 81);
275 cmd->error_string = "n/a (unpacker error)";
282 static const char *unpack(int *error_code)
287 code = run_command_v_opt(ARRAY_SIZE(keep_packer) - 1,
288 keep_packer, RUN_GIT_CMD);
290 code = run_command_v_opt(ARRAY_SIZE(unpacker) - 1,
291 unpacker, RUN_GIT_CMD);
297 case -ERR_RUN_COMMAND_FORK:
298 return "unpack fork failed";
299 case -ERR_RUN_COMMAND_EXEC:
300 return "unpack execute failed";
301 case -ERR_RUN_COMMAND_WAITPID:
302 return "waitpid failed";
303 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
304 return "waitpid is confused";
305 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
306 return "unpacker died of signal";
307 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
308 return "unpacker died strangely";
311 return "unpacker exited with error code";
315 static void report(const char *unpack_status)
318 packet_write(1, "unpack %s\n",
319 unpack_status ? unpack_status : "ok");
320 for (cmd = commands; cmd; cmd = cmd->next) {
321 if (!cmd->error_string)
322 packet_write(1, "ok %s\n",
325 packet_write(1, "ng %s %s\n",
326 cmd->ref_name, cmd->error_string);
331 int main(int argc, char **argv)
337 for (i = 1; i < argc; i++) {
341 /* Do flag handling here */
342 usage(receive_pack_usage);
345 usage(receive_pack_usage);
349 usage(receive_pack_usage);
351 if (!enter_repo(dir, 0))
352 die("'%s': unable to chdir or not a git archive", dir);
362 const char *unpack_status = unpack(&code);
366 report(unpack_status);