THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

828 lines
19KB

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