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 char update_hook
[] = "hooks/update";
72 static int run_update_hook(const char *refname
,
73 char *old_hex
, char *new_hex
)
77 if (access(update_hook
, X_OK
) < 0)
79 code
= run_command_opt(RUN_COMMAND_NO_STDIN
80 | RUN_COMMAND_STDOUT_TO_STDERR
,
81 update_hook
, refname
, old_hex
, new_hex
, NULL
);
85 case -ERR_RUN_COMMAND_FORK
:
86 return error("hook fork failed");
87 case -ERR_RUN_COMMAND_EXEC
:
88 return error("hook execute failed");
89 case -ERR_RUN_COMMAND_WAITPID
:
90 return error("waitpid failed");
91 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
92 return error("waitpid is confused");
93 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
94 return error("%s died of signal", update_hook
);
95 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
96 return error("%s died strangely", update_hook
);
98 error("%s exited with error code %d", update_hook
, -code
);
103 static int update(struct command
*cmd
)
105 const char *name
= cmd
->ref_name
;
106 unsigned char *old_sha1
= cmd
->old_sha1
;
107 unsigned char *new_sha1
= cmd
->new_sha1
;
108 char new_hex
[41], old_hex
[41];
109 struct ref_lock
*lock
;
111 cmd
->error_string
= NULL
;
112 if (!strncmp(name
, "refs/", 5) && check_ref_format(name
+ 5)) {
113 cmd
->error_string
= "funny refname";
114 return error("refusing to create funny ref '%s' locally",
118 strcpy(new_hex
, sha1_to_hex(new_sha1
));
119 strcpy(old_hex
, sha1_to_hex(old_sha1
));
121 if (!is_null_sha1(new_sha1
) && !has_sha1_file(new_sha1
)) {
122 cmd
->error_string
= "bad pack";
123 return error("unpack should have generated %s, "
124 "but I can't find it!", new_hex
);
126 if (deny_non_fast_forwards
&& !is_null_sha1(new_sha1
) &&
127 !is_null_sha1(old_sha1
) &&
128 !strncmp(name
, "refs/heads/", 11)) {
129 struct commit
*old_commit
, *new_commit
;
130 struct commit_list
*bases
, *ent
;
132 old_commit
= (struct commit
*)parse_object(old_sha1
);
133 new_commit
= (struct commit
*)parse_object(new_sha1
);
134 bases
= get_merge_bases(old_commit
, new_commit
, 1);
135 for (ent
= bases
; ent
; ent
= ent
->next
)
136 if (!hashcmp(old_sha1
, ent
->item
->object
.sha1
))
138 free_commit_list(bases
);
140 return error("denying non-fast forward;"
141 " you should pull first");
143 if (run_update_hook(name
, old_hex
, new_hex
)) {
144 cmd
->error_string
= "hook declined";
145 return error("hook declined to update %s", name
);
148 if (is_null_sha1(new_sha1
)) {
149 if (delete_ref(name
, old_sha1
)) {
150 cmd
->error_string
= "failed to delete";
151 return error("failed to delete %s", name
);
153 fprintf(stderr
, "%s: %s -> deleted\n", name
, old_hex
);
156 lock
= lock_any_ref_for_update(name
, old_sha1
);
158 cmd
->error_string
= "failed to lock";
159 return error("failed to lock %s", name
);
161 write_ref_sha1(lock
, new_sha1
, "push");
162 fprintf(stderr
, "%s: %s -> %s\n", name
, old_hex
, new_hex
);
167 static char update_post_hook
[] = "hooks/post-update";
169 static void run_update_post_hook(struct command
*cmd
)
171 struct command
*cmd_p
;
175 if (access(update_post_hook
, X_OK
) < 0)
177 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
178 if (cmd_p
->error_string
)
182 argv
= xmalloc(sizeof(*argv
) * (1 + argc
));
183 argv
[0] = update_post_hook
;
185 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
187 if (cmd_p
->error_string
)
189 p
= xmalloc(strlen(cmd_p
->ref_name
) + 1);
190 strcpy(p
, cmd_p
->ref_name
);
195 run_command_v_opt(argv
, RUN_COMMAND_NO_STDIN
196 | RUN_COMMAND_STDOUT_TO_STDERR
);
200 * This gets called after(if) we've successfully
201 * unpacked the data payload.
203 static void execute_commands(void)
205 struct command
*cmd
= commands
;
211 run_update_post_hook(commands
);
214 static void read_head_info(void)
216 struct command
**p
= &commands
;
218 static char line
[1000];
219 unsigned char old_sha1
[20], new_sha1
[20];
224 len
= packet_read_line(0, line
, sizeof(line
));
227 if (line
[len
-1] == '\n')
232 get_sha1_hex(line
, old_sha1
) ||
233 get_sha1_hex(line
+ 41, new_sha1
))
234 die("protocol error: expected old/new/ref, got '%s'",
238 reflen
= strlen(refname
);
239 if (reflen
+ 82 < len
) {
240 if (strstr(refname
+ reflen
+ 1, "report-status"))
243 cmd
= xmalloc(sizeof(struct command
) + len
- 80);
244 hashcpy(cmd
->old_sha1
, old_sha1
);
245 hashcpy(cmd
->new_sha1
, new_sha1
);
246 memcpy(cmd
->ref_name
, line
+ 82, len
- 81);
247 cmd
->error_string
= "n/a (unpacker error)";
254 static const char *parse_pack_header(struct pack_header
*hdr
)
256 switch (read_pack_header(0, hdr
)) {
258 return "eof before pack header was fully read";
260 case PH_ERROR_PACK_SIGNATURE
:
261 return "protocol error (pack signature mismatch detected)";
263 case PH_ERROR_PROTOCOL
:
264 return "protocol error (pack version unsupported)";
267 return "unknown error in parse_pack_header";
274 static const char *pack_lockfile
;
276 static const char *unpack(void)
278 struct pack_header hdr
;
282 hdr_err
= parse_pack_header(&hdr
);
285 snprintf(hdr_arg
, sizeof(hdr_arg
), "--pack_header=%u,%u",
286 ntohl(hdr
.hdr_version
), ntohl(hdr
.hdr_entries
));
288 if (ntohl(hdr
.hdr_entries
) < unpack_limit
) {
290 const char *unpacker
[3];
291 unpacker
[0] = "unpack-objects";
292 unpacker
[1] = hdr_arg
;
294 code
= run_command_v_opt(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";
311 return "unpacker exited with error code";
314 const char *keeper
[6];
315 int fd
[2], s
, len
, status
;
320 s
= sprintf(keep_arg
, "--keep=receive-pack %i on ", getpid());
321 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
322 strcpy(keep_arg
+ s
, "localhost");
324 keeper
[0] = "index-pack";
325 keeper
[1] = "--stdin";
326 keeper
[2] = "--fix-thin";
328 keeper
[4] = keep_arg
;
332 return "index-pack pipe failed";
335 return "index-pack fork failed";
340 execv_git_cmd(keeper
);
341 die("execv of index-pack failed");
346 * The first thing we expects from index-pack's output
347 * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where
348 * %40s is the newly created pack SHA1 name. In the "keep"
349 * case, we need it to remove the corresponding .keep file
350 * later on. If we don't get that then tough luck with it.
353 len
< 46 && (s
= xread(fd
[0], packname
+len
, 46-len
)) > 0;
356 if (len
== 46 && packname
[45] == '\n' &&
357 memcmp(packname
, "keep\t", 5) == 0) {
360 snprintf(path
, sizeof(path
), "%s/pack/pack-%s.keep",
361 get_object_directory(), packname
+ 5);
362 pack_lockfile
= xstrdup(path
);
365 /* Then wrap our index-pack process. */
366 while (waitpid(pid
, &status
, 0) < 0)
368 return "waitpid failed";
369 if (WIFEXITED(status
)) {
370 int code
= WEXITSTATUS(status
);
372 return "index-pack exited with error code";
373 reprepare_packed_git();
376 return "index-pack abnormal exit";
380 static void report(const char *unpack_status
)
383 packet_write(1, "unpack %s\n",
384 unpack_status
? unpack_status
: "ok");
385 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
386 if (!cmd
->error_string
)
387 packet_write(1, "ok %s\n",
390 packet_write(1, "ng %s %s\n",
391 cmd
->ref_name
, cmd
->error_string
);
396 static int delete_only(struct command
*cmd
)
399 if (!is_null_sha1(cmd
->new_sha1
))
406 int main(int argc
, char **argv
)
412 for (i
= 1; i
< argc
; i
++) {
416 /* Do flag handling here */
417 usage(receive_pack_usage
);
420 usage(receive_pack_usage
);
424 usage(receive_pack_usage
);
426 if (!enter_repo(dir
, 0))
427 die("'%s': unable to chdir or not a git archive", dir
);
429 if (is_repository_shallow())
430 die("attempt to push into a shallow repository");
432 git_config(receive_pack_config
);
434 if (0 <= transfer_unpack_limit
)
435 unpack_limit
= transfer_unpack_limit
;
436 else if (0 <= receive_unpack_limit
)
437 unpack_limit
= receive_unpack_limit
;
446 const char *unpack_status
= NULL
;
448 if (!delete_only(commands
))
449 unpack_status
= unpack();
453 unlink(pack_lockfile
);
455 report(unpack_status
);