add receive.denyNonFastforwards config variable
[git/dscho.git] / receive-pack.c
bloba6ec9f900d6ff57d6123dbd599e63324c642a79c
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include "run-command.h"
5 #include "commit.h"
6 #include "object.h"
8 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
10 static const char *unpacker[] = { "unpack-objects", NULL };
12 static int report_status;
14 static char capabilities[] = "report-status";
15 static int capabilities_sent;
17 static int show_ref(const char *path, const unsigned char *sha1)
19 if (capabilities_sent)
20 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
21 else
22 packet_write(1, "%s %s%c%s\n",
23 sha1_to_hex(sha1), path, 0, capabilities);
24 capabilities_sent = 1;
25 return 0;
28 static void write_head_info(void)
30 for_each_ref(show_ref);
31 if (!capabilities_sent)
32 show_ref("capabilities^{}", null_sha1);
36 struct command {
37 struct command *next;
38 const char *error_string;
39 unsigned char old_sha1[20];
40 unsigned char new_sha1[20];
41 char ref_name[FLEX_ARRAY]; /* more */
44 static struct command *commands;
46 static int is_all_zeroes(const char *hex)
48 int i;
49 for (i = 0; i < 40; i++)
50 if (*hex++ != '0')
51 return 0;
52 return 1;
55 static int verify_old_ref(const char *name, char *hex_contents)
57 int fd, ret;
58 char buffer[60];
60 if (is_all_zeroes(hex_contents))
61 return 0;
62 fd = open(name, O_RDONLY);
63 if (fd < 0)
64 return -1;
65 ret = read(fd, buffer, 40);
66 close(fd);
67 if (ret != 40)
68 return -1;
69 if (memcmp(buffer, hex_contents, 40))
70 return -1;
71 return 0;
74 static char update_hook[] = "hooks/update";
76 static int run_update_hook(const char *refname,
77 char *old_hex, char *new_hex)
79 int code;
81 if (access(update_hook, X_OK) < 0)
82 return 0;
83 code = run_command(update_hook, refname, old_hex, new_hex, NULL);
84 switch (code) {
85 case 0:
86 return 0;
87 case -ERR_RUN_COMMAND_FORK:
88 return error("hook fork failed");
89 case -ERR_RUN_COMMAND_EXEC:
90 return error("hook execute failed");
91 case -ERR_RUN_COMMAND_WAITPID:
92 return error("waitpid failed");
93 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
94 return error("waitpid is confused");
95 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
96 return error("%s died of signal", update_hook);
97 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
98 return error("%s died strangely", update_hook);
99 default:
100 error("%s exited with error code %d", update_hook, -code);
101 return -code;
105 static int update(struct command *cmd)
107 const char *name = cmd->ref_name;
108 unsigned char *old_sha1 = cmd->old_sha1;
109 unsigned char *new_sha1 = cmd->new_sha1;
110 char new_hex[60], *old_hex, *lock_name;
111 int newfd, namelen, written;
113 cmd->error_string = NULL;
114 if (!strncmp(name, "refs/", 5) && check_ref_format(name + 5)) {
115 cmd->error_string = "funny refname";
116 return error("refusing to create funny ref '%s' locally",
117 name);
120 namelen = strlen(name);
121 lock_name = xmalloc(namelen + 10);
122 memcpy(lock_name, name, namelen);
123 memcpy(lock_name + namelen, ".lock", 6);
125 strcpy(new_hex, sha1_to_hex(new_sha1));
126 old_hex = sha1_to_hex(old_sha1);
127 if (!has_sha1_file(new_sha1)) {
128 cmd->error_string = "bad pack";
129 return error("unpack should have generated %s, "
130 "but I can't find it!", new_hex);
132 if (deny_non_fast_forwards && !is_null_sha1(old_sha1)) {
133 struct commit *old_commit, *new_commit;
134 struct commit_list *bases;
136 old_commit = (struct commit *)parse_object(old_sha1);
137 new_commit = (struct commit *)parse_object(new_sha1);
138 for (bases = get_merge_bases(old_commit, new_commit, 1);
139 bases; bases = bases->next)
140 if (!hashcmp(old_sha1, bases->item->object.sha1))
141 break;
142 if (!bases)
143 return error("denying non-fast forward;"
144 " you should pull first");
146 safe_create_leading_directories(lock_name);
148 newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
149 if (newfd < 0) {
150 cmd->error_string = "can't lock";
151 return error("unable to create %s (%s)",
152 lock_name, strerror(errno));
155 /* Write the ref with an ending '\n' */
156 new_hex[40] = '\n';
157 new_hex[41] = 0;
158 written = write(newfd, new_hex, 41);
159 /* Remove the '\n' again */
160 new_hex[40] = 0;
162 close(newfd);
163 if (written != 41) {
164 unlink(lock_name);
165 cmd->error_string = "can't write";
166 return error("unable to write %s", lock_name);
168 if (verify_old_ref(name, old_hex) < 0) {
169 unlink(lock_name);
170 cmd->error_string = "raced";
171 return error("%s changed during push", name);
173 if (run_update_hook(name, old_hex, new_hex)) {
174 unlink(lock_name);
175 cmd->error_string = "hook declined";
176 return error("hook declined to update %s", name);
178 else if (rename(lock_name, name) < 0) {
179 unlink(lock_name);
180 cmd->error_string = "can't rename";
181 return error("unable to replace %s", name);
183 else {
184 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
185 return 0;
189 static char update_post_hook[] = "hooks/post-update";
191 static void run_update_post_hook(struct command *cmd)
193 struct command *cmd_p;
194 int argc;
195 const char **argv;
197 if (access(update_post_hook, X_OK) < 0)
198 return;
199 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
200 if (cmd_p->error_string)
201 continue;
202 argc++;
204 argv = xmalloc(sizeof(*argv) * (1 + argc));
205 argv[0] = update_post_hook;
207 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
208 char *p;
209 if (cmd_p->error_string)
210 continue;
211 p = xmalloc(strlen(cmd_p->ref_name) + 1);
212 strcpy(p, cmd_p->ref_name);
213 argv[argc] = p;
214 argc++;
216 argv[argc] = NULL;
217 run_command_v_opt(argc, argv, RUN_COMMAND_NO_STDIO);
221 * This gets called after(if) we've successfully
222 * unpacked the data payload.
224 static void execute_commands(void)
226 struct command *cmd = commands;
228 while (cmd) {
229 update(cmd);
230 cmd = cmd->next;
232 run_update_post_hook(commands);
235 static void read_head_info(void)
237 struct command **p = &commands;
238 for (;;) {
239 static char line[1000];
240 unsigned char old_sha1[20], new_sha1[20];
241 struct command *cmd;
242 char *refname;
243 int len, reflen;
245 len = packet_read_line(0, line, sizeof(line));
246 if (!len)
247 break;
248 if (line[len-1] == '\n')
249 line[--len] = 0;
250 if (len < 83 ||
251 line[40] != ' ' ||
252 line[81] != ' ' ||
253 get_sha1_hex(line, old_sha1) ||
254 get_sha1_hex(line + 41, new_sha1))
255 die("protocol error: expected old/new/ref, got '%s'",
256 line);
258 refname = line + 82;
259 reflen = strlen(refname);
260 if (reflen + 82 < len) {
261 if (strstr(refname + reflen + 1, "report-status"))
262 report_status = 1;
264 cmd = xmalloc(sizeof(struct command) + len - 80);
265 hashcpy(cmd->old_sha1, old_sha1);
266 hashcpy(cmd->new_sha1, new_sha1);
267 memcpy(cmd->ref_name, line + 82, len - 81);
268 cmd->error_string = "n/a (unpacker error)";
269 cmd->next = NULL;
270 *p = cmd;
271 p = &cmd->next;
275 static const char *unpack(int *error_code)
277 int code = run_command_v_opt(1, unpacker, RUN_GIT_CMD);
279 *error_code = 0;
280 switch (code) {
281 case 0:
282 return NULL;
283 case -ERR_RUN_COMMAND_FORK:
284 return "unpack fork failed";
285 case -ERR_RUN_COMMAND_EXEC:
286 return "unpack execute failed";
287 case -ERR_RUN_COMMAND_WAITPID:
288 return "waitpid failed";
289 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
290 return "waitpid is confused";
291 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
292 return "unpacker died of signal";
293 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
294 return "unpacker died strangely";
295 default:
296 *error_code = -code;
297 return "unpacker exited with error code";
301 static void report(const char *unpack_status)
303 struct command *cmd;
304 packet_write(1, "unpack %s\n",
305 unpack_status ? unpack_status : "ok");
306 for (cmd = commands; cmd; cmd = cmd->next) {
307 if (!cmd->error_string)
308 packet_write(1, "ok %s\n",
309 cmd->ref_name);
310 else
311 packet_write(1, "ng %s %s\n",
312 cmd->ref_name, cmd->error_string);
314 packet_flush(1);
317 int main(int argc, char **argv)
319 int i;
320 char *dir = NULL;
322 argv++;
323 for (i = 1; i < argc; i++) {
324 char *arg = *argv++;
326 if (*arg == '-') {
327 /* Do flag handling here */
328 usage(receive_pack_usage);
330 if (dir)
331 usage(receive_pack_usage);
332 dir = arg;
334 if (!dir)
335 usage(receive_pack_usage);
337 if(!enter_repo(dir, 0))
338 die("'%s': unable to chdir or not a git archive", dir);
340 write_head_info();
342 /* EOF */
343 packet_flush(1);
345 read_head_info();
346 if (commands) {
347 int code;
348 const char *unpack_status = unpack(&code);
349 if (!unpack_status)
350 execute_commands();
351 if (report_status)
352 report(unpack_status);
354 return 0;