Skip to content

Commit 7a6ccdf

Browse files
rjustogitster
authored andcommitted
branch: description for orphan branch errors
In bcfc82b (branch: description for non-existent branch errors, 2022-10-08) we checked the HEAD in the current worktree to detect if the branch to operate with is an orphan branch, so as to avoid the confusing error: "No branch named...". If we are asked to operate with an orphan branch in a different working tree than the current one, we need to check the HEAD in that different working tree. Let's extend the check we did in bcfc82b, to check the HEADs in all worktrees linked to the current repository, using the helper introduced in 31ad6b6 (branch: add branch_checked_out() helper, 2022-06-15). The helper, branch_checked_out(), does its work obtaining internally a list of worktrees linked to the current repository. Obtaining that list is not a lightweight work because it implies disk access. In copy_or_rename_branch() we already have a list of worktrees. Let's use that already obtained list, and avoid using here the helper. Signed-off-by: Rubén Justo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d7f4ca6 commit 7a6ccdf

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

builtin/branch.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -538,13 +538,15 @@ static int replace_each_worktree_head_symref(struct worktree **worktrees,
538538
return ret;
539539
}
540540

541+
#define IS_HEAD 1
542+
541543
static void copy_or_rename_branch(const char *oldname, const char *newname, int copy, int force)
542544
{
543545
struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
544546
struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
545547
const char *interpreted_oldname = NULL;
546548
const char *interpreted_newname = NULL;
547-
int recovery = 0;
549+
int recovery = 0, oldref_usage = 0;
548550
struct worktree **worktrees = get_worktrees();
549551

550552
if (strbuf_check_branch_ref(&oldref, oldname)) {
@@ -558,8 +560,17 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
558560
die(_("Invalid branch name: '%s'"), oldname);
559561
}
560562

561-
if ((copy || strcmp(head, oldname)) && !ref_exists(oldref.buf)) {
562-
if (copy && !strcmp(head, oldname))
563+
for (int i = 0; worktrees[i]; i++) {
564+
struct worktree *wt = worktrees[i];
565+
566+
if (wt->head_ref && !strcmp(oldref.buf, wt->head_ref)) {
567+
oldref_usage |= IS_HEAD;
568+
break;
569+
}
570+
}
571+
572+
if ((copy || !(oldref_usage & IS_HEAD)) && !ref_exists(oldref.buf)) {
573+
if (oldref_usage & IS_HEAD)
563574
die(_("No commit on branch '%s' yet."), oldname);
564575
else
565576
die(_("No branch named '%s'."), oldname);
@@ -838,7 +849,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
838849

839850
strbuf_addf(&branch_ref, "refs/heads/%s", branch_name);
840851
if (!ref_exists(branch_ref.buf))
841-
error((!argc || !strcmp(head, branch_name))
852+
error((!argc || branch_checked_out(branch_ref.buf))
842853
? _("No commit on branch '%s' yet.")
843854
: _("No branch named '%s'."),
844855
branch_name);
@@ -883,7 +894,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
883894
}
884895

885896
if (!ref_exists(branch->refname)) {
886-
if (!argc || !strcmp(head, branch->name))
897+
if (!argc || branch_checked_out(branch->refname))
887898
die(_("No commit on branch '%s' yet."), branch->name);
888899
die(_("branch '%s' does not exist"), branch->name);
889900
}

t/t3202-show-branch.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,22 @@ test_expect_success 'fatal descriptions on non-existent branch' '
221221
test_cmp expect actual
222222
'
223223

224+
test_expect_success 'error descriptions on orphan branch' '
225+
test_when_finished git worktree remove -f wt &&
226+
git worktree add wt --detach &&
227+
git -C wt checkout --orphan orphan-branch &&
228+
test_branch_op_in_wt() {
229+
test_orphan_error() {
230+
test_must_fail git $* 2>actual &&
231+
test_i18ngrep "No commit on branch .orphan-branch. yet.$" actual
232+
} &&
233+
test_orphan_error -C wt branch $1 $2 && # implicit branch
234+
test_orphan_error -C wt branch $1 orphan-branch $2 && # explicit branch
235+
test_orphan_error branch $1 orphan-branch $2 # different worktree
236+
} &&
237+
test_branch_op_in_wt --edit-description &&
238+
test_branch_op_in_wt --set-upstream-to=ne &&
239+
test_branch_op_in_wt -c new-branch
240+
'
241+
224242
test_done

0 commit comments

Comments
 (0)