Commit Diff


commit - 4e99b47e5866daf36b7280aa372ca5e7046ac586
commit + ad89fa31184ff8af48516d163f1146ceb3a654f1
blob - 87b6cffd29ca3fa2f2dbb432731c506f690c7afe
blob + 3f9ea49a0adb808b6114d4b64c6330f312aa7b0c
--- got/got.1
+++ 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 .
blob - 6950da9faa544e63ac44b6ff455ff315b08686be
blob + 13dea80561dab55f3899f8130a265eb49dc72ebd
--- got/got.c
+++ 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_wo
 }
 
 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) {
@@ -3409,15 +3429,18 @@ 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);
blob - 1b883c4e2688ea9c09323b246a34eeedd90657cb
blob + 05f7d1c68bc1b11636a716791ffe719b6799e6b7
--- regress/cmdline/branch.sh
+++ regress/cmdline/branch.sh
@@ -369,8 +369,59 @@ function test_branch_delete_packed {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stderr.expected $testroot/stderr
+	fi
+	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
@@ -378,3 +429,4 @@ 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