{"thread":{"id":"39215","subject":"[PATCH v2 0/12] implement @{push} shorthand","startedAt":"2015-05-01T22:44:15Z","lastAt":"2015-05-08T16:13:50Z","messageCount":21,"participants":["Jeff King","Eric Sunshine"],"isPatch":true,"patchVersion":2,"patchTotal":12},"messages":[{"id":"260383","messageId":"20150501224414.GA25551@peff.net","threadId":"39215","inReplyTo":null,"subject":"[PATCH v2 0/12] implement @{push} shorthand","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-05-01T22:44:15Z","receivedAt":"2015-05-01T22:44:15Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"This is a re-roll of the series at:\n\n  http://thread.gmane.org/gmane.comp.version-control.git/266532\n\nThere were some minor fixes in response to review, but the main change\nhere is support for \"git for-each-ref --format=%(push)\". To do that, I\npulled the push logic into remote.[ch], where it can be used from both\nsha1_name.c and for-each-ref.c. This is a better place for it to reside,\nanyway, and may help in the future unifying it with the other remote\ncode that is used by `git push`.\n\nIn an effort to reuse as much of the @{upstream} code as possible, I did\nsimilar refactoring for that side; we now have branch_get_upstream().\nEven though the logic for getting @{upstream} isn't nearly as\ncomplicated as for @{push}, I think several call-sites are improved by\nusing the new helper.\n\n  [01/12]: remote.c: drop default_remote_name variable\n  [02/12]: remote.c: drop \"remote\" pointer from \"struct branch\"\n  [03/12]: remote.c: hoist branch.*.remote lookup out of remote_get_1\n  [04/12]: remote.c: provide per-branch pushremote name\n  [05/12]: remote.c: introduce branch_get_upstream helper\n  [06/12]: remote.c: report specific errors from branch_get_upstream\n  [07/12]: remote.c: add branch_get_push\n  [08/12]: sha1_name: refactor upstream_mark\n  [09/12]: sha1_name: refactor interpret_upstream_mark\n  [10/12]: sha1_name: implement @{push} shorthand\n  [11/12]: for-each-ref: use skip_prefix instead of starts_with\n  [12/12]: for-each-ref: accept \"%(push)\" format\n\n-Peff\n"},{"id":"260384","messageId":"20150501224441.GA1534@peff.net","threadId":"39215","inReplyTo":"20150501224414.GA25551@peff.net","subject":"[PATCH 01/12] remote.c: drop default_remote_name variable","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-05-01T22:44:41Z","receivedAt":"2015-05-01T22:44:41Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"When we read the remote config from disk, we update a\ndefault_remote_name variable if we see branch.*.remote\nconfig for the current branch. This isn't wrong, or even all\nthat complicated, but it is a bit simpler (because it\nreduces our overall state) to just lazily compute the\ndefault when we need it.\n\nThe ulterior motive here is that the push config uses a\nsimilar structure, and _is_ much more complicated as a\nresult. That will be simplified in a future patch, and it's\nmore readable if the logic for remotes and push-remotes\nmatches.\n\nNote that we also used default_remote_name as a signal that\nthe remote config has been loaded; after this patch, we now\nuse an explicit flag.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nSame as v1.\n\n remote.c | 23 +++++++++++------------\n 1 file changed, 11 insertions(+), 12 deletions(-)\n\ndiff --git a/remote.c b/remote.c\nindex 68901b0..bec8b31 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -49,10 +49,8 @@ static int branches_alloc;\n static int branches_nr;\n \n static struct branch *current_branch;\n-static const char *default_remote_name;\n static const char *branch_pushremote_name;\n static const char *pushremote_name;\n-static int explicit_default_remote_name;\n \n static struct rewrites rewrites;\n static struct rewrites rewrites_push;\n@@ -367,12 +365,7 @@ static int handle_config(const char *key, const char *value, void *cb)\n \t\t\treturn 0;\n \t\tbranch = make_branch(name, subkey - name);\n \t\tif (!strcmp(subkey, \".remote\")) {\n-\t\t\tif (git_config_string(&branch->remote_name, key, value))\n-\t\t\t\treturn -1;\n-\t\t\tif (branch == current_branch) {\n-\t\t\t\tdefault_remote_name = branch->remote_name;\n-\t\t\t\texplicit_default_remote_name = 1;\n-\t\t\t}\n+\t\t\treturn git_config_string(&branch->remote_name, key, value);\n \t\t} else if (!strcmp(subkey, \".pushremote\")) {\n \t\t\tif (branch == current_branch)\n \t\t\t\tif (git_config_string(&branch_pushremote_name, key, value))\n@@ -501,12 +494,15 @@ static void alias_all_urls(void)\n \n static void read_config(void)\n {\n+\tstatic int loaded;\n \tunsigned char sha1[20];\n \tconst char *head_ref;\n \tint flag;\n-\tif (default_remote_name) /* did this already */\n+\n+\tif (loaded)\n \t\treturn;\n-\tdefault_remote_name = \"origin\";\n+\tloaded = 1;\n+\n \tcurrent_branch = NULL;\n \thead_ref = resolve_ref_unsafe(\"HEAD\", 0, sha1, &flag);\n \tif (head_ref && (flag & REF_ISSYMREF) &&\n@@ -708,8 +704,11 @@ static struct remote *remote_get_1(const char *name, const char *pushremote_name\n \t\t\tname = pushremote_name;\n \t\t\tname_given = 1;\n \t\t} else {\n-\t\t\tname = default_remote_name;\n-\t\t\tname_given = explicit_default_remote_name;\n+\t\t\tif (current_branch && current_branch->remote_name) {\n+\t\t\t\tname = current_branch->remote_name;\n+\t\t\t\tname_given = 1;\n+\t\t\t} else\n+\t\t\t\tname = \"origin\";\n \t\t}\n \t}\n \n-- \n2.4.0.rc3.477.gc25258d\n"},{"id":"260385","messageId":"20150501224515.GB1534@peff.net","threadId":"39215","inReplyTo":"20150501224414.GA25551@peff.net","subject":"[PATCH 02/12] remote.c: drop \"remote\" pointer from \"struct branch\"","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-05-01T22:45:15Z","receivedAt":"2015-05-01T22:45:15Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"When we create each branch struct, we fill in the\n\"remote_name\" field from the config, and then fill in the\nactual \"remote\" field (with a \"struct remote\") based on that\nname. However, it turns out that nobody really cares about\nthe latter field. The only two sites that access it at all\nare:\n\n  1. git-merge, which uses it to notice when the branch does\n     not have a remote defined. But we can easily replace this\n     with looking at remote_name instead.\n\n  2. remote.c itself, when setting up the @{upstream} merge\n     config. But we don't need to save the \"remote\" in the\n     \"struct branch\" for that; we can just look it up for\n     the duration of the operation.\n\nSo there is no need to have both fields; they are redundant\nwith each other (the struct remote contains the name, or you\ncan look up the struct from the name). It would be nice to\nsimplify this, especially as we are going to add matching\npushremote config in a future patch (and it would be nice to\nkeep them consistent).\n\nSo which one do we keep and which one do we get rid of?\n\nIf we had a lot of callers accessing the struct, it would be\nmore efficient to keep it (since you have to do a lookup to\ngo from the name to the struct, but not vice versa). But we\ndon't have a lot of callers; we have exactly one, so\nefficiency doesn't matter. We can decide this based on\nsimplicity and readability.\n\nAnd the meaning of the struct value is somewhat unclear. Is\nit always the remote matching remote_name? If remote_name is\nNULL (i.e., no per-branch config), does the struct fall back\nto the \"origin\" remote, or is it also NULL? These questions\nwill get even more tricky with pushremotes, whose fallback\nbehavior is more complicated. So let's just store the name,\nwhich pretty clearly represents the branch.*.remote config.\nAny lookup or fallback behavior can then be implemented in\nhelper functions.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nVersus v1, I tried to explain the rationale a bit better in the commit\nmessage. The code is the same.\n\n builtin/merge.c |  2 +-\n remote.c        | 14 ++++++++------\n remote.h        |  1 -\n 3 files changed, 9 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/merge.c b/builtin/merge.c\nindex 3b0f8f9..1840317 100644\n--- a/builtin/merge.c\n+++ b/builtin/merge.c\n@@ -955,7 +955,7 @@ static int setup_with_upstream(const char ***argv)\n \n \tif (!branch)\n \t\tdie(_(\"No current branch.\"));\n-\tif (!branch->remote)\n+\tif (!branch->remote_name)\n \t\tdie(_(\"No remote for the current branch.\"));\n \tif (!branch->merge_nr)\n \t\tdie(_(\"No default upstream defined for the current branch.\"));\ndiff --git a/remote.c b/remote.c\nindex bec8b31..c85ecef 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1632,15 +1632,20 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,\n \n static void set_merge(struct branch *ret)\n {\n+\tstruct remote *remote;\n \tchar *ref;\n \tunsigned char sha1[20];\n \tint i;\n \n+\tif (!ret->remote_name || !ret->merge_nr)\n+\t\treturn;\n+\tremote = remote_get(ret->remote_name);\n+\n \tret->merge = xcalloc(ret->merge_nr, sizeof(*ret->merge));\n \tfor (i = 0; i < ret->merge_nr; i++) {\n \t\tret->merge[i] = xcalloc(1, sizeof(**ret->merge));\n \t\tret->merge[i]->src = xstrdup(ret->merge_name[i]);\n-\t\tif (!remote_find_tracking(ret->remote, ret->merge[i]) ||\n+\t\tif (!remote_find_tracking(remote, ret->merge[i]) ||\n \t\t    strcmp(ret->remote_name, \".\"))\n \t\t\tcontinue;\n \t\tif (dwim_ref(ret->merge_name[i], strlen(ret->merge_name[i]),\n@@ -1660,11 +1665,8 @@ struct branch *branch_get(const char *name)\n \t\tret = current_branch;\n \telse\n \t\tret = make_branch(name, 0);\n-\tif (ret && ret->remote_name) {\n-\t\tret->remote = remote_get(ret->remote_name);\n-\t\tif (ret->merge_nr)\n-\t\t\tset_merge(ret);\n-\t}\n+\tif (ret)\n+\t       set_merge(ret);\n \treturn ret;\n }\n \ndiff --git a/remote.h b/remote.h\nindex 02d66ce..4bb6672 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -203,7 +203,6 @@ struct branch {\n \tconst char *refname;\n \n \tconst char *remote_name;\n-\tstruct remote *remote;\n \n \tconst char **merge_name;\n \tstruct refspec **merge;\n-- \n2.4.0.rc3.477.gc25258d\n"},{"id":"260386","messageId":"20150501224544.GC1534@peff.net","threadId":"39215","inReplyTo":"20150501224414.GA25551@peff.net","subject":"[PATCH 03/12] remote.c: hoist branch.*.remote lookup out of remote_get_1","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-05-01T22:45:44Z","receivedAt":"2015-05-01T22:45:44Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"We'll want to use this logic as a fallback when looking up\nthe pushremote, so let's pull it out into its own function.\n\nWe don't technically need to make this available outside of\nremote.c, but doing so will provide a consistent API with\npushremote_for_branch, which we will add later.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nSame as v1.\n\n remote.c | 21 ++++++++++++++-------\n remote.h |  1 +\n 2 files changed, 15 insertions(+), 7 deletions(-)\n\ndiff --git a/remote.c b/remote.c\nindex c85ecef..a27f795 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -692,6 +692,18 @@ static int valid_remote_nick(const char *name)\n \treturn !strchr(name, '/'); /* no slash */\n }\n \n+const char *remote_for_branch(struct branch *branch, int *explicit)\n+{\n+\tif (branch && branch->remote_name) {\n+\t\tif (explicit)\n+\t\t\t*explicit = 1;\n+\t\treturn branch->remote_name;\n+\t}\n+\tif (explicit)\n+\t\t*explicit = 0;\n+\treturn \"origin\";\n+}\n+\n static struct remote *remote_get_1(const char *name, const char *pushremote_name)\n {\n \tstruct remote *ret;\n@@ -703,13 +715,8 @@ static struct remote *remote_get_1(const char *name, const char *pushremote_name\n \t\tif (pushremote_name) {\n \t\t\tname = pushremote_name;\n \t\t\tname_given = 1;\n-\t\t} else {\n-\t\t\tif (current_branch && current_branch->remote_name) {\n-\t\t\t\tname = current_branch->remote_name;\n-\t\t\t\tname_given = 1;\n-\t\t\t} else\n-\t\t\t\tname = \"origin\";\n-\t\t}\n+\t\t} else\n+\t\t\tname = remote_for_branch(current_branch, &name_given);\n \t}\n \n \tret = make_remote(name, 0);\ndiff --git a/remote.h b/remote.h\nindex 4bb6672..2a7e7a6 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -211,6 +211,7 @@ struct branch {\n };\n \n struct branch *branch_get(const char *name);\n+const char *remote_for_branch(struct branch *branch, int *explicit);\n \n int branch_has_merge_config(struct branch *branch);\n int branch_merge_matches(struct branch *, int n, const char *);\n-- \n2.4.0.rc3.477.gc25258d\n"},{"id":"260387","messageId":"20150501224644.GD1534@peff.net","threadId":"39215","inReplyTo":"20150501224414.GA25551@peff.net","subject":"[PATCH 04/12] remote.c: provide per-branch pushremote name","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-05-01T22:46:44Z","receivedAt":"2015-05-01T22:46:44Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"When remote.c loads its config, it records the\nbranch.*.pushremote for the current branch along with the\nglobal remote.pushDefault value, and then binds them into a\nsingle value: the default push for the current branch. We\nthen pass this value (which may be NULL) to remote_get_1\nwhen looking up a remote for push.\n\nThis has a few downsides:\n\n  1. It's confusing. The early-binding of the \"current\n     value\" led to bugs like the one fixed by 98b406f\n     (remote: handle pushremote config in any order,\n     2014-02-24). And the fact that pushremotes fall back to\n     ordinary remotes is not explicit at all; it happens\n     because remote_get_1 cannot tell the difference between\n     \"we are not asking for the push remote\" and \"there is\n     no push remote configured\".\n\n  2. It throws away intermediate data. After read_config()\n     finishes, we have no idea what the value of\n     remote.pushDefault was, because the string has been\n     overwritten by the current branch's\n     branch.*.pushremote.\n\n  3. It doesn't record other data. We don't note the\n     branch.*.pushremote value for anything but the current\n     branch.\n\nLet's make this more like the fetch-remote config. We'll\nrecord the pushremote for each branch, and then explicitly\ncompute the correct remote for the current branch at the\ntime of reading.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nVersus v1, I did something a little clever by passing a function pointer\naround (versus a flag and letting the caller do a conditional based on\nthe flag). Too clever?\n\n remote.c | 40 ++++++++++++++++++++++------------------\n remote.h |  2 ++\n 2 files changed, 24 insertions(+), 18 deletions(-)\n\ndiff --git a/remote.c b/remote.c\nindex a27f795..9f84ea3 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -49,7 +49,6 @@ static int branches_alloc;\n static int branches_nr;\n \n static struct branch *current_branch;\n-static const char *branch_pushremote_name;\n static const char *pushremote_name;\n \n static struct rewrites rewrites;\n@@ -367,9 +366,7 @@ static int handle_config(const char *key, const char *value, void *cb)\n \t\tif (!strcmp(subkey, \".remote\")) {\n \t\t\treturn git_config_string(&branch->remote_name, key, value);\n \t\t} else if (!strcmp(subkey, \".pushremote\")) {\n-\t\t\tif (branch == current_branch)\n-\t\t\t\tif (git_config_string(&branch_pushremote_name, key, value))\n-\t\t\t\t\treturn -1;\n+\t\t\treturn git_config_string(&branch->pushremote_name, key, value);\n \t\t} else if (!strcmp(subkey, \".merge\")) {\n \t\t\tif (!value)\n \t\t\t\treturn config_error_nonbool(key);\n@@ -510,10 +507,6 @@ static void read_config(void)\n \t\tcurrent_branch = make_branch(head_ref, 0);\n \t}\n \tgit_config(handle_config, NULL);\n-\tif (branch_pushremote_name) {\n-\t\tfree((char *)pushremote_name);\n-\t\tpushremote_name = branch_pushremote_name;\n-\t}\n \talias_all_urls();\n }\n \n@@ -704,20 +697,31 @@ const char *remote_for_branch(struct branch *branch, int *explicit)\n \treturn \"origin\";\n }\n \n-static struct remote *remote_get_1(const char *name, const char *pushremote_name)\n+const char *pushremote_for_branch(struct branch *branch, int *explicit)\n+{\n+\tif (branch && branch->pushremote_name) {\n+\t\tif (explicit)\n+\t\t\t*explicit = 1;\n+\t\treturn branch->pushremote_name;\n+\t}\n+\tif (pushremote_name) {\n+\t\tif (explicit)\n+\t\t\t*explicit = 1;\n+\t\treturn pushremote_name;\n+\t}\n+\treturn remote_for_branch(branch, explicit);\n+}\n+\n+static struct remote *remote_get_1(const char *name,\n+\t\t\t\t   const char *(*get_default)(struct branch *, int *))\n {\n \tstruct remote *ret;\n \tint name_given = 0;\n \n \tif (name)\n \t\tname_given = 1;\n-\telse {\n-\t\tif (pushremote_name) {\n-\t\t\tname = pushremote_name;\n-\t\t\tname_given = 1;\n-\t\t} else\n-\t\t\tname = remote_for_branch(current_branch, &name_given);\n-\t}\n+\telse\n+\t\tname = get_default(current_branch, &name_given);\n \n \tret = make_remote(name, 0);\n \tif (valid_remote_nick(name)) {\n@@ -738,13 +742,13 @@ static struct remote *remote_get_1(const char *name, const char *pushremote_name\n struct remote *remote_get(const char *name)\n {\n \tread_config();\n-\treturn remote_get_1(name, NULL);\n+\treturn remote_get_1(name, remote_for_branch);\n }\n \n struct remote *pushremote_get(const char *name)\n {\n \tread_config();\n-\treturn remote_get_1(name, pushremote_name);\n+\treturn remote_get_1(name, pushremote_for_branch);\n }\n \n int remote_is_configured(const char *name)\ndiff --git a/remote.h b/remote.h\nindex 2a7e7a6..30a11da 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -203,6 +203,7 @@ struct branch {\n \tconst char *refname;\n \n \tconst char *remote_name;\n+\tconst char *pushremote_name;\n \n \tconst char **merge_name;\n \tstruct refspec **merge;\n@@ -212,6 +213,7 @@ struct branch {\n \n struct branch *branch_get(const char *name);\n const char *remote_for_branch(struct branch *branch, int *explicit);\n+const char *pushremote_for_branch(struct branch *branch, int *explicit);\n \n int branch_has_merge_config(struct branch *branch);\n int branch_merge_matches(struct branch *, int n, const char *);\n-- \n2.4.0.rc3.477.gc25258d\n"},{"id":"260388","messageId":"20150501224708.GE1534@peff.net","threadId":"39215","inReplyTo":"20150501224414.GA25551@peff.net","subject":"[PATCH 05/12] remote.c: introduce branch_get_upstream helper","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-05-01T22:47:09Z","receivedAt":"2015-05-01T22:47:09Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"All of the information needed to find the @{upstream} of a\nbranch is included in the branch struct, but callers have to\nnavigate a series of possible-NULL values to get there.\nLet's wrap that logic up in an easy-to-read helper.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nNew in v2.\n\n builtin/branch.c       |  8 +++-----\n builtin/for-each-ref.c |  5 ++---\n builtin/log.c          |  7 ++-----\n remote.c               | 12 +++++++++---\n remote.h               |  7 +++++++\n 5 files changed, 23 insertions(+), 16 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 1d15037..bd1fa0b 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -123,14 +123,12 @@ static int branch_merged(int kind, const char *name,\n \n \tif (kind == REF_LOCAL_BRANCH) {\n \t\tstruct branch *branch = branch_get(name);\n+\t\tconst char *upstream = branch_get_upstream(branch);\n \t\tunsigned char sha1[20];\n \n-\t\tif (branch &&\n-\t\t    branch->merge &&\n-\t\t    branch->merge[0] &&\n-\t\t    branch->merge[0]->dst &&\n+\t\tif (upstream &&\n \t\t    (reference_name = reference_name_to_free =\n-\t\t     resolve_refdup(branch->merge[0]->dst, RESOLVE_REF_READING,\n+\t\t     resolve_refdup(upstream, RESOLVE_REF_READING,\n \t\t\t\t    sha1, NULL)) != NULL)\n \t\t\treference_rev = lookup_commit_reference(sha1);\n \t}\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex 83f9cf9..dc2a201 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -664,10 +664,9 @@ static void populate_value(struct refinfo *ref)\n \t\t\t\tcontinue;\n \t\t\tbranch = branch_get(ref->refname + 11);\n \n-\t\t\tif (!branch || !branch->merge || !branch->merge[0] ||\n-\t\t\t    !branch->merge[0]->dst)\n+\t\t\trefname = branch_get_upstream(branch);\n+\t\t\tif (!refname)\n \t\t\t\tcontinue;\n-\t\t\trefname = branch->merge[0]->dst;\n \t\t} else if (starts_with(name, \"color:\")) {\n \t\t\tchar color[COLOR_MAXLEN] = \"\";\n \ndiff --git a/builtin/log.c b/builtin/log.c\nindex dd8f3fc..fb61c08 100644\n--- a/builtin/log.c\n+++ b/builtin/log.c\n@@ -1632,16 +1632,13 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)\n \t\tbreak;\n \tdefault:\n \t\tcurrent_branch = branch_get(NULL);\n-\t\tif (!current_branch || !current_branch->merge\n-\t\t\t\t\t|| !current_branch->merge[0]\n-\t\t\t\t\t|| !current_branch->merge[0]->dst) {\n+\t\tupstream = branch_get_upstream(current_branch);\n+\t\tif (!upstream) {\n \t\t\tfprintf(stderr, _(\"Could not find a tracked\"\n \t\t\t\t\t\" remote branch, please\"\n \t\t\t\t\t\" specify <upstream> manually.\\n\"));\n \t\t\tusage_with_options(cherry_usage, options);\n \t\t}\n-\n-\t\tupstream = current_branch->merge[0]->dst;\n \t}\n \n \tinit_revisions(&revs, prefix);\ndiff --git a/remote.c b/remote.c\nindex 9f84ea3..c826fad 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1695,6 +1695,13 @@ int branch_merge_matches(struct branch *branch,\n \treturn refname_match(branch->merge[i]->src, refname);\n }\n \n+const char *branch_get_upstream(struct branch *branch)\n+{\n+\tif (!branch || !branch->merge || !branch->merge[0])\n+\t\treturn NULL;\n+\treturn branch->merge[0]->dst;\n+}\n+\n static int ignore_symref_update(const char *refname)\n {\n \tunsigned char sha1[20];\n@@ -1904,12 +1911,11 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)\n \tint rev_argc;\n \n \t/* Cannot stat unless we are marked to build on top of somebody else. */\n-\tif (!branch ||\n-\t    !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)\n+\tbase = branch_get_upstream(branch);\n+\tif (!base)\n \t\treturn 0;\n \n \t/* Cannot stat if what we used to build on no longer exists */\n-\tbase = branch->merge[0]->dst;\n \tif (read_ref(base, sha1))\n \t\treturn -1;\n \ttheirs = lookup_commit_reference(sha1);\ndiff --git a/remote.h b/remote.h\nindex 30a11da..d968952 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -218,6 +218,13 @@ const char *pushremote_for_branch(struct branch *branch, int *explicit);\n int branch_has_merge_config(struct branch *branch);\n int branch_merge_matches(struct branch *, int n, const char *);\n \n+/**\n+ * Return the fully-qualified refname of the tracking branch for `branch`.\n+ * I.e., what \"branch@{upstream}\" would give you. Returns NULL if no\n+ * upstream is defined.\n+ */\n+const char *branch_get_upstream(struct branch *branch);\n+\n /* Flags to match_refs. */\n enum match_refs_flags {\n \tMATCH_REFS_NONE\t\t= 0,\n-- \n2.4.0.rc3.477.gc25258d\n"},{"id":"260389","messageId":"20150501225242.GF1534@peff.net","threadId":"39215","inReplyTo":"20150501224414.GA25551@peff.net","subject":"[PATCH 06/12] remote.c: report specific errors from branch_get_upstream","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-05-01T22:52:42Z","receivedAt":"2015-05-01T22:52:42Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"When the previous commit introduced the branch_get_upstream\nhelper, there was one call-site that could not be converted:\nthe one in sha1_name.c, which gives detailed error messages\nfor each possible failure.\n\nLet's teach the helper to optionally report these specific\nerrors. This lets us convert another callsite, and means we\ncan use the helper in other locations that want to give the\nsame error messages.\n\nThe logic and error messages come straight from sha1_name.c,\nwith the exception that we start each error with a lowercase\nletter, as is our usual style (note that a few tests need\nupdated as a result).\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nSo this uses the recently discussed \"pass the error back in a strbuf\"\ntechnique. We don't need an integer flag here because nobody cares about\nthe reason; only whether to die and report it (so a \"_gently\" form would\nalso work).\n\nThis adds an extra NULL parameter to every call-site of\nbranch_get_upstream (in the _gently paradigm, we'd pass an extra \"0\"\nflag). I waffled on just leaving that as-is, and implementing it as a\nwrapper to a new branch_get_upstream_err.\n\nOverall, I think it makes the flow work pretty well, and the allocation\nissues are non-existent because the callers all either want to die with\nthe error or ignore it. As an aside, while thinking about this I\nrecalled that one of the complaints against using a statically-sized\nbuffer for error messages is that we didn't want to truncate them. But\nnote that calling die() will end up in vreportf, which will itself\ntruncate.\n\n builtin/branch.c              |  2 +-\n builtin/for-each-ref.c        |  2 +-\n builtin/log.c                 |  2 +-\n remote.c                      | 33 +++++++++++++++++++++++++++++----\n remote.h                      |  6 +++++-\n sha1_name.c                   | 25 +++++++------------------\n t/t1507-rev-parse-upstream.sh |  8 ++++----\n 7 files changed, 48 insertions(+), 30 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex bd1fa0b..b7202b3 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -123,7 +123,7 @@ static int branch_merged(int kind, const char *name,\n \n \tif (kind == REF_LOCAL_BRANCH) {\n \t\tstruct branch *branch = branch_get(name);\n-\t\tconst char *upstream = branch_get_upstream(branch);\n+\t\tconst char *upstream = branch_get_upstream(branch, NULL);\n \t\tunsigned char sha1[20];\n \n \t\tif (upstream &&\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex dc2a201..18d209b 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -664,7 +664,7 @@ static void populate_value(struct refinfo *ref)\n \t\t\t\tcontinue;\n \t\t\tbranch = branch_get(ref->refname + 11);\n \n-\t\t\trefname = branch_get_upstream(branch);\n+\t\t\trefname = branch_get_upstream(branch, NULL);\n \t\t\tif (!refname)\n \t\t\t\tcontinue;\n \t\t} else if (starts_with(name, \"color:\")) {\ndiff --git a/builtin/log.c b/builtin/log.c\nindex fb61c08..6faeb82 100644\n--- a/builtin/log.c\n+++ b/builtin/log.c\n@@ -1632,7 +1632,7 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)\n \t\tbreak;\n \tdefault:\n \t\tcurrent_branch = branch_get(NULL);\n-\t\tupstream = branch_get_upstream(current_branch);\n+\t\tupstream = branch_get_upstream(current_branch, NULL);\n \t\tif (!upstream) {\n \t\t\tfprintf(stderr, _(\"Could not find a tracked\"\n \t\t\t\t\t\" remote branch, please\"\ndiff --git a/remote.c b/remote.c\nindex c826fad..146b94d 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1695,10 +1695,35 @@ int branch_merge_matches(struct branch *branch,\n \treturn refname_match(branch->merge[i]->src, refname);\n }\n \n-const char *branch_get_upstream(struct branch *branch)\n+__attribute((format (printf,2,3)))\n+static const char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\n-\tif (!branch || !branch->merge || !branch->merge[0])\n-\t\treturn NULL;\n+\tif (err) {\n+\t\tva_list ap;\n+\t\tva_start(ap, fmt);\n+\t\tstrbuf_vaddf(err, fmt, ap);\n+\t\tva_end(ap);\n+\t}\n+\treturn NULL;\n+}\n+\n+const char *branch_get_upstream(struct branch *branch, struct strbuf *err)\n+{\n+\tif (!branch)\n+\t\treturn error_buf(err, _(\"HEAD does not point to a branch\"));\n+\tif (!branch->merge || !branch->merge[0] || !branch->merge[0]->dst) {\n+\t\tif (!ref_exists(branch->refname))\n+\t\t\treturn error_buf(err, _(\"no such branch: '%s'\"),\n+\t\t\t\t\t branch->name);\n+\t\tif (!branch->merge)\n+\t\t\treturn error_buf(err,\n+\t\t\t\t\t _(\"no upstream configured for branch '%s'\"),\n+\t\t\t\t\t branch->name);\n+\t\treturn error_buf(err,\n+\t\t\t\t _(\"upstream branch '%s' not stored as a remote-tracking branch\"),\n+\t\t\t\t branch->merge[0]->src);\n+\t}\n+\n \treturn branch->merge[0]->dst;\n }\n \n@@ -1911,7 +1936,7 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)\n \tint rev_argc;\n \n \t/* Cannot stat unless we are marked to build on top of somebody else. */\n-\tbase = branch_get_upstream(branch);\n+\tbase = branch_get_upstream(branch, NULL);\n \tif (!base)\n \t\treturn 0;\n \ndiff --git a/remote.h b/remote.h\nindex d968952..03ca005 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -222,8 +222,12 @@ int branch_merge_matches(struct branch *, int n, const char *);\n  * Return the fully-qualified refname of the tracking branch for `branch`.\n  * I.e., what \"branch@{upstream}\" would give you. Returns NULL if no\n  * upstream is defined.\n+ *\n+ * If `err` is not NULL and no upstream is defined, a more specific error\n+ * message is recorded there (if the function does not return NULL, then\n+ * `err` is not touched).\n  */\n-const char *branch_get_upstream(struct branch *branch);\n+const char *branch_get_upstream(struct branch *branch, struct strbuf *err);\n \n /* Flags to match_refs. */\n enum match_refs_flags {\ndiff --git a/sha1_name.c b/sha1_name.c\nindex 6d10f05..461157a 100644\n--- a/sha1_name.c\n+++ b/sha1_name.c\n@@ -1059,27 +1059,16 @@ static const char *get_upstream_branch(const char *branch_buf, int len)\n {\n \tchar *branch = xstrndup(branch_buf, len);\n \tstruct branch *upstream = branch_get(*branch ? branch : NULL);\n+\tstruct strbuf err = STRBUF_INIT;\n+\tconst char *ret;\n \n-\t/*\n-\t * Upstream can be NULL only if branch refers to HEAD and HEAD\n-\t * points to something different than a branch.\n-\t */\n-\tif (!upstream)\n-\t\tdie(_(\"HEAD does not point to a branch\"));\n-\tif (!upstream->merge || !upstream->merge[0]->dst) {\n-\t\tif (!ref_exists(upstream->refname))\n-\t\t\tdie(_(\"No such branch: '%s'\"), branch);\n-\t\tif (!upstream->merge) {\n-\t\t\tdie(_(\"No upstream configured for branch '%s'\"),\n-\t\t\t\tupstream->name);\n-\t\t}\n-\t\tdie(\n-\t\t\t_(\"Upstream branch '%s' not stored as a remote-tracking branch\"),\n-\t\t\tupstream->merge[0]->src);\n-\t}\n \tfree(branch);\n \n-\treturn upstream->merge[0]->dst;\n+\tret = branch_get_upstream(upstream, &err);\n+\tif (!ret)\n+\t\tdie(\"%s\", err.buf);\n+\n+\treturn ret;\n }\n \n static int interpret_upstream_mark(const char *name, int namelen,\ndiff --git a/t/t1507-rev-parse-upstream.sh b/t/t1507-rev-parse-upstream.sh\nindex 1978947..46ef1f2 100755\n--- a/t/t1507-rev-parse-upstream.sh\n+++ b/t/t1507-rev-parse-upstream.sh\n@@ -150,7 +150,7 @@ test_expect_success 'branch@{u} works when tracking a local branch' '\n \n test_expect_success 'branch@{u} error message when no upstream' '\n \tcat >expect <<-EOF &&\n-\tfatal: No upstream configured for branch ${sq}non-tracking${sq}\n+\tfatal: no upstream configured for branch ${sq}non-tracking${sq}\n \tEOF\n \terror_message non-tracking@{u} 2>actual &&\n \ttest_i18ncmp expect actual\n@@ -158,7 +158,7 @@ test_expect_success 'branch@{u} error message when no upstream' '\n \n test_expect_success '@{u} error message when no upstream' '\n \tcat >expect <<-EOF &&\n-\tfatal: No upstream configured for branch ${sq}master${sq}\n+\tfatal: no upstream configured for branch ${sq}master${sq}\n \tEOF\n \ttest_must_fail git rev-parse --verify @{u} 2>actual &&\n \ttest_i18ncmp expect actual\n@@ -166,7 +166,7 @@ test_expect_success '@{u} error message when no upstream' '\n \n test_expect_success 'branch@{u} error message with misspelt branch' '\n \tcat >expect <<-EOF &&\n-\tfatal: No such branch: ${sq}no-such-branch${sq}\n+\tfatal: no such branch: ${sq}no-such-branch${sq}\n \tEOF\n \terror_message no-such-branch@{u} 2>actual &&\n \ttest_i18ncmp expect actual\n@@ -183,7 +183,7 @@ test_expect_success '@{u} error message when not on a branch' '\n \n test_expect_success 'branch@{u} error message if upstream branch not fetched' '\n \tcat >expect <<-EOF &&\n-\tfatal: Upstream branch ${sq}refs/heads/side${sq} not stored as a remote-tracking branch\n+\tfatal: upstream branch ${sq}refs/heads/side${sq} not stored as a remote-tracking branch\n \tEOF\n \terror_message bad-upstream@{u} 2>actual &&\n \ttest_i18ncmp expect actual\n-- \n2.4.0.rc3.477.gc25258d\n"},{"id":"260390","messageId":"20150501225331.GG1534@peff.net","threadId":"39215","inReplyTo":"20150501224414.GA25551@peff.net","subject":"[PATCH 07/12] remote.c: add branch_get_push","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-05-01T22:53:31Z","receivedAt":"2015-05-01T22:53:31Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"In a triangular workflow, the place you pull from and the\nplace you push to may be different. As we have\nbranch_get_upstream for the former, this patch adds\nbranch_get_push for the latter (and as the former implements\n@{upstream}, so will this implement @{push} in a future\npatch).\n\nNote that the memory-handling for the return value bears\nsome explanation. Some code paths require allocating a new\nstring, and some let us return an existing string. We should\nprovide a consistent interface to the caller, so it knows\nwhether to free the result or not.\n\nWe could do so by xstrdup-ing any existing strings, and\nhaving the caller always free. But that makes us\ninconsistent with branch_get_upstream, so we would prefer to\nsimply take ownership of the resulting string. We do so by\nstoring it inside the \"struct branch\", just as we do with\nthe upstream refname (in that case we compute it when the\nbranch is created, but there's no reason not to just fill\nit in lazily in this case).\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nThis patch is new in v2, but the logic is basically ripped from\nsha1_name.c in v1.\n\n remote.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n remote.h | 10 ++++++++\n 2 files changed, 95 insertions(+)\n\ndiff --git a/remote.c b/remote.c\nindex 146b94d..7f0fe28 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1727,6 +1727,91 @@ const char *branch_get_upstream(struct branch *branch, struct strbuf *err)\n \treturn branch->merge[0]->dst;\n }\n \n+static const char *tracking_for_push_dest(struct remote *remote,\n+\t\t\t\t\t  const char *refname,\n+\t\t\t\t\t  struct strbuf *err)\n+{\n+\tchar *ret;\n+\n+\tret = apply_refspecs(remote->fetch, remote->fetch_refspec_nr, refname);\n+\tif (!ret)\n+\t\treturn error_buf(err,\n+\t\t\t\t _(\"push destination '%s' on remote '%s' has no local tracking branch\"),\n+\t\t\t\t refname, remote->name);\n+\treturn ret;\n+}\n+\n+static const char *branch_get_push_1(struct branch *branch, struct strbuf *err)\n+{\n+\tstruct remote *remote;\n+\n+\tif (!branch)\n+\t\treturn error_buf(err, _(\"HEAD does not point to a branch\"));\n+\n+\tremote = remote_get(pushremote_for_branch(branch, NULL));\n+\tif (!remote)\n+\t\treturn error_buf(err,\n+\t\t\t\t _(\"branch '%s' has no remote for pushing\"),\n+\t\t\t\t branch->name);\n+\n+\tif (remote->push_refspec_nr) {\n+\t\tchar *dst;\n+\t\tconst char *ret;\n+\n+\t\tdst = apply_refspecs(remote->push, remote->push_refspec_nr,\n+\t\t\t\t     branch->refname);\n+\t\tif (!dst)\n+\t\t\treturn error_buf(err,\n+\t\t\t\t\t _(\"push refspecs for '%s' do not include '%s'\"),\n+\t\t\t\t\t remote->name, branch->name);\n+\n+\t\tret = tracking_for_push_dest(remote, dst, err);\n+\t\tfree(dst);\n+\t\treturn ret;\n+\t}\n+\n+\tif (remote->mirror)\n+\t\treturn tracking_for_push_dest(remote, branch->refname, err);\n+\n+\tswitch (push_default) {\n+\tcase PUSH_DEFAULT_NOTHING:\n+\t\treturn error_buf(err, _(\"push has no destination (push.default is 'nothing')\"));\n+\n+\tcase PUSH_DEFAULT_MATCHING:\n+\tcase PUSH_DEFAULT_CURRENT:\n+\t\treturn tracking_for_push_dest(remote, branch->refname, err);\n+\n+\tcase PUSH_DEFAULT_UPSTREAM:\n+\t\treturn branch_get_upstream(branch, err);\n+\n+\tcase PUSH_DEFAULT_UNSPECIFIED:\n+\tcase PUSH_DEFAULT_SIMPLE:\n+\t\t{\n+\t\t\tconst char *up, *cur;\n+\n+\t\t\tup = branch_get_upstream(branch, err);\n+\t\t\tif (!up)\n+\t\t\t\treturn NULL;\n+\t\t\tcur = tracking_for_push_dest(remote, branch->refname, err);\n+\t\t\tif (!cur)\n+\t\t\t\treturn NULL;\n+\t\t\tif (strcmp(cur, up))\n+\t\t\t\treturn error_buf(err,\n+\t\t\t\t\t\t _(\"cannot resolve 'simple' push to a single destination\"));\n+\t\t\treturn cur;\n+\t\t}\n+\t}\n+\n+\tdie(\"BUG: unhandled push situation\");\n+}\n+\n+const char *branch_get_push(struct branch *branch, struct strbuf *err)\n+{\n+\tif (!branch->push_tracking_ref)\n+\t\tbranch->push_tracking_ref = branch_get_push_1(branch, err);\n+\treturn branch->push_tracking_ref;\n+}\n+\n static int ignore_symref_update(const char *refname)\n {\n \tunsigned char sha1[20];\ndiff --git a/remote.h b/remote.h\nindex 03ca005..3326f2b 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -209,6 +209,8 @@ struct branch {\n \tstruct refspec **merge;\n \tint merge_nr;\n \tint merge_alloc;\n+\n+\tconst char *push_tracking_ref;\n };\n \n struct branch *branch_get(const char *name);\n@@ -229,6 +231,14 @@ int branch_merge_matches(struct branch *, int n, const char *);\n  */\n const char *branch_get_upstream(struct branch *branch, struct strbuf *err);\n \n+/**\n+ * Return the tracking branch that corresponds to the ref we would push to\n+ * given a bare `git push` while `branch` is checked out.\n+ *\n+ * The return value and `err` conventions match those of `branch_get_upstream`.\n+ */\n+const char *branch_get_push(struct branch *branch, struct strbuf *err);\n+\n /* Flags to match_refs. */\n enum match_refs_flags {\n \tMATCH_REFS_NONE\t\t= 0,\n-- \n2.4.0.rc3.477.gc25258d\n"},{"id":"260391","messageId":"20150501225341.GH1534@peff.net","threadId":"39215","inReplyTo":"20150501224414.GA25551@peff.net","subject":"[PATCH 08/12] sha1_name: refactor upstream_mark","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-05-01T22:53:41Z","receivedAt":"2015-05-01T22:53:41Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"We will be adding new mark types in the future, so separate\nthe suffix data from the logic.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nSame as v1.\n\n sha1_name.c | 12 +++++++++---\n 1 file changed, 9 insertions(+), 3 deletions(-)\n\ndiff --git a/sha1_name.c b/sha1_name.c\nindex 461157a..1005f45 100644\n--- a/sha1_name.c\n+++ b/sha1_name.c\n@@ -415,12 +415,12 @@ static int ambiguous_path(const char *path, int len)\n \treturn slash;\n }\n \n-static inline int upstream_mark(const char *string, int len)\n+static inline int at_mark(const char *string, int len,\n+\t\t\t  const char **suffix, int nr)\n {\n-\tconst char *suffix[] = { \"@{upstream}\", \"@{u}\" };\n \tint i;\n \n-\tfor (i = 0; i < ARRAY_SIZE(suffix); i++) {\n+\tfor (i = 0; i < nr; i++) {\n \t\tint suffix_len = strlen(suffix[i]);\n \t\tif (suffix_len <= len\n \t\t    && !memcmp(string, suffix[i], suffix_len))\n@@ -429,6 +429,12 @@ static inline int upstream_mark(const char *string, int len)\n \treturn 0;\n }\n \n+static inline int upstream_mark(const char *string, int len)\n+{\n+\tconst char *suffix[] = { \"@{upstream}\", \"@{u}\" };\n+\treturn at_mark(string, len, suffix, ARRAY_SIZE(suffix));\n+}\n+\n static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags);\n static int interpret_nth_prior_checkout(const char *name, int namelen, struct strbuf *buf);\n \n-- \n2.4.0.rc3.477.gc25258d\n"},{"id":"260392","messageId":"20150501225501.GI1534@peff.net","threadId":"39215","inReplyTo":"20150501224414.GA25551@peff.net","subject":"[PATCH 09/12] sha1_name: refactor interpret_upstream_mark","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-05-01T22:55:01Z","receivedAt":"2015-05-01T22:55:01Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"Now that most of the logic for our local get_upstream_branch\nhas been pushed into the generic branch_get_upstream, we can\nfold the remainder into interpret_upstream_mark.\n\nFurthermore, what remains is generic to any branch-related\n\"@{foo}\" we might add in the future, and there's enough\nboilerplate that we'd like to reuse it. Let's parameterize\nthe two operations (parsing the mark and computing its\nvalue) so that we can reuse this for \"@{push}\" in the near\nfuture.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nMore function pointer cleverness. Here it seems more justified to me,\nbecause we may eventually grow the list of interpret_branch_mark()\nusers (e.g., for the concept discussed previously as @{publish}).\n\n sha1_name.c | 44 +++++++++++++++++++++++---------------------\n 1 file changed, 23 insertions(+), 21 deletions(-)\n\ndiff --git a/sha1_name.c b/sha1_name.c\nindex 1005f45..506e0c9 100644\n--- a/sha1_name.c\n+++ b/sha1_name.c\n@@ -1061,35 +1061,36 @@ static void set_shortened_ref(struct strbuf *buf, const char *ref)\n \tfree(s);\n }\n \n-static const char *get_upstream_branch(const char *branch_buf, int len)\n-{\n-\tchar *branch = xstrndup(branch_buf, len);\n-\tstruct branch *upstream = branch_get(*branch ? branch : NULL);\n-\tstruct strbuf err = STRBUF_INIT;\n-\tconst char *ret;\n-\n-\tfree(branch);\n-\n-\tret = branch_get_upstream(upstream, &err);\n-\tif (!ret)\n-\t\tdie(\"%s\", err.buf);\n-\n-\treturn ret;\n-}\n-\n-static int interpret_upstream_mark(const char *name, int namelen,\n-\t\t\t\t   int at, struct strbuf *buf)\n+static int interpret_branch_mark(const char *name, int namelen,\n+\t\t\t\t int at, struct strbuf *buf,\n+\t\t\t\t int (*get_mark)(const char *, int),\n+\t\t\t\t const char *(*get_data)(struct branch *,\n+\t\t\t\t\t\t\t struct strbuf *))\n {\n \tint len;\n+\tstruct branch *branch;\n+\tstruct strbuf err = STRBUF_INIT;\n+\tconst char *value;\n \n-\tlen = upstream_mark(name + at, namelen - at);\n+\tlen = get_mark(name + at, namelen - at);\n \tif (!len)\n \t\treturn -1;\n \n \tif (memchr(name, ':', at))\n \t\treturn -1;\n \n-\tset_shortened_ref(buf, get_upstream_branch(name, at));\n+\tif (at) {\n+\t\tchar *name_str = xmemdupz(name, at);\n+\t\tbranch = branch_get(name_str);\n+\t\tfree(name_str);\n+\t} else\n+\t\tbranch = branch_get(NULL);\n+\n+\tvalue = get_data(branch, &err);\n+\tif (!value)\n+\t\tdie(\"%s\", err.buf);\n+\n+\tset_shortened_ref(buf, value);\n \treturn len + at;\n }\n \n@@ -1140,7 +1141,8 @@ int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)\n \t\tif (len > 0)\n \t\t\treturn reinterpret(name, namelen, len, buf);\n \n-\t\tlen = interpret_upstream_mark(name, namelen, at - name, buf);\n+\t\tlen = interpret_branch_mark(name, namelen, at - name, buf,\n+\t\t\t\t\t    upstream_mark, branch_get_upstream);\n \t\tif (len > 0)\n \t\t\treturn len;\n \t}\n-- \n2.4.0.rc3.477.gc25258d\n"},{"id":"260393","messageId":"20150501225541.GJ1534@peff.net","threadId":"39215","inReplyTo":"20150501224414.GA25551@peff.net","subject":"[PATCH 10/12] sha1_name: implement @{push} shorthand","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-05-01T22:55:41Z","receivedAt":"2015-05-01T22:55:41Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"In a triangular workflow, each branch may have two distinct\npoints of interest: the @{upstream} that you normally pull\nfrom, and the destination that you normally push to. There\nisn't a shorthand for the latter, but it's useful to have.\n\nFor instance, you may want to know which commits you haven't\npushed yet:\n\n  git log @{push}..\n\nOr as a more complicated example, imagine that you normally\npull changes from origin/master (which you set as your\n@{upstream}), and push changes to your own personal fork\n(e.g., as myfork/topic). You may push to your fork from\nmultiple machines, requiring you to integrate the changes\nfrom the push destination, rather than upstream. With this\npatch, you can just do:\n\n  git rebase @{push}\n\nrather than typing out the full name.\n\nThe heavy lifting is all done by branch_get_push; here we\njust wire it up to the \"@{push}\" syntax.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nMost of this is from v1, but the heavy lifting was already extracted.\n\n Documentation/revisions.txt | 25 ++++++++++++++++++\n sha1_name.c                 | 14 +++++++++-\n t/t1514-rev-parse-push.sh   | 63 +++++++++++++++++++++++++++++++++++++++++++++\n 3 files changed, 101 insertions(+), 1 deletion(-)\n create mode 100755 t/t1514-rev-parse-push.sh\n\ndiff --git a/Documentation/revisions.txt b/Documentation/revisions.txt\nindex 0796118..d85e303 100644\n--- a/Documentation/revisions.txt\n+++ b/Documentation/revisions.txt\n@@ -98,6 +98,31 @@ some output processing may assume ref names in UTF-8.\n   `branch.<name>.merge`).  A missing branchname defaults to the\n   current one.\n \n+'<branchname>@\\{push\\}', e.g. 'master@\\{push\\}', '@\\{push\\}'::\n+  The suffix '@\\{push}' reports the branch \"where we would push to\" if\n+  `git push` were run while `branchname` was checked out (or the current\n+  'HEAD' if no branchname is specified). Since our push destination is\n+  in a remote repository, of course, we report the local tracking branch\n+  that corresponds to that branch (i.e., something in 'refs/remotes/').\n++\n+Here's an example to make it more clear:\n++\n+------------------------------\n+$ git config push.default current\n+$ git config remote.pushdefault myfork\n+$ git checkout -b mybranch origin/master\n+\n+$ git rev-parse --symbolic-full-name @{upstream}\n+refs/remotes/origin/master\n+\n+$ git rev-parse --symbolic-full-name @{push}\n+refs/remotes/myfork/mybranch\n+------------------------------\n++\n+Note in the example that we set up a triangular workflow, where we pull\n+from one location and push to another. In a non-triangular workflow,\n+'@\\{push}' is the same as '@\\{upstream}', and there is no need for it.\n+\n '<rev>{caret}', e.g. 'HEAD{caret}, v1.5.1{caret}0'::\n   A suffix '{caret}' to a revision parameter means the first parent of\n   that commit object.  '{caret}<n>' means the <n>th parent (i.e.\ndiff --git a/sha1_name.c b/sha1_name.c\nindex 506e0c9..1096943 100644\n--- a/sha1_name.c\n+++ b/sha1_name.c\n@@ -435,6 +435,12 @@ static inline int upstream_mark(const char *string, int len)\n \treturn at_mark(string, len, suffix, ARRAY_SIZE(suffix));\n }\n \n+static inline int push_mark(const char *string, int len)\n+{\n+\tconst char *suffix[] = { \"@{push}\" };\n+\treturn at_mark(string, len, suffix, ARRAY_SIZE(suffix));\n+}\n+\n static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags);\n static int interpret_nth_prior_checkout(const char *name, int namelen, struct strbuf *buf);\n \n@@ -482,7 +488,8 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1,\n \t\t\t\t\tnth_prior = 1;\n \t\t\t\t\tcontinue;\n \t\t\t\t}\n-\t\t\t\tif (!upstream_mark(str + at, len - at)) {\n+\t\t\t\tif (!upstream_mark(str + at, len - at) &&\n+\t\t\t\t    !push_mark(str + at, len - at)) {\n \t\t\t\t\treflog_len = (len-1) - (at+2);\n \t\t\t\t\tlen = at;\n \t\t\t\t}\n@@ -1145,6 +1152,11 @@ int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)\n \t\t\t\t\t    upstream_mark, branch_get_upstream);\n \t\tif (len > 0)\n \t\t\treturn len;\n+\n+\t\tlen = interpret_branch_mark(name, namelen, at - name, buf,\n+\t\t\t\t\t    push_mark, branch_get_push);\n+\t\tif (len > 0)\n+\t\t\treturn len;\n \t}\n \n \treturn -1;\ndiff --git a/t/t1514-rev-parse-push.sh b/t/t1514-rev-parse-push.sh\nnew file mode 100755\nindex 0000000..7214f5b\n--- /dev/null\n+++ b/t/t1514-rev-parse-push.sh\n@@ -0,0 +1,63 @@\n+#!/bin/sh\n+\n+test_description='test <branch>@{push} syntax'\n+. ./test-lib.sh\n+\n+resolve () {\n+\techo \"$2\" >expect &&\n+\tgit rev-parse --symbolic-full-name \"$1\" >actual &&\n+\ttest_cmp expect actual\n+}\n+\n+test_expect_success 'setup' '\n+\tgit init --bare parent.git &&\n+\tgit init --bare other.git &&\n+\tgit remote add origin parent.git &&\n+\tgit remote add other other.git &&\n+\ttest_commit base &&\n+\tgit push origin HEAD &&\n+\tgit branch --set-upstream-to=origin/master master &&\n+\tgit branch --track topic origin/master &&\n+\tgit push origin topic &&\n+\tgit push other topic\n+'\n+\n+test_expect_success '@{push} with default=nothing' '\n+\ttest_config push.default nothing &&\n+\ttest_must_fail git rev-parse master@{push}\n+'\n+\n+test_expect_success '@{push} with default=simple' '\n+\ttest_config push.default simple &&\n+\tresolve master@{push} refs/remotes/origin/master\n+'\n+\n+test_expect_success 'triangular @{push} fails with default=simple' '\n+\ttest_config push.default simple &&\n+\ttest_must_fail git rev-parse topic@{push}\n+'\n+\n+test_expect_success '@{push} with default=current' '\n+\ttest_config push.default current &&\n+\tresolve topic@{push} refs/remotes/origin/topic\n+'\n+\n+test_expect_success '@{push} with default=matching' '\n+\ttest_config push.default matching &&\n+\tresolve topic@{push} refs/remotes/origin/topic\n+'\n+\n+test_expect_success '@{push} with pushremote defined' '\n+\ttest_config push.default current &&\n+\ttest_config branch.topic.pushremote other &&\n+\tresolve topic@{push} refs/remotes/other/topic\n+'\n+\n+test_expect_success '@{push} with push refspecs' '\n+\ttest_config push.default nothing &&\n+\ttest_config remote.origin.push refs/heads/*:refs/heads/magic/* &&\n+\tgit push &&\n+\tresolve topic@{push} refs/remotes/origin/magic/topic\n+'\n+\n+test_done\n-- \n2.4.0.rc3.477.gc25258d\n"},{"id":"260394","messageId":"20150501225559.GK1534@peff.net","threadId":"39215","inReplyTo":"20150501224414.GA25551@peff.net","subject":"[PATCH 11/12] for-each-ref: use skip_prefix instead of starts_with","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-05-01T22:55:59Z","receivedAt":"2015-05-01T22:55:59Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"This saves us having to maintain a magic number to skip past\nthe matched prefix.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nNoticed because I'm adding similar code in the next patch...\n\n builtin/for-each-ref.c | 6 ++++--\n 1 file changed, 4 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex 18d209b..345d8dd 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -659,10 +659,12 @@ static void populate_value(struct refinfo *ref)\n \t\telse if (starts_with(name, \"symref\"))\n \t\t\trefname = ref->symref ? ref->symref : \"\";\n \t\telse if (starts_with(name, \"upstream\")) {\n+\t\t\tconst char *branch_name;\n \t\t\t/* only local branches may have an upstream */\n-\t\t\tif (!starts_with(ref->refname, \"refs/heads/\"))\n+\t\t\tif (!skip_prefix(ref->refname, \"refs/heads/\",\n+\t\t\t\t\t &branch_name))\n \t\t\t\tcontinue;\n-\t\t\tbranch = branch_get(ref->refname + 11);\n+\t\t\tbranch = branch_get(branch_name);\n \n \t\t\trefname = branch_get_upstream(branch, NULL);\n \t\t\tif (!refname)\n-- \n2.4.0.rc3.477.gc25258d\n"},{"id":"260395","messageId":"20150501225645.GL1534@peff.net","threadId":"39215","inReplyTo":"20150501224414.GA25551@peff.net","subject":"[PATCH 12/12] for-each-ref: accept \"%(push)\" format","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-05-01T22:56:45Z","receivedAt":"2015-05-01T22:56:45Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"Just as we have \"%(upstream)\" to report the \"@{upstream}\"\nfor each ref, this patch adds \"%(push)\" to match \"@{push}\".\nIt supports the same tracking format modifiers as upstream\n(because you may want to know, for example, which branches\nhave commits to push).\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n Documentation/git-for-each-ref.txt |  6 ++++++\n builtin/for-each-ref.c             | 17 +++++++++++++++--\n t/t6300-for-each-ref.sh            | 13 ++++++++++++-\n 3 files changed, 33 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt\nindex 4240875..7f8d9a5 100644\n--- a/Documentation/git-for-each-ref.txt\n+++ b/Documentation/git-for-each-ref.txt\n@@ -97,6 +97,12 @@ upstream::\n \tor \"=\" (in sync).  Has no effect if the ref does not have\n \ttracking information associated with it.\n \n+push::\n+\tThe name of a local ref which represents the `@{push}` location\n+\tfor the displayed ref. Respects `:short`, `:track`, and\n+\t`:trackshort` options as `upstream` does. Produces an empty\n+\tstring if no `@{push}` ref is configured.\n+\n HEAD::\n \t'*' if HEAD matches current ref (the checked out branch), ' '\n \totherwise.\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex 345d8dd..6847400 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -74,6 +74,7 @@ static struct {\n \t{ \"contents:body\" },\n \t{ \"contents:signature\" },\n \t{ \"upstream\" },\n+\t{ \"push\" },\n \t{ \"symref\" },\n \t{ \"flag\" },\n \t{ \"HEAD\" },\n@@ -669,6 +670,16 @@ static void populate_value(struct refinfo *ref)\n \t\t\trefname = branch_get_upstream(branch, NULL);\n \t\t\tif (!refname)\n \t\t\t\tcontinue;\n+\t\t} else if (starts_with(name, \"push\")) {\n+\t\t\tconst char *branch_name;\n+\t\t\tif (!skip_prefix(ref->refname, \"refs/heads/\",\n+\t\t\t\t\t &branch_name))\n+\t\t\t\tcontinue;\n+\t\t\tbranch = branch_get(branch_name);\n+\n+\t\t\trefname = branch_get_push(branch, NULL);\n+\t\t\tif (!refname)\n+\t\t\t\tcontinue;\n \t\t} else if (starts_with(name, \"color:\")) {\n \t\t\tchar color[COLOR_MAXLEN] = \"\";\n \n@@ -714,7 +725,8 @@ static void populate_value(struct refinfo *ref)\n \t\t\t\trefname = shorten_unambiguous_ref(refname,\n \t\t\t\t\t\t      warn_ambiguous_refs);\n \t\t\telse if (!strcmp(formatp, \"track\") &&\n-\t\t\t\t starts_with(name, \"upstream\")) {\n+\t\t\t\t (starts_with(name, \"upstream\") ||\n+\t\t\t\t  starts_with(name, \"push\"))) {\n \t\t\t\tchar buf[40];\n \n \t\t\t\tif (stat_tracking_info(branch, &num_ours,\n@@ -736,7 +748,8 @@ static void populate_value(struct refinfo *ref)\n \t\t\t\t}\n \t\t\t\tcontinue;\n \t\t\t} else if (!strcmp(formatp, \"trackshort\") &&\n-\t\t\t\t   starts_with(name, \"upstream\")) {\n+\t\t\t\t   (starts_with(name, \"upstream\") ||\n+\t\t\t\t    starts_with(name, \"push\"))) {\n \t\t\t\tassert(branch);\n \n \t\t\t\tif (stat_tracking_info(branch, &num_ours,\ndiff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh\nindex c66bf79..24fc2ba 100755\n--- a/t/t6300-for-each-ref.sh\n+++ b/t/t6300-for-each-ref.sh\n@@ -28,7 +28,10 @@ test_expect_success setup '\n \tgit update-ref refs/remotes/origin/master master &&\n \tgit remote add origin nowhere &&\n \tgit config branch.master.remote origin &&\n-\tgit config branch.master.merge refs/heads/master\n+\tgit config branch.master.merge refs/heads/master &&\n+\tgit remote add myfork elsewhere &&\n+\tgit config remote.pushdefault myfork &&\n+\tgit config push.default current\n '\n \n test_atom() {\n@@ -47,6 +50,7 @@ test_atom() {\n \n test_atom head refname refs/heads/master\n test_atom head upstream refs/remotes/origin/master\n+test_atom head push refs/remotes/myfork/master\n test_atom head objecttype commit\n test_atom head objectsize 171\n test_atom head objectname $(git rev-parse refs/heads/master)\n@@ -83,6 +87,7 @@ test_atom head HEAD '*'\n \n test_atom tag refname refs/tags/testtag\n test_atom tag upstream ''\n+test_atom tag push ''\n test_atom tag objecttype tag\n test_atom tag objectsize 154\n test_atom tag objectname $(git rev-parse refs/tags/testtag)\n@@ -347,6 +352,12 @@ test_expect_success 'Check that :track[short] works when upstream is invalid' '\n \ttest_cmp expected actual\n '\n \n+test_expect_success '%(push) supports tracking specifiers, too' '\n+\techo \"[ahead 1]\" >expected &&\n+\tgit for-each-ref --format=\"%(push:track)\" refs/heads >actual &&\n+\ttest_cmp expected actual\n+'\n+\n cat >expected <<EOF\n $(git rev-parse --short HEAD)\n EOF\n-- \n2.4.0.rc3.477.gc25258d\n"},{"id":"260455","messageId":"CAPig+cQny16Jei9AtDWVY3ADbPGshFw7CYofD_TnyA+GL58AOg@mail.gmail.com","threadId":"39215","inReplyTo":"20150501224515.GB1534@peff.net","subject":"Re: [PATCH 02/12] remote.c: drop \"remote\" pointer from \"struct branch\"","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2015-05-03T03:34:25Z","receivedAt":"2015-05-03T03:34:25Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Fri, May 1, 2015 at 6:45 PM, Jeff King <peff@peff.net> wrote:\n> When we create each branch struct, we fill in the\n> \"remote_name\" field from the config, and then fill in the\n> actual \"remote\" field (with a \"struct remote\") based on that\n> name. However, it turns out that nobody really cares about\n> the latter field. The only two sites that access it at all\n> are:\n>\n>   1. git-merge, which uses it to notice when the branch does\n>      not have a remote defined. But we can easily replace this\n>      with looking at remote_name instead.\n>\n>   2. remote.c itself, when setting up the @{upstream} merge\n>      config. But we don't need to save the \"remote\" in the\n>      \"struct branch\" for that; we can just look it up for\n>      the duration of the operation.\n>\n> So there is no need to have both fields; they are redundant\n> with each other (the struct remote contains the name, or you\n> can look up the struct from the name). It would be nice to\n> simplify this, especially as we are going to add matching\n> pushremote config in a future patch (and it would be nice to\n> keep them consistent).\n> [...]\n\nNice clear explanation, but...\n\n> Signed-off-by: Jeff King <peff@peff.net>\n> ---\n> diff --git a/remote.c b/remote.c\n> index bec8b31..c85ecef 100644\n> --- a/remote.c\n> +++ b/remote.c\n> @@ -1632,15 +1632,20 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,\n>\n>  static void set_merge(struct branch *ret)\n>  {\n> +       struct remote *remote;\n>         char *ref;\n>         unsigned char sha1[20];\n>         int i;\n>\n> +       if (!ret->remote_name || !ret->merge_nr)\n> +               return;\n> +       remote = remote_get(ret->remote_name);\n> +\n>         ret->merge = xcalloc(ret->merge_nr, sizeof(*ret->merge));\n>         for (i = 0; i < ret->merge_nr; i++) {\n>                 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));\n>                 ret->merge[i]->src = xstrdup(ret->merge_name[i]);\n> -               if (!remote_find_tracking(ret->remote, ret->merge[i]) ||\n> +               if (!remote_find_tracking(remote, ret->merge[i]) ||\n>                     strcmp(ret->remote_name, \".\"))\n>                         continue;\n>                 if (dwim_ref(ret->merge_name[i], strlen(ret->merge_name[i]),\n> @@ -1660,11 +1665,8 @@ struct branch *branch_get(const char *name)\n>                 ret = current_branch;\n>         else\n>                 ret = make_branch(name, 0);\n> -       if (ret && ret->remote_name) {\n> -               ret->remote = remote_get(ret->remote_name);\n> -               if (ret->merge_nr)\n> -                       set_merge(ret);\n> -       }\n> +       if (ret)\n> +              set_merge(ret);\n\nWhen reading the actual patch, I was surprised to see unmentioned\nchanges to the reg->merge_nr check. Although the merge_nr\nsimplification seems sensible, it appears to be unrelated to the\nstated purpose of the patch, and made the review more difficult since\nit required keeping track of two distinct (yet textually intertwined)\nchanges. I wonder if it would make more sense to apply the merge_nr\nsimplification as a separate preparatory patch?\n\n>         return ret;\n>  }\n"},{"id":"260457","messageId":"CAPig+cT7pSxZahd1hmcwCW8ifZfvQ4YW6L7xXUrD7dTJt76srQ@mail.gmail.com","threadId":"39215","inReplyTo":"20150501224644.GD1534@peff.net","subject":"Re: [PATCH 04/12] remote.c: provide per-branch pushremote name","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2015-05-03T04:51:13Z","receivedAt":"2015-05-03T04:51:13Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Fri, May 1, 2015 at 6:46 PM, Jeff King <peff@peff.net> wrote:\n> When remote.c loads its config, it records the\n> branch.*.pushremote for the current branch along with the\n> global remote.pushDefault value, and then binds them into a\n> single value: the default push for the current branch. We\n> then pass this value (which may be NULL) to remote_get_1\n> when looking up a remote for push.\n>\n> This has a few downsides:\n>\n>   1. It's confusing. The early-binding of the \"current\n>      value\" led to bugs like the one fixed by 98b406f\n>      (remote: handle pushremote config in any order,\n>      2014-02-24). And the fact that pushremotes fall back to\n>      ordinary remotes is not explicit at all; it happens\n>      because remote_get_1 cannot tell the difference between\n>      \"we are not asking for the push remote\" and \"there is\n>      no push remote configured\".\n>\n>   2. It throws away intermediate data. After read_config()\n>      finishes, we have no idea what the value of\n>      remote.pushDefault was, because the string has been\n>      overwritten by the current branch's\n>      branch.*.pushremote.\n>\n>   3. It doesn't record other data. We don't note the\n>      branch.*.pushremote value for anything but the current\n>      branch.\n>\n> Let's make this more like the fetch-remote config. We'll\n> record the pushremote for each branch, and then explicitly\n> compute the correct remote for the current branch at the\n> time of reading.\n>\n> Signed-off-by: Jeff King <peff@peff.net>\n> ---\n> Versus v1, I did something a little clever by passing a function pointer\n> around (versus a flag and letting the caller do a conditional based on\n> the flag). Too clever?\n\nFWIW: I found this \"clever\" version easy enough to follow.\n\nHowever, if you push a tiny bit of the work into the callers of\nremote_get_1(), then you can do away with the \"cleverness\" altogether,\ncan't you? Something like this:\n\n    static struct remote_get_1(const char *name, int explicit)\n    {\n        struct remote *ret = make_remote(name, 0);\n        ...\n        if (explicit && valid_remote(ret))\n            ...\n        ...\n    }\n\n    struct remote *remote_get(const char *name)\n    {\n        int explicit = !!name;\n        read_config();\n        if (!name)\n            name = remote_for_branch(current_branch, &explicit);\n        return remote_get_1(name, explicit);\n    }\n\n    struct remote *pushremote_get(const char *name)\n    {\n        int explicit = !!name;\n        read_config();\n        if (!name)\n            name = pushremote_for_branch(current_branch, &explicit);\n        return remote_get_1(name, explicit);\n    }\n\n> diff --git a/remote.c b/remote.c\n> index a27f795..9f84ea3 100644\n> --- a/remote.c\n> +++ b/remote.c\n> @@ -704,20 +697,31 @@ const char *remote_for_branch(struct branch *branch, int *explicit)\n>         return \"origin\";\n>  }\n>\n> -static struct remote *remote_get_1(const char *name, const char *pushremote_name)\n> +const char *pushremote_for_branch(struct branch *branch, int *explicit)\n> +{\n> +       if (branch && branch->pushremote_name) {\n> +               if (explicit)\n> +                       *explicit = 1;\n> +               return branch->pushremote_name;\n> +       }\n> +       if (pushremote_name) {\n> +               if (explicit)\n> +                       *explicit = 1;\n> +               return pushremote_name;\n> +       }\n> +       return remote_for_branch(branch, explicit);\n> +}\n> +\n> +static struct remote *remote_get_1(const char *name,\n> +                                  const char *(*get_default)(struct branch *, int *))\n>  {\n>         struct remote *ret;\n>         int name_given = 0;\n>\n>         if (name)\n>                 name_given = 1;\n> -       else {\n> -               if (pushremote_name) {\n> -                       name = pushremote_name;\n> -                       name_given = 1;\n> -               } else\n> -                       name = remote_for_branch(current_branch, &name_given);\n> -       }\n> +       else\n> +               name = get_default(current_branch, &name_given);\n>\n>         ret = make_remote(name, 0);\n>         if (valid_remote_nick(name)) {\n> @@ -738,13 +742,13 @@ static struct remote *remote_get_1(const char *name, const char *pushremote_name\n>  struct remote *remote_get(const char *name)\n>  {\n>         read_config();\n> -       return remote_get_1(name, NULL);\n> +       return remote_get_1(name, remote_for_branch);\n>  }\n>\n>  struct remote *pushremote_get(const char *name)\n>  {\n>         read_config();\n> -       return remote_get_1(name, pushremote_name);\n> +       return remote_get_1(name, pushremote_for_branch);\n>  }\n>\n>  int remote_is_configured(const char *name)\n"},{"id":"260615","messageId":"20150505193105.GE10463@peff.net","threadId":"39215","inReplyTo":"CAPig+cQny16Jei9AtDWVY3ADbPGshFw7CYofD_TnyA+GL58AOg@mail.gmail.com","subject":"Re: [PATCH 02/12] remote.c: drop \"remote\" pointer from \"struct branch\"","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-05-05T19:31:05Z","receivedAt":"2015-05-05T19:31:05Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sat, May 02, 2015 at 11:34:25PM -0400, Eric Sunshine wrote:\n\n> > -       if (ret && ret->remote_name) {\n> > -               ret->remote = remote_get(ret->remote_name);\n> > -               if (ret->merge_nr)\n> > -                       set_merge(ret);\n> > -       }\n> > +       if (ret)\n> > +              set_merge(ret);\n> \n> When reading the actual patch, I was surprised to see unmentioned\n> changes to the reg->merge_nr check. Although the merge_nr\n> simplification seems sensible, it appears to be unrelated to the\n> stated purpose of the patch, and made the review more difficult since\n> it required keeping track of two distinct (yet textually intertwined)\n> changes. I wonder if it would make more sense to apply the merge_nr\n> simplification as a separate preparatory patch?\n\nI didn't actually mean to change any behavior with respect to\nret->merge_nr here (and I don't think I did). What I did was blindly\nmove everything in the conditional after the remote_get into set_merge,\nso that it happened in the same order (and the remote_get moves into\nset_merge, because we no longer have the struct element that it was\nformerly passed down in).\n\nBut actually, ret->merge_nr comes from make_branch, and we could\ncontinue to respect it regardless of the remote values (i.e., they are\nboth preconditions to setting up the merge data, but it doesn't matter\nin which order we check them).\n\nOne thing I did notice while looking at this is that it seems like we\nmay leak if you call branch_get multiple times. The make_branch()\nfunction may sometimes return a brand-new branch and sometimes return a\ncached version from the \"branches\" array. In the latter case, we\ncontinue to update the \"remote\" pointer (which is wasteful but at least\ndoes not leak because the remotes themselves are part of a cached list).\nBut then we will repeatedly re-allocate the ret->merge array. We\nprobably should make sure it is NULL before trying to fill it in.\n\nI'll see if I can insert a cleanup patch in this part of the series.\n\n-Peff\n"},{"id":"260616","messageId":"20150505193339.GF10463@peff.net","threadId":"39215","inReplyTo":"CAPig+cT7pSxZahd1hmcwCW8ifZfvQ4YW6L7xXUrD7dTJt76srQ@mail.gmail.com","subject":"Re: [PATCH 04/12] remote.c: provide per-branch pushremote name","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-05-05T19:33:39Z","receivedAt":"2015-05-05T19:33:39Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sun, May 03, 2015 at 12:51:13AM -0400, Eric Sunshine wrote:\n\n> > Versus v1, I did something a little clever by passing a function pointer\n> > around (versus a flag and letting the caller do a conditional based on\n> > the flag). Too clever?\n> \n> FWIW: I found this \"clever\" version easy enough to follow.\n> \n> However, if you push a tiny bit of the work into the callers of\n> remote_get_1(), then you can do away with the \"cleverness\" altogether,\n> can't you? Something like this:\n\nYeah, it's just that it goes in the opposite direction I was trying for,\nwhich is to have as little code as possible in the wrapper functions (in\nfact, I think after my changes you could even bump the read_config()\ncall into remote_get_1; before my changes, it depended on the pushremote\nconfig being set before the call).\n\nI agree it is not so much code, though, and maybe it makes the flow a\nlittle clearer. I'll play with it for the re-roll.\n\n-Peff\n"},{"id":"260618","messageId":"CAPig+cQ+AbgTXAWKaN4obdGLWFDxjfr4yAaJZgvnLLzzoqdPRQ@mail.gmail.com","threadId":"39215","inReplyTo":"20150505193339.GF10463@peff.net","subject":"Re: [PATCH 04/12] remote.c: provide per-branch pushremote name","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2015-05-05T19:48:29Z","receivedAt":"2015-05-05T19:48:29Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Tue, May 5, 2015 at 3:33 PM, Jeff King <peff@peff.net> wrote:\n> On Sun, May 03, 2015 at 12:51:13AM -0400, Eric Sunshine wrote:\n>\n>> > Versus v1, I did something a little clever by passing a function pointer\n>> > around (versus a flag and letting the caller do a conditional based on\n>> > the flag). Too clever?\n>>\n>> FWIW: I found this \"clever\" version easy enough to follow.\n>>\n>> However, if you push a tiny bit of the work into the callers of\n>> remote_get_1(), then you can do away with the \"cleverness\" altogether,\n>> can't you? Something like this:\n>\n> Yeah, it's just that it goes in the opposite direction I was trying for,\n> which is to have as little code as possible in the wrapper functions (in\n> fact, I think after my changes you could even bump the read_config()\n> call into remote_get_1; before my changes, it depended on the pushremote\n> config being set before the call).\n\nI also noticed that read_config() could be moved into remote_get_1().\nIn fact, with that change, then the wrappers really do collapse nicely\nto 1-liners, so the \"clever\" function pointer approach probably is\ncleaner; and it's nicely generalized over the previous round with the\nboolean argument to remote_get_1().\n"},{"id":"260721","messageId":"20150507093333.GA14524@peff.net","threadId":"39215","inReplyTo":"20150505193105.GE10463@peff.net","subject":"Re: [PATCH 02/12] remote.c: drop \"remote\" pointer from \"struct branch\"","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-05-07T09:33:34Z","receivedAt":"2015-05-07T09:33:34Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, May 05, 2015 at 03:31:05PM -0400, Jeff King wrote:\n\n> One thing I did notice while looking at this is that it seems like we\n> may leak if you call branch_get multiple times. The make_branch()\n> function may sometimes return a brand-new branch and sometimes return a\n> cached version from the \"branches\" array. In the latter case, we\n> continue to update the \"remote\" pointer (which is wasteful but at least\n> does not leak because the remotes themselves are part of a cached list).\n> But then we will repeatedly re-allocate the ret->merge array. We\n> probably should make sure it is NULL before trying to fill it in.\n> \n> I'll see if I can insert a cleanup patch in this part of the series.\n\nHere's what I came up with. I'm sending it separately in case you have\nany early comments, but it will be part of the next re-roll (just before\nthe existing patch 2 we're discussing here).\n\n-- >8 --\nSubject: remote.c: refactor setup of branch->merge list\n\nWhen we call branch_get() to lookup or create a \"struct\nbranch\", we make sure the \"merge\" field is filled in so that\ncallers can access it. But the conditions under which we do\nso are a little confusing, and can lead to two funny\nsituations:\n\n  1. If there's no branch.*.remote config, we cannot provide\n     branch->merge (because it is really just an application\n     of branch.*.merge to our remote's refspecs). But\n     branch->merge_nr may be non-zero, leading callers to be\n     believe they can access branch->merge (e.g., in\n     branch_merge_matches and elsewhere).\n\n     It doesn't look like this can cause a segfault in\n     practice, as most code paths dealing with merge config\n     will bail early if there is no remote defined. But it's\n     a bit of a dangerous construct.\n\n     We can fix this by setting merge_nr to \"0\" explicitly\n     when we realize that we have no merge config. Note that\n     merge_nr also counts the \"merge_name\" fields (which we\n     _do_ have; that's how merge_nr got incremented), so we\n     will \"lose\" access to them, in the sense that we forget\n     how many we had. But no callers actually care; we use\n     merge_name only while iteratively reading the config,\n     and then convert it to the final \"merge\" form the first\n     time somebody calls branch_get().\n\n  2. We set up the \"merge\" field every time branch_get is\n     called, even if it has already been done. This leaks\n     memory.\n\n     It's not a big deal in practice, since most code paths\n     will access only one branch, or perhaps each branch\n     only one time. But if you want to be pathological, you\n     can leak arbitrary memory with:\n\n       yes @{upstream} | head -1000 | git rev-list --stdin\n\n     We can fix this by skipping setup when branch->merge is\n     already non-NULL.\n\nIn addition to those two fixes, this patch pushes the \"do we\nneed to setup merge?\" logic down into set_merge, where it is\na bit easier to follow.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n remote.c | 19 +++++++++++++++----\n 1 file changed, 15 insertions(+), 4 deletions(-)\n\ndiff --git a/remote.c b/remote.c\nindex bec8b31..ac17e66 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1636,6 +1636,19 @@ static void set_merge(struct branch *ret)\n \tunsigned char sha1[20];\n \tint i;\n \n+\tif (!ret)\n+\t\treturn; /* no branch */\n+\tif (ret->merge)\n+\t\treturn; /* already run */\n+\tif (!ret->remote_name || !ret->merge_nr) {\n+\t\t/*\n+\t\t * no merge config; let's make sure we don't confuse callers\n+\t\t * with a non-zero merge_nr but a NULL merge\n+\t\t */\n+\t\tret->merge_nr = 0;\n+\t\treturn;\n+\t}\n+\n \tret->merge = xcalloc(ret->merge_nr, sizeof(*ret->merge));\n \tfor (i = 0; i < ret->merge_nr; i++) {\n \t\tret->merge[i] = xcalloc(1, sizeof(**ret->merge));\n@@ -1660,11 +1673,9 @@ struct branch *branch_get(const char *name)\n \t\tret = current_branch;\n \telse\n \t\tret = make_branch(name, 0);\n-\tif (ret && ret->remote_name) {\n+\tif (ret && ret->remote_name)\n \t\tret->remote = remote_get(ret->remote_name);\n-\t\tif (ret->merge_nr)\n-\t\t\tset_merge(ret);\n-\t}\n+\tset_merge(ret);\n \treturn ret;\n }\n \n-- \n2.4.0.488.gf55b16a\n"},{"id":"260722","messageId":"20150507093836.GB14524@peff.net","threadId":"39215","inReplyTo":"CAPig+cQ+AbgTXAWKaN4obdGLWFDxjfr4yAaJZgvnLLzzoqdPRQ@mail.gmail.com","subject":"Re: [PATCH 04/12] remote.c: provide per-branch pushremote name","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-05-07T09:38:37Z","receivedAt":"2015-05-07T09:38:37Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, May 05, 2015 at 03:48:29PM -0400, Eric Sunshine wrote:\n\n> > Yeah, it's just that it goes in the opposite direction I was trying for,\n> > which is to have as little code as possible in the wrapper functions (in\n> > fact, I think after my changes you could even bump the read_config()\n> > call into remote_get_1; before my changes, it depended on the pushremote\n> > config being set before the call).\n> \n> I also noticed that read_config() could be moved into remote_get_1().\n> In fact, with that change, then the wrappers really do collapse nicely\n> to 1-liners, so the \"clever\" function pointer approach probably is\n> cleaner; and it's nicely generalized over the previous round with the\n> boolean argument to remote_get_1().\n\nI ended up with this patch, which will go right after the one we're\ndiscussing:\n\n-- >8 --\nSubject: remote.c: hoist read_config into remote_get_1\n\nBefore the previous commit, we had to make sure that\nread_config() was called before entering remote_get_1,\nbecause we needed to pass pushremote_name by value. But now\nthat we pass a function, we can let remote_get_1 handle\nloading the config itself, turning our wrappers into true\none-liners.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n remote.c | 4 ++--\n 1 file changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git a/remote.c b/remote.c\nindex a91d063..e6b29b3 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -718,6 +718,8 @@ static struct remote *remote_get_1(const char *name,\n \tstruct remote *ret;\n \tint name_given = 0;\n \n+\tread_config();\n+\n \tif (name)\n \t\tname_given = 1;\n \telse\n@@ -741,13 +743,11 @@ static struct remote *remote_get_1(const char *name,\n \n struct remote *remote_get(const char *name)\n {\n-\tread_config();\n \treturn remote_get_1(name, remote_for_branch);\n }\n \n struct remote *pushremote_get(const char *name)\n {\n-\tread_config();\n \treturn remote_get_1(name, pushremote_for_branch);\n }\n \n-- \n2.4.0.488.gf55b16a\n"},{"id":"260816","messageId":"CAPig+cTZsUYAY_1f+0ieBuaRw9A-wSAKmpJ9Wj1kUuo=QBCNNw@mail.gmail.com","threadId":"39215","inReplyTo":"20150507093836.GB14524@peff.net","subject":"Re: [PATCH 04/12] remote.c: provide per-branch pushremote name","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2015-05-08T16:13:50Z","receivedAt":"2015-05-08T16:13:50Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, May 7, 2015 at 5:38 AM, Jeff King <peff@peff.net> wrote:\n> On Tue, May 05, 2015 at 03:48:29PM -0400, Eric Sunshine wrote:\n>\n>> > Yeah, it's just that it goes in the opposite direction I was trying for,\n>> > which is to have as little code as possible in the wrapper functions (in\n>> > fact, I think after my changes you could even bump the read_config()\n>> > call into remote_get_1; before my changes, it depended on the pushremote\n>> > config being set before the call).\n>>\n>> I also noticed that read_config() could be moved into remote_get_1().\n>> In fact, with that change, then the wrappers really do collapse nicely\n>> to 1-liners, so the \"clever\" function pointer approach probably is\n>> cleaner; and it's nicely generalized over the previous round with the\n>> boolean argument to remote_get_1().\n>\n> I ended up with this patch, which will go right after the one we're\n> discussing:\n\nNice, I like it.\n\n> -- >8 --\n> Subject: remote.c: hoist read_config into remote_get_1\n>\n> Before the previous commit, we had to make sure that\n> read_config() was called before entering remote_get_1,\n> because we needed to pass pushremote_name by value. But now\n> that we pass a function, we can let remote_get_1 handle\n> loading the config itself, turning our wrappers into true\n> one-liners.\n>\n> Signed-off-by: Jeff King <peff@peff.net>\n> ---\n>  remote.c | 4 ++--\n>  1 file changed, 2 insertions(+), 2 deletions(-)\n>\n> diff --git a/remote.c b/remote.c\n> index a91d063..e6b29b3 100644\n> --- a/remote.c\n> +++ b/remote.c\n> @@ -718,6 +718,8 @@ static struct remote *remote_get_1(const char *name,\n>         struct remote *ret;\n>         int name_given = 0;\n>\n> +       read_config();\n> +\n>         if (name)\n>                 name_given = 1;\n>         else\n> @@ -741,13 +743,11 @@ static struct remote *remote_get_1(const char *name,\n>\n>  struct remote *remote_get(const char *name)\n>  {\n> -       read_config();\n>         return remote_get_1(name, remote_for_branch);\n>  }\n>\n>  struct remote *pushremote_get(const char *name)\n>  {\n> -       read_config();\n>         return remote_get_1(name, pushremote_for_branch);\n>  }\n>\n> --\n> 2.4.0.488.gf55b16a\n"}]}