Skip to content

Commit bf01639

Browse files
jonathantanmygitster
authored andcommitted
fetch-pack: support more than one pack lockfile
Whenever a fetch results in a packfile being downloaded, a .keep file is generated, so that the packfile can be preserved (from, say, a running "git repack") until refs are written referring to the contents of the packfile. In a subsequent patch, a successful fetch using protocol v2 may result in more than one .keep file being generated. Therefore, teach fetch_pack() and the transport mechanism to support multiple .keep files. Implementation notes: - builtin/fetch-pack.c normally does not generate .keep files, and thus is unaffected by this or future changes. However, it has an undocumented "--lock-pack" feature, used by remote-curl.c when implementing the "fetch" remote helper command. In keeping with the remote helper protocol, only one "lock" line will ever be written; the rest will result in warnings to stderr. However, in practice, warnings will never be written because the remote-curl.c "fetch" is only used for protocol v0/v1 (which will not generate multiple .keep files). (Protocol v2 uses the "stateless-connect" command, not the "fetch" command.) - connected.c has an optimization in that connectivity checks on a ref need not be done if the target object is in a pack known to be self-contained and connected. If there are multiple packfiles, this optimization can no longer be done. Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a8d662e commit bf01639

File tree

7 files changed

+43
-32
lines changed

7 files changed

+43
-32
lines changed

builtin/fetch-pack.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
4848
struct ref **sought = NULL;
4949
int nr_sought = 0, alloc_sought = 0;
5050
int fd[2];
51-
char *pack_lockfile = NULL;
52-
char **pack_lockfile_ptr = NULL;
51+
struct string_list pack_lockfiles = STRING_LIST_INIT_DUP;
52+
struct string_list *pack_lockfiles_ptr = NULL;
5353
struct child_process *conn;
5454
struct fetch_pack_args args;
5555
struct oid_array shallow = OID_ARRAY_INIT;
@@ -134,7 +134,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
134134
}
135135
if (!strcmp("--lock-pack", arg)) {
136136
args.lock_pack = 1;
137-
pack_lockfile_ptr = &pack_lockfile;
137+
pack_lockfiles_ptr = &pack_lockfiles;
138138
continue;
139139
}
140140
if (!strcmp("--check-self-contained-and-connected", arg)) {
@@ -235,10 +235,15 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
235235
}
236236

237237
ref = fetch_pack(&args, fd, conn, ref, dest, sought, nr_sought,
238-
&shallow, pack_lockfile_ptr, version);
239-
if (pack_lockfile) {
240-
printf("lock %s\n", pack_lockfile);
238+
&shallow, pack_lockfiles_ptr, version);
239+
if (pack_lockfiles.nr) {
240+
int i;
241+
242+
printf("lock %s\n", pack_lockfiles.items[0].string);
241243
fflush(stdout);
244+
for (i = 1; i < pack_lockfiles.nr; i++)
245+
warning(_("Lockfile created but not reported: %s"),
246+
pack_lockfiles.items[i].string);
242247
}
243248
if (args.check_self_contained_and_connected &&
244249
args.self_contained_and_connected) {

connected.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ int check_connected(oid_iterate_fn fn, void *cb_data,
4040

4141
if (transport && transport->smart_options &&
4242
transport->smart_options->self_contained_and_connected &&
43-
transport->pack_lockfile &&
44-
strip_suffix(transport->pack_lockfile, ".keep", &base_len)) {
43+
transport->pack_lockfiles.nr == 1 &&
44+
strip_suffix(transport->pack_lockfiles.items[0].string,
45+
".keep", &base_len)) {
4546
struct strbuf idx_file = STRBUF_INIT;
46-
strbuf_add(&idx_file, transport->pack_lockfile, base_len);
47+
strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string,
48+
base_len);
4749
strbuf_addstr(&idx_file, ".idx");
4850
new_pack = add_packed_git(idx_file.buf, idx_file.len, 1);
4951
strbuf_release(&idx_file);

fetch-pack.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ static int sideband_demux(int in, int out, void *data)
755755
}
756756

757757
static int get_pack(struct fetch_pack_args *args,
758-
int xd[2], char **pack_lockfile)
758+
int xd[2], struct string_list *pack_lockfiles)
759759
{
760760
struct async demux;
761761
int do_keep = args->keep_pack;
@@ -798,7 +798,7 @@ static int get_pack(struct fetch_pack_args *args,
798798
}
799799

800800
if (do_keep || args->from_promisor) {
801-
if (pack_lockfile)
801+
if (pack_lockfiles)
802802
cmd.out = -1;
803803
cmd_name = "index-pack";
804804
argv_array_push(&cmd.args, cmd_name);
@@ -853,8 +853,9 @@ static int get_pack(struct fetch_pack_args *args,
853853
cmd.git_cmd = 1;
854854
if (start_command(&cmd))
855855
die(_("fetch-pack: unable to fork off %s"), cmd_name);
856-
if (do_keep && pack_lockfile) {
857-
*pack_lockfile = index_pack_lockfile(cmd.out);
856+
if (do_keep && pack_lockfiles) {
857+
string_list_append_nodup(pack_lockfiles,
858+
index_pack_lockfile(cmd.out));
858859
close(cmd.out);
859860
}
860861

@@ -886,7 +887,7 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
886887
const struct ref *orig_ref,
887888
struct ref **sought, int nr_sought,
888889
struct shallow_info *si,
889-
char **pack_lockfile)
890+
struct string_list *pack_lockfiles)
890891
{
891892
struct ref *ref = copy_ref_list(orig_ref);
892893
struct object_id oid;
@@ -992,7 +993,7 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
992993
alternate_shallow_file = setup_temporary_shallow(si->shallow);
993994
else
994995
alternate_shallow_file = NULL;
995-
if (get_pack(args, fd, pack_lockfile))
996+
if (get_pack(args, fd, pack_lockfiles))
996997
die(_("git fetch-pack: fetch failed."));
997998

998999
all_done:
@@ -1334,7 +1335,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
13341335
int fd[2],
13351336
const struct ref *orig_ref,
13361337
struct ref **sought, int nr_sought,
1337-
char **pack_lockfile)
1338+
struct string_list *pack_lockfiles)
13381339
{
13391340
struct ref *ref = copy_ref_list(orig_ref);
13401341
enum fetch_state state = FETCH_CHECK_LOCAL;
@@ -1415,7 +1416,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
14151416

14161417
/* get the pack */
14171418
process_section_header(&reader, "packfile", 0);
1418-
if (get_pack(args, fd, pack_lockfile))
1419+
if (get_pack(args, fd, pack_lockfiles))
14191420
die(_("git fetch-pack: fetch failed."));
14201421

14211422
state = FETCH_DONE;
@@ -1617,7 +1618,7 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
16171618
const char *dest,
16181619
struct ref **sought, int nr_sought,
16191620
struct oid_array *shallow,
1620-
char **pack_lockfile,
1621+
struct string_list *pack_lockfiles,
16211622
enum protocol_version version)
16221623
{
16231624
struct ref *ref_cpy;
@@ -1648,10 +1649,10 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
16481649
prepare_shallow_info(&si, shallow);
16491650
if (version == protocol_v2)
16501651
ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1651-
pack_lockfile);
1652+
pack_lockfiles);
16521653
else
16531654
ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1654-
&si, pack_lockfile);
1655+
&si, pack_lockfiles);
16551656
reprepare_packed_git(the_repository);
16561657

16571658
if (!args->cloning && args->deepen) {

fetch-pack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
8484
struct ref **sought,
8585
int nr_sought,
8686
struct oid_array *shallow,
87-
char **pack_lockfile,
87+
struct string_list *pack_lockfiles,
8888
enum protocol_version version);
8989

9090
/*

transport-helper.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,11 @@ static int fetch_with_fetch(struct transport *transport,
397397

398398
if (starts_with(buf.buf, "lock ")) {
399399
const char *name = buf.buf + 5;
400-
if (transport->pack_lockfile)
400+
if (transport->pack_lockfiles.nr)
401401
warning(_("%s also locked %s"), data->name, name);
402402
else
403-
transport->pack_lockfile = xstrdup(name);
403+
string_list_append(&transport->pack_lockfiles,
404+
name);
404405
}
405406
else if (data->check_connectivity &&
406407
data->transport_options.check_self_contained_and_connected &&

transport.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,14 @@ static int fetch_refs_via_pack(struct transport *transport,
359359
refs = fetch_pack(&args, data->fd, data->conn,
360360
refs_tmp ? refs_tmp : transport->remote_refs,
361361
dest, to_fetch, nr_heads, &data->shallow,
362-
&transport->pack_lockfile, data->version);
362+
&transport->pack_lockfiles, data->version);
363363
break;
364364
case protocol_v1:
365365
case protocol_v0:
366366
refs = fetch_pack(&args, data->fd, data->conn,
367367
refs_tmp ? refs_tmp : transport->remote_refs,
368368
dest, to_fetch, nr_heads, &data->shallow,
369-
&transport->pack_lockfile, data->version);
369+
&transport->pack_lockfiles, data->version);
370370
break;
371371
case protocol_unknown_version:
372372
BUG("unknown protocol version");
@@ -909,6 +909,7 @@ struct transport *transport_get(struct remote *remote, const char *url)
909909
struct transport *ret = xcalloc(1, sizeof(*ret));
910910

911911
ret->progress = isatty(2);
912+
string_list_init(&ret->pack_lockfiles, 1);
912913

913914
if (!remote)
914915
BUG("No remote provided to transport_get()");
@@ -1302,10 +1303,11 @@ int transport_fetch_refs(struct transport *transport, struct ref *refs)
13021303

13031304
void transport_unlock_pack(struct transport *transport)
13041305
{
1305-
if (transport->pack_lockfile) {
1306-
unlink_or_warn(transport->pack_lockfile);
1307-
FREE_AND_NULL(transport->pack_lockfile);
1308-
}
1306+
int i;
1307+
1308+
for (i = 0; i < transport->pack_lockfiles.nr; i++)
1309+
unlink_or_warn(transport->pack_lockfiles.items[i].string);
1310+
string_list_clear(&transport->pack_lockfiles, 0);
13091311
}
13101312

13111313
int transport_connect(struct transport *transport, const char *name,

transport.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
#include "run-command.h"
66
#include "remote.h"
77
#include "list-objects-filter-options.h"
8-
9-
struct string_list;
8+
#include "string-list.h"
109

1110
struct git_transport_options {
1211
unsigned thin : 1;
@@ -98,7 +97,8 @@ struct transport {
9897
*/
9998
const struct string_list *server_options;
10099

101-
char *pack_lockfile;
100+
struct string_list pack_lockfiles;
101+
102102
signed verbose : 3;
103103
/**
104104
* Transports should not set this directly, and should use this

0 commit comments

Comments
 (0)