allow deletion of refs/remotes/ branches with got branch -d Also, make requirements for branch name arguments more flexible. Absolute reference names are now accepted. ok naddy@
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
diff --git a/got/got.1 b/got/got.1
index 6cbbf9b..0dcc266 100644
--- a/got/got.1
+++ b/got/got.1
@@ -1014,7 +1014,16 @@ Local branches are managed via references which live in the
reference namespace.
The
.Cm got branch
-command creates or deletes references in this namespace only.
+command creates references in this namespace only.
+.Pp
+When deleting branches the specified
+.Ar name
+is searched in the
+.Dq refs/heads
+reference namespace first.
+If no corresponding branch is found the
+.Dq refs/remotes
+namespace will be searched next.
.Pp
If invoked in a work tree without any arguments, print the name of the
work tree's current branch.
@@ -1073,7 +1082,14 @@ with one the following annotations:
.It \(a~ Ta work tree's base commit is out-of-date
.El
.It Fl d Ar name
-Delete the branch with the specified name from the repository.
+Delete the branch with the specified
+.Ar name
+from the
+.Dq refs/heads
+or
+.Dq refs/remotes
+reference namespace.
+.Pp
Only the branch reference is deleted.
Any commit, tree, and blob objects belonging to the branch
remain in the repository and may be removed separately with
diff --git a/got/got.c b/got/got.c
index 5f57676..eaabe5b 100644
--- a/got/got.c
+++ b/got/got.c
@@ -5732,14 +5732,39 @@ delete_branch(struct got_repository *repo, struct got_worktree *worktree,
{
const struct got_error *err = NULL;
struct got_reference *ref = NULL;
- char *refname;
+ char *refname, *remote_refname = NULL;
+
+ if (strncmp(branch_name, "refs/", 5) == 0)
+ branch_name += 5;
+ if (strncmp(branch_name, "heads/", 6) == 0)
+ branch_name += 6;
+ else if (strncmp(branch_name, "remotes/", 8) == 0)
+ branch_name += 8;
if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
return got_error_from_errno("asprintf");
- err = got_ref_open(&ref, repo, refname, 0);
- if (err)
+ if (asprintf(&remote_refname, "refs/remotes/%s",
+ branch_name) == -1) {
+ err = got_error_from_errno("asprintf");
goto done;
+ }
+
+ err = got_ref_open(&ref, repo, refname, 0);
+ if (err) {
+ const struct got_error *err2;
+ if (err->code != GOT_ERR_NOT_REF)
+ goto done;
+ /*
+ * Keep 'err' intact such that if neither branch exists
+ * we report "refs/heads" rather than "refs/remotes" in
+ * our error message.
+ */
+ err2 = got_ref_open(&ref, repo, remote_refname, 0);
+ if (err2)
+ goto done;
+ err = NULL;
+ }
if (worktree &&
strcmp(got_worktree_get_head_ref_name(worktree),
@@ -5754,6 +5779,7 @@ done:
if (ref)
got_ref_close(ref);
free(refname);
+ free(remote_refname);
return err;
}
@@ -5773,6 +5799,9 @@ add_branch(struct got_repository *repo, const char *branch_name,
if (branch_name[0] == '-')
return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
+ if (strncmp(branch_name, "refs/heads/", 11) == 0)
+ branch_name += 11;
+
if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
err = got_error_from_errno("asprintf");
goto done;
diff --git a/regress/cmdline/branch.sh b/regress/cmdline/branch.sh
index a766241..fd9564b 100755
--- a/regress/cmdline/branch.sh
+++ b/regress/cmdline/branch.sh
@@ -59,7 +59,7 @@ test_branch_create() {
fi
# Create a branch based on the work tree's branch
- (cd $testroot/wt && got branch -n anotherbranch)
+ (cd $testroot/wt && got branch -n refs/heads/anotherbranch)
ret="$?"
if [ "$ret" != "0" ]; then
test_done "$testroot" "$ret"
@@ -250,7 +250,7 @@ test_branch_delete() {
got branch -d branch2 -r $testroot/repo > $testroot/stdout
ret="$?"
if [ "$ret" != "0" ]; then
- echo "got update command failed unexpectedly"
+ echo "got branch command failed unexpectedly"
test_done "$testroot" "$ret"
return 1
fi
@@ -284,7 +284,7 @@ test_branch_delete() {
> $testroot/stdout 2> $testroot/stderr
ret="$?"
if [ "$ret" = "0" ]; then
- echo "got update succeeded unexpectedly"
+ echo "got branch succeeded unexpectedly"
test_done "$testroot" "$ret"
return 1
fi
@@ -295,6 +295,61 @@ test_branch_delete() {
ret="$?"
if [ "$ret" != "0" ]; then
diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got ref -r $testroot/repo -c master refs/remotes/origin/master
+ got ref -r $testroot/repo -c branch1 refs/remotes/origin/branch1
+ got ref -r $testroot/repo -c branch3 refs/remotes/origin/branch3
+
+ 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
+ echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
+ echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+ echo "refs/remotes/origin/branch1: $commit_id" \
+ >> $testroot/stdout.expected
+ echo "refs/remotes/origin/branch3: $commit_id" \
+ >> $testroot/stdout.expected
+ echo "refs/remotes/origin/master: $commit_id" \
+ >> $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
+
+ got branch -d origin/branch1 -r $testroot/repo > $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got branch command failed unexpectedly"
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got branch -d refs/remotes/origin/branch3 -r $testroot/repo \
+ > $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "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
+ echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
+ echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+ echo "refs/remotes/origin/master: $commit_id" \
+ >> $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"
}
@@ -340,7 +395,7 @@ test_branch_delete_packed() {
(cd $testroot/repo && git pack-refs --all)
- got branch -d branch2 -r $testroot/repo > $testroot/stdout
+ got branch -d refs/heads/branch2 -r $testroot/repo > $testroot/stdout
ret="$?"
if [ "$ret" != "0" ]; then
echo "got update command failed unexpectedly"