Skip to contentSkip to conversation

prevent checkout from creating branches that start with a dash

PATCH In review Bart Trojanowski 3 participants 4 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 prevent checkout from creating branches that start with a dash

It was previously possible to create a -f branch with git-checkout, which could not be used or deleted.

        $ git checkout -b -f master
        Switched to a new branch "-f"
---
 branch.c           |   11 +++++++++++
 branch.h           |    5 +++++
 builtin-checkout.c |    4 ++++
 3 files changed, 20 insertions(+), 0 deletions(-)
branch.c +11 -0
index 56e9492..c25e362 100644
@@ -170,3 +170,14 @@ void remove_branch_state(void)
 	unlink(git_path("MERGE_MSG"));
 	unlink(git_path("SQUASH_MSG"));
 }
+
+int validate_branch_name(const char *branch_name)
+{
+	if (!*branch_name)
+		return -1;
+
+	if (*branch_name == '-')
+		return -1;
+
+	return 0;
+}
branch.h +5 -0
index 9f0c2a2..13999ba 100644
@@ -21,4 +21,9 @@ void create_branch(const char *head, const char *name, const char *start_name,
  */
 void remove_branch_state(void);
 
+/*
+ * Check if the branch name given is well formed.  Returns 0 on success.
+ */
+int validate_branch_name(const char *branch_name);
+
 #endif
builtin-checkout.c +4 -0
index 93ea69b..f425646 100644
@@ -553,6 +553,10 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	if (opts.force && opts.merge)
 		die("git checkout: -f and -m are incompatible");
 
+	if (opts.new_branch && validate_branch_name(opts.new_branch))
+		die("git checkout: '%s' does not look like a valid branch name",
+				opts.new_branch);
+
 	if (argc) {
 		const char **pathspec = get_pathspec(prefix, argv);
 
Signature · 1 line
1.5.6.1.109.ga974cd.dirty
#1 lore raw
Quoted 6 lines · Bart Trojanowski
builtin-checkout.c +4 -0
+	if (opts.new_branch && validate_branch_name(opts.new_branch))
+		die("git checkout: '%s' does not look like a valid branch name",
+				opts.new_branch);
+
	if (argc) {
		const char **pathspec = get_pathspec(prefix, argv);

Perhaps this should have !opts.force in that if? That way if someone needs something like "-mm" branch, they can have it.

And that way, if people really want to shoot themselves in the foot with a -f branch, they can? "git branch -f -b -f"?

~~ Brian

#2 lore raw
In reply to Bart Trojanowski
Quoted 5 lines · Bart Trojanowski

It was previously possible to create a -f branch with git-checkout, which could not be used or deleted.

        $ git checkout -b -f master
        Switched to a new branch "-f"

"-f" *is* a valid branch name and can be used and deleted:

$ git checkout -b -f next
Switched to a new branch "-f"
$ git checkout next
Switched to branch "next"
Your branch is ahead of the tracked remote branch 'origin/next' by 2 commits.
$ git checkout -- -f
Switched to branch "-f"
$ git checkout next
Switched to branch "next"
Your branch is ahead of the tracked remote branch 'origin/next' by 2 commits.
$ git branch -d -- -f
Deleted branch -f.

-- Hannes

#3 lore raw
Quoted 8 lines · Johannes Sixt
Quoted 5 lines · Bart Trojanowski

It was previously possible to create a -f branch with git-checkout, which could not be used or deleted.

        $ git checkout -b -f master
        Switched to a new branch "-f"

"-f" *is* a valid branch name and can be used and deleted:

Thanks, that I didn't know.

-Bart

Signature · 1 line
				WebSig: http://www.jukie.net/~bart/sig/