From 203462347fce0eab563fe77640648a7e8ae64d3b Mon Sep 17 00:00:00 2001 From: Michael J Gruber Date: Tue, 9 Jun 2009 18:01:34 +0200 Subject: [PATCH 1/6] Allow push and fetch urls to be different This introduces a config setting remote.$remotename.pushurl which is used for pushes only. If absent remote.$remotename.url is used for pushes and fetches as before. This is useful, for example, in order to do passwordless fetches (remote update) over the git transport but pushes over ssh. Signed-off-by: Michael J Gruber Signed-off-by: Junio C Hamano --- Documentation/config.txt | 3 +++ Documentation/urls-remotes.txt | 3 +++ builtin-push.c | 17 +++++++++++++---- remote.c | 14 ++++++++++++++ remote.h | 4 ++++ 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Documentation/config.txt b/Documentation/config.txt index 3a86d1f8f0..2fecbe32a6 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -1319,6 +1319,9 @@ remote..url:: The URL of a remote repository. See linkgit:git-fetch[1] or linkgit:git-push[1]. +remote..pushurl:: + The push URL of a remote repository. See linkgit:git-push[1]. + remote..proxy:: For remotes that require curl (http, https and ftp), the URL to the proxy to use for that remote. Set to the empty string to diff --git a/Documentation/urls-remotes.txt b/Documentation/urls-remotes.txt index 41ec7774f4..2a0e7b8944 100644 --- a/Documentation/urls-remotes.txt +++ b/Documentation/urls-remotes.txt @@ -27,10 +27,13 @@ config file would appear like this: ------------ [remote ""] url = + pushurl = push = fetch = ------------ +The `` is used for pushes only. It is optional and defaults +to ``. Named file in `$GIT_DIR/remotes` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/builtin-push.c b/builtin-push.c index c869974013..7be12399b6 100644 --- a/builtin-push.c +++ b/builtin-push.c @@ -117,6 +117,8 @@ static int do_push(const char *repo, int flags) { int i, errs; struct remote *remote = remote_get(repo); + const char **url; + int url_nr; if (!remote) { if (repo) @@ -152,9 +154,16 @@ static int do_push(const char *repo, int flags) setup_default_push_refspecs(); } errs = 0; - for (i = 0; i < remote->url_nr; i++) { + if (remote->pushurl_nr) { + url = remote->pushurl; + url_nr = remote->pushurl_nr; + } else { + url = remote->url; + url_nr = remote->url_nr; + } + for (i = 0; i < url_nr; i++) { struct transport *transport = - transport_get(remote, remote->url[i]); + transport_get(remote, url[i]); int err; if (receivepack) transport_set_option(transport, @@ -163,14 +172,14 @@ static int do_push(const char *repo, int flags) transport_set_option(transport, TRANS_OPT_THIN, "yes"); if (flags & TRANSPORT_PUSH_VERBOSE) - fprintf(stderr, "Pushing to %s\n", remote->url[i]); + fprintf(stderr, "Pushing to %s\n", url[i]); err = transport_push(transport, refspec_nr, refspec, flags); err |= transport_disconnect(transport); if (!err) continue; - error("failed to push some refs to '%s'", remote->url[i]); + error("failed to push some refs to '%s'", url[i]); errs++; } return !!errs; diff --git a/remote.c b/remote.c index 2c3e9053a4..4544d65c72 100644 --- a/remote.c +++ b/remote.c @@ -106,6 +106,12 @@ static void add_url_alias(struct remote *remote, const char *url) add_url(remote, alias_url(url)); } +static void add_pushurl(struct remote *remote, const char *pushurl) +{ + ALLOC_GROW(remote->pushurl, remote->pushurl_nr + 1, remote->pushurl_alloc); + remote->pushurl[remote->pushurl_nr++] = pushurl; +} + static struct remote *make_remote(const char *name, int len) { struct remote *ret; @@ -379,6 +385,11 @@ static int handle_config(const char *key, const char *value, void *cb) if (git_config_string(&v, key, value)) return -1; add_url(remote, v); + } else if (!strcmp(subkey, ".pushurl")) { + const char *v; + if (git_config_string(&v, key, value)) + return -1; + add_pushurl(remote, v); } else if (!strcmp(subkey, ".push")) { const char *v; if (git_config_string(&v, key, value)) @@ -424,6 +435,9 @@ static void alias_all_urls(void) for (j = 0; j < remotes[i]->url_nr; j++) { remotes[i]->url[j] = alias_url(remotes[i]->url[j]); } + for (j = 0; j < remotes[i]->pushurl_nr; j++) { + remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j]); + } } } diff --git a/remote.h b/remote.h index 99706a89bc..326bffeb0c 100644 --- a/remote.h +++ b/remote.h @@ -15,6 +15,10 @@ struct remote { int url_nr; int url_alloc; + const char **pushurl; + int pushurl_nr; + int pushurl_alloc; + const char **push_refspec; struct refspec *push; int push_refspec_nr; From e1ca424112d5e8923a4a83af0f79eadbff088b69 Mon Sep 17 00:00:00 2001 From: Michael J Gruber Date: Tue, 9 Jun 2009 18:01:35 +0200 Subject: [PATCH 2/6] t5516: Check pushurl config setting Check whether the new remote.${remotename}.pushurl setting is obeyed and whether it overrides remote.${remotename}.url. Signed-off-by: Michael J Gruber Signed-off-by: Junio C Hamano --- t/t5516-fetch-push.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh index 89649e7a9b..2d2633f3f8 100755 --- a/t/t5516-fetch-push.sh +++ b/t/t5516-fetch-push.sh @@ -419,6 +419,19 @@ test_expect_success 'push with config remote.*.push = HEAD' ' git config --remove-section remote.there git config --remove-section branch.master +test_expect_success 'push with config remote.*.pushurl' ' + + mk_test heads/master && + git checkout master && + git config remote.there.url test2repo && + git config remote.there.pushurl testrepo && + git push there && + check_push_result $the_commit heads/master +' + +# clean up the cruft left with the previous one +git config --remove-section remote.there + test_expect_success 'push with dry-run' ' mk_test heads/master && From 056724c6240ce79ff5ef4e91191bf7366dadba88 Mon Sep 17 00:00:00 2001 From: Michael J Gruber Date: Tue, 9 Jun 2009 18:01:36 +0200 Subject: [PATCH 3/6] technical/api-remote: Describe new struct remote member pushurl ...and pushurl_nr Signed-off-by: Michael J Gruber Signed-off-by: Junio C Hamano --- Documentation/technical/api-remote.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Documentation/technical/api-remote.txt b/Documentation/technical/api-remote.txt index 073b22bd83..c54b17db69 100644 --- a/Documentation/technical/api-remote.txt +++ b/Documentation/technical/api-remote.txt @@ -18,6 +18,10 @@ struct remote An array of all of the url_nr URLs configured for the remote +`pushurl`:: + + An array of all of the pushurl_nr push URLs configured for the remote + `push`:: An array of refspecs configured for pushing, with From 857f8c30d79ce15b6110b0df9d3ad87075ca5153 Mon Sep 17 00:00:00 2001 From: Michael J Gruber Date: Sat, 13 Jun 2009 18:29:10 +0200 Subject: [PATCH 4/6] builtin-remote: Show push urls as well Teach builtin remote to show push urls also when asked to "show" a specific remote. This improves upon the standard display mode: multiple specified "url"s mean that the first one is for fetching, all are used for pushing. We make this clearer now by displaying the first one prefixed with "Fetch URL", and all "url"s (or, if present, all "pushurl"s) prefixed with "Push URL". Example with "one" having one url, "two" two urls, "three" one url and one pushurl (URL part only): * remote one Fetch URL: hostone.com:/somepath/repoone.git Push URL: hostone.com:/somepath/repoone.git * remote two Fetch URL: hosttwo.com:/somepath/repotwo.git Push URL: hosttwo.com:/somepath/repotwo.git Push URL: hosttwobackup.com:/somewheresafe/repotwo.git * remote three Fetch URL: http://hostthree.com/otherpath/repothree.git Push URL: hostthree.com:/pathforpushes/repothree.git Also, adjust t5505 accordingly and make it test for the new output. Signed-off-by: Michael J Gruber Signed-off-by: Junio C Hamano --- builtin-remote.c | 20 +++++++++++++++----- t/t5505-remote.sh | 10 +++++++--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/builtin-remote.c b/builtin-remote.c index d7ab6b2d5f..8dcc6a4bf9 100644 --- a/builtin-remote.c +++ b/builtin-remote.c @@ -1002,15 +1002,25 @@ static int show(int argc, const char **argv) info.list = &info_list; for (; argc; argc--, argv++) { int i; + const char **url; + int url_nr; get_remote_ref_states(*argv, &states, query_flag); printf("* remote %s\n", *argv); - if (states.remote->url_nr) { - for (i=0; i < states.remote->url_nr; i++) - printf(" URL: %s\n", states.remote->url[i]); - } else - printf(" URL: %s\n", "(no URL)"); + printf(" Fetch URL: %s\n", states.remote->url_nr > 0 ? + states.remote->url[0] : "(no URL)"); + if (states.remote->pushurl_nr) { + url = states.remote->pushurl; + url_nr = states.remote->pushurl_nr; + } else { + url = states.remote->url; + url_nr = states.remote->url_nr; + } + for (i=0; i < url_nr; i++) + printf(" Push URL: %s\n", url[i]); + if (!i) + printf(" Push URL: %s\n", "(no URL)"); if (no_query) printf(" HEAD branch: (not queried)\n"); else if (!states.heads.nr) diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh index e70246b3fb..852ccb5d7d 100755 --- a/t/t5505-remote.sh +++ b/t/t5505-remote.sh @@ -135,7 +135,8 @@ EOF cat > test/expect << EOF * remote origin - URL: $(pwd)/one + Fetch URL: $(pwd)/one + Push URL: $(pwd)/one HEAD branch: master Remote branches: master new (next fetch will store in remotes/origin) @@ -151,7 +152,8 @@ cat > test/expect << EOF master pushes to master (local out of date) master pushes to upstream (create) * remote two - URL: ../two + Fetch URL: ../two + Push URL: ../three HEAD branch (remote HEAD is ambiguous, may be one of the following): another master @@ -173,6 +175,7 @@ test_expect_success 'show' ' git branch --track rebase origin/master && git branch -d -r origin/master && git config --add remote.two.url ../two && + git config --add remote.two.pushurl ../three && git config branch.rebase.rebase true && git config branch.octopus.merge "topic-a topic-b topic-c" && (cd ../one && @@ -191,7 +194,8 @@ test_expect_success 'show' ' cat > test/expect << EOF * remote origin - URL: $(pwd)/one + Fetch URL: $(pwd)/one + Push URL: $(pwd)/one HEAD branch: (not queried) Remote branches: (status not queried) master From 4a4b4cdaabde477514c4938b60961538e1d2d91f Mon Sep 17 00:00:00 2001 From: Michael J Gruber Date: Sat, 13 Jun 2009 18:29:11 +0200 Subject: [PATCH 5/6] builtin-remote: Make "remote -v" display push urls Currently, "remote -v" simply lists all urls so that one has to remember that only the first one is used for fetches, and all are used for pushes. Change this so that the role of an url is displayed in parentheses, and also display push urls. Example with "one" having one url, "two" two urls, "three" one url and one pushurl: one hostone.com:/somepath/repoone.git (fetch) one hostone.com:/somepath/repoone.git (push) three http://hostthree.com/otherpath/repothree.git (fetch) three hostthree.com:/pathforpushes/repothree.git (push) two hosttwo.com:/somepath/repotwo.git (fetch) two hosttwo.com:/somepath/repotwo.git (push) two hosttwobackup.com:/somewheresafe/repotwo.git (push) Signed-off-by: Michael J Gruber Signed-off-by: Junio C Hamano --- builtin-remote.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/builtin-remote.c b/builtin-remote.c index 8dcc6a4bf9..3f6f5c2318 100644 --- a/builtin-remote.c +++ b/builtin-remote.c @@ -1279,14 +1279,31 @@ static int update(int argc, const char **argv) static int get_one_entry(struct remote *remote, void *priv) { struct string_list *list = priv; + const char **url; + int i, url_nr; + void **utilp; if (remote->url_nr > 0) { - int i; - - for (i = 0; i < remote->url_nr; i++) - string_list_append(remote->name, list)->util = (void *)remote->url[i]; + utilp = &(string_list_append(remote->name, list)->util); + *utilp = malloc(strlen(remote->url[0])+strlen(" (fetch)")+1); + strcpy((char *) *utilp, remote->url[0]); + strcat((char *) *utilp, " (fetch)"); } else string_list_append(remote->name, list)->util = NULL; + if (remote->pushurl_nr) { + url = remote->pushurl; + url_nr = remote->pushurl_nr; + } else { + url = remote->url; + url_nr = remote->url_nr; + } + for (i = 0; i < url_nr; i++) + { + utilp = &(string_list_append(remote->name, list)->util); + *utilp = malloc(strlen(url[i])+strlen(" (push)")+1); + strcpy((char *) *utilp, url[i]); + strcat((char *) *utilp, " (push)"); + } return 0; } @@ -1294,7 +1311,10 @@ static int get_one_entry(struct remote *remote, void *priv) static int show_all(void) { struct string_list list = { NULL, 0, 0 }; - int result = for_each_remote(get_one_entry, &list); + int result; + + list.strdup_strings = 1; + result = for_each_remote(get_one_entry, &list); if (!result) { int i; @@ -1312,6 +1332,7 @@ static int show_all(void) } } } + string_list_clear(&list, 1); return result; } From ce61595ea7eb3df22f6a943a38a273141f1af978 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Sun, 14 Jun 2009 21:46:10 +0200 Subject: [PATCH 6/6] avoid NULL dereference on failed malloc * builtin-remote.c (get_one_entry): Use xmalloc, not malloc. Signed-off-by: Jim Meyering Signed-off-by: Junio C Hamano --- builtin-remote.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin-remote.c b/builtin-remote.c index 3f6f5c2318..fd0e63c5dc 100644 --- a/builtin-remote.c +++ b/builtin-remote.c @@ -1285,7 +1285,7 @@ static int get_one_entry(struct remote *remote, void *priv) if (remote->url_nr > 0) { utilp = &(string_list_append(remote->name, list)->util); - *utilp = malloc(strlen(remote->url[0])+strlen(" (fetch)")+1); + *utilp = xmalloc(strlen(remote->url[0])+strlen(" (fetch)")+1); strcpy((char *) *utilp, remote->url[0]); strcat((char *) *utilp, " (fetch)"); } else @@ -1300,7 +1300,7 @@ static int get_one_entry(struct remote *remote, void *priv) for (i = 0; i < url_nr; i++) { utilp = &(string_list_append(remote->name, list)->util); - *utilp = malloc(strlen(url[i])+strlen(" (push)")+1); + *utilp = xmalloc(strlen(url[i])+strlen(" (push)")+1); strcpy((char *) *utilp, url[i]); strcat((char *) *utilp, " (push)"); }