Update documentation for update hook.
[git/gitbuild.git] / send-pack.c
blob54de96e40c36428373b21f3dec4939cef767a900
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "refs.h"
5 #include "pkt-line.h"
6 #include "exec_cmd.h"
8 static const char send_pack_usage[] =
9 "git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n"
10 " --all and explicit <head> specification are mutually exclusive.";
11 static const char *exec = "git-receive-pack";
12 static int verbose;
13 static int send_all;
14 static int force_update;
15 static int use_thin_pack;
17 static int is_zero_sha1(const unsigned char *sha1)
19 int i;
21 for (i = 0; i < 20; i++) {
22 if (*sha1++)
23 return 0;
25 return 1;
28 static void exec_pack_objects(void)
30 static const char *args[] = {
31 "pack-objects",
32 "--all-progress",
33 "--stdout",
34 NULL
36 execv_git_cmd(args);
37 die("git-pack-objects exec failed (%s)", strerror(errno));
40 static void exec_rev_list(struct ref *refs)
42 static const char *args[4];
43 int i = 0;
45 args[i++] = "rev-list"; /* 0 */
46 if (use_thin_pack) /* 1 */
47 args[i++] = "--objects-edge";
48 else
49 args[i++] = "--objects";
51 args[i++] = "--stdin";
53 args[i] = NULL;
54 execv_git_cmd(args);
55 die("git-rev-list exec failed (%s)", strerror(errno));
59 * Run "rev-list --stdin | pack-objects" pipe.
61 static void rev_list(struct ref *refs)
63 int pipe_fd[2];
64 pid_t pack_objects_pid;
66 if (pipe(pipe_fd) < 0)
67 die("rev-list setup: pipe failed");
68 pack_objects_pid = fork();
69 if (!pack_objects_pid) {
70 /* The child becomes pack-objects; reads from pipe
71 * and writes to the original fd
73 dup2(pipe_fd[0], 0);
74 close(pipe_fd[0]);
75 close(pipe_fd[1]);
76 exec_pack_objects();
77 die("pack-objects setup failed");
79 if (pack_objects_pid < 0)
80 die("pack-objects fork failed");
82 /* We become rev-list --stdin; output goes to pipe. */
83 dup2(pipe_fd[1], 1);
84 close(pipe_fd[0]);
85 close(pipe_fd[1]);
86 exec_rev_list(refs);
90 * Create "rev-list --stdin | pack-objects" pipe and feed
91 * the refs into the pipeline.
93 static void rev_list_generate(int fd, struct ref *refs)
95 int pipe_fd[2];
96 pid_t rev_list_generate_pid;
98 if (pipe(pipe_fd) < 0)
99 die("rev-list-generate setup: pipe failed");
100 rev_list_generate_pid = fork();
101 if (!rev_list_generate_pid) {
102 /* The child becomes the "rev-list | pack-objects"
103 * pipeline. It takes input from us, and its output
104 * goes to fd.
106 dup2(pipe_fd[0], 0);
107 dup2(fd, 1);
108 close(pipe_fd[0]);
109 close(pipe_fd[1]);
110 close(fd);
111 rev_list(refs);
112 die("rev-list setup failed");
114 if (rev_list_generate_pid < 0)
115 die("rev-list-generate fork failed");
117 /* We feed the rev parameters to them. We do not write into
118 * fd nor read from the pipe.
120 close(pipe_fd[0]);
121 close(fd);
122 while (refs) {
123 char buf[42];
125 if (!is_null_sha1(refs->old_sha1) &&
126 has_sha1_file(refs->old_sha1)) {
127 memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40);
128 buf[0] = '^';
129 buf[41] = '\n';
130 write(pipe_fd[1], buf, 42);
132 if (!is_null_sha1(refs->new_sha1)) {
133 memcpy(buf, sha1_to_hex(refs->new_sha1), 40);
134 buf[40] = '\n';
135 write(pipe_fd[1], buf, 41);
137 refs = refs->next;
139 close(pipe_fd[1]);
140 // waitpid(rev_list_generate_pid);
141 exit(0);
145 * Make a pack stream and spit it out into file descriptor fd
147 static void pack_objects(int fd, struct ref *refs)
149 pid_t rev_list_pid;
151 rev_list_pid = fork();
152 if (!rev_list_pid) {
153 rev_list_generate(fd, refs);
154 die("rev-list setup failed");
156 if (rev_list_pid < 0)
157 die("rev-list fork failed");
159 * We don't wait for the rev-list pipeline in the parent:
160 * we end up waiting for the other end instead
164 static void unmark_and_free(struct commit_list *list, unsigned int mark)
166 while (list) {
167 struct commit_list *temp = list;
168 temp->item->object.flags &= ~mark;
169 list = temp->next;
170 free(temp);
174 static int ref_newer(const unsigned char *new_sha1,
175 const unsigned char *old_sha1)
177 struct object *o;
178 struct commit *old, *new;
179 struct commit_list *list, *used;
180 int found = 0;
182 /* Both new and old must be commit-ish and new is descendant of
183 * old. Otherwise we require --force.
185 o = deref_tag(parse_object(old_sha1), NULL, 0);
186 if (!o || o->type != OBJ_COMMIT)
187 return 0;
188 old = (struct commit *) o;
190 o = deref_tag(parse_object(new_sha1), NULL, 0);
191 if (!o || o->type != OBJ_COMMIT)
192 return 0;
193 new = (struct commit *) o;
195 if (parse_commit(new) < 0)
196 return 0;
198 used = list = NULL;
199 commit_list_insert(new, &list);
200 while (list) {
201 new = pop_most_recent_commit(&list, 1);
202 commit_list_insert(new, &used);
203 if (new == old) {
204 found = 1;
205 break;
208 unmark_and_free(list, 1);
209 unmark_and_free(used, 1);
210 return found;
213 static struct ref *local_refs, **local_tail;
214 static struct ref *remote_refs, **remote_tail;
216 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
218 struct ref *ref;
219 int len = strlen(refname) + 1;
220 ref = xcalloc(1, sizeof(*ref) + len);
221 hashcpy(ref->new_sha1, sha1);
222 memcpy(ref->name, refname, len);
223 *local_tail = ref;
224 local_tail = &ref->next;
225 return 0;
228 static void get_local_heads(void)
230 local_tail = &local_refs;
231 for_each_ref(one_local_ref, NULL);
234 static int receive_status(int in)
236 char line[1000];
237 int ret = 0;
238 int len = packet_read_line(in, line, sizeof(line));
239 if (len < 10 || memcmp(line, "unpack ", 7)) {
240 fprintf(stderr, "did not receive status back\n");
241 return -1;
243 if (memcmp(line, "unpack ok\n", 10)) {
244 fputs(line, stderr);
245 ret = -1;
247 while (1) {
248 len = packet_read_line(in, line, sizeof(line));
249 if (!len)
250 break;
251 if (len < 3 ||
252 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
253 fprintf(stderr, "protocol error: %s\n", line);
254 ret = -1;
255 break;
257 if (!memcmp(line, "ok", 2))
258 continue;
259 fputs(line, stderr);
260 ret = -1;
262 return ret;
265 static int send_pack(int in, int out, int nr_refspec, char **refspec)
267 struct ref *ref;
268 int new_refs;
269 int ret = 0;
270 int ask_for_status_report = 0;
271 int allow_deleting_refs = 0;
272 int expect_status_report = 0;
274 /* No funny business with the matcher */
275 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL);
276 get_local_heads();
278 /* Does the other end support the reporting? */
279 if (server_supports("report-status"))
280 ask_for_status_report = 1;
281 if (server_supports("delete-refs"))
282 allow_deleting_refs = 1;
284 /* match them up */
285 if (!remote_tail)
286 remote_tail = &remote_refs;
287 if (match_refs(local_refs, remote_refs, &remote_tail,
288 nr_refspec, refspec, send_all))
289 return -1;
291 if (!remote_refs) {
292 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
293 return 0;
297 * Finally, tell the other end!
299 new_refs = 0;
300 for (ref = remote_refs; ref; ref = ref->next) {
301 char old_hex[60], *new_hex;
302 int delete_ref;
304 if (!ref->peer_ref)
305 continue;
307 delete_ref = is_null_sha1(ref->peer_ref->new_sha1);
308 if (delete_ref && !allow_deleting_refs) {
309 error("remote does not support deleting refs");
310 ret = -2;
311 continue;
313 if (!delete_ref &&
314 !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
315 if (verbose)
316 fprintf(stderr, "'%s': up-to-date\n", ref->name);
317 continue;
320 /* This part determines what can overwrite what.
321 * The rules are:
323 * (0) you can always use --force or +A:B notation to
324 * selectively force individual ref pairs.
326 * (1) if the old thing does not exist, it is OK.
328 * (2) if you do not have the old thing, you are not allowed
329 * to overwrite it; you would not know what you are losing
330 * otherwise.
332 * (3) if both new and old are commit-ish, and new is a
333 * descendant of old, it is OK.
335 * (4) regardless of all of the above, removing :B is
336 * always allowed.
339 if (!force_update &&
340 !delete_ref &&
341 !is_zero_sha1(ref->old_sha1) &&
342 !ref->force) {
343 if (!has_sha1_file(ref->old_sha1) ||
344 !ref_newer(ref->peer_ref->new_sha1,
345 ref->old_sha1)) {
346 /* We do not have the remote ref, or
347 * we know that the remote ref is not
348 * an ancestor of what we are trying to
349 * push. Either way this can be losing
350 * commits at the remote end and likely
351 * we were not up to date to begin with.
353 error("remote '%s' is not a strict "
354 "subset of local ref '%s'. "
355 "maybe you are not up-to-date and "
356 "need to pull first?",
357 ref->name,
358 ref->peer_ref->name);
359 ret = -2;
360 continue;
363 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
364 if (!delete_ref)
365 new_refs++;
366 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
367 new_hex = sha1_to_hex(ref->new_sha1);
369 if (ask_for_status_report) {
370 packet_write(out, "%s %s %s%c%s",
371 old_hex, new_hex, ref->name, 0,
372 "report-status");
373 ask_for_status_report = 0;
374 expect_status_report = 1;
376 else
377 packet_write(out, "%s %s %s",
378 old_hex, new_hex, ref->name);
379 if (delete_ref)
380 fprintf(stderr, "deleting '%s'\n", ref->name);
381 else {
382 fprintf(stderr, "updating '%s'", ref->name);
383 if (strcmp(ref->name, ref->peer_ref->name))
384 fprintf(stderr, " using '%s'",
385 ref->peer_ref->name);
386 fprintf(stderr, "\n from %s\n to %s\n",
387 old_hex, new_hex);
391 packet_flush(out);
392 if (new_refs)
393 pack_objects(out, remote_refs);
394 close(out);
396 if (expect_status_report) {
397 if (receive_status(in))
398 ret = -4;
401 if (!new_refs && ret == 0)
402 fprintf(stderr, "Everything up-to-date\n");
403 return ret;
406 static void verify_remote_names(int nr_heads, char **heads)
408 int i;
410 for (i = 0; i < nr_heads; i++) {
411 const char *remote = strchr(heads[i], ':');
413 remote = remote ? (remote + 1) : heads[i];
414 switch (check_ref_format(remote)) {
415 case 0: /* ok */
416 case -2: /* ok but a single level -- that is fine for
417 * a match pattern.
419 continue;
421 die("remote part of refspec is not a valid name in %s",
422 heads[i]);
426 int main(int argc, char **argv)
428 int i, nr_heads = 0;
429 char *dest = NULL;
430 char **heads = NULL;
431 int fd[2], ret;
432 pid_t pid;
434 setup_git_directory();
435 git_config(git_default_config);
437 argv++;
438 for (i = 1; i < argc; i++, argv++) {
439 char *arg = *argv;
441 if (*arg == '-') {
442 if (!strncmp(arg, "--exec=", 7)) {
443 exec = arg + 7;
444 continue;
446 if (!strcmp(arg, "--all")) {
447 send_all = 1;
448 continue;
450 if (!strcmp(arg, "--force")) {
451 force_update = 1;
452 continue;
454 if (!strcmp(arg, "--verbose")) {
455 verbose = 1;
456 continue;
458 if (!strcmp(arg, "--thin")) {
459 use_thin_pack = 1;
460 continue;
462 usage(send_pack_usage);
464 if (!dest) {
465 dest = arg;
466 continue;
468 heads = argv;
469 nr_heads = argc - i;
470 break;
472 if (!dest)
473 usage(send_pack_usage);
474 if (heads && send_all)
475 usage(send_pack_usage);
476 verify_remote_names(nr_heads, heads);
478 pid = git_connect(fd, dest, exec);
479 if (pid < 0)
480 return 1;
481 ret = send_pack(fd[0], fd[1], nr_heads, heads);
482 close(fd[0]);
483 close(fd[1]);
484 ret |= finish_connect(pid);
485 return !!ret;