]> git.ipfire.org Git - thirdparty/git.git/blame - send-pack.c
Merge branch 'master' into js/shallow
[thirdparty/git.git] / send-pack.c
CommitLineData
61221472 1#include "cache.h"
2a9c3fe8 2#include "commit.h"
37fde874 3#include "tag.h"
584c6cc9 4#include "refs.h"
f3a3214e 5#include "pkt-line.h"
77cb17e9 6#include "exec_cmd.h"
61221472 7
2a245013 8static const char send_pack_usage[] =
0bc3cdfc
JH
9"git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n"
10" --all and explicit <head> specification are mutually exclusive.";
61221472 11static const char *exec = "git-receive-pack";
96f1e58f
DR
12static int verbose;
13static int send_all;
14static int force_update;
15static int use_thin_pack;
61221472 16
584c6cc9
LT
17static int is_zero_sha1(const unsigned char *sha1)
18{
19 int i;
20
21 for (i = 0; i < 20; i++) {
22 if (*sha1++)
23 return 0;
24 }
25 return 1;
26}
27
94fdb7aa
LT
28static void exec_pack_objects(void)
29{
9201c707 30 static const char *args[] = {
77cb17e9 31 "pack-objects",
fa438a2e 32 "--all-progress",
94fdb7aa
LT
33 "--stdout",
34 NULL
35 };
77cb17e9 36 execv_git_cmd(args);
94fdb7aa
LT
37 die("git-pack-objects exec failed (%s)", strerror(errno));
38}
39
40static void exec_rev_list(struct ref *refs)
41{
c727fe2a
AW
42 static const char *args[4];
43 int i = 0;
94fdb7aa 44
77cb17e9 45 args[i++] = "rev-list"; /* 0 */
2245be3e
JH
46 if (use_thin_pack) /* 1 */
47 args[i++] = "--objects-edge";
48 else
49 args[i++] = "--objects";
797656e5 50
c727fe2a 51 args[i++] = "--stdin";
797656e5 52
94fdb7aa 53 args[i] = NULL;
77cb17e9 54 execv_git_cmd(args);
94fdb7aa
LT
55 die("git-rev-list exec failed (%s)", strerror(errno));
56}
57
c727fe2a
AW
58/*
59 * Run "rev-list --stdin | pack-objects" pipe.
60 */
94fdb7aa
LT
61static void rev_list(int fd, struct ref *refs)
62{
63 int pipe_fd[2];
64 pid_t pack_objects_pid;
65
66 if (pipe(pipe_fd) < 0)
67 die("rev-list setup: pipe failed");
68 pack_objects_pid = fork();
69 if (!pack_objects_pid) {
c727fe2a
AW
70 /* The child becomes pack-objects; reads from pipe
71 * and writes to the original fd
72 */
94fdb7aa
LT
73 dup2(pipe_fd[0], 0);
74 dup2(fd, 1);
75 close(pipe_fd[0]);
76 close(pipe_fd[1]);
77 close(fd);
78 exec_pack_objects();
79 die("pack-objects setup failed");
80 }
81 if (pack_objects_pid < 0)
82 die("pack-objects fork failed");
c727fe2a
AW
83
84 /* We become rev-list --stdin; output goes to pipe. */
94fdb7aa
LT
85 dup2(pipe_fd[1], 1);
86 close(pipe_fd[0]);
87 close(pipe_fd[1]);
88 close(fd);
89 exec_rev_list(refs);
90}
91
c727fe2a
AW
92/*
93 * Create "rev-list --stdin | pack-objects" pipe and feed
94 * the refs into the pipeline.
95 */
96static void rev_list_generate(int fd, struct ref *refs)
97{
98 int pipe_fd[2];
99 pid_t rev_list_generate_pid;
100
101 if (pipe(pipe_fd) < 0)
102 die("rev-list-generate setup: pipe failed");
103 rev_list_generate_pid = fork();
104 if (!rev_list_generate_pid) {
105 /* The child becomes the "rev-list | pack-objects"
106 * pipeline. It takes input from us, and its output
107 * goes to fd.
108 */
109 dup2(pipe_fd[0], 0);
110 dup2(fd, 1);
111 close(pipe_fd[0]);
112 close(pipe_fd[1]);
113 close(fd);
114 rev_list(fd, refs);
115 die("rev-list setup failed");
116 }
117 if (rev_list_generate_pid < 0)
118 die("rev-list-generate fork failed");
119
120 /* We feed the rev parameters to them. We do not write into
121 * fd nor read from the pipe.
122 */
123 close(pipe_fd[0]);
124 close(fd);
125 while (refs) {
126 char buf[42];
127
128 if (!is_null_sha1(refs->old_sha1) &&
129 has_sha1_file(refs->old_sha1)) {
130 memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40);
131 buf[0] = '^';
132 buf[41] = '\n';
133 write(pipe_fd[1], buf, 42);
134 }
135 if (!is_null_sha1(refs->new_sha1)) {
136 memcpy(buf, sha1_to_hex(refs->new_sha1), 40);
137 buf[40] = '\n';
138 write(pipe_fd[1], buf, 41);
139 }
140 refs = refs->next;
141 }
142 close(pipe_fd[1]);
143 // waitpid(rev_list_generate_pid);
144 exit(0);
145}
146
147/*
148 * Make a pack stream and spit it out into file descriptor fd
149 */
aa145403 150static void pack_objects(int fd, struct ref *refs)
94fdb7aa
LT
151{
152 pid_t rev_list_pid;
153
154 rev_list_pid = fork();
155 if (!rev_list_pid) {
c727fe2a 156 rev_list_generate(fd, refs);
94fdb7aa
LT
157 die("rev-list setup failed");
158 }
159 if (rev_list_pid < 0)
160 die("rev-list fork failed");
161 /*
162 * We don't wait for the rev-list pipeline in the parent:
163 * we end up waiting for the other end instead
164 */
165}
e4b5c7ff 166
51b0fca0
JH
167static void unmark_and_free(struct commit_list *list, unsigned int mark)
168{
169 while (list) {
170 struct commit_list *temp = list;
171 temp->item->object.flags &= ~mark;
172 list = temp->next;
173 free(temp);
174 }
175}
176
37fde874
JH
177static int ref_newer(const unsigned char *new_sha1,
178 const unsigned char *old_sha1)
584c6cc9 179{
37fde874
JH
180 struct object *o;
181 struct commit *old, *new;
51b0fca0
JH
182 struct commit_list *list, *used;
183 int found = 0;
2a9c3fe8 184
37fde874
JH
185 /* Both new and old must be commit-ish and new is descendant of
186 * old. Otherwise we require --force.
187 */
9534f40b 188 o = deref_tag(parse_object(old_sha1), NULL, 0);
1974632c 189 if (!o || o->type != OBJ_COMMIT)
584c6cc9 190 return 0;
37fde874
JH
191 old = (struct commit *) o;
192
9534f40b 193 o = deref_tag(parse_object(new_sha1), NULL, 0);
1974632c 194 if (!o || o->type != OBJ_COMMIT)
2a9c3fe8 195 return 0;
37fde874
JH
196 new = (struct commit *) o;
197
2a9c3fe8
LT
198 if (parse_commit(new) < 0)
199 return 0;
51b0fca0
JH
200
201 used = list = NULL;
2a9c3fe8 202 commit_list_insert(new, &list);
bdf25142
LT
203 while (list) {
204 new = pop_most_recent_commit(&list, 1);
51b0fca0
JH
205 commit_list_insert(new, &used);
206 if (new == old) {
207 found = 1;
208 break;
209 }
2a9c3fe8 210 }
51b0fca0
JH
211 unmark_and_free(list, 1);
212 unmark_and_free(used, 1);
213 return found;
584c6cc9
LT
214}
215
f88395ac
JH
216static struct ref *local_refs, **local_tail;
217static struct ref *remote_refs, **remote_tail;
584c6cc9 218
8da19775 219static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
584c6cc9
LT
220{
221 struct ref *ref;
f88395ac
JH
222 int len = strlen(refname) + 1;
223 ref = xcalloc(1, sizeof(*ref) + len);
e702496e 224 hashcpy(ref->new_sha1, sha1);
584c6cc9 225 memcpy(ref->name, refname, len);
f88395ac
JH
226 *local_tail = ref;
227 local_tail = &ref->next;
584c6cc9
LT
228 return 0;
229}
230
f88395ac
JH
231static void get_local_heads(void)
232{
233 local_tail = &local_refs;
cb5d709f 234 for_each_ref(one_local_ref, NULL);
f88395ac
JH
235}
236
cfee10a7
JH
237static int receive_status(int in)
238{
239 char line[1000];
240 int ret = 0;
241 int len = packet_read_line(in, line, sizeof(line));
242 if (len < 10 || memcmp(line, "unpack ", 7)) {
243 fprintf(stderr, "did not receive status back\n");
244 return -1;
245 }
246 if (memcmp(line, "unpack ok\n", 10)) {
247 fputs(line, stderr);
248 ret = -1;
249 }
250 while (1) {
251 len = packet_read_line(in, line, sizeof(line));
252 if (!len)
253 break;
254 if (len < 3 ||
255 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
256 fprintf(stderr, "protocol error: %s\n", line);
257 ret = -1;
258 break;
259 }
260 if (!memcmp(line, "ok", 2))
261 continue;
262 fputs(line, stderr);
263 ret = -1;
264 }
265 return ret;
266}
267
f88395ac 268static int send_pack(int in, int out, int nr_refspec, char **refspec)
61221472 269{
7f8e9828 270 struct ref *ref;
584c6cc9 271 int new_refs;
ed24928e 272 int ret = 0;
cfee10a7 273 int ask_for_status_report = 0;
d4f694ba 274 int allow_deleting_refs = 0;
cfee10a7 275 int expect_status_report = 0;
7f8e9828 276
f88395ac 277 /* No funny business with the matcher */
2718ff09 278 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL);
f88395ac 279 get_local_heads();
584c6cc9 280
cfee10a7
JH
281 /* Does the other end support the reporting? */
282 if (server_supports("report-status"))
283 ask_for_status_report = 1;
d4f694ba
JH
284 if (server_supports("delete-refs"))
285 allow_deleting_refs = 1;
cfee10a7 286
f88395ac
JH
287 /* match them up */
288 if (!remote_tail)
289 remote_tail = &remote_refs;
290 if (match_refs(local_refs, remote_refs, &remote_tail,
291 nr_refspec, refspec, send_all))
292 return -1;
4c353e89
DB
293
294 if (!remote_refs) {
295 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
296 return 0;
297 }
298
584c6cc9 299 /*
f88395ac 300 * Finally, tell the other end!
584c6cc9 301 */
f88395ac
JH
302 new_refs = 0;
303 for (ref = remote_refs; ref; ref = ref->next) {
304 char old_hex[60], *new_hex;
d4f694ba
JH
305 int delete_ref;
306
f88395ac 307 if (!ref->peer_ref)
7f8e9828 308 continue;
d4f694ba
JH
309
310 delete_ref = is_null_sha1(ref->peer_ref->new_sha1);
311 if (delete_ref && !allow_deleting_refs) {
312 error("remote does not support deleting refs");
313 ret = -2;
314 continue;
315 }
316 if (!delete_ref &&
317 !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
41f93a2c
LT
318 if (verbose)
319 fprintf(stderr, "'%s': up-to-date\n", ref->name);
37fde874
JH
320 continue;
321 }
322
323 /* This part determines what can overwrite what.
324 * The rules are:
325 *
ff27adf3
JH
326 * (0) you can always use --force or +A:B notation to
327 * selectively force individual ref pairs.
37fde874
JH
328 *
329 * (1) if the old thing does not exist, it is OK.
330 *
331 * (2) if you do not have the old thing, you are not allowed
332 * to overwrite it; you would not know what you are losing
333 * otherwise.
334 *
335 * (3) if both new and old are commit-ish, and new is a
336 * descendant of old, it is OK.
d4f694ba
JH
337 *
338 * (4) regardless of all of the above, removing :B is
339 * always allowed.
37fde874
JH
340 */
341
ff27adf3 342 if (!force_update &&
d4f694ba 343 !delete_ref &&
ff27adf3
JH
344 !is_zero_sha1(ref->old_sha1) &&
345 !ref->force) {
69310a34
JH
346 if (!has_sha1_file(ref->old_sha1) ||
347 !ref_newer(ref->peer_ref->new_sha1,
f88395ac 348 ref->old_sha1)) {
69310a34
JH
349 /* We do not have the remote ref, or
350 * we know that the remote ref is not
351 * an ancestor of what we are trying to
352 * push. Either way this can be losing
353 * commits at the remote end and likely
354 * we were not up to date to begin with.
355 */
356 error("remote '%s' is not a strict "
357 "subset of local ref '%s'. "
358 "maybe you are not up-to-date and "
359 "need to pull first?",
360 ref->name,
f88395ac 361 ref->peer_ref->name);
ed24928e 362 ret = -2;
f88395ac
JH
363 continue;
364 }
e4b5c7ff 365 }
e702496e 366 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
d4f694ba
JH
367 if (!delete_ref)
368 new_refs++;
7f8e9828
LT
369 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
370 new_hex = sha1_to_hex(ref->new_sha1);
cfee10a7
JH
371
372 if (ask_for_status_report) {
373 packet_write(out, "%s %s %s%c%s",
374 old_hex, new_hex, ref->name, 0,
375 "report-status");
376 ask_for_status_report = 0;
377 expect_status_report = 1;
378 }
379 else
380 packet_write(out, "%s %s %s",
381 old_hex, new_hex, ref->name);
d4f694ba
JH
382 if (delete_ref)
383 fprintf(stderr, "deleting '%s'\n", ref->name);
384 else {
385 fprintf(stderr, "updating '%s'", ref->name);
386 if (strcmp(ref->name, ref->peer_ref->name))
387 fprintf(stderr, " using '%s'",
388 ref->peer_ref->name);
389 fprintf(stderr, "\n from %s\n to %s\n",
390 old_hex, new_hex);
391 }
61221472 392 }
f88395ac 393
f3a3214e 394 packet_flush(out);
584c6cc9 395 if (new_refs)
f88395ac 396 pack_objects(out, remote_refs);
61221472 397 close(out);
cfee10a7
JH
398
399 if (expect_status_report) {
400 if (receive_status(in))
401 ret = -4;
402 }
403
404 if (!new_refs && ret == 0)
405 fprintf(stderr, "Everything up-to-date\n");
ed24928e 406 return ret;
61221472
LT
407}
408
37adac76
JH
409static void verify_remote_names(int nr_heads, char **heads)
410{
411 int i;
412
413 for (i = 0; i < nr_heads; i++) {
414 const char *remote = strchr(heads[i], ':');
415
416 remote = remote ? (remote + 1) : heads[i];
417 switch (check_ref_format(remote)) {
418 case 0: /* ok */
419 case -2: /* ok but a single level -- that is fine for
420 * a match pattern.
421 */
422 continue;
423 }
424 die("remote part of refspec is not a valid name in %s",
425 heads[i]);
426 }
427}
f88395ac 428
61221472
LT
429int main(int argc, char **argv)
430{
431 int i, nr_heads = 0;
432 char *dest = NULL;
433 char **heads = NULL;
7f8e9828
LT
434 int fd[2], ret;
435 pid_t pid;
61221472 436
5a327713 437 setup_git_directory();
84a9b58c
JH
438 git_config(git_default_config);
439
61221472 440 argv++;
d089391c
LT
441 for (i = 1; i < argc; i++, argv++) {
442 char *arg = *argv;
61221472
LT
443
444 if (*arg == '-') {
445 if (!strncmp(arg, "--exec=", 7)) {
446 exec = arg + 7;
447 continue;
448 }
d089391c
LT
449 if (!strcmp(arg, "--all")) {
450 send_all = 1;
451 continue;
452 }
2a9c3fe8
LT
453 if (!strcmp(arg, "--force")) {
454 force_update = 1;
455 continue;
456 }
41f93a2c
LT
457 if (!strcmp(arg, "--verbose")) {
458 verbose = 1;
459 continue;
460 }
2245be3e
JH
461 if (!strcmp(arg, "--thin")) {
462 use_thin_pack = 1;
463 continue;
464 }
61221472
LT
465 usage(send_pack_usage);
466 }
d089391c
LT
467 if (!dest) {
468 dest = arg;
469 continue;
470 }
61221472 471 heads = argv;
d089391c 472 nr_heads = argc - i;
61221472
LT
473 break;
474 }
475 if (!dest)
476 usage(send_pack_usage);
0bc3cdfc
JH
477 if (heads && send_all)
478 usage(send_pack_usage);
37adac76
JH
479 verify_remote_names(nr_heads, heads);
480
f7192598 481 pid = git_connect(fd, dest, exec);
7f8e9828 482 if (pid < 0)
61221472 483 return 1;
d0efc8a7 484 ret = send_pack(fd[0], fd[1], nr_heads, heads);
7f8e9828
LT
485 close(fd[0]);
486 close(fd[1]);
8a5dbef8
FBH
487 ret |= finish_connect(pid);
488 return !!ret;
61221472 489}