]> git.ipfire.org Git - thirdparty/git.git/blame - fetch-pack.c
user-manual: add references to git-config man page
[thirdparty/git.git] / fetch-pack.c
CommitLineData
def88e9a 1#include "cache.h"
fb9040cc 2#include "refs.h"
def88e9a 3#include "pkt-line.h"
49bb805e
JH
4#include "commit.h"
5#include "tag.h"
da093d37
NP
6#include "exec_cmd.h"
7#include "sideband.h"
def88e9a 8
ad897215 9static int keep_pack;
8b3d9dc0 10static int quiet;
33b83034 11static int verbose;
dfeff66e 12static int fetch_all;
016e6ccb 13static int depth;
33b83034 14static const char fetch_pack_usage[] =
016e6ccb 15"git-fetch-pack [--all] [-q] [-v] [-k] [--thin] [--exec=upload-pack] [--depth=<n>] [host:]directory <refs>...";
def88e9a
LT
16static const char *exec = "git-upload-pack";
17
0a8944dd 18#define COMPLETE (1U << 0)
23d61f83
JS
19#define COMMON (1U << 1)
20#define COMMON_REF (1U << 2)
21#define SEEN (1U << 3)
22#define POPPED (1U << 4)
23
f061e5fd
JH
24/*
25 * After sending this many "have"s if we do not get any new ACK , we
26 * give up traversing our history.
27 */
28#define MAX_IN_VAIN 256
29
96f1e58f
DR
30static struct commit_list *rev_list;
31static int non_common_revs, multi_ack, use_thin_pack, use_sideband;
23d61f83
JS
32
33static void rev_list_push(struct commit *commit, int mark)
34{
35 if (!(commit->object.flags & mark)) {
36 commit->object.flags |= mark;
37
38 if (!(commit->object.parsed))
39 parse_commit(commit);
40
41 insert_by_date(commit, &rev_list);
42
43 if (!(commit->object.flags & COMMON))
44 non_common_revs++;
45 }
46}
47
8da19775 48static int rev_list_insert_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
23d61f83 49{
9534f40b 50 struct object *o = deref_tag(parse_object(sha1), path, 0);
23d61f83 51
1974632c 52 if (o && o->type == OBJ_COMMIT)
23d61f83
JS
53 rev_list_push((struct commit *)o, SEEN);
54
55 return 0;
56}
57
58/*
59 This function marks a rev and its ancestors as common.
60 In some cases, it is desirable to mark only the ancestors (for example
61 when only the server does not yet know that they are common).
62*/
63
64static void mark_common(struct commit *commit,
65 int ancestors_only, int dont_parse)
66{
67 if (commit != NULL && !(commit->object.flags & COMMON)) {
68 struct object *o = (struct object *)commit;
69
70 if (!ancestors_only)
71 o->flags |= COMMON;
72
73 if (!(o->flags & SEEN))
74 rev_list_push(commit, SEEN);
75 else {
76 struct commit_list *parents;
77
78 if (!ancestors_only && !(o->flags & POPPED))
79 non_common_revs--;
80 if (!o->parsed && !dont_parse)
81 parse_commit(commit);
82
83 for (parents = commit->parents;
84 parents;
85 parents = parents->next)
86 mark_common(parents->item, 0, dont_parse);
87 }
88 }
89}
90
91/*
92 Get the next rev to send, ignoring the common.
93*/
94
962554c6 95static const unsigned char* get_rev(void)
23d61f83
JS
96{
97 struct commit *commit = NULL;
98
99 while (commit == NULL) {
100 unsigned int mark;
101 struct commit_list* parents;
102
103 if (rev_list == NULL || non_common_revs == 0)
104 return NULL;
105
106 commit = rev_list->item;
107 if (!(commit->object.parsed))
108 parse_commit(commit);
109 commit->object.flags |= POPPED;
110 if (!(commit->object.flags & COMMON))
111 non_common_revs--;
112
113 parents = commit->parents;
114
115 if (commit->object.flags & COMMON) {
116 /* do not send "have", and ignore ancestors */
117 commit = NULL;
118 mark = COMMON | SEEN;
119 } else if (commit->object.flags & COMMON_REF)
120 /* send "have", and ignore ancestors */
121 mark = COMMON | SEEN;
122 else
123 /* send "have", also for its ancestors */
124 mark = SEEN;
125
126 while (parents) {
127 if (!(parents->item->object.flags & SEEN))
128 rev_list_push(parents->item, mark);
129 if (mark & COMMON)
130 mark_common(parents->item, 1, 0);
131 parents = parents->next;
132 }
133
134 rev_list = rev_list->next;
135 }
136
137 return commit->object.sha1;
138}
0a8944dd 139
33b83034
JH
140static int find_common(int fd[2], unsigned char *result_sha1,
141 struct ref *refs)
def88e9a 142{
2759cbc7 143 int fetching;
23d61f83
JS
144 int count = 0, flushes = 0, retval;
145 const unsigned char *sha1;
f061e5fd
JH
146 unsigned in_vain = 0;
147 int got_continue = 0;
23d61f83 148
cb5d709f 149 for_each_ref(rev_list_insert_ref, NULL);
def88e9a 150
2759cbc7
LT
151 fetching = 0;
152 for ( ; refs ; refs = refs->next) {
33b83034 153 unsigned char *remote = refs->old_sha1;
4dab94d5 154 struct object *o;
2759cbc7 155
0a8944dd 156 /*
4dab94d5
JH
157 * If that object is complete (i.e. it is an ancestor of a
158 * local ref), we tell them we have it but do not have to
159 * tell them about its ancestors, which they already know
160 * about.
f1f0a2be
JH
161 *
162 * We use lookup_object here because we are only
163 * interested in the case we *know* the object is
164 * reachable and we have already scanned it.
4dab94d5 165 */
f1f0a2be 166 if (((o = lookup_object(remote)) != NULL) &&
1baaae5e 167 (o->flags & COMPLETE)) {
2759cbc7 168 continue;
0a8944dd 169 }
23d61f83 170
583b7ea3 171 if (!fetching)
e4fe4b8e 172 packet_write(fd[1], "want %s%s%s%s%s%s\n",
583b7ea3
JH
173 sha1_to_hex(remote),
174 (multi_ack ? " multi_ack" : ""),
d47f3db7
JH
175 (use_sideband == 2 ? " side-band-64k" : ""),
176 (use_sideband == 1 ? " side-band" : ""),
e4fe4b8e
NP
177 (use_thin_pack ? " thin-pack" : ""),
178 " ofs-delta");
583b7ea3
JH
179 else
180 packet_write(fd[1], "want %s\n", sha1_to_hex(remote));
2759cbc7 181 fetching++;
33b83034 182 }
ed09aef0
JS
183 if (is_repository_shallow())
184 write_shallow_commits(fd[1], 1);
016e6ccb
JS
185 if (depth > 0)
186 packet_write(fd[1], "deepen %d", depth);
fb9040cc 187 packet_flush(fd[1]);
2759cbc7
LT
188 if (!fetching)
189 return 1;
0a8944dd 190
cf01bd52 191 if (depth > 0) {
016e6ccb
JS
192 char line[1024];
193 unsigned char sha1[20];
194 int len;
195
196 while ((len = packet_read_line(fd[0], line, sizeof(line)))) {
197 if (!strncmp("shallow ", line, 8)) {
198 if (get_sha1_hex(line + 8, sha1))
199 die("invalid shallow line: %s", line);
016e6ccb 200 register_shallow(sha1);
cf01bd52
JH
201 continue;
202 }
203 if (!strncmp("unshallow ", line, 10)) {
f53514bc
JS
204 if (get_sha1_hex(line + 10, sha1))
205 die("invalid unshallow line: %s", line);
206 if (!lookup_object(sha1))
207 die("object not found: %s", line);
208 /* make sure that it is parsed as shallow */
209 parse_object(sha1);
210 if (unregister_shallow(sha1))
211 die("no shallow found: %s", line);
cf01bd52
JH
212 continue;
213 }
214 die("expected shallow/unshallow, got %s", line);
016e6ccb
JS
215 }
216 }
217
23d61f83 218 flushes = 0;
75bfc6c2 219 retval = -1;
23d61f83 220 while ((sha1 = get_rev())) {
def88e9a 221 packet_write(fd[1], "have %s\n", sha1_to_hex(sha1));
33b83034
JH
222 if (verbose)
223 fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
f061e5fd 224 in_vain++;
def88e9a 225 if (!(31 & ++count)) {
c4c86f07
JS
226 int ack;
227
def88e9a
LT
228 packet_flush(fd[1]);
229 flushes++;
230
231 /*
232 * We keep one window "ahead" of the other side, and
233 * will wait for an ACK only on the next one
234 */
235 if (count == 32)
236 continue;
c4c86f07
JS
237
238 do {
239 ack = get_ack(fd[0], result_sha1);
240 if (verbose && ack)
241 fprintf(stderr, "got ack %d %s\n", ack,
242 sha1_to_hex(result_sha1));
243 if (ack == 1) {
244 flushes = 0;
245 multi_ack = 0;
246 retval = 0;
247 goto done;
248 } else if (ack == 2) {
249 struct commit *commit =
250 lookup_commit(result_sha1);
251 mark_common(commit, 0, 1);
252 retval = 0;
f061e5fd
JH
253 in_vain = 0;
254 got_continue = 1;
c4c86f07
JS
255 }
256 } while (ack);
def88e9a 257 flushes--;
f061e5fd
JH
258 if (got_continue && MAX_IN_VAIN < in_vain) {
259 if (verbose)
260 fprintf(stderr, "giving up\n");
261 break; /* give up */
262 }
def88e9a
LT
263 }
264 }
c4c86f07 265done:
75bfc6c2 266 packet_write(fd[1], "done\n");
33b83034
JH
267 if (verbose)
268 fprintf(stderr, "done\n");
c4c86f07
JS
269 if (retval != 0) {
270 multi_ack = 0;
23d61f83 271 flushes++;
c4c86f07
JS
272 }
273 while (flushes || multi_ack) {
274 int ack = get_ack(fd[0], result_sha1);
275 if (ack) {
33b83034 276 if (verbose)
c4c86f07
JS
277 fprintf(stderr, "got ack (%d) %s\n", ack,
278 sha1_to_hex(result_sha1));
279 if (ack == 1)
280 return 0;
281 multi_ack = 1;
282 continue;
33b83034 283 }
c4c86f07 284 flushes--;
def88e9a 285 }
75bfc6c2 286 return retval;
def88e9a
LT
287}
288
96f1e58f 289static struct commit_list *complete;
49bb805e 290
8da19775 291static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
49bb805e
JH
292{
293 struct object *o = parse_object(sha1);
294
1974632c 295 while (o && o->type == OBJ_TAG) {
f1f0a2be
JH
296 struct tag *t = (struct tag *) o;
297 if (!t->tagged)
298 break; /* broken repository */
49bb805e 299 o->flags |= COMPLETE;
f1f0a2be 300 o = parse_object(t->tagged->sha1);
49bb805e 301 }
1974632c 302 if (o && o->type == OBJ_COMMIT) {
49bb805e
JH
303 struct commit *commit = (struct commit *)o;
304 commit->object.flags |= COMPLETE;
305 insert_by_date(commit, &complete);
306 }
307 return 0;
308}
309
310static void mark_recent_complete_commits(unsigned long cutoff)
311{
312 while (complete && cutoff <= complete->item->date) {
313 if (verbose)
314 fprintf(stderr, "Marking %s as complete\n",
315 sha1_to_hex(complete->item->object.sha1));
316 pop_most_recent_commit(&complete, COMPLETE);
317 }
318}
319
1baaae5e
JS
320static void filter_refs(struct ref **refs, int nr_match, char **match)
321{
9546010b
JH
322 struct ref **return_refs;
323 struct ref *newlist = NULL;
324 struct ref **newtail = &newlist;
325 struct ref *ref, *next;
326 struct ref *fastarray[32];
327
328 if (nr_match && !fetch_all) {
329 if (ARRAY_SIZE(fastarray) < nr_match)
330 return_refs = xcalloc(nr_match, sizeof(struct ref *));
331 else {
332 return_refs = fastarray;
333 memset(return_refs, 0, sizeof(struct ref *) * nr_match);
334 }
335 }
336 else
337 return_refs = NULL;
338
339 for (ref = *refs; ref; ref = next) {
340 next = ref->next;
341 if (!memcmp(ref->name, "refs/", 5) &&
342 check_ref_format(ref->name + 5))
343 ; /* trash */
4bcb310c
AJ
344 else if (fetch_all &&
345 (!depth || strncmp(ref->name, "refs/tags/", 10) )) {
9546010b
JH
346 *newtail = ref;
347 ref->next = NULL;
348 newtail = &ref->next;
349 continue;
350 }
351 else {
352 int order = path_match(ref->name, nr_match, match);
353 if (order) {
354 return_refs[order-1] = ref;
355 continue; /* we will link it later */
356 }
357 }
358 free(ref);
359 }
360
361 if (!fetch_all) {
362 int i;
363 for (i = 0; i < nr_match; i++) {
364 ref = return_refs[i];
365 if (ref) {
366 *newtail = ref;
367 ref->next = NULL;
368 newtail = &ref->next;
369 }
370 }
371 if (return_refs != fastarray)
372 free(return_refs);
1baaae5e 373 }
9546010b 374 *refs = newlist;
1baaae5e
JS
375}
376
377static int everything_local(struct ref **refs, int nr_match, char **match)
2759cbc7 378{
49bb805e 379 struct ref *ref;
2759cbc7 380 int retval;
49bb805e
JH
381 unsigned long cutoff = 0;
382
383 track_object_refs = 0;
384 save_commit_buffer = 0;
385
1baaae5e 386 for (ref = *refs; ref; ref = ref->next) {
49bb805e
JH
387 struct object *o;
388
389 o = parse_object(ref->old_sha1);
390 if (!o)
391 continue;
392
393 /* We already have it -- which may mean that we were
394 * in sync with the other side at some time after
395 * that (it is OK if we guess wrong here).
396 */
1974632c 397 if (o->type == OBJ_COMMIT) {
49bb805e
JH
398 struct commit *commit = (struct commit *)o;
399 if (!cutoff || cutoff < commit->date)
400 cutoff = commit->date;
401 }
402 }
403
f53514bc
JS
404 if (!depth) {
405 for_each_ref(mark_complete, NULL);
406 if (cutoff)
407 mark_recent_complete_commits(cutoff);
408 }
2759cbc7 409
1baaae5e
JS
410 /*
411 * Mark all complete remote refs as common refs.
412 * Don't mark them common yet; the server has to be told so first.
413 */
414 for (ref = *refs; ref; ref = ref->next) {
9534f40b
JH
415 struct object *o = deref_tag(lookup_object(ref->old_sha1),
416 NULL, 0);
1baaae5e 417
1974632c 418 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
1baaae5e
JS
419 continue;
420
421 if (!(o->flags & SEEN)) {
422 rev_list_push((struct commit *)o, COMMON_REF | SEEN);
423
424 mark_common((struct commit *)o, 1, 1);
425 }
426 }
427
428 filter_refs(refs, nr_match, match);
429
430 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
431 const unsigned char *remote = ref->old_sha1;
2759cbc7 432 unsigned char local[20];
49bb805e 433 struct object *o;
2759cbc7 434
1baaae5e 435 o = lookup_object(remote);
49bb805e 436 if (!o || !(o->flags & COMPLETE)) {
2759cbc7
LT
437 retval = 0;
438 if (!verbose)
439 continue;
440 fprintf(stderr,
441 "want %s (%s)\n", sha1_to_hex(remote),
1baaae5e 442 ref->name);
2759cbc7
LT
443 continue;
444 }
445
e702496e 446 hashcpy(ref->new_sha1, local);
2759cbc7
LT
447 if (!verbose)
448 continue;
449 fprintf(stderr,
450 "already have %s (%s)\n", sha1_to_hex(remote),
1baaae5e 451 ref->name);
2759cbc7
LT
452 }
453 return retval;
454}
455
da093d37
NP
456static pid_t setup_sideband(int fd[2], int xd[2])
457{
458 pid_t side_pid;
459
460 if (!use_sideband) {
461 fd[0] = xd[0];
462 fd[1] = xd[1];
463 return 0;
464 }
465 /* xd[] is talking with upload-pack; subprocess reads from
466 * xd[0], spits out band#2 to stderr, and feeds us band#1
467 * through our fd[0].
468 */
469 if (pipe(fd) < 0)
470 die("fetch-pack: unable to set up pipe");
471 side_pid = fork();
472 if (side_pid < 0)
473 die("fetch-pack: unable to fork off sideband demultiplexer");
474 if (!side_pid) {
475 /* subprocess */
476 close(fd[0]);
477 if (xd[0] != xd[1])
478 close(xd[1]);
479 if (recv_sideband("fetch-pack", xd[0], fd[1], 2))
480 exit(1);
481 exit(0);
482 }
483 close(xd[0]);
484 close(fd[1]);
485 fd[1] = xd[1];
486 return side_pid;
487}
488
489static int get_pack(int xd[2], const char **argv)
490{
491 int status;
492 pid_t pid, side_pid;
493 int fd[2];
494
495 side_pid = setup_sideband(fd, xd);
496 pid = fork();
497 if (pid < 0)
498 die("fetch-pack: unable to fork off %s", argv[0]);
499 if (!pid) {
500 dup2(fd[0], 0);
501 close(fd[0]);
502 close(fd[1]);
503 execv_git_cmd(argv);
504 die("%s exec failed", argv[0]);
505 }
506 close(fd[0]);
507 close(fd[1]);
508 while (waitpid(pid, &status, 0) < 0) {
509 if (errno != EINTR)
510 die("waiting for %s: %s", argv[0], strerror(errno));
511 }
512 if (WIFEXITED(status)) {
513 int code = WEXITSTATUS(status);
514 if (code)
515 die("%s died with error code %d", argv[0], code);
516 return 0;
517 }
518 if (WIFSIGNALED(status)) {
519 int sig = WTERMSIG(status);
520 die("%s died of signal %d", argv[0], sig);
521 }
522 die("%s died of unnatural causes %d", argv[0], status);
523}
524
525static int explode_rx_pack(int xd[2])
526{
527 const char *argv[3] = { "unpack-objects", quiet ? "-q" : NULL, NULL };
528 return get_pack(xd, argv);
529}
530
531static int keep_rx_pack(int xd[2])
532{
533 const char *argv[6];
534 char keep_arg[256];
535 int n = 0;
536
537 argv[n++] = "index-pack";
538 argv[n++] = "--stdin";
539 if (!quiet)
540 argv[n++] = "-v";
541 if (use_thin_pack)
542 argv[n++] = "--fix-thin";
543 if (keep_pack > 1) {
544 int s = sprintf(keep_arg, "--keep=fetch-pack %i on ", getpid());
545 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
546 strcpy(keep_arg + s, "localhost");
547 argv[n++] = keep_arg;
548 }
549 argv[n] = NULL;
550 return get_pack(xd, argv);
551}
552
def88e9a
LT
553static int fetch_pack(int fd[2], int nr_match, char **match)
554{
d1c133f5
LT
555 struct ref *ref;
556 unsigned char sha1[20];
557 int status;
def88e9a 558
1baaae5e 559 get_remote_heads(fd[0], &ref, 0, NULL, 0);
ed09aef0
JS
560 if (is_repository_shallow() && !server_supports("shallow"))
561 die("Server does not support shallow clients");
c4c86f07
JS
562 if (server_supports("multi_ack")) {
563 if (verbose)
564 fprintf(stderr, "Server supports multi_ack\n");
565 multi_ack = 1;
566 }
d47f3db7
JH
567 if (server_supports("side-band-64k")) {
568 if (verbose)
569 fprintf(stderr, "Server supports side-band-64k\n");
570 use_sideband = 2;
571 }
572 else if (server_supports("side-band")) {
583b7ea3
JH
573 if (verbose)
574 fprintf(stderr, "Server supports side-band\n");
575 use_sideband = 1;
576 }
d1c133f5
LT
577 if (!ref) {
578 packet_flush(fd[1]);
579 die("no matching remote head");
580 }
1baaae5e 581 if (everything_local(&ref, nr_match, match)) {
2759cbc7
LT
582 packet_flush(fd[1]);
583 goto all_done;
584 }
33b83034 585 if (find_common(fd, sha1, ref) < 0)
da093d37 586 if (keep_pack != 1)
dfeff66e
JH
587 /* When cloning, it is not unusual to have
588 * no common commit.
589 */
590 fprintf(stderr, "warning: no common commits\n");
ad897215 591
da093d37 592 status = (keep_pack) ? keep_rx_pack(fd) : explode_rx_pack(fd);
ad897215
JH
593 if (status)
594 die("git-fetch-pack: fetch failed.");
595
596 all_done:
597 while (ref) {
598 printf("%s %s\n",
599 sha1_to_hex(ref->old_sha1), ref->name);
600 ref = ref->next;
75bfc6c2 601 }
ad897215 602 return 0;
def88e9a
LT
603}
604
310b86d4
JH
605static int remove_duplicates(int nr_heads, char **heads)
606{
607 int src, dst;
608
609 for (src = dst = 0; src < nr_heads; src++) {
610 /* If heads[src] is different from any of
611 * heads[0..dst], push it in.
612 */
613 int i;
614 for (i = 0; i < dst; i++) {
615 if (!strcmp(heads[i], heads[src]))
616 break;
617 }
618 if (i < dst)
619 continue;
620 if (src != dst)
621 heads[dst] = heads[src];
622 dst++;
623 }
624 heads[dst] = 0;
625 return dst;
626}
627
54b9e022
JH
628static struct lock_file lock;
629
def88e9a
LT
630int main(int argc, char **argv)
631{
632 int i, ret, nr_heads;
633 char *dest = NULL, **heads;
634 int fd[2];
635 pid_t pid;
016e6ccb 636 struct stat st;
def88e9a 637
5a327713
JH
638 setup_git_directory();
639
def88e9a
LT
640 nr_heads = 0;
641 heads = NULL;
642 for (i = 1; i < argc; i++) {
643 char *arg = argv[i];
644
645 if (*arg == '-') {
8b3d9dc0
JH
646 if (!strncmp("--exec=", arg, 7)) {
647 exec = arg + 7;
648 continue;
649 }
2247efb4 650 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
33b83034
JH
651 quiet = 1;
652 continue;
653 }
2247efb4 654 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
da093d37 655 keep_pack++;
ad897215
JH
656 continue;
657 }
b19696c2
JH
658 if (!strcmp("--thin", arg)) {
659 use_thin_pack = 1;
660 continue;
661 }
dfeff66e
JH
662 if (!strcmp("--all", arg)) {
663 fetch_all = 1;
664 continue;
665 }
33b83034
JH
666 if (!strcmp("-v", arg)) {
667 verbose = 1;
668 continue;
669 }
016e6ccb
JS
670 if (!strncmp("--depth=", arg, 8)) {
671 depth = strtol(arg + 8, NULL, 0);
672 if (stat(git_path("shallow"), &st))
673 st.st_mtime = 0;
674 continue;
675 }
def88e9a
LT
676 usage(fetch_pack_usage);
677 }
678 dest = arg;
679 heads = argv + i + 1;
680 nr_heads = argc - i - 1;
681 break;
682 }
683 if (!dest)
684 usage(fetch_pack_usage);
685 pid = git_connect(fd, dest, exec);
686 if (pid < 0)
687 return 1;
310b86d4
JH
688 if (heads && nr_heads)
689 nr_heads = remove_duplicates(nr_heads, heads);
def88e9a
LT
690 ret = fetch_pack(fd, nr_heads, heads);
691 close(fd[0]);
692 close(fd[1]);
8a5dbef8 693 ret |= finish_connect(pid);
9e5d2b40
JH
694
695 if (!ret && nr_heads) {
696 /* If the heads to pull were given, we should have
697 * consumed all of them by matching the remote.
698 * Otherwise, 'git-fetch remote no-such-ref' would
699 * silently succeed without issuing an error.
700 */
701 for (i = 0; i < nr_heads; i++)
702 if (heads[i] && heads[i][0]) {
703 error("no such remote ref %s", heads[i]);
704 ret = 1;
705 }
706 }
707
016e6ccb
JS
708 if (!ret && depth > 0) {
709 struct cache_time mtime;
710 char *shallow = git_path("shallow");
711 int fd;
712
713 mtime.sec = st.st_mtime;
714#ifdef USE_NSEC
715 mtime.usec = st.st_mtim.usec;
716#endif
717 if (stat(shallow, &st)) {
718 if (mtime.sec)
719 die("shallow file was removed during fetch");
720 } else if (st.st_mtime != mtime.sec
721#ifdef USE_NSEC
722 || st.st_mtim.usec != mtime.usec
723#endif
724 )
725 die("shallow file was changed during fetch");
726
727 fd = hold_lock_file_for_update(&lock, shallow, 1);
728 if (!write_shallow_commits(fd, 0)) {
d6491e3a 729 unlink(shallow);
016e6ccb
JS
730 rollback_lock_file(&lock);
731 } else {
732 close(fd);
733 commit_lock_file(&lock);
734 }
735 }
736
8a5dbef8 737 return !!ret;
def88e9a 738}