Skip to content

Commit 0faff98

Browse files
committed
Merge branch 'ks/branch-cleanup'
Code clean-up. * ks/branch-cleanup: builtin/branch: strip refs/heads/ using skip_prefix branch: update warning message shown when copying a misnamed branch branch: group related arguments of create_branch() branch: improve documentation and naming of create_branch() parameters
2 parents a13e45f + 255073c commit 0faff98

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

branch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ N_("\n"
244244
"\"git push -u\" to set the upstream config as you push.");
245245

246246
void create_branch(const char *name, const char *start_name,
247-
int force, int reflog, int clobber_head,
247+
int force, int clobber_head_ok, int reflog,
248248
int quiet, enum branch_track track)
249249
{
250250
struct commit *commit;
@@ -258,7 +258,7 @@ void create_branch(const char *name, const char *start_name,
258258
if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
259259
explicit_tracking = 1;
260260

261-
if ((track == BRANCH_TRACK_OVERRIDE || clobber_head)
261+
if ((track == BRANCH_TRACK_OVERRIDE || clobber_head_ok)
262262
? validate_branchname(name, &ref)
263263
: validate_new_branchname(name, &ref, force)) {
264264
if (!force)

branch.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,20 @@
1313
*
1414
* - force enables overwriting an existing (non-head) branch
1515
*
16+
* - clobber_head_ok allows the currently checked out (hence existing)
17+
* branch to be overwritten; without 'force', it has no effect.
18+
*
1619
* - reflog creates a reflog for the branch
1720
*
21+
* - quiet suppresses tracking information
22+
*
1823
* - track causes the new branch to be configured to merge the remote branch
1924
* that start_name is a tracking branch for (if any).
25+
*
2026
*/
2127
void create_branch(const char *name, const char *start_name,
22-
int force, int reflog,
23-
int clobber_head, int quiet, enum branch_track track);
28+
int force, int clobber_head_ok,
29+
int reflog, int quiet, enum branch_track track);
2430

2531
/*
2632
* Check if 'name' can be a valid name for a branch; die otherwise.

builtin/branch.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
462462
{
463463
struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
464464
struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
465+
const char *interpreted_oldname = NULL;
466+
const char *interpreted_newname = NULL;
465467
int recovery = 0;
466468

467469
if (!oldname) {
@@ -493,6 +495,11 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
493495

494496
reject_rebase_or_bisect_branch(oldref.buf);
495497

498+
if (!skip_prefix(oldref.buf, "refs/heads/", &interpreted_oldname) ||
499+
!skip_prefix(newref.buf, "refs/heads/", &interpreted_newname)) {
500+
die("BUG: expected prefix missing for refs");
501+
}
502+
496503
if (copy)
497504
strbuf_addf(&logmsg, "Branch: copied %s to %s",
498505
oldref.buf, newref.buf);
@@ -507,11 +514,11 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
507514

508515
if (recovery) {
509516
if (copy)
510-
warning(_("Copied a misnamed branch '%s' away"),
511-
oldref.buf + 11);
517+
warning(_("Created a copy of a misnamed branch '%s'"),
518+
interpreted_oldname);
512519
else
513520
warning(_("Renamed a misnamed branch '%s' away"),
514-
oldref.buf + 11);
521+
interpreted_oldname);
515522
}
516523

517524
if (!copy &&
@@ -520,9 +527,9 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
520527

521528
strbuf_release(&logmsg);
522529

523-
strbuf_addf(&oldsection, "branch.%s", oldref.buf + 11);
530+
strbuf_addf(&oldsection, "branch.%s", interpreted_oldname);
524531
strbuf_release(&oldref);
525-
strbuf_addf(&newsection, "branch.%s", newref.buf + 11);
532+
strbuf_addf(&newsection, "branch.%s", interpreted_newname);
526533
strbuf_release(&newref);
527534
if (!copy && git_config_rename_section(oldsection.buf, newsection.buf) < 0)
528535
die(_("Branch is renamed, but update of config-file failed"));
@@ -806,7 +813,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
806813
die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead."));
807814

808815
create_branch(argv[0], (argc == 2) ? argv[1] : head,
809-
force, reflog, 0, quiet, track);
816+
force, 0, reflog, quiet, track);
810817

811818
} else
812819
usage_with_options(builtin_branch_usage, options);

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
647647
else
648648
create_branch(opts->new_branch, new->name,
649649
opts->new_branch_force ? 1 : 0,
650-
opts->new_branch_log,
651650
opts->new_branch_force ? 1 : 0,
651+
opts->new_branch_log,
652652
opts->quiet,
653653
opts->track);
654654
new->name = opts->new_branch;

0 commit comments

Comments
 (0)