make 'got branch' without args show work tree's branch; requested by benno@
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
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