From f6cea74de63c2926a15b711bec09ed3ab34c2c1e Mon Sep 17 00:00:00 2001 From: Kaartic Sivaraam Date: Sat, 18 Nov 2017 22:56:45 +0530 Subject: branch: improve documentation and naming of create_branch() parameters The documentation for 'create_branch()' was incomplete as it didn't say what certain parameters were used for. Further a parameter name wasn't very communicative. So, add missing documentation for the sake of completeness and easy reference. Also, rename the concerned parameter to make its name more communicative. Signed-off-by: Kaartic Sivaraam Signed-off-by: Junio C Hamano diff --git a/branch.c b/branch.c index 62f7b0d..3e8d2f9 100644 --- a/branch.c +++ b/branch.c @@ -228,7 +228,7 @@ N_("\n" "\"git push -u\" to set the upstream config as you push."); void create_branch(const char *name, const char *start_name, - int force, int reflog, int clobber_head, + int force, int reflog, int clobber_head_ok, int quiet, enum branch_track track) { struct commit *commit; @@ -244,7 +244,7 @@ void create_branch(const char *name, const char *start_name, if (validate_new_branchname(name, &ref, force, track == BRANCH_TRACK_OVERRIDE || - clobber_head)) { + clobber_head_ok)) { if (!force) dont_change_ref = 1; else diff --git a/branch.h b/branch.h index b077885..cb6411f 100644 --- a/branch.h +++ b/branch.h @@ -15,12 +15,17 @@ * * - reflog creates a reflog for the branch * + * - clobber_head_ok allows the currently checked out (hence existing) + * branch to be overwritten; without 'force', it has no effect. + * + * - quiet suppresses tracking information + * * - track causes the new branch to be configured to merge the remote branch * that start_name is a tracking branch for (if any). */ void create_branch(const char *name, const char *start_name, int force, int reflog, - int clobber_head, int quiet, enum branch_track track); + int clobber_head_ok, int quiet, enum branch_track track); /* * Validates that the requested branch may be created, returning the -- cgit v0.10.2-6-g49f6 From e2bbd0cc4c1b661cc04620a6704003793b190e7e Mon Sep 17 00:00:00 2001 From: Kaartic Sivaraam Date: Sat, 18 Nov 2017 22:56:46 +0530 Subject: branch: group related arguments of create_branch() 39bd6f726 (Allow checkout -B to update the current branch, 2011-11-26) added 'clobber_head' (now, 'clobber_head_ok') "before" 'track' as 'track' was closely related 'clobber_head' for the purpose the commit wanted to achieve. Looking from the perspective of how the arguments are used it turns out that 'clobber_head' is more related to 'force' than it is to 'track'. So, re-order the arguments to keep the related arguments close to each other. Signed-off-by: Kaartic Sivaraam Signed-off-by: Junio C Hamano diff --git a/branch.c b/branch.c index 3e8d2f9..bd607ae 100644 --- a/branch.c +++ b/branch.c @@ -228,7 +228,7 @@ N_("\n" "\"git push -u\" to set the upstream config as you push."); void create_branch(const char *name, const char *start_name, - int force, int reflog, int clobber_head_ok, + int force, int clobber_head_ok, int reflog, int quiet, enum branch_track track) { struct commit *commit; diff --git a/branch.h b/branch.h index cb6411f..f66536a 100644 --- a/branch.h +++ b/branch.h @@ -13,19 +13,20 @@ * * - force enables overwriting an existing (non-head) branch * - * - reflog creates a reflog for the branch - * * - clobber_head_ok allows the currently checked out (hence existing) * branch to be overwritten; without 'force', it has no effect. * + * - reflog creates a reflog for the branch + * * - quiet suppresses tracking information * * - track causes the new branch to be configured to merge the remote branch * that start_name is a tracking branch for (if any). + * */ void create_branch(const char *name, const char *start_name, - int force, int reflog, - int clobber_head_ok, int quiet, enum branch_track track); + int force, int clobber_head_ok, + int reflog, int quiet, enum branch_track track); /* * Validates that the requested branch may be created, returning the diff --git a/builtin/branch.c b/builtin/branch.c index 33fd5fc..4edef5b 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -806,7 +806,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead.")); create_branch(argv[0], (argc == 2) ? argv[1] : head, - force, reflog, 0, quiet, track); + force, 0, reflog, quiet, track); } else usage_with_options(builtin_branch_usage, options); diff --git a/builtin/checkout.c b/builtin/checkout.c index 7d8bcc3..6068f8d 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -640,8 +640,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts, else create_branch(opts->new_branch, new->name, opts->new_branch_force ? 1 : 0, - opts->new_branch_log, opts->new_branch_force ? 1 : 0, + opts->new_branch_log, opts->quiet, opts->track); new->name = opts->new_branch; -- cgit v0.10.2-6-g49f6 From a48ebe9724b01e53f23e2a44a85f52237aaf5c34 Mon Sep 17 00:00:00 2001 From: Kaartic Sivaraam Date: Sat, 18 Nov 2017 22:56:47 +0530 Subject: branch: update warning message shown when copying a misnamed branch When a user tries to rename a branch that has a "bad name" (e.g., starts with a '-') then we warn them that the misnamed branch has been renamed "away". A similar message is shown when trying to create a copy of a misnamed branch even though it doesn't remove the misnamed branch. This is not correct and may confuse the user. So, update the warning message shown to be more precise that only a copy of the misnamed branch has been created. It's better to show the warning message than not showing it at all as it makes the user aware of the presence of a misnamed branch. Signed-off-by: Kaartic Sivaraam Signed-off-by: Junio C Hamano diff --git a/builtin/branch.c b/builtin/branch.c index 4edef5b..ca9d8ab 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -507,7 +507,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int if (recovery) { if (copy) - warning(_("Copied a misnamed branch '%s' away"), + warning(_("Created a copy of a misnamed branch '%s'"), oldref.buf + 11); else warning(_("Renamed a misnamed branch '%s' away"), -- cgit v0.10.2-6-g49f6 From 255073ca59de7cd293e4a9d6c12831b6e505c37e Mon Sep 17 00:00:00 2001 From: Kaartic Sivaraam Date: Fri, 1 Dec 2017 11:29:33 +0530 Subject: builtin/branch: strip refs/heads/ using skip_prefix Instead of hard-coding the offset strlen("refs/heads/") to skip the prefix "refs/heads/" use the skip_prefix() function which is more communicative and verifies that the string actually starts with that prefix. Signed-off-by: Kaartic Sivaraam Signed-off-by: Junio C Hamano diff --git a/builtin/branch.c b/builtin/branch.c index ca9d8ab..196d5fe 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -462,6 +462,8 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int { struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT; struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT; + const char *interpreted_oldname = NULL; + const char *interpreted_newname = NULL; int recovery = 0; int clobber_head_ok; @@ -493,6 +495,11 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int reject_rebase_or_bisect_branch(oldref.buf); + if (!skip_prefix(oldref.buf, "refs/heads/", &interpreted_oldname) || + !skip_prefix(newref.buf, "refs/heads/", &interpreted_newname)) { + die("BUG: expected prefix missing for refs"); + } + if (copy) strbuf_addf(&logmsg, "Branch: copied %s to %s", oldref.buf, newref.buf); @@ -508,10 +515,10 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int if (recovery) { if (copy) warning(_("Created a copy of a misnamed branch '%s'"), - oldref.buf + 11); + interpreted_oldname); else warning(_("Renamed a misnamed branch '%s' away"), - oldref.buf + 11); + interpreted_oldname); } if (!copy && @@ -520,9 +527,9 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int strbuf_release(&logmsg); - strbuf_addf(&oldsection, "branch.%s", oldref.buf + 11); + strbuf_addf(&oldsection, "branch.%s", interpreted_oldname); strbuf_release(&oldref); - strbuf_addf(&newsection, "branch.%s", newref.buf + 11); + strbuf_addf(&newsection, "branch.%s", interpreted_newname); strbuf_release(&newref); if (!copy && git_config_rename_section(oldsection.buf, newsection.buf) < 0) die(_("Branch is renamed, but update of config-file failed")); -- cgit v0.10.2-6-g49f6