From f2a37151d4624906e34a9bcafb2ad79d0e8cb7ec Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Wed, 18 Nov 2009 02:42:21 +0100 Subject: Fix memory leak in helper method for disconnect Since some cases may need to disconnect from the helper and reconnect, wrap the function that just disconnects in a function that also frees transport->data. Signed-off-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- transport-helper.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'transport-helper.c') diff --git a/transport-helper.c b/transport-helper.c index f57e84c676..e24fcbb17d 100644 --- a/transport-helper.c +++ b/transport-helper.c @@ -67,6 +67,13 @@ static int disconnect_helper(struct transport *transport) return 0; } +static int release_helper(struct transport *transport) +{ + disconnect_helper(transport); + free(transport->data); + return 0; +} + static int fetch_with_fetch(struct transport *transport, int nr_heads, const struct ref **to_fetch) { @@ -163,6 +170,6 @@ int transport_helper_init(struct transport *transport, const char *name) transport->data = data; transport->get_refs_list = get_refs_list; transport->fetch = fetch; - transport->disconnect = disconnect_helper; + transport->disconnect = release_helper; return 0; } -- cgit v1.2.1 From 3714831189b32591ffe33c08e209a9a61c25a2f6 Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Wed, 18 Nov 2009 02:42:24 +0100 Subject: Allow fetch to modify refs This allows the transport to use the null sha1 for a ref reported to be present in the remote repository to indicate that a ref exists but its actual value is presently unknown and will be set if the objects are fetched. Also adds documentation to the API to specify exactly what the methods should do and how they should interpret arguments. Signed-off-by: Daniel Barkalow Signed-off-by: Sverre Rabbelier Signed-off-by: Junio C Hamano --- transport-helper.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'transport-helper.c') diff --git a/transport-helper.c b/transport-helper.c index e24fcbb17d..53d8f08ee9 100644 --- a/transport-helper.c +++ b/transport-helper.c @@ -75,7 +75,7 @@ static int release_helper(struct transport *transport) } static int fetch_with_fetch(struct transport *transport, - int nr_heads, const struct ref **to_fetch) + int nr_heads, struct ref **to_fetch) { struct child_process *helper = get_helper(transport); FILE *file = xfdopen(helper->out, "r"); @@ -99,7 +99,7 @@ static int fetch_with_fetch(struct transport *transport, } static int fetch(struct transport *transport, - int nr_heads, const struct ref **to_fetch) + int nr_heads, struct ref **to_fetch) { struct helper_data *data = transport->data; int i, count; -- cgit v1.2.1 From e65e91ed4af5ed9c5c810a2cd77b8648a0287e66 Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Wed, 18 Nov 2009 02:42:27 +0100 Subject: Add support for "import" helper command This command, supported if the "import" capability is advertized, allows a helper to support fetching by outputting a git-fast-import stream. If both "fetch" and "import" are advertized, git itself will use "fetch" (although other users may use "import" in this case). Signed-off-by: Daniel Barkalow Signed-off-by: Sverre Rabbelier Signed-off-by: Junio C Hamano --- transport-helper.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'transport-helper.c') diff --git a/transport-helper.c b/transport-helper.c index 53d8f08ee9..82caaaead6 100644 --- a/transport-helper.c +++ b/transport-helper.c @@ -11,6 +11,7 @@ struct helper_data const char *name; struct child_process *helper; unsigned fetch : 1; + unsigned import : 1; }; static struct child_process *get_helper(struct transport *transport) @@ -48,6 +49,8 @@ static struct child_process *get_helper(struct transport *transport) break; if (!strcmp(buf.buf, "fetch")) data->fetch = 1; + if (!strcmp(buf.buf, "import")) + data->import = 1; } return data->helper; } @@ -98,6 +101,52 @@ static int fetch_with_fetch(struct transport *transport, return 0; } +static int get_importer(struct transport *transport, struct child_process *fastimport) +{ + struct child_process *helper = get_helper(transport); + memset(fastimport, 0, sizeof(*fastimport)); + fastimport->in = helper->out; + fastimport->argv = xcalloc(5, sizeof(*fastimport->argv)); + fastimport->argv[0] = "fast-import"; + fastimport->argv[1] = "--quiet"; + + fastimport->git_cmd = 1; + return start_command(fastimport); +} + +static int fetch_with_import(struct transport *transport, + int nr_heads, struct ref **to_fetch) +{ + struct child_process fastimport; + struct child_process *helper = get_helper(transport); + int i; + struct ref *posn; + struct strbuf buf = STRBUF_INIT; + + if (get_importer(transport, &fastimport)) + die("Couldn't run fast-import"); + + for (i = 0; i < nr_heads; i++) { + posn = to_fetch[i]; + if (posn->status & REF_STATUS_UPTODATE) + continue; + + strbuf_addf(&buf, "import %s\n", posn->name); + write_in_full(helper->in, buf.buf, buf.len); + strbuf_reset(&buf); + } + disconnect_helper(transport); + finish_command(&fastimport); + + for (i = 0; i < nr_heads; i++) { + posn = to_fetch[i]; + if (posn->status & REF_STATUS_UPTODATE) + continue; + read_ref(posn->name, posn->old_sha1); + } + return 0; +} + static int fetch(struct transport *transport, int nr_heads, struct ref **to_fetch) { @@ -115,6 +164,9 @@ static int fetch(struct transport *transport, if (data->fetch) return fetch_with_fetch(transport, nr_heads, to_fetch); + if (data->import) + return fetch_with_import(transport, nr_heads, to_fetch); + return -1; } -- cgit v1.2.1 From 72ff894308d4f6eb9f081167377857f7a3268bca Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Wed, 18 Nov 2009 02:42:28 +0100 Subject: Allow helper to map private ref names into normal names This allows a helper to say that, when it handles "import refs/heads/topic", the script it outputs will actually write to refs/svn/origin/branches/topic; therefore, transport-helper should read it from the latter location after git-fast-import completes. Signed-off-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- transport-helper.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'transport-helper.c') diff --git a/transport-helper.c b/transport-helper.c index 82caaaead6..da8185a981 100644 --- a/transport-helper.c +++ b/transport-helper.c @@ -5,6 +5,7 @@ #include "commit.h" #include "diff.h" #include "revision.h" +#include "remote.h" struct helper_data { @@ -12,6 +13,9 @@ struct helper_data struct child_process *helper; unsigned fetch : 1; unsigned import : 1; + /* These go from remote name (as in "list") to private name */ + struct refspec *refspecs; + int refspec_nr; }; static struct child_process *get_helper(struct transport *transport) @@ -20,6 +24,9 @@ static struct child_process *get_helper(struct transport *transport) struct strbuf buf = STRBUF_INIT; struct child_process *helper; FILE *file; + const char **refspecs = NULL; + int refspec_nr = 0; + int refspec_alloc = 0; if (data->helper) return data->helper; @@ -51,6 +58,21 @@ static struct child_process *get_helper(struct transport *transport) data->fetch = 1; if (!strcmp(buf.buf, "import")) data->import = 1; + if (!data->refspecs && !prefixcmp(buf.buf, "refspec ")) { + ALLOC_GROW(refspecs, + refspec_nr + 1, + refspec_alloc); + refspecs[refspec_nr++] = strdup(buf.buf + strlen("refspec ")); + } + } + if (refspecs) { + int i; + data->refspec_nr = refspec_nr; + data->refspecs = parse_fetch_refspec(refspec_nr, refspecs); + for (i = 0; i < refspec_nr; i++) { + free((char *)refspecs[i]); + } + free(refspecs); } return data->helper; } @@ -72,6 +94,9 @@ static int disconnect_helper(struct transport *transport) static int release_helper(struct transport *transport) { + struct helper_data *data = transport->data; + free_refspec(data->refspec_nr, data->refspecs); + data->refspecs = NULL; disconnect_helper(transport); free(transport->data); return 0; @@ -119,6 +144,7 @@ static int fetch_with_import(struct transport *transport, { struct child_process fastimport; struct child_process *helper = get_helper(transport); + struct helper_data *data = transport->data; int i; struct ref *posn; struct strbuf buf = STRBUF_INIT; @@ -139,10 +165,16 @@ static int fetch_with_import(struct transport *transport, finish_command(&fastimport); for (i = 0; i < nr_heads; i++) { + char *private; posn = to_fetch[i]; if (posn->status & REF_STATUS_UPTODATE) continue; - read_ref(posn->name, posn->old_sha1); + if (data->refspecs) + private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name); + else + private = strdup(posn->name); + read_ref(private, posn->old_sha1); + free(private); } return 0; } -- cgit v1.2.1 From b962dbdc806510a4572a5574245d825a414231ab Mon Sep 17 00:00:00 2001 From: Sverre Rabbelier Date: Wed, 18 Nov 2009 02:42:29 +0100 Subject: Fix various memory leaks in transport-helper.c Found with: valgrind --tool=memcheck --leak-check=full --show-reachable=yes Signed-off-by: Sverre Rabbelier Signed-off-by: Junio C Hamano --- transport-helper.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'transport-helper.c') diff --git a/transport-helper.c b/transport-helper.c index da8185a981..628a5ca210 100644 --- a/transport-helper.c +++ b/transport-helper.c @@ -74,6 +74,7 @@ static struct child_process *get_helper(struct transport *transport) } free(refspecs); } + strbuf_release(&buf); return data->helper; } @@ -163,6 +164,8 @@ static int fetch_with_import(struct transport *transport, } disconnect_helper(transport); finish_command(&fastimport); + free(fastimport.argv); + fastimport.argv = NULL; for (i = 0; i < nr_heads; i++) { char *private; @@ -176,6 +179,7 @@ static int fetch_with_import(struct transport *transport, read_ref(private, posn->old_sha1); free(private); } + strbuf_release(&buf); return 0; } -- cgit v1.2.1 From f8ec916731ef8d81eefc5db61e3405dade65d821 Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Wed, 18 Nov 2009 02:42:30 +0100 Subject: Allow helpers to report in "list" command that the ref is unchanged Helpers may use a line like "? name unchanged" to specify that there is nothing new at that name, without any git-specific code to determine the correct response. Signed-off-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- transport-helper.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'transport-helper.c') diff --git a/transport-helper.c b/transport-helper.c index 628a5ca210..c87530e87d 100644 --- a/transport-helper.c +++ b/transport-helper.c @@ -206,6 +206,22 @@ static int fetch(struct transport *transport, return -1; } +static int has_attribute(const char *attrs, const char *attr) { + int len; + if (!attrs) + return 0; + + len = strlen(attr); + for (;;) { + const char *space = strchrnul(attrs, ' '); + if (len == space - attrs && !strncmp(attrs, attr, len)) + return 1; + if (!*space) + return 0; + attrs = space + 1; + } +} + static struct ref *get_refs_list(struct transport *transport, int for_push) { struct child_process *helper; @@ -240,6 +256,12 @@ static struct ref *get_refs_list(struct transport *transport, int for_push) (*tail)->symref = xstrdup(buf.buf + 1); else if (buf.buf[0] != '?') get_sha1_hex(buf.buf, (*tail)->old_sha1); + if (eon) { + if (has_attribute(eon + 1, "unchanged")) { + (*tail)->status |= REF_STATUS_UPTODATE; + read_ref((*tail)->name, (*tail)->old_sha1); + } + } tail = &((*tail)->next); } strbuf_release(&buf); -- cgit v1.2.1