{"thread":{"id":"14259","subject":"[PATCH] prevent checkout from creating branches that start with a dash","startedAt":"2008-07-02T15:01:28Z","lastAt":"2008-07-02T16:34:31Z","messageCount":4,"participants":["Bart Trojanowski","Brian Gernhardt","Johannes Sixt"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"81988","messageId":"20080702150128.GH26300@jukie.net","threadId":"14259","inReplyTo":null,"subject":"[PATCH] prevent checkout from creating branches that start with a dash","fromName":"Bart Trojanowski","fromEmail":"bart@jukie.net","sentAt":"2008-07-02T15:01:28Z","receivedAt":"2008-07-02T15:01:28Z","isPatch":true,"sender":{"key":"bart@jukie.net","avatar":"https://avatars.githubusercontent.com/u/6721?v=4"},"body":"It was previously possible to create a -f branch with git-checkout, which\ncould not be used or deleted.\n\n        $ git checkout -b -f master\n        Switched to a new branch \"-f\"\n\nSigned-off-by: Bart Trojanowski <bart@jukie.net>\n---\n branch.c           |   11 +++++++++++\n branch.h           |    5 +++++\n builtin-checkout.c |    4 ++++\n 3 files changed, 20 insertions(+), 0 deletions(-)\n\ndiff --git a/branch.c b/branch.c\nindex 56e9492..c25e362 100644\n--- a/branch.c\n+++ b/branch.c\n@@ -170,3 +170,14 @@ void remove_branch_state(void)\n \tunlink(git_path(\"MERGE_MSG\"));\n \tunlink(git_path(\"SQUASH_MSG\"));\n }\n+\n+int validate_branch_name(const char *branch_name)\n+{\n+\tif (!*branch_name)\n+\t\treturn -1;\n+\n+\tif (*branch_name == '-')\n+\t\treturn -1;\n+\n+\treturn 0;\n+}\ndiff --git a/branch.h b/branch.h\nindex 9f0c2a2..13999ba 100644\n--- a/branch.h\n+++ b/branch.h\n@@ -21,4 +21,9 @@ void create_branch(const char *head, const char *name, const char *start_name,\n  */\n void remove_branch_state(void);\n \n+/*\n+ * Check if the branch name given is well formed.  Returns 0 on success.\n+ */\n+int validate_branch_name(const char *branch_name);\n+\n #endif\ndiff --git a/builtin-checkout.c b/builtin-checkout.c\nindex 93ea69b..f425646 100644\n--- a/builtin-checkout.c\n+++ b/builtin-checkout.c\n@@ -553,6 +553,10 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)\n \tif (opts.force && opts.merge)\n \t\tdie(\"git checkout: -f and -m are incompatible\");\n \n+\tif (opts.new_branch && validate_branch_name(opts.new_branch))\n+\t\tdie(\"git checkout: '%s' does not look like a valid branch name\",\n+\t\t\t\topts.new_branch);\n+\n \tif (argc) {\n \t\tconst char **pathspec = get_pathspec(prefix, argv);\n \n-- \n1.5.6.1.109.ga974cd.dirty\n"},{"id":"81989","messageId":"3B740C23-95C7-44AD-8A36-9EFA2EEF0E8A@silverinsanity.com","threadId":"14259","inReplyTo":"20080702150128.GH26300@jukie.net","subject":"Re: [PATCH] prevent checkout from creating branches that start with a dash","fromName":"Brian Gernhardt","fromEmail":"benji@silverinsanity.com","sentAt":"2008-07-02T15:22:14Z","receivedAt":"2008-07-02T15:22:14Z","isPatch":true,"sender":{"key":"benji@silverinsanity.com","avatar":"https://gravatar.com/avatar/e06c101dbc25c68114d859b4a9ec7cf8a2c52fd2b0270ef0eac0e2e63ff22311?d=mp&s=160"},"body":"\nOn Jul 2, 2008, at 11:01 AM, Bart Trojanowski wrote:\n\n> +\tif (opts.new_branch && validate_branch_name(opts.new_branch))\n> +\t\tdie(\"git checkout: '%s' does not look like a valid branch name\",\n> +\t\t\t\topts.new_branch);\n> +\n> \tif (argc) {\n> \t\tconst char **pathspec = get_pathspec(prefix, argv);\n\nPerhaps this should have !opts.force in that if?  That way if someone  \nneeds something like \"-mm\" branch, they can have it.\n\nAnd that way, if people really want to shoot themselves in the foot  \nwith a -f branch, they can?  \"git branch -f -b -f\"?\n\n~~ Brian\n"},{"id":"81990","messageId":"486BA0DD.5030608@viscovery.net","threadId":"14259","inReplyTo":"20080702150128.GH26300@jukie.net","subject":"Re: [PATCH] prevent checkout from creating branches that start with a dash","fromName":"Johannes Sixt","fromEmail":"j.sixt@viscovery.net","sentAt":"2008-07-02T15:38:05Z","receivedAt":"2008-07-02T15:38:05Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Bart Trojanowski schrieb:\n> It was previously possible to create a -f branch with git-checkout, which\n> could not be used or deleted.\n> \n>         $ git checkout -b -f master\n>         Switched to a new branch \"-f\"\n\n\"-f\" *is* a valid branch name and can be used and deleted:\n\n$ git checkout -b -f next\nSwitched to a new branch \"-f\"\n$ git checkout next\nSwitched to branch \"next\"\nYour branch is ahead of the tracked remote branch 'origin/next' by 2 commits.\n$ git checkout -- -f\nSwitched to branch \"-f\"\n$ git checkout next\nSwitched to branch \"next\"\nYour branch is ahead of the tracked remote branch 'origin/next' by 2 commits.\n$ git branch -d -- -f\nDeleted branch -f.\n\n-- Hannes\n"},{"id":"81995","messageId":"20080702163431.GI26300@jukie.net","threadId":"14259","inReplyTo":"486BA0DD.5030608@viscovery.net","subject":"Re: [PATCH] prevent checkout from creating branches that start with a dash","fromName":"Bart Trojanowski","fromEmail":"bart@jukie.net","sentAt":"2008-07-02T16:34:31Z","receivedAt":"2008-07-02T16:34:31Z","isPatch":true,"sender":{"key":"bart@jukie.net","avatar":"https://avatars.githubusercontent.com/u/6721?v=4"},"body":"* Johannes Sixt <j.sixt@viscovery.net> [080702 12:00]:\n> Bart Trojanowski schrieb:\n> > It was previously possible to create a -f branch with git-checkout, which\n> > could not be used or deleted.\n> > \n> >         $ git checkout -b -f master\n> >         Switched to a new branch \"-f\"\n> \n> \"-f\" *is* a valid branch name and can be used and deleted:\n\nThanks, that I didn't know.\n\n-Bart\n\n-- \n\t\t\t\tWebSig: http://www.jukie.net/~bart/sig/\n"}]}