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_fsck_objects
;
14 static int receive_unpack_limit
= -1;
15 static int transfer_unpack_limit
= -1;
16 static int unpack_limit
= 100;
17 static int report_status
;
19 static char capabilities
[] = " report-status delete-refs tellme-more ";
20 static int capabilities_sent
;
22 static int receive_pack_config(const char *var
, const char *value
)
24 if (strcmp(var
, "receive.denynonfastforwards") == 0) {
25 deny_non_fast_forwards
= git_config_bool(var
, value
);
29 if (strcmp(var
, "receive.unpacklimit") == 0) {
30 receive_unpack_limit
= git_config_int(var
, value
);
34 if (strcmp(var
, "transfer.unpacklimit") == 0) {
35 transfer_unpack_limit
= git_config_int(var
, value
);
39 if (strcmp(var
, "receive.fsckobjects") == 0) {
40 receive_fsck_objects
= git_config_bool(var
, value
);
44 return git_default_config(var
, value
);
47 static int show_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
49 if (capabilities_sent
)
50 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), path
);
52 packet_write(1, "%s %s%c%s\n",
53 sha1_to_hex(sha1
), path
, 0, capabilities
);
54 capabilities_sent
= 1;
58 static void write_head_info(void)
60 for_each_ref(show_ref
, NULL
);
61 if (!capabilities_sent
)
62 show_ref("capabilities^{}", null_sha1
, 0, NULL
);
68 const char *error_string
;
69 unsigned char old_sha1
[20];
70 unsigned char new_sha1
[20];
71 char ref_name
[FLEX_ARRAY
]; /* more */
74 static struct command
*commands
;
76 static const char pre_receive_hook
[] = "hooks/pre-receive";
77 static const char post_receive_hook
[] = "hooks/post-receive";
79 static int hook_status(int code
, const char *hook_name
)
84 case -ERR_RUN_COMMAND_FORK
:
85 return error("hook fork failed");
86 case -ERR_RUN_COMMAND_EXEC
:
87 return error("hook execute failed");
88 case -ERR_RUN_COMMAND_PIPE
:
89 return error("hook pipe 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", hook_name
);
96 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
97 return error("%s died strangely", hook_name
);
99 error("%s exited with error code %d", hook_name
, -code
);
104 static int run_hook(const char *hook_name
)
106 static char buf
[sizeof(commands
->old_sha1
) * 2 + PATH_MAX
+ 4];
108 struct child_process proc
;
110 int have_input
= 0, code
;
112 for (cmd
= commands
; !have_input
&& cmd
; cmd
= cmd
->next
) {
113 if (!cmd
->error_string
)
117 if (!have_input
|| access(hook_name
, X_OK
) < 0)
123 memset(&proc
, 0, sizeof(proc
));
126 proc
.stdout_to_stderr
= 1;
128 code
= start_command(&proc
);
130 return hook_status(code
, hook_name
);
131 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
132 if (!cmd
->error_string
) {
133 size_t n
= snprintf(buf
, sizeof(buf
), "%s %s %s\n",
134 sha1_to_hex(cmd
->old_sha1
),
135 sha1_to_hex(cmd
->new_sha1
),
137 if (write_in_full(proc
.in
, buf
, n
) != n
)
142 return hook_status(finish_command(&proc
), hook_name
);
145 static int run_update_hook(struct command
*cmd
)
147 static const char update_hook
[] = "hooks/update";
148 struct child_process proc
;
151 if (access(update_hook
, X_OK
) < 0)
154 argv
[0] = update_hook
;
155 argv
[1] = cmd
->ref_name
;
156 argv
[2] = sha1_to_hex(cmd
->old_sha1
);
157 argv
[3] = sha1_to_hex(cmd
->new_sha1
);
160 memset(&proc
, 0, sizeof(proc
));
163 proc
.stdout_to_stderr
= 1;
165 return hook_status(run_command(&proc
), update_hook
);
168 static const char *update(struct command
*cmd
)
170 const char *name
= cmd
->ref_name
;
171 unsigned char *old_sha1
= cmd
->old_sha1
;
172 unsigned char *new_sha1
= cmd
->new_sha1
;
173 struct ref_lock
*lock
;
175 /* only refs/... are allowed */
176 if (prefixcmp(name
, "refs/") || check_ref_format(name
+ 5)) {
177 error("refusing to create funny ref '%s' remotely", name
);
178 return "funny refname";
181 if (!is_null_sha1(new_sha1
) && !has_sha1_file(new_sha1
)) {
182 error("unpack should have generated %s, "
183 "but I can't find it!", sha1_to_hex(new_sha1
));
186 if (deny_non_fast_forwards
&& !is_null_sha1(new_sha1
) &&
187 !is_null_sha1(old_sha1
) &&
188 !prefixcmp(name
, "refs/heads/")) {
189 struct object
*old_object
, *new_object
;
190 struct commit
*old_commit
, *new_commit
;
191 struct commit_list
*bases
, *ent
;
193 old_object
= parse_object(old_sha1
);
194 new_object
= parse_object(new_sha1
);
196 if (!old_object
|| !new_object
||
197 old_object
->type
!= OBJ_COMMIT
||
198 new_object
->type
!= OBJ_COMMIT
) {
199 error("bad sha1 objects for %s", name
);
202 old_commit
= (struct commit
*)old_object
;
203 new_commit
= (struct commit
*)new_object
;
204 bases
= get_merge_bases(old_commit
, new_commit
, 1);
205 for (ent
= bases
; ent
; ent
= ent
->next
)
206 if (!hashcmp(old_sha1
, ent
->item
->object
.sha1
))
208 free_commit_list(bases
);
210 error("denying non-fast forward %s"
211 " (you should pull first)", name
);
212 return "non-fast forward";
215 if (run_update_hook(cmd
)) {
216 error("hook declined to update %s", name
);
217 return "hook declined";
220 if (is_null_sha1(new_sha1
)) {
221 if (!parse_object(old_sha1
)) {
222 warning ("Allowing deletion of corrupt ref.");
225 if (delete_ref(name
, old_sha1
)) {
226 error("failed to delete %s", name
);
227 return "failed to delete";
229 return NULL
; /* good */
232 lock
= lock_any_ref_for_update(name
, old_sha1
, 0);
234 error("failed to lock %s", name
);
235 return "failed to lock";
237 if (write_ref_sha1(lock
, new_sha1
, "push")) {
238 return "failed to write"; /* error() already called */
240 return NULL
; /* good */
244 static char update_post_hook
[] = "hooks/post-update";
246 static void run_update_post_hook(struct command
*cmd
)
248 struct command
*cmd_p
;
252 for (argc
= 0, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
253 if (cmd_p
->error_string
)
257 if (!argc
|| access(update_post_hook
, X_OK
) < 0)
259 argv
= xmalloc(sizeof(*argv
) * (2 + argc
));
260 argv
[0] = update_post_hook
;
262 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
264 if (cmd_p
->error_string
)
266 p
= xmalloc(strlen(cmd_p
->ref_name
) + 1);
267 strcpy(p
, cmd_p
->ref_name
);
272 run_command_v_opt(argv
, RUN_COMMAND_NO_STDIN
273 | RUN_COMMAND_STDOUT_TO_STDERR
);
276 static void execute_commands(const char *unpacker_error
)
278 struct command
*cmd
= commands
;
280 if (unpacker_error
) {
282 cmd
->error_string
= "n/a (unpacker error)";
288 if (run_hook(pre_receive_hook
)) {
290 cmd
->error_string
= "pre-receive hook declined";
297 cmd
->error_string
= update(cmd
);
302 static int parse_tellme_more(char *line
, int len
, struct commit_list
**list
)
304 unsigned char sha1
[20];
308 !strncmp(line
, "tellme-more ", 12) &&
309 !get_sha1_hex(line
+ 12, sha1
)) {
310 c
= lookup_commit_reference_gently(sha1
, 1);
312 commit_list_insert(c
, list
);
318 static void read_head_info(struct commit_list
**tellme_more
)
320 struct command
**p
= &commands
;
322 static char line
[1000];
323 unsigned char old_sha1
[20], new_sha1
[20];
328 len
= packet_read_line(0, line
, sizeof(line
));
331 if (line
[len
-1] == '\n')
333 if (parse_tellme_more(line
, len
, tellme_more
))
338 get_sha1_hex(line
, old_sha1
) ||
339 get_sha1_hex(line
+ 41, new_sha1
))
340 die("protocol error: expected old/new/ref, got '%s'",
344 reflen
= strlen(refname
);
345 if (reflen
+ 82 < len
) {
346 if (strstr(refname
+ reflen
+ 1, "report-status"))
349 cmd
= xmalloc(sizeof(struct command
) + len
- 80);
350 hashcpy(cmd
->old_sha1
, old_sha1
);
351 hashcpy(cmd
->new_sha1
, new_sha1
);
352 memcpy(cmd
->ref_name
, line
+ 82, len
- 81);
353 cmd
->error_string
= NULL
;
360 static void sender_wants_more(struct commit_list
**tellme_more
)
366 len
= packet_read_line(0, line
, sizeof(line
));
369 if (line
[len
-1] == '\n')
371 if (parse_tellme_more(line
, len
, tellme_more
))
373 /* ignore other responses for future extension */
377 #define SHOWN_TO_SENDER (1<<20)
378 static struct commit_list
**add_parents(struct commit
*commit
, struct commit_list
**tail
, int round
)
380 struct commit_list
*parents
= commit
->parents
;
383 * In round N, we will send one commit for every (N+1)^2
384 * commits in single parent chain to optimize for very stale
389 if (!parents
|| (parents
&& parents
->next
))
390 break; /* do not skip merges */
391 commit
= parents
->item
;
392 if (parse_commit(commit
))
393 return tail
; /* oops */
394 if (!commit
->parents
)
395 break; /* do not skip root */
397 * Before skipping this commit, mark it so that
398 * later clear_commit_marks() can clear its ancestors.
400 commit
->object
.flags
|= SHOWN_TO_SENDER
;
401 parents
= commit
->parents
;
405 commit
= parents
->item
;
406 parents
= parents
->next
;
407 if (commit
->object
.flags
& SHOWN_TO_SENDER
)
409 if (parse_commit(commit
))
411 tail
= &commit_list_insert(commit
, tail
)->next
;
417 static void send_ancestors(struct commit
*commit
, int round
)
419 struct commit_list
*list
= NULL
;
420 struct commit_list
**tail
= &list
;
423 tail
= &commit_list_insert(commit
, tail
)->next
;
424 while (list
&& count
< BATCH
) {
425 struct commit_list
*elem
= list
;
426 struct commit
*c
= elem
->item
;
433 if (c
->object
.flags
& SHOWN_TO_SENDER
)
437 c
->object
.flags
|= SHOWN_TO_SENDER
;
439 packet_write(1, "%s %s\n",
440 sha1_to_hex(c
->object
.sha1
),
441 sha1_to_hex(commit
->object
.sha1
));
444 tail
= add_parents(c
, tail
, round
);
446 clear_commit_marks(commit
, SHOWN_TO_SENDER
);
447 free_commit_list(list
);
451 static void exchange_history(struct commit_list
*list
)
457 struct commit_list
*elem
= list
;
458 struct commit
*commit
= elem
->item
;
464 * The sender does not know about commit and asks its
465 * ancestors to be sent.
467 if (parse_commit(commit
))
470 send_ancestors(commit
, round
);
472 packet_flush(1); /* have you heard enough? */
474 sender_wants_more(&list
);
476 if (MAX_ROUND
< round
) {
480 free_commit_list(list
);
482 /* wait until the other end gives up */
483 sender_wants_more(&list
);
490 static const char *parse_pack_header(struct pack_header
*hdr
)
492 switch (read_pack_header(0, hdr
)) {
494 return "eof before pack header was fully read";
496 case PH_ERROR_PACK_SIGNATURE
:
497 return "protocol error (pack signature mismatch detected)";
499 case PH_ERROR_PROTOCOL
:
500 return "protocol error (pack version unsupported)";
503 return "unknown error in parse_pack_header";
510 static const char *pack_lockfile
;
512 static const char *unpack(void)
514 struct pack_header hdr
;
518 hdr_err
= parse_pack_header(&hdr
);
521 snprintf(hdr_arg
, sizeof(hdr_arg
), "--pack_header=%u,%u",
522 ntohl(hdr
.hdr_version
), ntohl(hdr
.hdr_entries
));
524 if (ntohl(hdr
.hdr_entries
) < unpack_limit
) {
526 const char *unpacker
[4];
527 unpacker
[i
++] = "unpack-objects";
528 if (receive_fsck_objects
)
529 unpacker
[i
++] = "--strict";
530 unpacker
[i
++] = hdr_arg
;
531 unpacker
[i
++] = NULL
;
532 code
= run_command_v_opt(unpacker
, RUN_GIT_CMD
);
536 case -ERR_RUN_COMMAND_FORK
:
537 return "unpack fork failed";
538 case -ERR_RUN_COMMAND_EXEC
:
539 return "unpack execute failed";
540 case -ERR_RUN_COMMAND_WAITPID
:
541 return "waitpid failed";
542 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
543 return "waitpid is confused";
544 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
545 return "unpacker died of signal";
546 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
547 return "unpacker died strangely";
549 return "unpacker exited with error code";
552 const char *keeper
[7];
553 int s
, status
, i
= 0;
555 struct child_process ip
;
557 s
= sprintf(keep_arg
, "--keep=receive-pack %i on ", getpid());
558 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
559 strcpy(keep_arg
+ s
, "localhost");
561 keeper
[i
++] = "index-pack";
562 keeper
[i
++] = "--stdin";
563 if (receive_fsck_objects
)
564 keeper
[i
++] = "--strict";
565 keeper
[i
++] = "--fix-thin";
566 keeper
[i
++] = hdr_arg
;
567 keeper
[i
++] = keep_arg
;
569 memset(&ip
, 0, sizeof(ip
));
573 if (start_command(&ip
))
574 return "index-pack fork failed";
575 pack_lockfile
= index_pack_lockfile(ip
.out
);
577 status
= finish_command(&ip
);
579 reprepare_packed_git();
582 return "index-pack abnormal exit";
586 static void report(const char *unpack_status
)
589 packet_write(1, "unpack %s\n",
590 unpack_status
? unpack_status
: "ok");
591 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
592 if (!cmd
->error_string
)
593 packet_write(1, "ok %s\n",
596 packet_write(1, "ng %s %s\n",
597 cmd
->ref_name
, cmd
->error_string
);
602 static int delete_only(struct command
*cmd
)
605 if (!is_null_sha1(cmd
->new_sha1
))
612 int main(int argc
, char **argv
)
616 struct commit_list
*tellme_more
= NULL
;
619 for (i
= 1; i
< argc
; i
++) {
623 /* Do flag handling here */
624 usage(receive_pack_usage
);
627 usage(receive_pack_usage
);
631 usage(receive_pack_usage
);
635 if (!enter_repo(dir
, 0))
636 die("'%s': unable to chdir or not a git archive", dir
);
638 if (is_repository_shallow())
639 die("attempt to push into a shallow repository");
641 git_config(receive_pack_config
);
643 if (0 <= transfer_unpack_limit
)
644 unpack_limit
= transfer_unpack_limit
;
645 else if (0 <= receive_unpack_limit
)
646 unpack_limit
= receive_unpack_limit
;
653 read_head_info(&tellme_more
);
656 exchange_history(tellme_more
);
659 const char *unpack_status
= NULL
;
661 if (!delete_only(commands
))
662 unpack_status
= unpack();
663 execute_commands(unpack_status
);
665 unlink(pack_lockfile
);
667 report(unpack_status
);
668 run_hook(post_receive_hook
);
669 run_update_post_hook(commands
);