5 #include "run-command.h"
10 static const char receive_pack_usage
[] = "git-receive-pack <git-dir>";
12 static int deny_non_fast_forwards
= 0;
13 static int receive_unpack_limit
= -1;
14 static int transfer_unpack_limit
= -1;
15 static int unpack_limit
= 100;
16 static int report_status
;
18 static char capabilities
[] = " report-status delete-refs ";
19 static int capabilities_sent
;
21 static int receive_pack_config(const char *var
, const char *value
)
23 if (strcmp(var
, "receive.denynonfastforwards") == 0) {
24 deny_non_fast_forwards
= git_config_bool(var
, value
);
28 if (strcmp(var
, "receive.unpacklimit") == 0) {
29 receive_unpack_limit
= git_config_int(var
, value
);
33 if (strcmp(var
, "transfer.unpacklimit") == 0) {
34 transfer_unpack_limit
= git_config_int(var
, value
);
38 return git_default_config(var
, value
);
41 static int show_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
43 if (capabilities_sent
)
44 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), path
);
46 packet_write(1, "%s %s%c%s\n",
47 sha1_to_hex(sha1
), path
, 0, capabilities
);
48 capabilities_sent
= 1;
52 static void write_head_info(void)
54 for_each_ref(show_ref
, NULL
);
55 if (!capabilities_sent
)
56 show_ref("capabilities^{}", null_sha1
, 0, NULL
);
62 const char *error_string
;
63 unsigned char old_sha1
[20];
64 unsigned char new_sha1
[20];
65 char ref_name
[FLEX_ARRAY
]; /* more */
68 static struct command
*commands
;
70 static const char update_hook
[] = "hooks/update";
71 static const char pre_receive_hook
[] = "hooks/pre-receive";
72 static const char post_receive_hook
[] = "hooks/post-receive";
74 static int run_hook(const char *hook_name
,
75 struct command
*first_cmd
,
82 for (argc
= 0, cmd
= first_cmd
; cmd
; cmd
= cmd
->next
) {
83 if (!cmd
->error_string
)
89 if (!argc
|| access(hook_name
, X_OK
) < 0)
92 argv
= xmalloc(sizeof(*argv
) * (2 + argc
));
94 for (argc
= 1, cmd
= first_cmd
; cmd
; cmd
= cmd
->next
) {
95 if (!cmd
->error_string
) {
96 argv
[argc
++] = xstrdup(cmd
->ref_name
);
97 argv
[argc
++] = xstrdup(sha1_to_hex(cmd
->old_sha1
));
98 argv
[argc
++] = xstrdup(sha1_to_hex(cmd
->new_sha1
));
105 code
= run_command_v_opt(argv
,
106 RUN_COMMAND_NO_STDIN
| RUN_COMMAND_STDOUT_TO_STDERR
);
108 free((char*)argv
[argc
]);
114 case -ERR_RUN_COMMAND_FORK
:
115 return error("hook fork failed");
116 case -ERR_RUN_COMMAND_EXEC
:
117 return error("hook execute failed");
118 case -ERR_RUN_COMMAND_WAITPID
:
119 return error("waitpid failed");
120 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
121 return error("waitpid is confused");
122 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
123 return error("%s died of signal", hook_name
);
124 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
125 return error("%s died strangely", hook_name
);
127 error("%s exited with error code %d", hook_name
, -code
);
132 static const char *update(struct command
*cmd
)
134 const char *name
= cmd
->ref_name
;
135 unsigned char *old_sha1
= cmd
->old_sha1
;
136 unsigned char *new_sha1
= cmd
->new_sha1
;
137 struct ref_lock
*lock
;
139 if (!prefixcmp(name
, "refs/") && check_ref_format(name
+ 5)) {
140 error("refusing to create funny ref '%s' locally", name
);
141 return "funny refname";
144 if (!is_null_sha1(new_sha1
) && !has_sha1_file(new_sha1
)) {
145 error("unpack should have generated %s, "
146 "but I can't find it!", sha1_to_hex(new_sha1
));
149 if (deny_non_fast_forwards
&& !is_null_sha1(new_sha1
) &&
150 !is_null_sha1(old_sha1
) &&
151 !prefixcmp(name
, "refs/heads/")) {
152 struct commit
*old_commit
, *new_commit
;
153 struct commit_list
*bases
, *ent
;
155 old_commit
= (struct commit
*)parse_object(old_sha1
);
156 new_commit
= (struct commit
*)parse_object(new_sha1
);
157 bases
= get_merge_bases(old_commit
, new_commit
, 1);
158 for (ent
= bases
; ent
; ent
= ent
->next
)
159 if (!hashcmp(old_sha1
, ent
->item
->object
.sha1
))
161 free_commit_list(bases
);
163 error("denying non-fast forward %s"
164 " (you should pull first)", name
);
165 return "non-fast forward";
168 if (run_hook(update_hook
, cmd
, 1)) {
169 error("hook declined to update %s", name
);
170 return "hook declined";
173 if (is_null_sha1(new_sha1
)) {
174 if (delete_ref(name
, old_sha1
)) {
175 error("failed to delete %s", name
);
176 return "failed to delete";
178 fprintf(stderr
, "%s: %s -> deleted\n", name
,
179 sha1_to_hex(old_sha1
));
180 return NULL
; /* good */
183 lock
= lock_any_ref_for_update(name
, old_sha1
);
185 error("failed to lock %s", name
);
186 return "failed to lock";
188 if (write_ref_sha1(lock
, new_sha1
, "push")) {
189 return "failed to write"; /* error() already called */
191 fprintf(stderr
, "%s: %s -> %s\n", name
,
192 sha1_to_hex(old_sha1
), sha1_to_hex(new_sha1
));
193 return NULL
; /* good */
197 static char update_post_hook
[] = "hooks/post-update";
199 static void run_update_post_hook(struct command
*cmd
)
201 struct command
*cmd_p
;
205 for (argc
= 0, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
206 if (cmd_p
->error_string
)
210 if (!argc
|| access(update_post_hook
, X_OK
) < 0)
212 argv
= xmalloc(sizeof(*argv
) * (2 + argc
));
213 argv
[0] = update_post_hook
;
215 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
217 if (cmd_p
->error_string
)
219 p
= xmalloc(strlen(cmd_p
->ref_name
) + 1);
220 strcpy(p
, cmd_p
->ref_name
);
225 run_command_v_opt(argv
, RUN_COMMAND_NO_STDIN
226 | RUN_COMMAND_STDOUT_TO_STDERR
);
229 static void execute_commands(const char *unpacker_error
)
231 struct command
*cmd
= commands
;
233 if (unpacker_error
) {
235 cmd
->error_string
= "n/a (unpacker error)";
241 if (run_hook(pre_receive_hook
, commands
, 0)) {
243 cmd
->error_string
= "pre-receive hook declined";
250 cmd
->error_string
= update(cmd
);
255 static void read_head_info(void)
257 struct command
**p
= &commands
;
259 static char line
[1000];
260 unsigned char old_sha1
[20], new_sha1
[20];
265 len
= packet_read_line(0, line
, sizeof(line
));
268 if (line
[len
-1] == '\n')
273 get_sha1_hex(line
, old_sha1
) ||
274 get_sha1_hex(line
+ 41, new_sha1
))
275 die("protocol error: expected old/new/ref, got '%s'",
279 reflen
= strlen(refname
);
280 if (reflen
+ 82 < len
) {
281 if (strstr(refname
+ reflen
+ 1, "report-status"))
284 cmd
= xmalloc(sizeof(struct command
) + len
- 80);
285 hashcpy(cmd
->old_sha1
, old_sha1
);
286 hashcpy(cmd
->new_sha1
, new_sha1
);
287 memcpy(cmd
->ref_name
, line
+ 82, len
- 81);
288 cmd
->error_string
= NULL
;
295 static const char *parse_pack_header(struct pack_header
*hdr
)
297 switch (read_pack_header(0, hdr
)) {
299 return "eof before pack header was fully read";
301 case PH_ERROR_PACK_SIGNATURE
:
302 return "protocol error (pack signature mismatch detected)";
304 case PH_ERROR_PROTOCOL
:
305 return "protocol error (pack version unsupported)";
308 return "unknown error in parse_pack_header";
315 static const char *pack_lockfile
;
317 static const char *unpack(void)
319 struct pack_header hdr
;
323 hdr_err
= parse_pack_header(&hdr
);
326 snprintf(hdr_arg
, sizeof(hdr_arg
), "--pack_header=%u,%u",
327 ntohl(hdr
.hdr_version
), ntohl(hdr
.hdr_entries
));
329 if (ntohl(hdr
.hdr_entries
) < unpack_limit
) {
331 const char *unpacker
[3];
332 unpacker
[0] = "unpack-objects";
333 unpacker
[1] = hdr_arg
;
335 code
= run_command_v_opt(unpacker
, RUN_GIT_CMD
);
339 case -ERR_RUN_COMMAND_FORK
:
340 return "unpack fork failed";
341 case -ERR_RUN_COMMAND_EXEC
:
342 return "unpack execute failed";
343 case -ERR_RUN_COMMAND_WAITPID
:
344 return "waitpid failed";
345 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
346 return "waitpid is confused";
347 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
348 return "unpacker died of signal";
349 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
350 return "unpacker died strangely";
352 return "unpacker exited with error code";
355 const char *keeper
[6];
356 int fd
[2], s
, len
, status
;
361 s
= sprintf(keep_arg
, "--keep=receive-pack %i on ", getpid());
362 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
363 strcpy(keep_arg
+ s
, "localhost");
365 keeper
[0] = "index-pack";
366 keeper
[1] = "--stdin";
367 keeper
[2] = "--fix-thin";
369 keeper
[4] = keep_arg
;
373 return "index-pack pipe failed";
376 return "index-pack fork failed";
381 execv_git_cmd(keeper
);
382 die("execv of index-pack failed");
387 * The first thing we expects from index-pack's output
388 * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where
389 * %40s is the newly created pack SHA1 name. In the "keep"
390 * case, we need it to remove the corresponding .keep file
391 * later on. If we don't get that then tough luck with it.
394 len
< 46 && (s
= xread(fd
[0], packname
+len
, 46-len
)) > 0;
397 if (len
== 46 && packname
[45] == '\n' &&
398 memcmp(packname
, "keep\t", 5) == 0) {
401 snprintf(path
, sizeof(path
), "%s/pack/pack-%s.keep",
402 get_object_directory(), packname
+ 5);
403 pack_lockfile
= xstrdup(path
);
406 /* Then wrap our index-pack process. */
407 while (waitpid(pid
, &status
, 0) < 0)
409 return "waitpid failed";
410 if (WIFEXITED(status
)) {
411 int code
= WEXITSTATUS(status
);
413 return "index-pack exited with error code";
414 reprepare_packed_git();
417 return "index-pack abnormal exit";
421 static void report(const char *unpack_status
)
424 packet_write(1, "unpack %s\n",
425 unpack_status
? unpack_status
: "ok");
426 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
427 if (!cmd
->error_string
)
428 packet_write(1, "ok %s\n",
431 packet_write(1, "ng %s %s\n",
432 cmd
->ref_name
, cmd
->error_string
);
437 static int delete_only(struct command
*cmd
)
440 if (!is_null_sha1(cmd
->new_sha1
))
447 int main(int argc
, char **argv
)
453 for (i
= 1; i
< argc
; i
++) {
457 /* Do flag handling here */
458 usage(receive_pack_usage
);
461 usage(receive_pack_usage
);
465 usage(receive_pack_usage
);
467 if (!enter_repo(dir
, 0))
468 die("'%s': unable to chdir or not a git archive", dir
);
470 if (is_repository_shallow())
471 die("attempt to push into a shallow repository");
473 git_config(receive_pack_config
);
475 if (0 <= transfer_unpack_limit
)
476 unpack_limit
= transfer_unpack_limit
;
477 else if (0 <= receive_unpack_limit
)
478 unpack_limit
= receive_unpack_limit
;
487 const char *unpack_status
= NULL
;
489 if (!delete_only(commands
))
490 unpack_status
= unpack();
491 execute_commands(unpack_status
);
493 unlink(pack_lockfile
);
495 report(unpack_status
);
496 run_hook(post_receive_hook
, commands
, 0);
497 run_update_post_hook(commands
);