5 #include "run-command.h"
9 static const char receive_pack_usage
[] = "git-receive-pack <git-dir>";
11 static int deny_non_fast_forwards
= 0;
12 static int unpack_limit
= 5000;
13 static int report_status
;
15 static char capabilities
[] = "report-status";
16 static int capabilities_sent
;
18 static int receive_pack_config(const char *var
, const char *value
)
20 git_default_config(var
, value
);
22 if (strcmp(var
, "receive.denynonfastforwards") == 0)
24 deny_non_fast_forwards
= git_config_bool(var
, value
);
28 if (strcmp(var
, "receive.unpacklimit") == 0)
30 unpack_limit
= git_config_int(var
, value
);
37 static int show_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
39 if (capabilities_sent
)
40 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), path
);
42 packet_write(1, "%s %s%c%s\n",
43 sha1_to_hex(sha1
), path
, 0, capabilities
);
44 capabilities_sent
= 1;
48 static void write_head_info(void)
50 for_each_ref(show_ref
, NULL
);
51 if (!capabilities_sent
)
52 show_ref("capabilities^{}", null_sha1
, 0, NULL
);
58 const char *error_string
;
59 unsigned char old_sha1
[20];
60 unsigned char new_sha1
[20];
61 char ref_name
[FLEX_ARRAY
]; /* more */
64 static struct command
*commands
;
66 static char update_hook
[] = "hooks/update";
68 static int run_update_hook(const char *refname
,
69 char *old_hex
, char *new_hex
)
73 if (access(update_hook
, X_OK
) < 0)
75 code
= run_command(update_hook
, refname
, old_hex
, new_hex
, NULL
);
79 case -ERR_RUN_COMMAND_FORK
:
80 return error("hook fork failed");
81 case -ERR_RUN_COMMAND_EXEC
:
82 return error("hook execute failed");
83 case -ERR_RUN_COMMAND_WAITPID
:
84 return error("waitpid failed");
85 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
86 return error("waitpid is confused");
87 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
88 return error("%s died of signal", update_hook
);
89 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
90 return error("%s died strangely", update_hook
);
92 error("%s exited with error code %d", update_hook
, -code
);
97 static int update(struct command
*cmd
)
99 const char *name
= cmd
->ref_name
;
100 unsigned char *old_sha1
= cmd
->old_sha1
;
101 unsigned char *new_sha1
= cmd
->new_sha1
;
102 char new_hex
[41], old_hex
[41];
103 struct ref_lock
*lock
;
105 cmd
->error_string
= NULL
;
106 if (!strncmp(name
, "refs/", 5) && check_ref_format(name
+ 5)) {
107 cmd
->error_string
= "funny refname";
108 return error("refusing to create funny ref '%s' locally",
112 strcpy(new_hex
, sha1_to_hex(new_sha1
));
113 strcpy(old_hex
, sha1_to_hex(old_sha1
));
114 if (!has_sha1_file(new_sha1
)) {
115 cmd
->error_string
= "bad pack";
116 return error("unpack should have generated %s, "
117 "but I can't find it!", new_hex
);
119 if (deny_non_fast_forwards
&& !is_null_sha1(old_sha1
)) {
120 struct commit
*old_commit
, *new_commit
;
121 struct commit_list
*bases
, *ent
;
123 old_commit
= (struct commit
*)parse_object(old_sha1
);
124 new_commit
= (struct commit
*)parse_object(new_sha1
);
125 bases
= get_merge_bases(old_commit
, new_commit
, 1);
126 for (ent
= bases
; ent
; ent
= ent
->next
)
127 if (!hashcmp(old_sha1
, ent
->item
->object
.sha1
))
129 free_commit_list(bases
);
131 return error("denying non-fast forward;"
132 " you should pull first");
134 if (run_update_hook(name
, old_hex
, new_hex
)) {
135 cmd
->error_string
= "hook declined";
136 return error("hook declined to update %s", name
);
139 lock
= lock_any_ref_for_update(name
, old_sha1
);
141 cmd
->error_string
= "failed to lock";
142 return error("failed to lock %s", name
);
144 write_ref_sha1(lock
, new_sha1
, "push");
146 fprintf(stderr
, "%s: %s -> %s\n", name
, old_hex
, new_hex
);
150 static char update_post_hook
[] = "hooks/post-update";
152 static void run_update_post_hook(struct command
*cmd
)
154 struct command
*cmd_p
;
158 if (access(update_post_hook
, X_OK
) < 0)
160 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
161 if (cmd_p
->error_string
)
165 argv
= xmalloc(sizeof(*argv
) * (1 + argc
));
166 argv
[0] = update_post_hook
;
168 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
170 if (cmd_p
->error_string
)
172 p
= xmalloc(strlen(cmd_p
->ref_name
) + 1);
173 strcpy(p
, cmd_p
->ref_name
);
178 run_command_v_opt(argc
, argv
, RUN_COMMAND_NO_STDIO
);
182 * This gets called after(if) we've successfully
183 * unpacked the data payload.
185 static void execute_commands(void)
187 struct command
*cmd
= commands
;
193 run_update_post_hook(commands
);
196 static void read_head_info(void)
198 struct command
**p
= &commands
;
200 static char line
[1000];
201 unsigned char old_sha1
[20], new_sha1
[20];
206 len
= packet_read_line(0, line
, sizeof(line
));
209 if (line
[len
-1] == '\n')
214 get_sha1_hex(line
, old_sha1
) ||
215 get_sha1_hex(line
+ 41, new_sha1
))
216 die("protocol error: expected old/new/ref, got '%s'",
220 reflen
= strlen(refname
);
221 if (reflen
+ 82 < len
) {
222 if (strstr(refname
+ reflen
+ 1, "report-status"))
225 cmd
= xmalloc(sizeof(struct command
) + len
- 80);
226 hashcpy(cmd
->old_sha1
, old_sha1
);
227 hashcpy(cmd
->new_sha1
, new_sha1
);
228 memcpy(cmd
->ref_name
, line
+ 82, len
- 81);
229 cmd
->error_string
= "n/a (unpacker error)";
236 static const char *parse_pack_header(struct pack_header
*hdr
)
238 char *c
= (char*)hdr
;
239 ssize_t remaining
= sizeof(struct pack_header
);
241 ssize_t r
= xread(0, c
, remaining
);
243 return "eof before pack header was fully read";
246 } while (remaining
> 0);
247 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
248 return "protocol error (pack signature mismatch detected)";
249 if (!pack_version_ok(hdr
->hdr_version
))
250 return "protocol error (pack version unsupported)";
254 static const char *unpack(void)
256 struct pack_header hdr
;
261 hdr_err
= parse_pack_header(&hdr
);
264 snprintf(hdr_arg
, sizeof(hdr_arg
), "--pack_header=%u,%u",
265 ntohl(hdr
.hdr_version
), ntohl(hdr
.hdr_entries
));
267 if (ntohl(hdr
.hdr_entries
) < unpack_limit
) {
268 const char *unpacker
[3];
269 unpacker
[0] = "unpack-objects";
270 unpacker
[1] = hdr_arg
;
272 code
= run_command_v_opt(1, unpacker
, RUN_GIT_CMD
);
274 const char *keeper
[6];
275 char my_host
[255], keep_arg
[128 + 255];
277 if (gethostname(my_host
, sizeof(my_host
)))
278 strcpy(my_host
, "localhost");
279 snprintf(keep_arg
, sizeof(keep_arg
),
280 "--keep=receive-pack %i on %s",
283 keeper
[0] = "index-pack";
284 keeper
[1] = "--stdin";
285 keeper
[2] = "--fix-thin";
287 keeper
[4] = keep_arg
;
289 code
= run_command_v_opt(1, keeper
, RUN_GIT_CMD
);
291 reprepare_packed_git();
297 case -ERR_RUN_COMMAND_FORK
:
298 return "unpack fork failed";
299 case -ERR_RUN_COMMAND_EXEC
:
300 return "unpack execute failed";
301 case -ERR_RUN_COMMAND_WAITPID
:
302 return "waitpid failed";
303 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
304 return "waitpid is confused";
305 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
306 return "unpacker died of signal";
307 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
308 return "unpacker died strangely";
310 return "unpacker exited with error code";
314 static void report(const char *unpack_status
)
317 packet_write(1, "unpack %s\n",
318 unpack_status
? unpack_status
: "ok");
319 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
320 if (!cmd
->error_string
)
321 packet_write(1, "ok %s\n",
324 packet_write(1, "ng %s %s\n",
325 cmd
->ref_name
, cmd
->error_string
);
330 int main(int argc
, char **argv
)
336 for (i
= 1; i
< argc
; i
++) {
340 /* Do flag handling here */
341 usage(receive_pack_usage
);
344 usage(receive_pack_usage
);
348 usage(receive_pack_usage
);
350 if (!enter_repo(dir
, 0))
351 die("'%s': unable to chdir or not a git archive", dir
);
354 git_config(receive_pack_config
);
363 const char *unpack_status
= unpack();
367 report(unpack_status
);