🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.git
1 messages
1 total messages Started by "Harald Nordgren via GitGitGadge... Sat, 21 Nov 2026 20:41
[PATCH v10 2/5] branch: let delete_branches() name where a branch landed
#3554
Author: "Harald Nordgren via GitGitGadge...
Date: Sat, 21 Nov 2026 20:41
134 lines
15856 bytes
From: Harald Nordgren <haraldnordgren@gmail.com>

A later commit lets "git branch" delete branches whose commits are not
in their upstream but whose changes are, such as after a squash merge.
Their tips are not reachable from the upstream, so the merge check in
delete_branches() would refuse to delete them, and the output should
say where their changes went.

Let delete_branches() take a map from branch names to the upstream
commit where each branch landed. A branch in the map skips the merge
check and is reported as

    Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).

No caller passes a map yet, so there is no change in behavior.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 builtin/branch.c | 58 ++++++++++++++++++++++++++++++++----------------
 1 file changed, 39 insertions(+), 19 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 7ccb36364a..b7c3554b63 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -236,7 +236,7 @@ static void delete_branch_config(const char *branchname)
 }
 
 static int delete_branches(int argc, const char **argv, int kinds,
-			   unsigned int flags)
+			   unsigned int flags, struct strmap *landed_commits)
 {
 	struct commit *head_rev = NULL;
 	struct object_id oid;
@@ -251,6 +251,20 @@ static int delete_branches(int argc, const char **argv, int kinds,
 	struct string_list_item *item;
 	int branch_name_pos;
 	const char *fmt_remotes = "refs/remotes/%s";
+	static const char *const msg_templates[2][3] = {
+		{
+			N_("Would delete branch %s (was %s, landed as %s).\n"),
+			N_("Would delete remote-tracking branch %s (was %s).\n"),
+			N_("Would delete branch %s (was %s).\n"),
+		},
+		{
+			N_("Deleted branch %s (was %s, landed as %s).\n"),
+			N_("Deleted remote-tracking branch %s (was %s).\n"),
+			N_("Deleted branch %s (was %s).\n"),
+		},
+	};
+	bool dry_run = flags & DELETE_BRANCH_DRY_RUN;
+	const char *const *use_msg = msg_templates[!dry_run];
 
 	switch (kinds) {
 	case FILTER_REFS_REMOTES:
@@ -334,6 +348,8 @@ static int delete_branches(int argc, const char **argv, int kinds,
 		}
 
 		if (!(ref_flags & (REF_ISSYMREF|REF_ISBROKEN)) &&
+		    !(landed_commits &&
+		      strmap_contains(landed_commits, bname.buf)) &&
 		    check_branch_commit(bname.buf, name, &oid, head_rev, kinds,
 					flags)) {
 			if (!(flags & DELETE_BRANCH_SKIP_UNMERGED))
@@ -350,28 +366,32 @@ static int delete_branches(int argc, const char **argv, int kinds,
 		free(target);
 	}
 
-	if (!(flags & DELETE_BRANCH_DRY_RUN) &&
+	if (!dry_run &&
 	    refs_delete_refs(get_main_ref_store(the_repository), NULL, &refs_to_delete, REF_NO_DEREF))
 		ret = 1;
 
 	for_each_string_list_item(item, &refs_to_delete) {
 		char *describe_ref = item->util;
 		char *name = item->string;
-		if (flags & DELETE_BRANCH_DRY_RUN) {
-			if (!(flags & DELETE_BRANCH_QUIET))
-				printf(remote_branch
-					? _("Would delete remote-tracking branch %s (was %s).\n")
-					: _("Would delete branch %s (was %s).\n"),
-					name + branch_name_pos, describe_ref);
-		} else if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {
-			char *refname = name + branch_name_pos;
-			if (!(flags & DELETE_BRANCH_QUIET))
-				printf(remote_branch
-					? _("Deleted remote-tracking branch %s (was %s).\n")
-					: _("Deleted branch %s (was %s).\n"),
-					name + branch_name_pos, describe_ref);
-
-			delete_branch_config(refname);
+		char *refname = name + branch_name_pos;
+		struct commit *landed = landed_commits ?
+			strmap_get(landed_commits, refname) : NULL;
+
+		if (dry_run ||
+		    !refs_ref_exists(get_main_ref_store(the_repository), name)) {
+			if (flags & DELETE_BRANCH_QUIET)
+				;
+			else if (landed)
+				printf(_(use_msg[0]), refname, describe_ref,
+				       repo_find_unique_abbrev(the_repository,
+							       &landed->object.oid,
+							       DEFAULT_ABBREV));
+			else if (remote_branch)
+				printf(_(use_msg[1]), refname, describe_ref);
+			else
+				printf(_(use_msg[2]), refname, describe_ref);
+			if (!dry_run)
+				delete_branch_config(refname);
 		}
 		free(describe_ref);
 	}
@@ -902,7 +922,7 @@ static int delete_merged_branches(const struct strvec *upstreams,
 				      FILTER_REFS_BRANCHES,
 				      DELETE_BRANCH_SKIP_UNMERGED |
 				      DELETE_BRANCH_NO_HEAD_FALLBACK |
-				      flags);
+				      flags, NULL);
 
 	if (!ret && !(flags & DELETE_BRANCH_DRY_RUN))
 		clear_deleted_upstreams(&protected_branch_names,
@@ -1142,7 +1162,7 @@ int cmd_branch(int argc,
 			die(_("branch name required"));
 		ret = delete_branches(argc, argv, filter.kind,
 				      (delete > 1 ? DELETE_BRANCH_FORCE : 0) |
-				      (quiet ? DELETE_BRANCH_QUIET : 0));
+				      (quiet ? DELETE_BRANCH_QUIET : 0), NULL);
 		goto out;
 	} else if (delete_merged.nr) {
 		ret = delete_merged_branches(&delete_merged, argv,
-- 
gitgitgadget
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads