Merge branch 'nd/branch-error-cases'
authorJunio C Hamano <gitster@pobox.com>
Thu, 21 Mar 2013 21:02:51 +0000 (14:02 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 21 Mar 2013 21:02:51 +0000 (14:02 -0700)
"git branch" had more cases where it did not bother to check
nonsense command line parameters.

* nd/branch-error-cases:
branch: segfault fixes and validation

1  2 
builtin/branch.c
diff --combined builtin/branch.c
index 6371bf96c4841ca263f6bee8a844743c058a5561,22ecde5254b26f8e72decb26f3ace99a32f230a5..00d17d25d1866323f9e1dffcb334a573f5de5221
@@@ -706,11 -706,11 +706,11 @@@ static int edit_branch_description(cons
        read_branch_desc(&buf, branch_name);
        if (!buf.len || buf.buf[buf.len-1] != '\n')
                strbuf_addch(&buf, '\n');
 -      strbuf_addf(&buf,
 -                  "Please edit the description for the branch\n"
 -                  "  %s\n"
 -                  "# Lines starting with '#' will be stripped.\n",
 -                  branch_name);
 +      strbuf_commented_addf(&buf,
 +                  "Please edit the description for the branch\n"
 +                  "  %s\n"
 +                  "Lines starting with '%c' will be stripped.\n",
 +                  branch_name, comment_line_char);
        fp = fopen(git_path(edit_description), "w");
        if ((fwrite(buf.buf, 1, buf.len, fp) < buf.len) || fclose(fp)) {
                strbuf_release(&buf);
@@@ -889,6 -889,17 +889,17 @@@ int cmd_branch(int argc, const char **a
        } else if (new_upstream) {
                struct branch *branch = branch_get(argv[0]);
  
+               if (argc > 1)
+                       die(_("too many branches to set new upstream"));
+               if (!branch) {
+                       if (!argc || !strcmp(argv[0], "HEAD"))
+                               die(_("could not set upstream of HEAD to %s when "
+                                     "it does not point to any branch."),
+                                   new_upstream);
+                       die(_("no such branch '%s'"), argv[0]);
+               }
                if (!ref_exists(branch->refname))
                        die(_("branch '%s' does not exist"), branch->name);
  
                struct branch *branch = branch_get(argv[0]);
                struct strbuf buf = STRBUF_INIT;
  
+               if (argc > 1)
+                       die(_("too many branches to unset upstream"));
+               if (!branch) {
+                       if (!argc || !strcmp(argv[0], "HEAD"))
+                               die(_("could not unset upstream of HEAD when "
+                                     "it does not point to any branch."));
+                       die(_("no such branch '%s'"), argv[0]);
+               }
                if (!branch_has_merge_config(branch)) {
                        die(_("Branch '%s' has no upstream information"), branch->name);
                }
                int branch_existed = 0, remote_tracking = 0;
                struct strbuf buf = STRBUF_INIT;
  
+               if (!strcmp(argv[0], "HEAD"))
+                       die(_("it does not make sense to create 'HEAD' manually"));
+               if (!branch)
+                       die(_("no such branch '%s'"), argv[0]);
                if (kinds != REF_LOCAL_BRANCH)
                        die(_("-a and -r options to 'git branch' do not make sense with a branch name"));