push: prepare sender to receive extended ref information from the receiver

"git push" enhancement allows the receiving end to report not only its own
refs but refs in repositories it borrows from via the alternate object
store mechanism.  By telling the sender that objects reachable from these
extra refs are already complete in the receiving end, the number of
objects that need to be transfered can be cut down.

These entries are sent over the wire with string ".have", instead of the
actual names of the refs.  This string was chosen so that they are ignored
by older programs at the sending end.  If we sent some random but valid
looking refnames for these entries, "matching refs" rule (triggered when
running "git push" without explicit refspecs, where the sender learns what
refs the receiver has, and updates only the ones with the names of the
refs the sender also has) and "delete missing" rule (triggered when "git
push --mirror" is used, where the sender tells the receiver to delete the
refs it itself does not have) would try to update/delete them, which is
not what we want.

This prepares the send-pack (and "push" that runs native protocol) to
accept extended existing ref information and make use of it.  The ".have"
entries are excluded from ref matching rules, and are exempt from deletion
rule while pushing with --mirror option, but are still used for pack
generation purposes by providing more "bottom" range commits.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Junio C Hamano 2008-09-09 01:27:09 -07:00
parent be5908aed3
commit 40c155ff14
5 changed files with 39 additions and 10 deletions

View file

@ -735,7 +735,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
conn = git_connect(fd, (char *)dest, args.uploadpack, conn = git_connect(fd, (char *)dest, args.uploadpack,
args.verbose ? CONNECT_VERBOSE : 0); args.verbose ? CONNECT_VERBOSE : 0);
if (conn) { if (conn) {
get_remote_heads(fd[0], &ref, 0, NULL, 0); get_remote_heads(fd[0], &ref, 0, NULL, 0, NULL);
ref = fetch_pack(&args, fd, conn, ref, dest, nr_heads, heads, NULL); ref = fetch_pack(&args, fd, conn, ref, dest, nr_heads, heads, NULL);
close(fd[0]); close(fd[0]);

View file

@ -18,7 +18,7 @@ static struct send_pack_args args = {
/* /*
* Make a pack stream and spit it out into file descriptor fd * Make a pack stream and spit it out into file descriptor fd
*/ */
static int pack_objects(int fd, struct ref *refs) static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra)
{ {
/* /*
* The child becomes pack-objects --revs; we feed * The child becomes pack-objects --revs; we feed
@ -34,6 +34,8 @@ static int pack_objects(int fd, struct ref *refs)
NULL, NULL,
}; };
struct child_process po; struct child_process po;
int i;
char buf[42];
if (args.use_thin_pack) if (args.use_thin_pack)
argv[4] = "--thin"; argv[4] = "--thin";
@ -49,9 +51,15 @@ static int pack_objects(int fd, struct ref *refs)
* We feed the pack-objects we just spawned with revision * We feed the pack-objects we just spawned with revision
* parameters by writing to the pipe. * parameters by writing to the pipe.
*/ */
while (refs) { for (i = 0; i < extra->nr; i++) {
char buf[42]; memcpy(buf + 1, sha1_to_hex(&extra->array[i][0]), 40);
buf[0] = '^';
buf[41] = '\n';
if (!write_or_whine(po.in, buf, 42, "send-pack: send refs"))
break;
}
while (refs) {
if (!is_null_sha1(refs->old_sha1) && if (!is_null_sha1(refs->old_sha1) &&
has_sha1_file(refs->old_sha1)) { has_sha1_file(refs->old_sha1)) {
memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40); memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40);
@ -381,14 +389,17 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
int expect_status_report = 0; int expect_status_report = 0;
int flags = MATCH_REFS_NONE; int flags = MATCH_REFS_NONE;
int ret; int ret;
struct extra_have_objects extra_have;
memset(&extra_have, 0, sizeof(extra_have));
if (args.send_all) if (args.send_all)
flags |= MATCH_REFS_ALL; flags |= MATCH_REFS_ALL;
if (args.send_mirror) if (args.send_mirror)
flags |= MATCH_REFS_MIRROR; flags |= MATCH_REFS_MIRROR;
/* No funny business with the matcher */ /* No funny business with the matcher */
remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL); remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL,
&extra_have);
get_local_heads(); get_local_heads();
/* Does the other end support the reporting? */ /* Does the other end support the reporting? */
@ -496,7 +507,7 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
packet_flush(out); packet_flush(out);
if (new_refs && !args.dry_run) { if (new_refs && !args.dry_run) {
if (pack_objects(out, remote_refs) < 0) if (pack_objects(out, remote_refs, &extra_have) < 0)
return -1; return -1;
} }
else else

View file

@ -709,7 +709,11 @@ extern struct child_process *git_connect(int fd[2], const char *url, const char
extern int finish_connect(struct child_process *conn); extern int finish_connect(struct child_process *conn);
extern int path_match(const char *path, int nr, char **match); extern int path_match(const char *path, int nr, char **match);
extern int get_ack(int fd, unsigned char *result_sha1); extern int get_ack(int fd, unsigned char *result_sha1);
extern struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match, unsigned int flags); struct extra_have_objects {
int nr, alloc;
unsigned char (*array)[20];
};
extern struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match, unsigned int flags, struct extra_have_objects *);
extern int server_supports(const char *feature); extern int server_supports(const char *feature);
extern struct packed_git *parse_pack_index(unsigned char *sha1); extern struct packed_git *parse_pack_index(unsigned char *sha1);

View file

@ -41,12 +41,20 @@ int check_ref_type(const struct ref *ref, int flags)
return check_ref(ref->name, strlen(ref->name), flags); return check_ref(ref->name, strlen(ref->name), flags);
} }
static void add_extra_have(struct extra_have_objects *extra, unsigned char *sha1)
{
ALLOC_GROW(extra->array, extra->nr + 1, extra->alloc);
hashcpy(&(extra->array[extra->nr][0]), sha1);
extra->nr++;
}
/* /*
* Read all the refs from the other end * Read all the refs from the other end
*/ */
struct ref **get_remote_heads(int in, struct ref **list, struct ref **get_remote_heads(int in, struct ref **list,
int nr_match, char **match, int nr_match, char **match,
unsigned int flags) unsigned int flags,
struct extra_have_objects *extra_have)
{ {
*list = NULL; *list = NULL;
for (;;) { for (;;) {
@ -72,6 +80,12 @@ struct ref **get_remote_heads(int in, struct ref **list,
server_capabilities = xstrdup(name + name_len + 1); server_capabilities = xstrdup(name + name_len + 1);
} }
if (extra_have &&
name_len == 5 && !memcmp(".have", name, 5)) {
add_extra_have(extra_have, old_sha1);
continue;
}
if (!check_ref(name, name_len, flags)) if (!check_ref(name, name_len, flags))
continue; continue;
if (nr_match && !path_match(name, nr_match, match)) if (nr_match && !path_match(name, nr_match, match))

View file

@ -619,7 +619,7 @@ static struct ref *get_refs_via_connect(struct transport *transport)
struct ref *refs; struct ref *refs;
connect_setup(transport); connect_setup(transport);
get_remote_heads(data->fd[0], &refs, 0, NULL, 0); get_remote_heads(data->fd[0], &refs, 0, NULL, 0, NULL);
return refs; return refs;
} }
@ -652,7 +652,7 @@ static int fetch_refs_via_pack(struct transport *transport,
if (!data->conn) { if (!data->conn) {
connect_setup(transport); connect_setup(transport);
get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0); get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL);
} }
refs = fetch_pack(&args, data->fd, data->conn, refs = fetch_pack(&args, data->fd, data->conn,