@matheusbsbs / matheus-bagliano / commits / 1788c39cd0
Fork 0

Remove pack.keep after ref updates in git-fetch

If we are using a native packfile to perform a git-fetch invocation and the received packfile contained more than the configured limits of fetch.unpackLimit/transfer.unpackLimit then index-pack will output a single line saying "keep\t$sha1\n" to stdout. This line needs to be captured and retained so we can delete the corresponding .keep file ("$GIT_DIR/objects/pack/pack-$sha1.keep") once all refs have been safely updated. This trick has long been in use with git-fetch.sh and its lower level helper git-fetch--tool as a way to allow index-pack to save the new packfile before the refs have been updated and yet avoid a race with any concurrently running git-repack process. It was unfortunately lost when git-fetch.sh was converted to pure C and fetch--tool was no longer being invoked. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Shawn O. Pearce committed Sep 14, 2007 at 03:31 UTC 1788c39cd0742439b9bedc28bc10bc4d105b6c0f
5 files changed +44 -13
builtin-fetch-pack.c
+25 -6
@@ -493,7 +493,7 @@ static pid_t setup_sideband(int fd[2], int xd[2])
493 return side_pid;
494 }
495
496 -static int get_pack(int xd[2])
496 +static int get_pack(int xd[2], char **pack_lockfile)
497 {
498 int status;
499 pid_t pid, side_pid;
@@ -503,6 +503,7 @@ static int get_pack(int xd[2])
503 char hdr_arg[256];
504 const char **av;
505 int do_keep = keep_pack;
506 + int keep_pipe[2];
507
508 side_pid = setup_sideband(fd, xd);
509
@@ -522,6 +523,8 @@ static int get_pack(int xd[2])
523 }
524
525 if (do_keep) {
526 + if (pack_lockfile && pipe(keep_pipe))
527 + die("fetch-pack: pipe setup failure: %s", strerror(errno));
528 *av++ = "index-pack";
529 *av++ = "--stdin";
530 if (!quiet && !no_progress)
@@ -550,6 +553,11 @@ static int get_pack(int xd[2])
553 die("fetch-pack: unable to fork off %s", argv[0]);
554 if (!pid) {
555 dup2(fd[0], 0);
556 + if (do_keep && pack_lockfile) {
557 + dup2(keep_pipe[1], 1);
558 + close(keep_pipe[0]);
559 + close(keep_pipe[1]);
560 + }
561 close(fd[0]);
562 close(fd[1]);
563 execv_git_cmd(argv);
@@ -557,6 +565,11 @@ static int get_pack(int xd[2])
565 }
566 close(fd[0]);
567 close(fd[1]);
568 + if (do_keep && pack_lockfile) {
569 + close(keep_pipe[1]);
570 + *pack_lockfile = index_pack_lockfile(keep_pipe[0]);
571 + close(keep_pipe[0]);
572 + }
573 while (waitpid(pid, &status, 0) < 0) {
574 if (errno != EINTR)
575 die("waiting for %s: %s", argv[0], strerror(errno));
@@ -574,7 +587,10 @@ static int get_pack(int xd[2])
587 die("%s died of unnatural causes %d", argv[0], status);
588 }
589
577 -static struct ref *do_fetch_pack(int fd[2], int nr_match, char **match)
590 +static struct ref *do_fetch_pack(int fd[2],
591 + int nr_match,
592 + char **match,
593 + char **pack_lockfile)
594 {
595 struct ref *ref;
596 unsigned char sha1[20];
@@ -612,7 +628,7 @@ static struct ref *do_fetch_pack(int fd[2], int nr_match, char **match)
628 */
629 fprintf(stderr, "warning: no common commits\n");
630
615 - if (get_pack(fd))
631 + if (get_pack(fd, pack_lockfile))
632 die("git-fetch-pack: fetch failed.");
633
634 all_done:
@@ -741,7 +757,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
757 if (!dest)
758 usage(fetch_pack_usage);
759
744 - ref = fetch_pack(dest, nr_heads, heads);
760 + ref = fetch_pack(dest, nr_heads, heads, NULL);
761
762 ret = !ref;
763
@@ -754,7 +770,10 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
770 return ret;
771 }
772
757 -struct ref *fetch_pack(const char *dest, int nr_heads, char **heads)
773 +struct ref *fetch_pack(const char *dest,
774 + int nr_heads,
775 + char **heads,
776 + char **pack_lockfile)
777 {
778 int i, ret;
779 int fd[2];
@@ -773,7 +792,7 @@ struct ref *fetch_pack(const char *dest, int nr_heads, char **heads)
792 return NULL;
793 if (heads && nr_heads)
794 nr_heads = remove_duplicates(nr_heads, heads);
776 - ref = do_fetch_pack(fd, nr_heads, heads);
795 + ref = do_fetch_pack(fd, nr_heads, heads, pack_lockfile);
796 close(fd[0]);
797 close(fd[1]);
798 ret = finish_connect(pid);
builtin-fetch.c
+1
@@ -274,6 +274,7 @@ static int fetch_refs(struct transport *transport, struct ref *ref_map)
274 int ret = transport_fetch_refs(transport, ref_map);
275 if (!ret)
276 store_updated_refs(transport->url, ref_map);
277 + transport_unlock_pack(transport);
278 return ret;
279 }
280
fetch-pack.h
+1 -1
@@ -16,6 +16,6 @@ struct fetch_pack_args
16
17 void setup_fetch_pack(struct fetch_pack_args *args);
18
19 -struct ref *fetch_pack(const char *dest, int nr_heads, char **heads);
19 +struct ref *fetch_pack(const char *dest, int nr_heads, char **heads, char **pack_lockfile);
20
21 #endif
transport.c
+14 -4
@@ -9,7 +9,7 @@
9
10 /* Generic functions for using commit walkers */
11
12 -static int fetch_objs_via_walker(const struct transport *transport,
12 +static int fetch_objs_via_walker(struct transport *transport,
13 int nr_objs, struct ref **to_fetch)
14 {
15 char *dest = xstrdup(transport->url);
@@ -219,7 +219,7 @@ static struct ref *get_refs_from_bundle(const struct transport *transport)
219 return result;
220 }
221
222 -static int fetch_refs_from_bundle(const struct transport *transport,
222 +static int fetch_refs_from_bundle(struct transport *transport,
223 int nr_heads, struct ref **to_fetch)
224 {
225 struct bundle_transport_data *data = transport->data;
@@ -306,7 +306,7 @@ static struct ref *get_refs_via_connect(const struct transport *transport)
306 return refs;
307 }
308
309 -static int fetch_refs_via_pack(const struct transport *transport,
309 +static int fetch_refs_via_pack(struct transport *transport,
310 int nr_heads, struct ref **to_fetch)
311 {
312 struct git_transport_data *data = transport->data;
@@ -330,7 +330,7 @@ static int fetch_refs_via_pack(const struct transport *transport,
330
331 for (i = 0; i < nr_heads; i++)
332 heads[i] = xstrdup(to_fetch[i]->name);
333 - refs = fetch_pack(dest, nr_heads, heads);
333 + refs = fetch_pack(dest, nr_heads, heads, &transport->pack_lockfile);
334
335 for (i = 0; i < nr_heads; i++)
336 free(heads[i]);
@@ -445,6 +445,7 @@ struct transport *transport_get(struct remote *remote, const char *url,
445 ret->url = url;
446 ret->remote_refs = NULL;
447 ret->fetch = !!fetch;
448 + ret->pack_lockfile = NULL;
449 }
450 return ret;
451 }
@@ -500,6 +501,15 @@ int transport_fetch_refs(struct transport *transport, struct ref *refs)
501 return rc;
502 }
503
504 +void transport_unlock_pack(struct transport *transport)
505 +{
506 + if (transport->pack_lockfile) {
507 + unlink(transport->pack_lockfile);
508 + free(transport->pack_lockfile);
509 + transport->pack_lockfile = NULL;
510 + }
511 +}
512 +
513 int transport_disconnect(struct transport *transport)
514 {
515 int ret = 0;
transport.h
+3 -2
@@ -15,6 +15,7 @@ struct transport {
15 struct ref *remote_refs;
16
17 const struct transport_ops *ops;
18 + char *pack_lockfile;
19 };
20
21 #define TRANSPORT_PUSH_ALL 1
@@ -30,7 +31,7 @@ struct transport_ops {
31 const char *value);
32
33 struct ref *(*get_refs_list)(const struct transport *transport);
33 - int (*fetch)(const struct transport *transport, int refs_nr, struct ref **refs);
34 + int (*fetch)(struct transport *transport, int refs_nr, struct ref **refs);
35 int (*push)(struct transport *connection, int refspec_nr, const char **refspec, int flags);
36
37 int (*disconnect)(struct transport *connection);
@@ -73,7 +74,7 @@ int transport_push(struct transport *connection,
74 struct ref *transport_get_remote_refs(struct transport *transport);
75
76 int transport_fetch_refs(struct transport *transport, struct ref *refs);
76 -
77 +void transport_unlock_pack(struct transport *transport);
78 int transport_disconnect(struct transport *transport);
79
80 #endif