diff mbox series

[v2,2/4] branch: Prevent creation of local branches named "refs" or "refs/*"

Message ID 20191107190750.26674-3-ingo.rohloff@lauterbach.com (mailing list archive)
State New, archived
Headers show
Series Do not create new refnames "refs" or "refs/*" | expand

Commit Message

Ingo Rohloff Nov. 7, 2019, 7:07 p.m. UTC
To explain the intention, here is an example:

A user executes
   git branch refs/remotes/origin/master
or
   git checkout -b refs/remotes/origin/master
after this operation
   git log refs/remotes/origin/master
will very likely complain that this reference is ambiguous.

The reason is, that you now very likely have the following two
references which both match:

   refs/remotes/origin/master
   refs/heads/refs/remotes/origin/master

git cannot decide which of the two references is meant.

By preventing the creation of local branches which are named
  refs  or  refs/*
this issue is circumvented:
  git log refs/*
will never refer to local branches located under
  refs/heads/refs/*
because such local branches should not exist.

Signed-off-by: Ingo Rohloff <ingo.rohloff@lauterbach.com>
---
 builtin/branch.c   | 6 ++++++
 builtin/checkout.c | 3 +++
 2 files changed, 9 insertions(+)
diff mbox series

Patch

diff --git a/builtin/branch.c b/builtin/branch.c
index 2ef214632f..c2e45ff52c 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -497,6 +497,9 @@  static void copy_or_rename_branch(const char *oldname, const char *newname, int
 	const char *interpreted_newname = NULL;
 	int recovery = 0;
 
+	if (newname_has_bad_prefix(newname))
+		die(_("Invalid new branch name: '%s'"), newname);
+
 	if (!oldname) {
 		if (copy)
 			die(_("cannot copy the current branch while not on any."));
@@ -844,6 +847,9 @@  int cmd_branch(int argc, const char **argv, const char *prefix)
 		if (track == BRANCH_TRACK_OVERRIDE)
 			die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead."));
 
+		if (newname_has_bad_prefix(argv[0]))
+			die(_("Invalid new branch name: '%s'"), argv[0]);
+
 		create_branch(the_repository,
 			      argv[0], (argc == 2) ? argv[1] : head,
 			      force, 0, reflog, quiet, track);
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 3634a3dac1..51ac2cae43 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1566,6 +1566,9 @@  static int checkout_main(int argc, const char **argv, const char *prefix,
 	if (opts->new_orphan_branch)
 		opts->new_branch = opts->new_orphan_branch;
 
+	if (opts->new_branch && newname_has_bad_prefix(opts->new_branch))
+		die(_("Invalid new branch name: '%s'"), opts->new_branch);
+
 	/* --track without -b/-B/--orphan should DWIM */
 	if (opts->track != BRANCH_TRACK_UNSPECIFIED && !opts->new_branch) {
 		const char *argv0 = argv[0];