git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 4/8] interpret_branch_name: allow callers to restrict expansions

From
Jeff King <peff@peff.net>
Date
Feb 28, 2017, 12:23 UTC
Message-ID
<20170228122338.xkefanyhtwbomoit@sigill.intra.peff.net>
In-Reply-To
<20170228121434.2dhngs4peq5acic2@sigill.intra.peff.net>
On Tue, Feb 28, 2017 at 07:14:34AM -0500, Jeff King wrote:
Show 6 quoted lines
> However, out-parameters make calling interface somewhat
> cumbersome. Instead, let's do the opposite: let the caller
> tell us which elements to expand. That's easier to pass in,
> and none of the callers give more precise error messages
> than "@{upstream} isn't a valid branch name" anyway (which
> should be sufficient).
Two things to call attention to:
Show 10 quoted lines
> -extern int interpret_branch_name(const char *str, int len, struct strbuf *);
> + *
> + * If "allowed" is non-zero, it is a treated as a bitfield of allowable
> + * expansions: local branches ("refs/heads/"), remote branches
> + * ("refs/remotes/"), or "HEAD". If no "allowed" bits are set, any expansion is
> + * allowed, even ones to refs outside of those namespaces.
> + */
> +#define INTERPRET_BRANCH_LOCAL (1<<0)
> +#define INTERPRET_BRANCH_REMOTE (1<<1)
> +#define INTERPRET_BRANCH_HEAD (1<<2)

Is the "0 allows everything, but any bit turns on restrictions" convention too confusing? It's convenient to use in the callers which do not need restrictions, but we could add an INTERPRET_BRANCH_ALL flag if that is more clear (but note that it _isn't_ just bitwise-AND of the other flags, because technically an @{upstream} could point to "refs/foo" or some other location).

Show 13 quoted lines
> -int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
> +int interpret_branch_name(const char *name, int namelen, struct strbuf *buf,
> +			  unsigned allowed)
>  {
>  	char *at;
>  	const char *start;
> @@ -1254,24 +1275,29 @@ int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
>  		if (len == namelen)
>  			return len; /* consumed all */
>  		else
> -			return reinterpret(name, namelen, len, buf);
> +			return reinterpret(name, namelen, len, buf, allowed);
>  	}

It's hard to see from this context, but a careful reader may note that we do not check "allowed" at all before calling interpret_nth_prior_checkout(). This is looking for branch names via HEAD, so I don't think it can ever return anything but a local name.

Which, hmm. I guess was valid when the flag was "only_branches", but would not be valid under INTERPRET_BRANCH_REMOTE. I wonder if

  git branch -r -D @{-1}

incorrectly deletes refs/remotes/origin/master if you previously had refs/heads/origin/master checked out.

-Peff
Previous: Jeff KingNext: Jeff King
Message 4 of 15 in “Re: [BUG] branch renamed to 'HEAD'”
  1. Jeff KingFeb 28, 2017
  2. 1/8 interpret_branch_name: move docstring to header fileJeff King, Feb 28, 2017
  3. 4/8 interpret_branch_name: allow callers to restrict expansionsJeff King, Feb 28, 2017
  4. Jeff KingFeb 28, 2017
  5. Jeff KingFeb 28, 2017
  6. Junio C HamanoFeb 28, 2017
  7. Jeff KingFeb 28, 2017
  8. 3/8 strbuf_branchname: add docstringJeff King, Feb 28, 2017
  9. 8/8 checkout: restrict @-expansions when finding branchJeff King, Feb 28, 2017
  10. 6/8 branch: restrict @-expansions when deletingJeff King, Feb 28, 2017
  11. 7/8 strbuf_check_ref_format(): expand only local branchesJeff King, Feb 28, 2017
  12. 5/8 t3204: test git-branch @-expansion corner casesJeff King, Feb 28, 2017
  13. 2/8 strbuf_branchname: drop return valueJeff King, Feb 28, 2017
  14. Jacob KellerFeb 28, 2017
  15. Junio C HamanoMar 1, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.