5 #include "run-command.h"
10 #include "transport.h"
12 static const char receive_pack_usage
[] = "git-receive-pack <git-dir>";
20 static int deny_deletes
= 0;
21 static int deny_non_fast_forwards
= 0;
22 static enum deny_action deny_current_branch
= DENY_WARN
;
23 static int receive_fsck_objects
;
24 static int receive_unpack_limit
= -1;
25 static int transfer_unpack_limit
= -1;
26 static int unpack_limit
= 100;
27 static int report_status
;
29 static char capabilities
[] = " report-status delete-refs ";
30 static int capabilities_sent
;
32 static enum deny_action
parse_deny_action(const char *var
, const char *value
)
35 if (!strcasecmp(value
, "ignore"))
37 if (!strcasecmp(value
, "warn"))
39 if (!strcasecmp(value
, "refuse"))
42 if (git_config_bool(var
, value
))
47 static int receive_pack_config(const char *var
, const char *value
, void *cb
)
49 if (strcmp(var
, "receive.denydeletes") == 0) {
50 deny_deletes
= git_config_bool(var
, value
);
54 if (strcmp(var
, "receive.denynonfastforwards") == 0) {
55 deny_non_fast_forwards
= git_config_bool(var
, value
);
59 if (strcmp(var
, "receive.unpacklimit") == 0) {
60 receive_unpack_limit
= git_config_int(var
, value
);
64 if (strcmp(var
, "transfer.unpacklimit") == 0) {
65 transfer_unpack_limit
= git_config_int(var
, value
);
69 if (strcmp(var
, "receive.fsckobjects") == 0) {
70 receive_fsck_objects
= git_config_bool(var
, value
);
74 if (!strcmp(var
, "receive.denycurrentbranch")) {
75 deny_current_branch
= parse_deny_action(var
, value
);
79 return git_default_config(var
, value
, cb
);
82 static int show_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
84 if (capabilities_sent
)
85 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), path
);
87 packet_write(1, "%s %s%c%s\n",
88 sha1_to_hex(sha1
), path
, 0, capabilities
);
89 capabilities_sent
= 1;
93 static void write_head_info(void)
95 for_each_ref(show_ref
, NULL
);
96 if (!capabilities_sent
)
97 show_ref("capabilities^{}", null_sha1
, 0, NULL
);
102 struct command
*next
;
103 const char *error_string
;
104 unsigned char old_sha1
[20];
105 unsigned char new_sha1
[20];
106 char ref_name
[FLEX_ARRAY
]; /* more */
109 static struct command
*commands
;
111 static const char pre_receive_hook
[] = "hooks/pre-receive";
112 static const char post_receive_hook
[] = "hooks/post-receive";
114 static int hook_status(int code
, const char *hook_name
)
119 case -ERR_RUN_COMMAND_FORK
:
120 return error("hook fork failed");
121 case -ERR_RUN_COMMAND_EXEC
:
122 return error("hook execute failed");
123 case -ERR_RUN_COMMAND_PIPE
:
124 return error("hook pipe failed");
125 case -ERR_RUN_COMMAND_WAITPID
:
126 return error("waitpid failed");
127 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
128 return error("waitpid is confused");
129 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
130 return error("%s died of signal", hook_name
);
131 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
132 return error("%s died strangely", hook_name
);
134 error("%s exited with error code %d", hook_name
, -code
);
139 static int run_hook(const char *hook_name
)
141 static char buf
[sizeof(commands
->old_sha1
) * 2 + PATH_MAX
+ 4];
143 struct child_process proc
;
145 int have_input
= 0, code
;
147 for (cmd
= commands
; !have_input
&& cmd
; cmd
= cmd
->next
) {
148 if (!cmd
->error_string
)
152 if (!have_input
|| access(hook_name
, X_OK
) < 0)
158 memset(&proc
, 0, sizeof(proc
));
161 proc
.stdout_to_stderr
= 1;
163 code
= start_command(&proc
);
165 return hook_status(code
, hook_name
);
166 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
167 if (!cmd
->error_string
) {
168 size_t n
= snprintf(buf
, sizeof(buf
), "%s %s %s\n",
169 sha1_to_hex(cmd
->old_sha1
),
170 sha1_to_hex(cmd
->new_sha1
),
172 if (write_in_full(proc
.in
, buf
, n
) != n
)
177 return hook_status(finish_command(&proc
), hook_name
);
180 static int run_update_hook(struct command
*cmd
)
182 static const char update_hook
[] = "hooks/update";
183 struct child_process proc
;
186 if (access(update_hook
, X_OK
) < 0)
189 argv
[0] = update_hook
;
190 argv
[1] = cmd
->ref_name
;
191 argv
[2] = sha1_to_hex(cmd
->old_sha1
);
192 argv
[3] = sha1_to_hex(cmd
->new_sha1
);
195 memset(&proc
, 0, sizeof(proc
));
198 proc
.stdout_to_stderr
= 1;
200 return hook_status(run_command(&proc
), update_hook
);
203 static int is_ref_checked_out(const char *ref
)
205 unsigned char sha1
[20];
208 if (is_bare_repository())
211 head
= resolve_ref("HEAD", sha1
, 0, NULL
);
214 return !strcmp(head
, ref
);
217 static const char *update(struct command
*cmd
)
219 const char *name
= cmd
->ref_name
;
220 unsigned char *old_sha1
= cmd
->old_sha1
;
221 unsigned char *new_sha1
= cmd
->new_sha1
;
222 struct ref_lock
*lock
;
224 /* only refs/... are allowed */
225 if (prefixcmp(name
, "refs/") || check_ref_format(name
+ 5)) {
226 error("refusing to create funny ref '%s' remotely", name
);
227 return "funny refname";
230 switch (deny_current_branch
) {
234 if (!is_ref_checked_out(name
))
236 warning("updating the currently checked out branch; this may"
237 " cause confusion,\n"
238 "as the index and working tree do not reflect changes"
239 " that are now in HEAD.");
242 if (!is_ref_checked_out(name
))
244 error("refusing to update checked out branch: %s", name
);
245 return "branch is currently checked out";
248 if (!is_null_sha1(new_sha1
) && !has_sha1_file(new_sha1
)) {
249 error("unpack should have generated %s, "
250 "but I can't find it!", sha1_to_hex(new_sha1
));
253 if (deny_deletes
&& is_null_sha1(new_sha1
) &&
254 !is_null_sha1(old_sha1
) &&
255 !prefixcmp(name
, "refs/heads/")) {
256 error("denying ref deletion for %s", name
);
257 return "deletion prohibited";
259 if (deny_non_fast_forwards
&& !is_null_sha1(new_sha1
) &&
260 !is_null_sha1(old_sha1
) &&
261 !prefixcmp(name
, "refs/heads/")) {
262 struct object
*old_object
, *new_object
;
263 struct commit
*old_commit
, *new_commit
;
264 struct commit_list
*bases
, *ent
;
266 old_object
= parse_object(old_sha1
);
267 new_object
= parse_object(new_sha1
);
269 if (!old_object
|| !new_object
||
270 old_object
->type
!= OBJ_COMMIT
||
271 new_object
->type
!= OBJ_COMMIT
) {
272 error("bad sha1 objects for %s", name
);
275 old_commit
= (struct commit
*)old_object
;
276 new_commit
= (struct commit
*)new_object
;
277 bases
= get_merge_bases(old_commit
, new_commit
, 1);
278 for (ent
= bases
; ent
; ent
= ent
->next
)
279 if (!hashcmp(old_sha1
, ent
->item
->object
.sha1
))
281 free_commit_list(bases
);
283 error("denying non-fast forward %s"
284 " (you should pull first)", name
);
285 return "non-fast forward";
288 if (run_update_hook(cmd
)) {
289 error("hook declined to update %s", name
);
290 return "hook declined";
293 if (is_null_sha1(new_sha1
)) {
294 if (!parse_object(old_sha1
)) {
295 warning ("Allowing deletion of corrupt ref.");
298 if (delete_ref(name
, old_sha1
, 0)) {
299 error("failed to delete %s", name
);
300 return "failed to delete";
302 return NULL
; /* good */
305 lock
= lock_any_ref_for_update(name
, old_sha1
, 0);
307 error("failed to lock %s", name
);
308 return "failed to lock";
310 if (write_ref_sha1(lock
, new_sha1
, "push")) {
311 return "failed to write"; /* error() already called */
313 return NULL
; /* good */
317 static char update_post_hook
[] = "hooks/post-update";
319 static void run_update_post_hook(struct command
*cmd
)
321 struct command
*cmd_p
;
325 for (argc
= 0, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
326 if (cmd_p
->error_string
)
330 if (!argc
|| access(update_post_hook
, X_OK
) < 0)
332 argv
= xmalloc(sizeof(*argv
) * (2 + argc
));
333 argv
[0] = update_post_hook
;
335 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
337 if (cmd_p
->error_string
)
339 p
= xmalloc(strlen(cmd_p
->ref_name
) + 1);
340 strcpy(p
, cmd_p
->ref_name
);
345 run_command_v_opt(argv
, RUN_COMMAND_NO_STDIN
346 | RUN_COMMAND_STDOUT_TO_STDERR
);
349 static void execute_commands(const char *unpacker_error
)
351 struct command
*cmd
= commands
;
353 if (unpacker_error
) {
355 cmd
->error_string
= "n/a (unpacker error)";
361 if (run_hook(pre_receive_hook
)) {
363 cmd
->error_string
= "pre-receive hook declined";
370 cmd
->error_string
= update(cmd
);
375 static void read_head_info(void)
377 struct command
**p
= &commands
;
379 static char line
[1000];
380 unsigned char old_sha1
[20], new_sha1
[20];
385 len
= packet_read_line(0, line
, sizeof(line
));
388 if (line
[len
-1] == '\n')
393 get_sha1_hex(line
, old_sha1
) ||
394 get_sha1_hex(line
+ 41, new_sha1
))
395 die("protocol error: expected old/new/ref, got '%s'",
399 reflen
= strlen(refname
);
400 if (reflen
+ 82 < len
) {
401 if (strstr(refname
+ reflen
+ 1, "report-status"))
404 cmd
= xmalloc(sizeof(struct command
) + len
- 80);
405 hashcpy(cmd
->old_sha1
, old_sha1
);
406 hashcpy(cmd
->new_sha1
, new_sha1
);
407 memcpy(cmd
->ref_name
, line
+ 82, len
- 81);
408 cmd
->error_string
= NULL
;
415 static const char *parse_pack_header(struct pack_header
*hdr
)
417 switch (read_pack_header(0, hdr
)) {
419 return "eof before pack header was fully read";
421 case PH_ERROR_PACK_SIGNATURE
:
422 return "protocol error (pack signature mismatch detected)";
424 case PH_ERROR_PROTOCOL
:
425 return "protocol error (pack version unsupported)";
428 return "unknown error in parse_pack_header";
435 static const char *pack_lockfile
;
437 static const char *unpack(void)
439 struct pack_header hdr
;
443 hdr_err
= parse_pack_header(&hdr
);
446 snprintf(hdr_arg
, sizeof(hdr_arg
),
447 "--pack_header=%"PRIu32
",%"PRIu32
,
448 ntohl(hdr
.hdr_version
), ntohl(hdr
.hdr_entries
));
450 if (ntohl(hdr
.hdr_entries
) < unpack_limit
) {
452 const char *unpacker
[4];
453 unpacker
[i
++] = "unpack-objects";
454 if (receive_fsck_objects
)
455 unpacker
[i
++] = "--strict";
456 unpacker
[i
++] = hdr_arg
;
457 unpacker
[i
++] = NULL
;
458 code
= run_command_v_opt(unpacker
, RUN_GIT_CMD
);
462 case -ERR_RUN_COMMAND_FORK
:
463 return "unpack fork failed";
464 case -ERR_RUN_COMMAND_EXEC
:
465 return "unpack execute failed";
466 case -ERR_RUN_COMMAND_WAITPID
:
467 return "waitpid failed";
468 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
469 return "waitpid is confused";
470 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
471 return "unpacker died of signal";
472 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
473 return "unpacker died strangely";
475 return "unpacker exited with error code";
478 const char *keeper
[7];
479 int s
, status
, i
= 0;
481 struct child_process ip
;
483 s
= sprintf(keep_arg
, "--keep=receive-pack %"PRIuMAX
" on ", (uintmax_t) getpid());
484 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
485 strcpy(keep_arg
+ s
, "localhost");
487 keeper
[i
++] = "index-pack";
488 keeper
[i
++] = "--stdin";
489 if (receive_fsck_objects
)
490 keeper
[i
++] = "--strict";
491 keeper
[i
++] = "--fix-thin";
492 keeper
[i
++] = hdr_arg
;
493 keeper
[i
++] = keep_arg
;
495 memset(&ip
, 0, sizeof(ip
));
499 if (start_command(&ip
))
500 return "index-pack fork failed";
501 pack_lockfile
= index_pack_lockfile(ip
.out
);
503 status
= finish_command(&ip
);
505 reprepare_packed_git();
508 return "index-pack abnormal exit";
512 static void report(const char *unpack_status
)
515 packet_write(1, "unpack %s\n",
516 unpack_status
? unpack_status
: "ok");
517 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
518 if (!cmd
->error_string
)
519 packet_write(1, "ok %s\n",
522 packet_write(1, "ng %s %s\n",
523 cmd
->ref_name
, cmd
->error_string
);
528 static int delete_only(struct command
*cmd
)
531 if (!is_null_sha1(cmd
->new_sha1
))
538 static int add_refs_from_alternate(struct alternate_object_database
*e
, void *unused
)
542 struct remote
*remote
;
543 struct transport
*transport
;
544 const struct ref
*extra
;
547 other
= xstrdup(make_absolute_path(e
->base
));
551 while (other
[len
-1] == '/')
553 if (len
< 8 || memcmp(other
+ len
- 8, "/objects", 8))
555 /* Is this a git repository with refs? */
556 memcpy(other
+ len
- 8, "/refs", 6);
557 if (!is_directory(other
))
559 other
[len
- 8] = '\0';
560 remote
= remote_get(other
);
561 transport
= transport_get(remote
, other
);
562 for (extra
= transport_get_remote_refs(transport
);
564 extra
= extra
->next
) {
565 add_extra_ref(".have", extra
->old_sha1
, 0);
567 transport_disconnect(transport
);
572 static void add_alternate_refs(void)
574 foreach_alt_odb(add_refs_from_alternate
, NULL
);
577 int cmd_receive_pack(int argc
, const char **argv
, const char *prefix
)
582 if (argv
[0] && *argv
[0])
583 git_extract_argv0_path(argv
[0]);
586 for (i
= 1; i
< argc
; i
++) {
587 const char *arg
= *argv
++;
590 /* Do flag handling here */
591 usage(receive_pack_usage
);
594 usage(receive_pack_usage
);
598 usage(receive_pack_usage
);
602 if (!enter_repo(dir
, 0))
603 die("'%s': unable to chdir or not a git archive", dir
);
605 if (is_repository_shallow())
606 die("attempt to push into a shallow repository");
608 git_config(receive_pack_config
, NULL
);
610 if (0 <= transfer_unpack_limit
)
611 unpack_limit
= transfer_unpack_limit
;
612 else if (0 <= receive_unpack_limit
)
613 unpack_limit
= receive_unpack_limit
;
615 add_alternate_refs();
624 const char *unpack_status
= NULL
;
626 if (!delete_only(commands
))
627 unpack_status
= unpack();
628 execute_commands(unpack_status
);
630 unlink(pack_lockfile
);
632 report(unpack_status
);
633 run_hook(post_receive_hook
);
634 run_update_post_hook(commands
);