git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Bug: git branch -D can be used to delete branch which is currently checked out - Part 2

From
Marcus Kida <marcus.kida@gmail.com>
Date
Mar 10, 2016, 21:20 UTC
Message-ID
<41E8F374-9E8A-48AE-A5D5-A3C24B79ED7E@gmail.com>
In-Reply-To
<alpine.DEB.2.20.1603101313220.4690@virtualbox>

Hi Johannes, Hi Junio,

here you’ll find a patch to hotfix the “delete-a-branch-you’re-on" issue. As Junio already stated there’s many more places where case (in)sensitivity is not handled correctly but this patch would at least prevent you from deleting the branch you’re currently working on (which happened to me yesterday and lead to quite some additional stress).

If you think this isn’t worth patching now, please discard my patch. If you think it’s worth patching and implementing a more advanced filesystem backend at a later time, please apply it.

If tested this and validate it works @ 2.8.0-rc1. 
Thank you.
---
diff --git a/builtin/branch.c b/builtin/branch.c
index 7b45b6b..46bde61 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -215,7 +215,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 		int flags = 0;
 
 		strbuf_branchname(&bname, argv[i]);
-		if (kinds == FILTER_REFS_BRANCHES && !strcmp(head, bname.buf)) {
+		if (kinds == FILTER_REFS_BRANCHES && !strcasecmp(head, bname.buf)) {
 			error(_("Cannot delete the branch '%s' "
 			      "which you are currently on."), bname.buf);
 			ret = 1;
---

Cheers,
Marcus

> On 10 Mar 2016, at 11:15 PM, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> 
> Hi Marcus,
> 
> On Thu, 10 Mar 2016, Marcus Kida wrote:
> 
>> Proposed solution:
>> 
>> Use `strcasecmp`, `stricmp`, `strcmpi` here: https://github.com/git/git/blob/f02fbc4f9433937ee0463d0342d6d7d97e1f6f1e/builtin/branch.c#L218
>> 
>> Not sure if/which one of this will work on POSIX as well as MS too though.
> 
> This is not quite a solution (it is not a patch). And you *definitely*
> want to make the use of strcasecmp() contingent on ignore_case. You are
> not alone on this world, after all, and other people have case-sensitive
> filesystems. It's totally doable, of course.
> 
> So, do you feel up to the task?
> 
> Ciao,
> Johannes
Previous: Marcus Kida
Message 6 of 6 in “Bug: git branch -D can be used to delete branch which is currently checked out - Part 2”
  1. Marcus KidaMar 10, 2016
  2. Johannes SchindelinMar 10, 2016
  3. Marcus KidaMar 10, 2016
  4. Jeff KingMar 10, 2016
  5. Marcus KidaMar 10, 2016
  6. Marcus KidaMar 10, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.