Skip to content

Commit 4c643fb

Browse files
ctmblgitster
authored andcommitted
branch: improve error log on branch not found by checking remotes refs
New git users may want to locally delete remote-tracking branches but don't really understand how they are distinguished from branches by git. Then one may naively try: `git branch -d foo/bar` and get a correct error `branch foo/bar not found` but hard to understand for a newbie, this patch aims to guide one in such case. when failing to delete a branch with `git branch -d <branch>` because of branch not found, try to find a **remote refs** matching `<branch>` and if so, add an hint: `Did you forget --remote?` to the error message Signed-off-by: Clement Mabileau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ae73b2c commit 4c643fb

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

builtin/branch.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,11 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
216216
struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
217217
struct string_list_item *item;
218218
int branch_name_pos;
219+
const char *fmt_remotes = "refs/remotes/%s";
219220

220221
switch (kinds) {
221222
case FILTER_REFS_REMOTES:
222-
fmt = "refs/remotes/%s";
223+
fmt = fmt_remotes;
223224
/* For subsequent UI messages */
224225
remote_branch = 1;
225226
allowed_interpret = INTERPRET_BRANCH_REMOTE;
@@ -263,9 +264,25 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
263264
| RESOLVE_REF_ALLOW_BAD_NAME,
264265
&oid, &flags);
265266
if (!target) {
266-
error(remote_branch
267-
? _("remote-tracking branch '%s' not found.")
268-
: _("branch '%s' not found."), bname.buf);
267+
if (remote_branch) {
268+
error(_("remote-tracking branch '%s' not found."), bname.buf);
269+
} else {
270+
char *virtual_name = mkpathdup(fmt_remotes, bname.buf);
271+
char *virtual_target = resolve_refdup(virtual_name,
272+
RESOLVE_REF_READING
273+
| RESOLVE_REF_NO_RECURSE
274+
| RESOLVE_REF_ALLOW_BAD_NAME,
275+
&oid, &flags);
276+
FREE_AND_NULL(virtual_name);
277+
278+
if (virtual_target)
279+
error(_("branch '%s' not found.\n"
280+
"Did you forget --remote?"),
281+
bname.buf);
282+
else
283+
error(_("branch '%s' not found."), bname.buf);
284+
FREE_AND_NULL(virtual_target);
285+
}
269286
ret = 1;
270287
continue;
271288
}

0 commit comments

Comments
 (0)