From patchwork Thu Aug 26 18:19:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 12460453 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.2 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 903CEC432BE for ; Thu, 26 Aug 2021 18:19:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7AE9360FC1 for ; Thu, 26 Aug 2021 18:19:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243332AbhHZSUK (ORCPT ); Thu, 26 Aug 2021 14:20:10 -0400 Received: from mout.web.de ([212.227.15.4]:41307 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243329AbhHZSUJ (ORCPT ); Thu, 26 Aug 2021 14:20:09 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1630001955; bh=V6gbBHbRB1vDP5PGMsa5+BfP8B72V9GzUHv0Joa126o=; h=X-UI-Sender-Class:Subject:From:To:Cc:References:Date:In-Reply-To; b=NogSqCH4CnAO8TGuX94QYENfGUE8W3594+r2ApQNbPqs3vHRu6LxLlrjPOqYsd753 Alx3uJL2um3IwiToAkh/R4+PBAcYlG3Lx6WpPIJIe4SeP7BDKPioVpbkDp0Ry+7XM4 Ci+8Co1pajLLtUnniByvd5+hMeWlHr6jSokBqezQ= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from Mini-von-Rene.fritz.box ([79.203.27.185]) by smtp.web.de (mrweb006 [213.165.67.108]) with ESMTPSA (Nemesis) id 1MKdLM-1mbwml3G4i-00LCC9; Thu, 26 Aug 2021 20:19:15 +0200 Subject: [PATCH v2] branch: allow deleting dangling branches with --force From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Junio C Hamano , Ulrich Windl , =?utf-8?b?w4Z2YXIgQXJu?= =?utf-8?b?ZmrDtnLDsCBCamFybWFzb24=?= , Han-Wen Nienhuys References: <7894f736-4681-7656-e2d4-5945d2c71d31@web.de> Message-ID: <325d64e9-8a31-6ba0-73f2-5e9d67b8682f@web.de> Date: Thu, 26 Aug 2021 20:19:15 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: <7894f736-4681-7656-e2d4-5945d2c71d31@web.de> Content-Language: en-US X-Provags-ID: V03:K1:koGSlEpd9XrKuzD5sk11tyjWfXVP6D0FWidVnu7j8IInC/OkzK9 EM0HTXd1T8IQI6JwbL5tH3DnGlYo3YOL83gZFBpZP9gfBIE5xmFWbhs2cjV6xMfG+2tTee8 OPj7rLTKgECPFa7yvttyQj1RtpXtLR9OhxSU23BWfrcjWgLupeNHl6D/K1Z+VqKqidUE9ji /alv9gzco3xfzv7GuzvtQ== X-UI-Out-Filterresults: notjunk:1;V03:K0:d2lrs6RbkJo=:MRnGmv1Jazwxi/JEgkgMQ1 8sAZj0pv3iIuOIfJ3mOovGMMsuz3+wlyeAEc/KDoc82pFYHwCqdbHfSCtwJ7ENpVwHjB3mzlH VFSh/ux5ISFkbJyRiTEPUg6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only allows deleting branches that point to valid commits. Skip that check if --force is given, as the caller is indicating with it that they know what they are doing and accept the consequences. This allows deleting dangling branches, which previously had to be reset to a valid start-point using --force first. Reported-by: Ulrich Windl Helped-by: Ævar Arnfjörð Bjarmason Helped-by: Junio C Hamano Signed-off-by: René Scharfe --- Changes since v1: - Added Reported-by and Helped-by. - Made test independent of ref store. Documentation/git-branch.txt | 3 ++- builtin/branch.c | 2 +- t/t3200-branch.sh | 12 ++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) -- 2.33.0 diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt index 94dc9a54f2..5449767121 100644 --- a/Documentation/git-branch.txt +++ b/Documentation/git-branch.txt @@ -118,7 +118,8 @@ OPTIONS Reset to , even if exists already. Without `-f`, 'git branch' refuses to change an existing branch. In combination with `-d` (or `--delete`), allow deleting the - branch irrespective of its merged status. In combination with + branch irrespective of its merged status, or whether it even + points to a valid commit. In combination with `-m` (or `--move`), allow renaming the branch even if the new branch name already exists, the same applies for `-c` (or `--copy`). diff --git a/builtin/branch.c b/builtin/branch.c index b23b1d1752..03c7b7253a 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -168,7 +168,7 @@ static int check_branch_commit(const char *branchname, const char *refname, int kinds, int force) { struct commit *rev = lookup_commit_reference(the_repository, oid); - if (!rev) { + if (!force && !rev) { error(_("Couldn't look up commit object for '%s'"), refname); return -1; } diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index cc4b10236e..d0d28c8ea7 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -1272,6 +1272,18 @@ test_expect_success 'attempt to delete a branch merged to its base' ' test_must_fail git branch -d my10 ' +test_expect_success 'branch --delete --force removes dangling branch' ' + git checkout main && + test_commit unstable && + hash=$(git rev-parse HEAD) && + objpath=$(echo $hash | sed -e "s|^..|.git/objects/&/|") && + git branch --no-track dangling && + test_when_finished "test -f $objpath.x && mv $objpath.x $objpath" && + mv $objpath $objpath.x && + git branch --delete --force dangling && + test -z "$(git for-each-ref refs/heads/dangling)" +' + test_expect_success 'use --edit-description' ' write_script editor <<-\EOF && echo "New contents" >"$1"