{"thread":{"id":"45261","subject":"[PATCH v2] fixing corner-cases with interpret_branch_name()","startedAt":"2017-03-02T08:27:50Z","lastAt":"2017-03-02T10:10:06Z","messageCount":10,"participants":["Jeff King","Jacob Keller"],"isPatch":true,"patchVersion":2,"patchTotal":null},"messages":[{"id":"313045","messageId":"20170302082100.edaretznmlralswa@sigill.intra.peff.net","threadId":"45261","inReplyTo":null,"subject":"[PATCH v2] fixing corner-cases with interpret_branch_name()","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-03-02T08:21:00Z","receivedAt":"2017-03-02T08:27:50Z","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 from:\n\n  http://public-inbox.org/git/20170228120633.zkwfqms57fk7dkl5@sigill.intra.peff.net/\n\nThanks Junio and Jake for reviewing the original. This is mostly the\nsame, but:\n\n  - it fixes the case where \"branch -r @{-1}\" mistakes a local branch\n    for a remote (and adds a test)\n\n  - as a result of the above fix, the series needs to be applied on top\n    of jk/auto-namelen-in-interpret-branch-name.\n\n  - I clarified the history in the commit message of patch 4\n\n  - the commit message for patch 4 now explicitly mentions which\n    callers can be left alone (so anybody blaming the history won't\n    think they were simply forgotten).\n\nWith the exception of patch 6 flipping the failure/success bit on the\nnew test, the rest of the patches should be identical.\n\n  [1/8]: interpret_branch_name: move docstring to header file\n  [2/8]: strbuf_branchname: drop return value\n  [3/8]: strbuf_branchname: add docstring\n  [4/8]: interpret_branch_name: allow callers to restrict expansions\n  [5/8]: t3204: test git-branch @-expansion corner cases\n  [6/8]: branch: restrict @-expansions when deleting\n  [7/8]: strbuf_check_ref_format(): expand only local branches\n  [8/8]: checkout: restrict @-expansions when finding branch\n\n builtin/branch.c                      |   5 +-\n builtin/checkout.c                    |   2 +-\n builtin/merge.c                       |   2 +-\n cache.h                               |  32 +++++++-\n refs.c                                |   2 +-\n revision.c                            |   2 +-\n sha1_name.c                           |  92 ++++++++++++-----------\n strbuf.h                              |  21 +++++-\n t/t3204-branch-name-interpretation.sh | 133 ++++++++++++++++++++++++++++++++++\n 9 files changed, 240 insertions(+), 51 deletions(-)\n create mode 100755 t/t3204-branch-name-interpretation.sh\n\n-Peff\n"},{"id":"313046","messageId":"20170302082123.5flyazh5vll4ce7t@sigill.intra.peff.net","threadId":"45261","inReplyTo":"20170302082100.edaretznmlralswa@sigill.intra.peff.net","subject":"[PATCH v2 1/8] interpret_branch_name: move docstring to header file","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-03-02T08:21:23Z","receivedAt":"2017-03-02T08:28:12Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"We generally put docstrings with function declarations,\nbecause it's the callers who need to know how the function\nworks. Let's do so for interpret_branch_name().\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n cache.h     | 21 +++++++++++++++++++++\n sha1_name.c | 21 ---------------------\n 2 files changed, 21 insertions(+), 21 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex 80b6372cf..c67995caa 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1363,6 +1363,27 @@ extern char *oid_to_hex_r(char *out, const struct object_id *oid);\n extern char *sha1_to_hex(const unsigned char *sha1);\t/* static buffer result! */\n extern char *oid_to_hex(const struct object_id *oid);\t/* same static buffer as sha1_to_hex */\n \n+/*\n+ * This reads short-hand syntax that not only evaluates to a commit\n+ * object name, but also can act as if the end user spelled the name\n+ * of the branch from the command line.\n+ *\n+ * - \"@{-N}\" finds the name of the Nth previous branch we were on, and\n+ *   places the name of the branch in the given buf and returns the\n+ *   number of characters parsed if successful.\n+ *\n+ * - \"<branch>@{upstream}\" finds the name of the other ref that\n+ *   <branch> is configured to merge with (missing <branch> defaults\n+ *   to the current branch), and places the name of the branch in the\n+ *   given buf and returns the number of characters parsed if\n+ *   successful.\n+ *\n+ * If the input is not of the accepted format, it returns a negative\n+ * number to signal an error.\n+ *\n+ * If the input was ok but there are not N branch switches in the\n+ * reflog, it returns 0.\n+ */\n extern int interpret_branch_name(const char *str, int len, struct strbuf *);\n extern int get_oid_mb(const char *str, struct object_id *oid);\n \ndiff --git a/sha1_name.c b/sha1_name.c\nindex 9b5d14b4b..28865b3a1 100644\n--- a/sha1_name.c\n+++ b/sha1_name.c\n@@ -1238,27 +1238,6 @@ static int interpret_branch_mark(const char *name, int namelen,\n \treturn len + at;\n }\n \n-/*\n- * This reads short-hand syntax that not only evaluates to a commit\n- * object name, but also can act as if the end user spelled the name\n- * of the branch from the command line.\n- *\n- * - \"@{-N}\" finds the name of the Nth previous branch we were on, and\n- *   places the name of the branch in the given buf and returns the\n- *   number of characters parsed if successful.\n- *\n- * - \"<branch>@{upstream}\" finds the name of the other ref that\n- *   <branch> is configured to merge with (missing <branch> defaults\n- *   to the current branch), and places the name of the branch in the\n- *   given buf and returns the number of characters parsed if\n- *   successful.\n- *\n- * If the input is not of the accepted format, it returns a negative\n- * number to signal an error.\n- *\n- * If the input was ok but there are not N branch switches in the\n- * reflog, it returns 0.\n- */\n int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)\n {\n \tchar *at;\n-- \n2.12.0.367.gb23790f66\n\n"},{"id":"313047","messageId":"20170302082130.ynfctf73qbvwt3qo@sigill.intra.peff.net","threadId":"45261","inReplyTo":"20170302082100.edaretznmlralswa@sigill.intra.peff.net","subject":"[PATCH v2 3/8] strbuf_branchname: add docstring","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-03-02T08:21:30Z","receivedAt":"2017-03-02T08:28:19Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"This function and its companion, strbuf_check_branch_ref(),\ndid not have their purpose or semantics explained. Let's do\nso.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n strbuf.h | 15 +++++++++++++++\n 1 file changed, 15 insertions(+)\n\ndiff --git a/strbuf.h b/strbuf.h\nindex 47df0500d..6b51b2604 100644\n--- a/strbuf.h\n+++ b/strbuf.h\n@@ -560,7 +560,22 @@ static inline void strbuf_complete_line(struct strbuf *sb)\n \tstrbuf_complete(sb, '\\n');\n }\n \n+/*\n+ * Copy \"name\" to \"sb\", expanding any special @-marks as handled by\n+ * interpret_branch_name(). The result is a non-qualified branch name\n+ * (so \"foo\" or \"origin/master\" instead of \"refs/heads/foo\" or\n+ * \"refs/remotes/origin/master\").\n+ *\n+ * Note that the resulting name may not be a syntactically valid refname.\n+ */\n extern void strbuf_branchname(struct strbuf *sb, const char *name);\n+\n+/*\n+ * Like strbuf_branchname() above, but confirm that the result is\n+ * syntactically valid to be used as a local branch name in refs/heads/.\n+ *\n+ * The return value is \"0\" if the result is valid, and \"-1\" otherwise.\n+ */\n extern int strbuf_check_branch_ref(struct strbuf *sb, const char *name);\n \n extern void strbuf_addstr_urlencode(struct strbuf *, const char *,\n-- \n2.12.0.367.gb23790f66\n\n"},{"id":"313048","messageId":"20170302082126.dupi3jcclhj6ejtv@sigill.intra.peff.net","threadId":"45261","inReplyTo":"20170302082100.edaretznmlralswa@sigill.intra.peff.net","subject":"[PATCH v2 2/8] strbuf_branchname: drop return value","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-03-02T08:21:27Z","receivedAt":"2017-03-02T08:28:55Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"The return value from strbuf_branchname() is confusing and\nuseless: it's 0 if the whole name was consumed by an @-mark,\nbut otherwise is the length of the original name we fed.\n\nNo callers actually look at the return value, so let's just\nget rid of it.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n sha1_name.c | 5 +----\n strbuf.h    | 2 +-\n 2 files changed, 2 insertions(+), 5 deletions(-)\n\ndiff --git a/sha1_name.c b/sha1_name.c\nindex 28865b3a1..4c1e91184 100644\n--- a/sha1_name.c\n+++ b/sha1_name.c\n@@ -1279,17 +1279,14 @@ int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)\n \treturn -1;\n }\n \n-int strbuf_branchname(struct strbuf *sb, const char *name)\n+void strbuf_branchname(struct strbuf *sb, const char *name)\n {\n \tint len = strlen(name);\n \tint used = interpret_branch_name(name, len, sb);\n \n-\tif (used == len)\n-\t\treturn 0;\n \tif (used < 0)\n \t\tused = 0;\n \tstrbuf_add(sb, name + used, len - used);\n-\treturn len;\n }\n \n int strbuf_check_branch_ref(struct strbuf *sb, const char *name)\ndiff --git a/strbuf.h b/strbuf.h\nindex cf1b5409e..47df0500d 100644\n--- a/strbuf.h\n+++ b/strbuf.h\n@@ -560,7 +560,7 @@ static inline void strbuf_complete_line(struct strbuf *sb)\n \tstrbuf_complete(sb, '\\n');\n }\n \n-extern int strbuf_branchname(struct strbuf *sb, const char *name);\n+extern void strbuf_branchname(struct strbuf *sb, const char *name);\n extern int strbuf_check_branch_ref(struct strbuf *sb, const char *name);\n \n extern void strbuf_addstr_urlencode(struct strbuf *, const char *,\n-- \n2.12.0.367.gb23790f66\n\n"},{"id":"313049","messageId":"20170302082300.j2eclnifvlh3pbq3@sigill.intra.peff.net","threadId":"45261","inReplyTo":"20170302082100.edaretznmlralswa@sigill.intra.peff.net","subject":"[PATCH v2 4/8] interpret_branch_name: allow callers to restrict expansions","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-03-02T08:23:01Z","receivedAt":"2017-03-02T08:29:49Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"The interpret_branch_name() function converts names like\n@{-1} and @{upstream} into branch names. The expanded ref\nnames are not fully qualified, and may be outside of the\nrefs/heads/ namespace (e.g., \"@\" expands to \"HEAD\", and\n\"@{upstream}\" is likely to be in \"refs/remotes/\").\n\nThis is OK for callers like dwim_ref() which are primarily\ninterested in resolving the resulting name, no matter where\nit is. But callers like \"git branch\" treat the result as a\nbranch name in refs/heads/.  When we expand to a ref outside\nthat namespace, the results are very confusing (e.g., \"git\nbranch @\" tries to create refs/heads/HEAD, which is\nnonsense).\n\nCallers can't know from the returned string how the\nexpansion happened (e.g., did the user really ask for a\nbranch named \"HEAD\", or did we do a bogus expansion?). One\nfix would be to return some out-parameters describing the\ntypes of expansion that occurred. This has the benefit that\nthe caller can generate precise error messages (\"I\nunderstood @{upstream} to mean origin/master, but that is a\nremote tracking branch, so you cannot create it as a local\nname\").\n\nHowever, out-parameters make the function interface somewhat\ncumbersome. Instead, let's do the opposite: let the caller\ntell us which elements to expand. That's easier to pass in,\nand none of the callers give more precise error messages\nthan \"@{upstream} isn't a valid branch name\" anyway (which\nshould be sufficient).\n\nThe strbuf_branchname() function needs a similar parameter,\nas most of the callers access interpret_branch_name()\nthrough it.\n\nWe can break the callers down into two groups:\n\n  1. Callers that are happy with any kind of ref in the\n     result. We pass \"0\" here, so they continue to work\n     without restrictions. This includes merge_name(),\n     the reflog handling in add_pending_object_with_path(),\n     and substitute_branch_name(). This last is what powers\n     dwim_ref().\n\n  2. Callers that have funny corner cases (mostly in\n     git-branch and git-checkout). These need to make use of\n     the new parameter, but I've left them as \"0\" in this\n     patch, and will address them individually in follow-on\n     patches.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n builtin/branch.c   |  2 +-\n builtin/checkout.c |  2 +-\n builtin/merge.c    |  2 +-\n cache.h            | 13 +++++++++--\n refs.c             |  2 +-\n revision.c         |  2 +-\n sha1_name.c        | 68 ++++++++++++++++++++++++++++++++++++++----------------\n strbuf.h           |  6 ++++-\n 8 files changed, 69 insertions(+), 28 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 94f7de7fa..cf0ece55d 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -216,7 +216,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,\n \t\tchar *target = NULL;\n \t\tint flags = 0;\n \n-\t\tstrbuf_branchname(&bname, argv[i]);\n+\t\tstrbuf_branchname(&bname, argv[i], 0);\n \t\tfree(name);\n \t\tname = mkpathdup(fmt, bname.buf);\n \ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex f174f5030..05eefd994 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -452,7 +452,7 @@ static void setup_branch_path(struct branch_info *branch)\n {\n \tstruct strbuf buf = STRBUF_INIT;\n \n-\tstrbuf_branchname(&buf, branch->name);\n+\tstrbuf_branchname(&buf, branch->name, 0);\n \tif (strcmp(buf.buf, branch->name))\n \t\tbranch->name = xstrdup(buf.buf);\n \tstrbuf_splice(&buf, 0, 0, \"refs/heads/\", 11);\ndiff --git a/builtin/merge.c b/builtin/merge.c\nindex a96d4fb50..848a29855 100644\n--- a/builtin/merge.c\n+++ b/builtin/merge.c\n@@ -438,7 +438,7 @@ static void merge_name(const char *remote, struct strbuf *msg)\n \tchar *found_ref;\n \tint len, early;\n \n-\tstrbuf_branchname(&bname, remote);\n+\tstrbuf_branchname(&bname, remote, 0);\n \tremote = bname.buf;\n \n \tmemset(branch_head, 0, sizeof(branch_head));\ndiff --git a/cache.h b/cache.h\nindex c67995caa..a8816c914 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1383,8 +1383,17 @@ extern char *oid_to_hex(const struct object_id *oid);\t/* same static buffer as s\n  *\n  * If the input was ok but there are not N branch switches in the\n  * reflog, it returns 0.\n- */\n-extern int interpret_branch_name(const char *str, int len, struct strbuf *);\n+ *\n+ * If \"allowed\" is non-zero, it is a treated as a bitfield of allowable\n+ * expansions: local branches (\"refs/heads/\"), remote branches\n+ * (\"refs/remotes/\"), or \"HEAD\". If no \"allowed\" bits are set, any expansion is\n+ * allowed, even ones to refs outside of those namespaces.\n+ */\n+#define INTERPRET_BRANCH_LOCAL (1<<0)\n+#define INTERPRET_BRANCH_REMOTE (1<<1)\n+#define INTERPRET_BRANCH_HEAD (1<<2)\n+extern int interpret_branch_name(const char *str, int len, struct strbuf *,\n+\t\t\t\t unsigned allowed);\n extern int get_oid_mb(const char *str, struct object_id *oid);\n \n extern int validate_headref(const char *ref);\ndiff --git a/refs.c b/refs.c\nindex 6d0961921..da62119c2 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -405,7 +405,7 @@ int refname_match(const char *abbrev_name, const char *full_name)\n static char *substitute_branch_name(const char **string, int *len)\n {\n \tstruct strbuf buf = STRBUF_INIT;\n-\tint ret = interpret_branch_name(*string, *len, &buf);\n+\tint ret = interpret_branch_name(*string, *len, &buf, 0);\n \n \tif (ret == *len) {\n \t\tsize_t size;\ndiff --git a/revision.c b/revision.c\nindex b37dbec37..771d079f6 100644\n--- a/revision.c\n+++ b/revision.c\n@@ -147,7 +147,7 @@ static void add_pending_object_with_path(struct rev_info *revs,\n \t\trevs->no_walk = 0;\n \tif (revs->reflog_info && obj->type == OBJ_COMMIT) {\n \t\tstruct strbuf buf = STRBUF_INIT;\n-\t\tint len = interpret_branch_name(name, 0, &buf);\n+\t\tint len = interpret_branch_name(name, 0, &buf, 0);\n \t\tint st;\n \n \t\tif (0 < len && name[len] && buf.len)\ndiff --git a/sha1_name.c b/sha1_name.c\nindex 4c1e91184..7f754b60c 100644\n--- a/sha1_name.c\n+++ b/sha1_name.c\n@@ -1176,7 +1176,8 @@ static int interpret_empty_at(const char *name, int namelen, int len, struct str\n \treturn 1;\n }\n \n-static int reinterpret(const char *name, int namelen, int len, struct strbuf *buf)\n+static int reinterpret(const char *name, int namelen, int len,\n+\t\t       struct strbuf *buf, unsigned allowed)\n {\n \t/* we have extra data, which might need further processing */\n \tstruct strbuf tmp = STRBUF_INIT;\n@@ -1184,7 +1185,7 @@ static int reinterpret(const char *name, int namelen, int len, struct strbuf *bu\n \tint ret;\n \n \tstrbuf_add(buf, name + len, namelen - len);\n-\tret = interpret_branch_name(buf->buf, buf->len, &tmp);\n+\tret = interpret_branch_name(buf->buf, buf->len, &tmp, allowed);\n \t/* that data was not interpreted, remove our cruft */\n \tif (ret < 0) {\n \t\tstrbuf_setlen(buf, used);\n@@ -1205,11 +1206,27 @@ static void set_shortened_ref(struct strbuf *buf, const char *ref)\n \tfree(s);\n }\n \n+static int branch_interpret_allowed(const char *refname, unsigned allowed)\n+{\n+\tif (!allowed)\n+\t\treturn 1;\n+\n+\tif ((allowed & INTERPRET_BRANCH_LOCAL) &&\n+\t    starts_with(refname, \"refs/heads/\"))\n+\t\treturn 1;\n+\tif ((allowed & INTERPRET_BRANCH_REMOTE) &&\n+\t    starts_with(refname, \"refs/remotes/\"))\n+\t\treturn 1;\n+\n+\treturn 0;\n+}\n+\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+\t\t\t\t\t\t\t struct strbuf *),\n+\t\t\t\t unsigned allowed)\n {\n \tint len;\n \tstruct branch *branch;\n@@ -1234,11 +1251,15 @@ static int interpret_branch_mark(const char *name, int namelen,\n \tif (!value)\n \t\tdie(\"%s\", err.buf);\n \n+\tif (!branch_interpret_allowed(value, allowed))\n+\t\treturn -1;\n+\n \tset_shortened_ref(buf, value);\n \treturn len + at;\n }\n \n-int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)\n+int interpret_branch_name(const char *name, int namelen, struct strbuf *buf,\n+\t\t\t  unsigned allowed)\n {\n \tchar *at;\n \tconst char *start;\n@@ -1247,31 +1268,38 @@ int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)\n \tif (!namelen)\n \t\tnamelen = strlen(name);\n \n-\tlen = interpret_nth_prior_checkout(name, namelen, buf);\n-\tif (!len) {\n-\t\treturn len; /* syntax Ok, not enough switches */\n-\t} else if (len > 0) {\n-\t\tif (len == namelen)\n-\t\t\treturn len; /* consumed all */\n-\t\telse\n-\t\t\treturn reinterpret(name, namelen, len, buf);\n+\tif (!allowed || (allowed & INTERPRET_BRANCH_LOCAL)) {\n+\t\tlen = interpret_nth_prior_checkout(name, namelen, buf);\n+\t\tif (!len) {\n+\t\t\treturn len; /* syntax Ok, not enough switches */\n+\t\t} else if (len > 0) {\n+\t\t\tif (len == namelen)\n+\t\t\t\treturn len; /* consumed all */\n+\t\t\telse\n+\t\t\t\treturn reinterpret(name, namelen, len, buf, allowed);\n+\t\t}\n \t}\n \n \tfor (start = name;\n \t     (at = memchr(start, '@', namelen - (start - name)));\n \t     start = at + 1) {\n \n-\t\tlen = interpret_empty_at(name, namelen, at - name, buf);\n-\t\tif (len > 0)\n-\t\t\treturn reinterpret(name, namelen, len, buf);\n+\t\tif (!allowed || (allowed & INTERPRET_BRANCH_HEAD)) {\n+\t\t\tlen = interpret_empty_at(name, namelen, at - name, buf);\n+\t\t\tif (len > 0)\n+\t\t\t\treturn reinterpret(name, namelen, len, buf,\n+\t\t\t\t\t\t   allowed);\n+\t\t}\n \n \t\tlen = interpret_branch_mark(name, namelen, at - name, buf,\n-\t\t\t\t\t    upstream_mark, branch_get_upstream);\n+\t\t\t\t\t    upstream_mark, branch_get_upstream,\n+\t\t\t\t\t    allowed);\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\t\t\t\t    push_mark, branch_get_push,\n+\t\t\t\t\t    allowed);\n \t\tif (len > 0)\n \t\t\treturn len;\n \t}\n@@ -1279,10 +1307,10 @@ int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)\n \treturn -1;\n }\n \n-void strbuf_branchname(struct strbuf *sb, const char *name)\n+void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)\n {\n \tint len = strlen(name);\n-\tint used = interpret_branch_name(name, len, sb);\n+\tint used = interpret_branch_name(name, len, sb, allowed);\n \n \tif (used < 0)\n \t\tused = 0;\n@@ -1291,7 +1319,7 @@ void strbuf_branchname(struct strbuf *sb, const char *name)\n \n int strbuf_check_branch_ref(struct strbuf *sb, const char *name)\n {\n-\tstrbuf_branchname(sb, name);\n+\tstrbuf_branchname(sb, name, 0);\n \tif (name[0] == '-')\n \t\treturn -1;\n \tstrbuf_splice(sb, 0, 0, \"refs/heads/\", 11);\ndiff --git a/strbuf.h b/strbuf.h\nindex 6b51b2604..17e5f29a5 100644\n--- a/strbuf.h\n+++ b/strbuf.h\n@@ -567,8 +567,12 @@ static inline void strbuf_complete_line(struct strbuf *sb)\n  * \"refs/remotes/origin/master\").\n  *\n  * Note that the resulting name may not be a syntactically valid refname.\n+ *\n+ * If \"allowed\" is non-zero, restrict the set of allowed expansions. See\n+ * interpret_branch_name() for details.\n  */\n-extern void strbuf_branchname(struct strbuf *sb, const char *name);\n+extern void strbuf_branchname(struct strbuf *sb, const char *name,\n+\t\t\t      unsigned allowed);\n \n /*\n  * Like strbuf_branchname() above, but confirm that the result is\n-- \n2.12.0.367.gb23790f66\n\n"},{"id":"313050","messageId":"20170302082306.n6kfc5uqz2kdxtpm@sigill.intra.peff.net","threadId":"45261","inReplyTo":"20170302082100.edaretznmlralswa@sigill.intra.peff.net","subject":"[PATCH v2 5/8] t3204: test git-branch @-expansion corner cases","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-03-02T08:23:06Z","receivedAt":"2017-03-02T08:29:54Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"git-branch feeds the branch names from the command line to\nstrbuf_branchname(), but we do not yet tell that function\nwhich kinds of expansions should be allowed. Let's create a\nset of tests that cover both the allowed and disallowed\ncases.\n\nThat shows off some breakages where we currently create or\ndelete the wrong ref (and will make sure that we do not\nbreak any cases that _should_ be working when we do add more\nrestrictions).\n\nNote that we check branch creation and deletion, but do not\nbother with renames. Those follow the same code path as\ncreation.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n t/t3204-branch-name-interpretation.sh | 123 ++++++++++++++++++++++++++++++++++\n 1 file changed, 123 insertions(+)\n create mode 100755 t/t3204-branch-name-interpretation.sh\n\ndiff --git a/t/t3204-branch-name-interpretation.sh b/t/t3204-branch-name-interpretation.sh\nnew file mode 100755\nindex 000000000..e671a7a64\n--- /dev/null\n+++ b/t/t3204-branch-name-interpretation.sh\n@@ -0,0 +1,123 @@\n+#!/bin/sh\n+\n+test_description='interpreting exotic branch name arguments\n+\n+Branch name arguments are usually names which are taken to be inside of\n+refs/heads/, but we interpret some magic syntax like @{-1}, @{upstream}, etc.\n+This script aims to check the behavior of those corner cases.\n+'\n+. ./test-lib.sh\n+\n+expect_branch() {\n+\tgit log -1 --format=%s \"$1\" >actual &&\n+\techo \"$2\" >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+expect_deleted() {\n+\ttest_must_fail git rev-parse --verify \"$1\"\n+}\n+\n+test_expect_success 'set up repo' '\n+\ttest_commit one &&\n+\ttest_commit two &&\n+\tgit remote add origin foo.git\n+'\n+\n+test_expect_success 'update branch via @{-1}' '\n+\tgit branch previous one &&\n+\n+\tgit checkout previous &&\n+\tgit checkout master &&\n+\n+\tgit branch -f @{-1} two &&\n+\texpect_branch previous two\n+'\n+\n+test_expect_success 'update branch via local @{upstream}' '\n+\tgit branch local one &&\n+\tgit branch --set-upstream-to=local &&\n+\n+\tgit branch -f @{upstream} two &&\n+\texpect_branch local two\n+'\n+\n+test_expect_failure 'disallow updating branch via remote @{upstream}' '\n+\tgit update-ref refs/remotes/origin/remote one &&\n+\tgit branch --set-upstream-to=origin/remote &&\n+\n+\ttest_must_fail git branch -f @{upstream} two\n+'\n+\n+test_expect_success 'create branch with pseudo-qualified name' '\n+\tgit branch refs/heads/qualified two &&\n+\texpect_branch refs/heads/refs/heads/qualified two\n+'\n+\n+test_expect_success 'delete branch via @{-1}' '\n+\tgit branch previous-del &&\n+\n+\tgit checkout previous-del &&\n+\tgit checkout master &&\n+\n+\tgit branch -D @{-1} &&\n+\texpect_deleted previous-del\n+'\n+\n+test_expect_success 'delete branch via local @{upstream}' '\n+\tgit branch local-del &&\n+\tgit branch --set-upstream-to=local-del &&\n+\n+\tgit branch -D @{upstream} &&\n+\texpect_deleted local-del\n+'\n+\n+test_expect_success 'delete branch via remote @{upstream}' '\n+\tgit update-ref refs/remotes/origin/remote-del two &&\n+\tgit branch --set-upstream-to=origin/remote-del &&\n+\n+\tgit branch -r -D @{upstream} &&\n+\texpect_deleted origin/remote-del\n+'\n+\n+# Note that we create two oddly named local branches here. We want to make\n+# sure that we do not accidentally delete either of them, even if\n+# shorten_unambiguous_ref() tweaks the name to avoid ambiguity.\n+test_expect_failure 'delete @{upstream} expansion matches -r option' '\n+\tgit update-ref refs/remotes/origin/remote-del two &&\n+\tgit branch --set-upstream-to=origin/remote-del &&\n+\tgit update-ref refs/heads/origin/remote-del two &&\n+\tgit update-ref refs/heads/remotes/origin/remote-del two &&\n+\n+\ttest_must_fail git branch -D @{upstream} &&\n+\texpect_branch refs/heads/origin/remote-del two &&\n+\texpect_branch refs/heads/remotes/origin/remote-del two\n+'\n+\n+test_expect_failure 'disallow deleting remote branch via @{-1}' '\n+\tgit update-ref refs/remotes/origin/previous one &&\n+\n+\tgit checkout -b origin/previous two &&\n+\tgit checkout master &&\n+\n+\ttest_must_fail git branch -r -D @{-1} &&\n+\texpect_branch refs/remotes/origin/previous one &&\n+\texpect_branch refs/heads/origin/previous two\n+'\n+\n+# The thing we are testing here is that \"@\" is the real branch refs/heads/@,\n+# and not refs/heads/HEAD. These tests should not imply that refs/heads/@ is a\n+# sane thing, but it _is_ technically allowed for now. If we disallow it, these\n+# can be switched to test_must_fail.\n+test_expect_failure 'create branch named \"@\"' '\n+\tgit branch -f @ one &&\n+\texpect_branch refs/heads/@ one\n+'\n+\n+test_expect_failure 'delete branch named \"@\"' '\n+\tgit update-ref refs/heads/@ two &&\n+\tgit branch -D @ &&\n+\texpect_deleted refs/heads/@\n+'\n+\n+test_done\n-- \n2.12.0.367.gb23790f66\n\n"},{"id":"313051","messageId":"20170302082310.y24ex3ado4st7onw@sigill.intra.peff.net","threadId":"45261","inReplyTo":"20170302082100.edaretznmlralswa@sigill.intra.peff.net","subject":"[PATCH v2 6/8] branch: restrict @-expansions when deleting","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-03-02T08:23:10Z","receivedAt":"2017-03-02T08:29:58Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"We use strbuf_branchname() to expand the branch name from\nthe command line, so you can delete the branch given by\n@{-1}, for example.  However, we allow other nonsense like\n\"@\", and we do not respect our \"-r\" flag (so we may end up\ndeleting an oddly-named local ref instead of a remote one).\n\nWe can fix this by passing the appropriate \"allowed\" flag to\nstrbuf_branchname().\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n builtin/branch.c                      | 5 ++++-\n t/t3204-branch-name-interpretation.sh | 6 +++---\n 2 files changed, 7 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex cf0ece55d..291fe90de 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -191,17 +191,20 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,\n \tint ret = 0;\n \tint remote_branch = 0;\n \tstruct strbuf bname = STRBUF_INIT;\n+\tunsigned allowed_interpret;\n \n \tswitch (kinds) {\n \tcase FILTER_REFS_REMOTES:\n \t\tfmt = \"refs/remotes/%s\";\n \t\t/* For subsequent UI messages */\n \t\tremote_branch = 1;\n+\t\tallowed_interpret = INTERPRET_BRANCH_REMOTE;\n \n \t\tforce = 1;\n \t\tbreak;\n \tcase FILTER_REFS_BRANCHES:\n \t\tfmt = \"refs/heads/%s\";\n+\t\tallowed_interpret = INTERPRET_BRANCH_LOCAL;\n \t\tbreak;\n \tdefault:\n \t\tdie(_(\"cannot use -a with -d\"));\n@@ -216,7 +219,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,\n \t\tchar *target = NULL;\n \t\tint flags = 0;\n \n-\t\tstrbuf_branchname(&bname, argv[i], 0);\n+\t\tstrbuf_branchname(&bname, argv[i], allowed_interpret);\n \t\tfree(name);\n \t\tname = mkpathdup(fmt, bname.buf);\n \ndiff --git a/t/t3204-branch-name-interpretation.sh b/t/t3204-branch-name-interpretation.sh\nindex e671a7a64..4f4af1fb4 100755\n--- a/t/t3204-branch-name-interpretation.sh\n+++ b/t/t3204-branch-name-interpretation.sh\n@@ -83,7 +83,7 @@ test_expect_success 'delete branch via remote @{upstream}' '\n # Note that we create two oddly named local branches here. We want to make\n # sure that we do not accidentally delete either of them, even if\n # shorten_unambiguous_ref() tweaks the name to avoid ambiguity.\n-test_expect_failure 'delete @{upstream} expansion matches -r option' '\n+test_expect_success 'delete @{upstream} expansion matches -r option' '\n \tgit update-ref refs/remotes/origin/remote-del two &&\n \tgit branch --set-upstream-to=origin/remote-del &&\n \tgit update-ref refs/heads/origin/remote-del two &&\n@@ -94,7 +94,7 @@ test_expect_failure 'delete @{upstream} expansion matches -r option' '\n \texpect_branch refs/heads/remotes/origin/remote-del two\n '\n \n-test_expect_failure 'disallow deleting remote branch via @{-1}' '\n+test_expect_success 'disallow deleting remote branch via @{-1}' '\n \tgit update-ref refs/remotes/origin/previous one &&\n \n \tgit checkout -b origin/previous two &&\n@@ -114,7 +114,7 @@ test_expect_failure 'create branch named \"@\"' '\n \texpect_branch refs/heads/@ one\n '\n \n-test_expect_failure 'delete branch named \"@\"' '\n+test_expect_success 'delete branch named \"@\"' '\n \tgit update-ref refs/heads/@ two &&\n \tgit branch -D @ &&\n \texpect_deleted refs/heads/@\n-- \n2.12.0.367.gb23790f66\n\n"},{"id":"313052","messageId":"20170302082313.uzh7vburrk6tqcuo@sigill.intra.peff.net","threadId":"45261","inReplyTo":"20170302082100.edaretznmlralswa@sigill.intra.peff.net","subject":"[PATCH v2 7/8] strbuf_check_ref_format(): expand only local branches","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-03-02T08:23:14Z","receivedAt":"2017-03-02T08:30:00Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"This function asks strbuf_branchname() to expand any @-marks\nin the branchname, and then we blindly stick refs/heads/ in\nfront of the result. This is obviously nonsense if the\nexpansion is \"HEAD\" or a ref in refs/remotes/.\n\nThe most obvious end-user effect is that creating or\nrenaming a branch with an expansion may have confusing\nresults (e.g., creating refs/heads/origin/master from\n\"@{upstream}\" when the operation should be disallowed).\n\nWe can fix this by telling strbuf_branchname() that we are\nonly interested in local expansions. Any unexpanded bits are\nthen fed to check_ref_format(), which either disallows them\n(in the case of \"@{upstream}\") or lets them through\n(\"refs/heads/@\" is technically valid, if a bit silly).\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n sha1_name.c                           | 2 +-\n t/t3204-branch-name-interpretation.sh | 4 ++--\n 2 files changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/sha1_name.c b/sha1_name.c\nindex 7f754b60c..26ceec1d7 100644\n--- a/sha1_name.c\n+++ b/sha1_name.c\n@@ -1319,7 +1319,7 @@ void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)\n \n int strbuf_check_branch_ref(struct strbuf *sb, const char *name)\n {\n-\tstrbuf_branchname(sb, name, 0);\n+\tstrbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);\n \tif (name[0] == '-')\n \t\treturn -1;\n \tstrbuf_splice(sb, 0, 0, \"refs/heads/\", 11);\ndiff --git a/t/t3204-branch-name-interpretation.sh b/t/t3204-branch-name-interpretation.sh\nindex 4f4af1fb4..05e88f92d 100755\n--- a/t/t3204-branch-name-interpretation.sh\n+++ b/t/t3204-branch-name-interpretation.sh\n@@ -42,7 +42,7 @@ test_expect_success 'update branch via local @{upstream}' '\n \texpect_branch local two\n '\n \n-test_expect_failure 'disallow updating branch via remote @{upstream}' '\n+test_expect_success 'disallow updating branch via remote @{upstream}' '\n \tgit update-ref refs/remotes/origin/remote one &&\n \tgit branch --set-upstream-to=origin/remote &&\n \n@@ -109,7 +109,7 @@ test_expect_success 'disallow deleting remote branch via @{-1}' '\n # and not refs/heads/HEAD. These tests should not imply that refs/heads/@ is a\n # sane thing, but it _is_ technically allowed for now. If we disallow it, these\n # can be switched to test_must_fail.\n-test_expect_failure 'create branch named \"@\"' '\n+test_expect_success 'create branch named \"@\"' '\n \tgit branch -f @ one &&\n \texpect_branch refs/heads/@ one\n '\n-- \n2.12.0.367.gb23790f66\n\n"},{"id":"313054","messageId":"CA+P7+xq+kJ4nq2LQq+KijK5Vj_+_RXfRt5LDecoEywsUdt0YXA@mail.gmail.com","threadId":"45261","inReplyTo":"20170302082100.edaretznmlralswa@sigill.intra.peff.net","subject":"Re: [PATCH v2] fixing corner-cases with interpret_branch_name()","fromName":"Jacob Keller","fromEmail":"jacob.keller@gmail.com","sentAt":"2017-03-02T08:47:00Z","receivedAt":"2017-03-02T08:57:24Z","isPatch":true,"sender":{"key":"jacob.keller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/874719?v=4"},"body":"On Thu, Mar 2, 2017 at 12:21 AM, Jeff King <peff@peff.net> wrote:\n> This is a re-roll of the series from:\n>\n>   http://public-inbox.org/git/20170228120633.zkwfqms57fk7dkl5@sigill.intra.peff.net/\n>\n> Thanks Junio and Jake for reviewing the original. This is mostly the\n> same, but:\n>\n>   - it fixes the case where \"branch -r @{-1}\" mistakes a local branch\n>     for a remote (and adds a test)\n>\n>   - as a result of the above fix, the series needs to be applied on top\n>     of jk/auto-namelen-in-interpret-branch-name.\n>\n>   - I clarified the history in the commit message of patch 4\n>\n>   - the commit message for patch 4 now explicitly mentions which\n>     callers can be left alone (so anybody blaming the history won't\n>     think they were simply forgotten).\n>\n> With the exception of patch 6 flipping the failure/success bit on the\n> new test, the rest of the patches should be identical.\n>\n\nI didn't find any comments, it was quite pleasant and well explained.\n\nThanks,\nJake\n\n>   [1/8]: interpret_branch_name: move docstring to header file\n>   [2/8]: strbuf_branchname: drop return value\n>   [3/8]: strbuf_branchname: add docstring\n>   [4/8]: interpret_branch_name: allow callers to restrict expansions\n>   [5/8]: t3204: test git-branch @-expansion corner cases\n>   [6/8]: branch: restrict @-expansions when deleting\n>   [7/8]: strbuf_check_ref_format(): expand only local branches\n>   [8/8]: checkout: restrict @-expansions when finding branch\n>\n>  builtin/branch.c                      |   5 +-\n>  builtin/checkout.c                    |   2 +-\n>  builtin/merge.c                       |   2 +-\n>  cache.h                               |  32 +++++++-\n>  refs.c                                |   2 +-\n>  revision.c                            |   2 +-\n>  sha1_name.c                           |  92 ++++++++++++-----------\n>  strbuf.h                              |  21 +++++-\n>  t/t3204-branch-name-interpretation.sh | 133 ++++++++++++++++++++++++++++++++++\n>  9 files changed, 240 insertions(+), 51 deletions(-)\n>  create mode 100755 t/t3204-branch-name-interpretation.sh\n>\n> -Peff\n"},{"id":"313056","messageId":"20170302082318.cw7fyll6saittcoh@sigill.intra.peff.net","threadId":"45261","inReplyTo":"20170302082100.edaretznmlralswa@sigill.intra.peff.net","subject":"[PATCH v2 8/8] checkout: restrict @-expansions when finding branch","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-03-02T08:23:18Z","receivedAt":"2017-03-02T10:10:06Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"When we parse \"git checkout $NAME\", we try to interpret\n$NAME as a local branch-name. If it is, then we point HEAD\nto that branch. Otherwise, we detach the HEAD at whatever\ncommit $NAME points to.\n\nWe do the interpretation by calling strbuf_branchname(), and\nthen blindly sticking \"refs/heads/\" on the front. This leads\nto nonsense results when expansions like \"@{upstream}\" or\n\"@\" point to something besides a local branch. We end up\nwith a local branch name like \"refs/heads/origin/master\" or\n\"refs/heads/HEAD\".\n\nNormally this has no user-visible effect because those\nbranches don't exist, and so we fallback to feeding the\nresult to get_sha1(), which resolves them correctly.\n\nBut as the new test in t3204 shows, there are corner cases\nwhere the effect is observable, and we check out the wrong\nlocal branch rather than detaching to the correct one.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n builtin/checkout.c                    |  2 +-\n t/t3204-branch-name-interpretation.sh | 10 ++++++++++\n 2 files changed, 11 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 05eefd994..81f07c3ef 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -452,7 +452,7 @@ static void setup_branch_path(struct branch_info *branch)\n {\n \tstruct strbuf buf = STRBUF_INIT;\n \n-\tstrbuf_branchname(&buf, branch->name, 0);\n+\tstrbuf_branchname(&buf, branch->name, INTERPRET_BRANCH_LOCAL);\n \tif (strcmp(buf.buf, branch->name))\n \t\tbranch->name = xstrdup(buf.buf);\n \tstrbuf_splice(&buf, 0, 0, \"refs/heads/\", 11);\ndiff --git a/t/t3204-branch-name-interpretation.sh b/t/t3204-branch-name-interpretation.sh\nindex 05e88f92d..698d9cc4f 100755\n--- a/t/t3204-branch-name-interpretation.sh\n+++ b/t/t3204-branch-name-interpretation.sh\n@@ -120,4 +120,14 @@ test_expect_success 'delete branch named \"@\"' '\n \texpect_deleted refs/heads/@\n '\n \n+test_expect_success 'checkout does not treat remote @{upstream} as a branch' '\n+\tgit update-ref refs/remotes/origin/checkout one &&\n+\tgit branch --set-upstream-to=origin/checkout &&\n+\tgit update-ref refs/heads/origin/checkout two &&\n+\tgit update-ref refs/heads/remotes/origin/checkout two &&\n+\n+\tgit checkout @{upstream} &&\n+\texpect_branch HEAD one\n+'\n+\n test_done\n-- \n2.12.0.367.gb23790f66\n"}]}