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 };
12 static int deny_non_fast_forwards = 0;
13 static int report_status;
15 static char capabilities[] = "report-status";
16 static int capabilities_sent;
18 static int receive_pack_config(const char *var, const char *value)
20 git_default_config(var, value);
22 if (strcmp(var, "receive.denynonfastforwards") == 0)
24 deny_non_fast_forwards = git_config_bool(var, value);
31 static int show_ref(const char *path, const unsigned char *sha1)
33 if (capabilities_sent)
34 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
36 packet_write(1, "%s %s%c%s\n",
37 sha1_to_hex(sha1), path, 0, capabilities);
38 capabilities_sent = 1;
42 static void write_head_info(void)
44 for_each_ref(show_ref);
45 if (!capabilities_sent)
46 show_ref("capabilities^{}", null_sha1);
52 const char *error_string;
53 unsigned char old_sha1[20];
54 unsigned char new_sha1[20];
55 char ref_name[FLEX_ARRAY]; /* more */
58 static struct command *commands;
60 static int is_all_zeroes(const char *hex)
63 for (i = 0; i < 40; i++)
69 static int verify_old_ref(const char *name, char *hex_contents)
74 if (is_all_zeroes(hex_contents))
76 fd = open(name, O_RDONLY);
79 ret = read(fd, buffer, 40);
83 if (memcmp(buffer, hex_contents, 40))
88 static char update_hook[] = "hooks/update";
90 static int run_update_hook(const char *refname,
91 char *old_hex, char *new_hex)
95 if (access(update_hook, X_OK) < 0)
97 code = run_command(update_hook, refname, old_hex, new_hex, NULL);
101 case -ERR_RUN_COMMAND_FORK:
102 return error("hook fork failed");
103 case -ERR_RUN_COMMAND_EXEC:
104 return error("hook execute failed");
105 case -ERR_RUN_COMMAND_WAITPID:
106 return error("waitpid failed");
107 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
108 return error("waitpid is confused");
109 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
110 return error("%s died of signal", update_hook);
111 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
112 return error("%s died strangely", update_hook);
114 error("%s exited with error code %d", update_hook, -code);
119 static int update(struct command *cmd)
121 const char *name = cmd->ref_name;
122 unsigned char *old_sha1 = cmd->old_sha1;
123 unsigned char *new_sha1 = cmd->new_sha1;
124 char new_hex[60], *old_hex, *lock_name;
125 int newfd, namelen, written;
127 cmd->error_string = NULL;
128 if (!strncmp(name, "refs/", 5) && check_ref_format(name + 5)) {
129 cmd->error_string = "funny refname";
130 return error("refusing to create funny ref '%s' locally",
134 namelen = strlen(name);
135 lock_name = xmalloc(namelen + 10);
136 memcpy(lock_name, name, namelen);
137 memcpy(lock_name + namelen, ".lock", 6);
139 strcpy(new_hex, sha1_to_hex(new_sha1));
140 old_hex = sha1_to_hex(old_sha1);
141 if (!has_sha1_file(new_sha1)) {
142 cmd->error_string = "bad pack";
143 return error("unpack should have generated %s, "
144 "but I can't find it!", new_hex);
146 if (deny_non_fast_forwards && !is_null_sha1(old_sha1)) {
147 struct commit *old_commit, *new_commit;
148 struct commit_list *bases, *ent;
150 old_commit = (struct commit *)parse_object(old_sha1);
151 new_commit = (struct commit *)parse_object(new_sha1);
152 bases = get_merge_bases(old_commit, new_commit, 1);
153 for (ent = bases; ent; ent = ent->next)
154 if (!hashcmp(old_sha1, ent->item->object.sha1))
156 free_commit_list(bases);
158 return error("denying non-fast forward;"
159 " you should pull first");
161 safe_create_leading_directories(lock_name);
163 newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
165 cmd->error_string = "can't lock";
166 return error("unable to create %s (%s)",
167 lock_name, strerror(errno));
170 /* Write the ref with an ending '\n' */
173 written = write(newfd, new_hex, 41);
174 /* Remove the '\n' again */
180 cmd->error_string = "can't write";
181 return error("unable to write %s", lock_name);
183 if (verify_old_ref(name, old_hex) < 0) {
185 cmd->error_string = "raced";
186 return error("%s changed during push", name);
188 if (run_update_hook(name, old_hex, new_hex)) {
190 cmd->error_string = "hook declined";
191 return error("hook declined to update %s", name);
193 else if (rename(lock_name, name) < 0) {
195 cmd->error_string = "can't rename";
196 return error("unable to replace %s", name);
199 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
204 static char update_post_hook[] = "hooks/post-update";
206 static void run_update_post_hook(struct command *cmd)
208 struct command *cmd_p;
212 if (access(update_post_hook, X_OK) < 0)
214 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
215 if (cmd_p->error_string)
219 argv = xmalloc(sizeof(*argv) * (1 + argc));
220 argv[0] = update_post_hook;
222 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
224 if (cmd_p->error_string)
226 p = xmalloc(strlen(cmd_p->ref_name) + 1);
227 strcpy(p, cmd_p->ref_name);
232 run_command_v_opt(argc, argv, RUN_COMMAND_NO_STDIO);
236 * This gets called after(if) we've successfully
237 * unpacked the data payload.
239 static void execute_commands(void)
241 struct command *cmd = commands;
247 run_update_post_hook(commands);
250 static void read_head_info(void)
252 struct command **p = &commands;
254 static char line[1000];
255 unsigned char old_sha1[20], new_sha1[20];
260 len = packet_read_line(0, line, sizeof(line));
263 if (line[len-1] == '\n')
268 get_sha1_hex(line, old_sha1) ||
269 get_sha1_hex(line + 41, new_sha1))
270 die("protocol error: expected old/new/ref, got '%s'",
274 reflen = strlen(refname);
275 if (reflen + 82 < len) {
276 if (strstr(refname + reflen + 1, "report-status"))
279 cmd = xmalloc(sizeof(struct command) + len - 80);
280 hashcpy(cmd->old_sha1, old_sha1);
281 hashcpy(cmd->new_sha1, new_sha1);
282 memcpy(cmd->ref_name, line + 82, len - 81);
283 cmd->error_string = "n/a (unpacker error)";
290 static const char *unpack(int *error_code)
292 int code = run_command_v_opt(1, unpacker, RUN_GIT_CMD);
298 case -ERR_RUN_COMMAND_FORK:
299 return "unpack fork failed";
300 case -ERR_RUN_COMMAND_EXEC:
301 return "unpack execute failed";
302 case -ERR_RUN_COMMAND_WAITPID:
303 return "waitpid failed";
304 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
305 return "waitpid is confused";
306 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
307 return "unpacker died of signal";
308 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
309 return "unpacker died strangely";
312 return "unpacker exited with error code";
316 static void report(const char *unpack_status)
319 packet_write(1, "unpack %s\n",
320 unpack_status ? unpack_status : "ok");
321 for (cmd = commands; cmd; cmd = cmd->next) {
322 if (!cmd->error_string)
323 packet_write(1, "ok %s\n",
326 packet_write(1, "ng %s %s\n",
327 cmd->ref_name, cmd->error_string);
332 int main(int argc, char **argv)
338 for (i = 1; i < argc; i++) {
342 /* Do flag handling here */
343 usage(receive_pack_usage);
346 usage(receive_pack_usage);
350 usage(receive_pack_usage);
352 if(!enter_repo(dir, 0))
353 die("'%s': unable to chdir or not a git archive", dir);
355 git_config(receive_pack_config);
365 const char *unpack_status = unpack(&code);
369 report(unpack_status);