5 #include "run-command.h"
11 static const char receive_pack_usage
[] = "git-receive-pack <git-dir>";
13 static int deny_non_fast_forwards
= 0;
14 static int unpack_limit
= 100;
15 static int report_status
;
17 static char capabilities
[] = " report-status delete-refs ";
18 static int capabilities_sent
;
20 static int receive_pack_config(const char *var
, const char *value
)
22 git_default_config(var
, value
);
24 if (strcmp(var
, "receive.denynonfastforwards") == 0)
26 deny_non_fast_forwards
= git_config_bool(var
, value
);
30 if (strcmp(var
, "receive.unpacklimit") == 0)
32 unpack_limit
= git_config_int(var
, value
);
39 static int show_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
41 if (capabilities_sent
)
42 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), path
);
44 packet_write(1, "%s %s%c%s\n",
45 sha1_to_hex(sha1
), path
, 0, capabilities
);
46 capabilities_sent
= 1;
50 static void write_head_info(void)
52 for_each_ref(show_ref
, NULL
);
53 if (!capabilities_sent
)
54 show_ref("capabilities^{}", null_sha1
, 0, NULL
);
60 const char *error_string
;
61 unsigned char old_sha1
[20];
62 unsigned char new_sha1
[20];
63 char ref_name
[FLEX_ARRAY
]; /* more */
66 static struct command
*commands
;
68 static char update_hook
[] = "hooks/update";
70 static int run_update_hook(const char *refname
,
71 char *old_hex
, char *new_hex
)
75 if (access(update_hook
, X_OK
) < 0)
77 code
= run_command(update_hook
, refname
, old_hex
, new_hex
, NULL
);
81 case -ERR_RUN_COMMAND_FORK
:
82 return error("hook fork failed");
83 case -ERR_RUN_COMMAND_EXEC
:
84 return error("hook execute failed");
85 case -ERR_RUN_COMMAND_WAITPID
:
86 return error("waitpid failed");
87 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
88 return error("waitpid is confused");
89 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
90 return error("%s died of signal", update_hook
);
91 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
92 return error("%s died strangely", update_hook
);
94 error("%s exited with error code %d", update_hook
, -code
);
99 static int update(struct command
*cmd
)
101 const char *name
= cmd
->ref_name
;
102 unsigned char *old_sha1
= cmd
->old_sha1
;
103 unsigned char *new_sha1
= cmd
->new_sha1
;
104 char new_hex
[41], old_hex
[41];
105 struct ref_lock
*lock
;
107 cmd
->error_string
= NULL
;
108 if (!strncmp(name
, "refs/", 5) && check_ref_format(name
+ 5)) {
109 cmd
->error_string
= "funny refname";
110 return error("refusing to create funny ref '%s' locally",
114 strcpy(new_hex
, sha1_to_hex(new_sha1
));
115 strcpy(old_hex
, sha1_to_hex(old_sha1
));
117 if (!is_null_sha1(new_sha1
) && !has_sha1_file(new_sha1
)) {
118 cmd
->error_string
= "bad pack";
119 return error("unpack should have generated %s, "
120 "but I can't find it!", new_hex
);
122 if (deny_non_fast_forwards
&& !is_null_sha1(new_sha1
) &&
123 !is_null_sha1(old_sha1
) &&
124 !strncmp(name
, "refs/heads/", 11)) {
125 struct commit
*old_commit
, *new_commit
;
126 struct commit_list
*bases
, *ent
;
128 old_commit
= (struct commit
*)parse_object(old_sha1
);
129 new_commit
= (struct commit
*)parse_object(new_sha1
);
130 bases
= get_merge_bases(old_commit
, new_commit
, 1);
131 for (ent
= bases
; ent
; ent
= ent
->next
)
132 if (!hashcmp(old_sha1
, ent
->item
->object
.sha1
))
134 free_commit_list(bases
);
136 return error("denying non-fast forward;"
137 " you should pull first");
139 if (run_update_hook(name
, old_hex
, new_hex
)) {
140 cmd
->error_string
= "hook declined";
141 return error("hook declined to update %s", name
);
144 if (is_null_sha1(new_sha1
)) {
145 if (delete_ref(name
, old_sha1
)) {
146 cmd
->error_string
= "failed to delete";
147 return error("failed to delete %s", name
);
149 fprintf(stderr
, "%s: %s -> deleted\n", name
, old_hex
);
152 lock
= lock_any_ref_for_update(name
, old_sha1
);
154 cmd
->error_string
= "failed to lock";
155 return error("failed to lock %s", name
);
157 write_ref_sha1(lock
, new_sha1
, "push");
158 fprintf(stderr
, "%s: %s -> %s\n", name
, old_hex
, new_hex
);
163 static char update_post_hook
[] = "hooks/post-update";
165 static void run_update_post_hook(struct command
*cmd
)
167 struct command
*cmd_p
;
171 if (access(update_post_hook
, X_OK
) < 0)
173 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
174 if (cmd_p
->error_string
)
178 argv
= xmalloc(sizeof(*argv
) * (1 + argc
));
179 argv
[0] = update_post_hook
;
181 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
183 if (cmd_p
->error_string
)
185 p
= xmalloc(strlen(cmd_p
->ref_name
) + 1);
186 strcpy(p
, cmd_p
->ref_name
);
191 run_command_v_opt(argc
, argv
, RUN_COMMAND_NO_STDIO
);
195 * This gets called after(if) we've successfully
196 * unpacked the data payload.
198 static void execute_commands(void)
200 struct command
*cmd
= commands
;
206 run_update_post_hook(commands
);
209 static void read_head_info(void)
211 struct command
**p
= &commands
;
213 static char line
[1000];
214 unsigned char old_sha1
[20], new_sha1
[20];
219 len
= packet_read_line(0, line
, sizeof(line
));
222 if (line
[len
-1] == '\n')
227 get_sha1_hex(line
, old_sha1
) ||
228 get_sha1_hex(line
+ 41, new_sha1
))
229 die("protocol error: expected old/new/ref, got '%s'",
233 reflen
= strlen(refname
);
234 if (reflen
+ 82 < len
) {
235 if (strstr(refname
+ reflen
+ 1, "report-status"))
238 cmd
= xmalloc(sizeof(struct command
) + len
- 80);
239 hashcpy(cmd
->old_sha1
, old_sha1
);
240 hashcpy(cmd
->new_sha1
, new_sha1
);
241 memcpy(cmd
->ref_name
, line
+ 82, len
- 81);
242 cmd
->error_string
= "n/a (unpacker error)";
249 static const char *parse_pack_header(struct pack_header
*hdr
)
251 char *c
= (char*)hdr
;
252 ssize_t remaining
= sizeof(struct pack_header
);
254 ssize_t r
= xread(0, c
, remaining
);
256 return "eof before pack header was fully read";
259 } while (remaining
> 0);
260 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
261 return "protocol error (pack signature mismatch detected)";
262 if (!pack_version_ok(hdr
->hdr_version
))
263 return "protocol error (pack version unsupported)";
267 static const char *pack_lockfile
;
269 static const char *unpack(void)
271 struct pack_header hdr
;
275 hdr_err
= parse_pack_header(&hdr
);
278 snprintf(hdr_arg
, sizeof(hdr_arg
), "--pack_header=%u,%u",
279 ntohl(hdr
.hdr_version
), ntohl(hdr
.hdr_entries
));
281 if (ntohl(hdr
.hdr_entries
) < unpack_limit
) {
283 const char *unpacker
[3];
284 unpacker
[0] = "unpack-objects";
285 unpacker
[1] = hdr_arg
;
287 code
= run_command_v_opt(1, unpacker
, RUN_GIT_CMD
);
291 case -ERR_RUN_COMMAND_FORK
:
292 return "unpack fork failed";
293 case -ERR_RUN_COMMAND_EXEC
:
294 return "unpack execute failed";
295 case -ERR_RUN_COMMAND_WAITPID
:
296 return "waitpid failed";
297 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
298 return "waitpid is confused";
299 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
300 return "unpacker died of signal";
301 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
302 return "unpacker died strangely";
304 return "unpacker exited with error code";
307 const char *keeper
[6];
308 int fd
[2], s
, len
, status
;
313 s
= sprintf(keep_arg
, "--keep=receive-pack %i on ", getpid());
314 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
315 strcpy(keep_arg
+ s
, "localhost");
317 keeper
[0] = "index-pack";
318 keeper
[1] = "--stdin";
319 keeper
[2] = "--fix-thin";
321 keeper
[4] = keep_arg
;
325 return "index-pack pipe failed";
328 return "index-pack fork failed";
333 execv_git_cmd(keeper
);
334 die("execv of index-pack failed");
339 * The first thing we expects from index-pack's output
340 * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where
341 * %40s is the newly created pack SHA1 name. In the "keep"
342 * case, we need it to remove the corresponding .keep file
343 * later on. If we don't get that then tough luck with it.
346 len
< 46 && (s
= xread(fd
[0], packname
+len
, 46-len
)) > 0;
349 if (len
== 46 && packname
[45] == '\n' &&
350 memcmp(packname
, "keep\t", 5) == 0) {
353 snprintf(path
, sizeof(path
), "%s/pack/pack-%s.keep",
354 get_object_directory(), packname
+ 5);
355 pack_lockfile
= xstrdup(path
);
358 /* Then wrap our index-pack process. */
359 while (waitpid(pid
, &status
, 0) < 0)
361 return "waitpid failed";
362 if (WIFEXITED(status
)) {
363 int code
= WEXITSTATUS(status
);
365 return "index-pack exited with error code";
366 reprepare_packed_git();
369 return "index-pack abnormal exit";
373 static void report(const char *unpack_status
)
376 packet_write(1, "unpack %s\n",
377 unpack_status
? unpack_status
: "ok");
378 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
379 if (!cmd
->error_string
)
380 packet_write(1, "ok %s\n",
383 packet_write(1, "ng %s %s\n",
384 cmd
->ref_name
, cmd
->error_string
);
389 static int delete_only(struct command
*cmd
)
392 if (!is_null_sha1(cmd
->new_sha1
))
399 int main(int argc
, char **argv
)
405 for (i
= 1; i
< argc
; i
++) {
409 /* Do flag handling here */
410 usage(receive_pack_usage
);
413 usage(receive_pack_usage
);
417 usage(receive_pack_usage
);
419 if (!enter_repo(dir
, 0))
420 die("'%s': unable to chdir or not a git archive", dir
);
423 git_config(receive_pack_config
);
432 const char *unpack_status
= NULL
;
434 if (!delete_only(commands
))
435 unpack_status
= unpack();
439 unlink(pack_lockfile
);
441 report(unpack_status
);