"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Stefan Sperling <stsp@stsp.name>
Subject:
got branch: support multiple -d options
To:
gameoftrees@openbsd.org
Date:
Sun, 15 May 2022 13:47:30 +0200

Download raw body.

Thread
If multiple -d options are passed to 'got branch' all arguments
except the last one end up being ignored.

It seems more useful to delete all specified branches instead.

ok?
 
diff 50fa49718233a5cfbfc5160b8dd184d7b203ad3a eb22755e7b25da7c4de1dc247d135cfb1a6c0d7f
blob - b91cfb2cb4963721a9a86be9c2cdb397bc1a9278
blob + 8f10ab729ced9f0f2cc04a33336f59d048d9acb9
--- got/got.1
+++ got/got.1
@@ -1155,9 +1155,11 @@ from the
 or
 .Dq refs/remotes
 reference namespace.
+This option may be specified multiple times to build a list of branches
+to delete.
 .Pp
-Only the branch reference is deleted.
-Any commit, tree, and blob objects belonging to the branch
+Only branch references are deleted.
+Any commit, tree, and blob objects belonging to a branch
 remain in the repository and may be removed separately with
 Git's garbage collector or
 .Cm gotadmin cleanup .
blob - 80e5d2fbcd031246f1194edf450fa9da6e1a2901
blob + 0333604cb5526b7fae12fa0535bed75d9a437843
--- got/got.c
+++ got/got.c
@@ -6153,7 +6153,8 @@ cmd_branch(int argc, char *argv[])
 	struct got_worktree *worktree = NULL;
 	char *cwd = NULL, *repo_path = NULL;
 	int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
-	const char *delref = NULL, *commit_id_arg = NULL;
+	const char *commit_id_arg = NULL;
+	struct got_pathlist_head delrefs;
 	struct got_reference *ref = NULL;
 	struct got_pathlist_head paths;
 	struct got_pathlist_entry *pe;
@@ -6161,6 +6162,7 @@ cmd_branch(int argc, char *argv[])
 	char *commit_id_str = NULL;
 
 	TAILQ_INIT(&paths);
+	TAILQ_INIT(&delrefs);
 
 	while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
 		switch (ch) {
@@ -6168,7 +6170,9 @@ cmd_branch(int argc, char *argv[])
 			commit_id_arg = optarg;
 			break;
 		case 'd':
-			delref = optarg;
+			error = got_pathlist_append(&delrefs, optarg, NULL);
+			if (error)
+				return error;
 			break;
 		case 'r':
 			repo_path = realpath(optarg, NULL);
@@ -6192,7 +6196,7 @@ cmd_branch(int argc, char *argv[])
 		}
 	}
 
-	if (do_list && delref)
+	if (do_list && !TAILQ_EMPTY(&delrefs))
 		option_conflict('l', 'd');
 	if (sort_by_time && !do_list)
 		errx(1, "-t option requires -l option");
@@ -6200,13 +6204,14 @@ cmd_branch(int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
-	if (!do_list && !delref && argc == 0)
+	if (!do_list && TAILQ_EMPTY(&delrefs) && argc == 0)
 		do_show = 1;
 
-	if ((do_list || delref || do_show) && commit_id_arg != NULL)
+	if ((do_list || !TAILQ_EMPTY(&delrefs) || do_show) &&
+	    commit_id_arg != NULL)
 		errx(1, "-c option can only be used when creating a branch");
 
-	if (do_list || delref) {
+	if (do_list || !TAILQ_EMPTY(&delrefs)) {
 		if (argc > 0)
 			usage_branch();
 	} else if (!do_show && argc != 1)
@@ -6264,9 +6269,15 @@ cmd_branch(int argc, char *argv[])
 		error = show_current_branch(repo, worktree);
 	else if (do_list)
 		error = list_branches(repo, worktree, sort_by_time);
-	else if (delref)
-		error = delete_branch(repo, worktree, delref);
-	else {
+	else if (!TAILQ_EMPTY(&delrefs)) {
+		struct got_pathlist_entry *pe;
+		TAILQ_FOREACH(pe, &delrefs, entry) {
+			const char *delref = pe->path;
+			error = delete_branch(repo, worktree, delref);
+			if (error)
+				goto done;
+		}
+	} else {
 		struct got_reflist_head refs;
 		TAILQ_INIT(&refs);
 		error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
@@ -6344,6 +6355,7 @@ done:
 	TAILQ_FOREACH(pe, &paths, entry)
 		free((char *)pe->path);
 	got_pathlist_free(&paths);
+	got_pathlist_free(&delrefs);
 	return error;
 }
 
blob - 8e459f575af5f369a7a0987fa5f5eb2e1680b947
blob + c32a7e87abd5f17d60003589abc7aeccc08a3240
--- regress/cmdline/branch.sh
+++ regress/cmdline/branch.sh
@@ -323,7 +323,8 @@ test_branch_delete() {
 		return 1
 	fi
 
-	got branch -d origin/branch1 -r $testroot/repo > $testroot/stdout
+	got branch -d origin/branch1 -d refs/remotes/origin/branch3 \
+		-r $testroot/repo > $testroot/stdout
 	ret=$?
 	if [ $ret -ne 0 ]; then
 		echo "got branch command failed unexpectedly"
@@ -331,15 +332,6 @@ test_branch_delete() {
 		return 1
 	fi
 
-	got branch -d refs/remotes/origin/branch3 -r $testroot/repo \
-		> $testroot/stdout
-	ret=$?
-	if [ $ret -ne 0 ]; then
-		echo "got branch command failed unexpectedly"
-		test_done "$testroot" "$ret"
-		return 1
-	fi
-
 	got ref -l -r $testroot/repo > $testroot/stdout
 	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
 	echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected