Commit ad89fa31184ff8af48516d163f1146ceb3a654f1

Stefan Sperling 2019-10-04T15:56:03

make 'got branch' without args show work tree's branch; requested by benno@

diff --git a/got/got.1 b/got/got.1
index 87b6cff..3f9ea49 100644
--- a/got/got.1
+++ b/got/got.1
@@ -514,8 +514,10 @@ The
 .Cm got branch
 command operates on references in this namespace only.
 .Pp
-If no options are passed, expect one or two arguments and attempt to create
-a branch reference with the given
+If invoked in a work tree without any arguments, print the name of the
+work tree's current branch.
+Otherwise, if no options are passed, expect one or two arguments and attempt
+to create a branch reference with the given
 .Ar name ,
 and make it point at the given
 .Ar commit .
diff --git a/got/got.c b/got/got.c
index 6950da9..13dea80 100644
--- a/got/got.c
+++ b/got/got.c
@@ -3207,8 +3207,8 @@ __dead static void
 usage_branch(void)
 {
 	fprintf(stderr,
-	    "usage: %s branch [-r repository] -l | -d name | "
-	    "name [commit]\n", getprogname());
+	    "usage: %s branch [-r repository] [-l] | -d name | "
+	    "[name [commit]]\n", getprogname());
 	exit(1);
 }
 
@@ -3251,6 +3251,26 @@ list_branch(struct got_repository *repo, struct got_worktree *worktree,
 }
 
 static const struct got_error *
+show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
+{
+	const char *refname;
+
+	if (worktree == NULL)
+		return got_error(GOT_ERR_NOT_WORKTREE);
+
+	refname = got_worktree_get_head_ref_name(worktree);
+
+	if (strncmp(refname, "refs/heads/", 11) == 0)
+		refname += 11;
+	if (strncmp(refname, "refs/got/worktree/", 18) == 0)
+		refname += 18;
+
+	printf("%s\n", refname);
+
+	return NULL;
+}
+
+static const struct got_error *
 list_branches(struct got_repository *repo, struct got_worktree *worktree)
 {
 	static const struct got_error *err = NULL;
@@ -3381,7 +3401,7 @@ cmd_branch(int argc, char *argv[])
 	struct got_repository *repo = NULL;
 	struct got_worktree *worktree = NULL;
 	char *cwd = NULL, *repo_path = NULL;
-	int ch, do_list = 0;
+	int ch, do_list = 0, do_show = 0;
 	const char *delref = NULL;
 
 	while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
@@ -3410,14 +3430,17 @@ cmd_branch(int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
+	if (!do_list && !delref && argc == 0)
+		do_show = 1;
+
 	if (do_list || delref) {
 		if (argc > 0)
 			usage_branch();
-	} else if (argc < 1 || argc > 2)
+	} else if (!do_show && (argc < 1 || argc > 2))
 		usage_branch();
 
 #ifndef PROFILE
-	if (do_list) {
+	if (do_list || do_show) {
 		if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
 		    NULL) == -1)
 			err(1, "pledge");
@@ -3464,7 +3487,9 @@ cmd_branch(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	if (do_list)
+	if (do_show)
+		error = show_current_branch(repo, worktree);
+	else if (do_list)
 		error = list_branches(repo, worktree);
 	else if (delref)
 		error = delete_branch(repo, worktree, delref);
diff --git a/regress/cmdline/branch.sh b/regress/cmdline/branch.sh
index 1b883c4..05f7d1c 100755
--- a/regress/cmdline/branch.sh
+++ b/regress/cmdline/branch.sh
@@ -373,8 +373,60 @@ function test_branch_delete_packed {
 	test_done "$testroot" "$ret"
 }
 
+function test_branch_show {
+	local testroot=`test_init branch_show`
+	local commit_id=`git_show_head $testroot/repo`
+
+	for b in branch1 branch2 branch3; do
+		got branch -r $testroot/repo $b
+		ret="$?"
+		if [ "$ret" != "0" ]; then
+			echo "got branch command failed unexpectedly"
+			test_done "$testroot" "$ret"
+			return 1
+		fi
+	done
+
+	got checkout $testroot/repo $testroot/wt >/dev/null
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got checkout command failed unexpectedly"
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	(cd $testroot/wt && got branch > $testroot/stdout)
+	echo "master" > $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	(cd $testroot/wt && got update -b branch1 > /dev/null)
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got update command failed unexpectedly"
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	(cd $testroot/wt && got branch > $testroot/stdout)
+	echo "branch1" > $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" "$ret"
+
+}
+
 run_test test_branch_create
 run_test test_branch_list
 run_test test_branch_delete
 run_test test_branch_delete_current_branch
 run_test test_branch_delete_packed
+run_test test_branch_show