Skip to content

Commit 6b145e0

Browse files
peffgitster
authored andcommitted
branch: restrict @-expansions when deleting
We use strbuf_branchname() to expand the branch name from the command line, so you can delete the branch given by @{-1}, for example. However, we allow other nonsense like "@", and we do not respect our "-r" flag (so we may end up deleting an oddly-named local ref instead of a remote one). We can fix this by passing the appropriate "allowed" flag to strbuf_branchname(). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a356e8e commit 6b145e0

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

builtin/branch.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,20 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
190190
int ret = 0;
191191
int remote_branch = 0;
192192
struct strbuf bname = STRBUF_INIT;
193+
unsigned allowed_interpret;
193194

194195
switch (kinds) {
195196
case FILTER_REFS_REMOTES:
196197
fmt = "refs/remotes/%s";
197198
/* For subsequent UI messages */
198199
remote_branch = 1;
200+
allowed_interpret = INTERPRET_BRANCH_REMOTE;
199201

200202
force = 1;
201203
break;
202204
case FILTER_REFS_BRANCHES:
203205
fmt = "refs/heads/%s";
206+
allowed_interpret = INTERPRET_BRANCH_LOCAL;
204207
break;
205208
default:
206209
die(_("cannot use -a with -d"));
@@ -215,7 +218,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
215218
char *target = NULL;
216219
int flags = 0;
217220

218-
strbuf_branchname(&bname, argv[i], 0);
221+
strbuf_branchname(&bname, argv[i], allowed_interpret);
219222
free(name);
220223
name = mkpathdup(fmt, bname.buf);
221224

t/t3204-branch-name-interpretation.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ test_expect_success 'delete branch via remote @{upstream}' '
8383
# Note that we create two oddly named local branches here. We want to make
8484
# sure that we do not accidentally delete either of them, even if
8585
# shorten_unambiguous_ref() tweaks the name to avoid ambiguity.
86-
test_expect_failure 'delete @{upstream} expansion matches -r option' '
86+
test_expect_success 'delete @{upstream} expansion matches -r option' '
8787
git update-ref refs/remotes/origin/remote-del two &&
8888
git branch --set-upstream-to=origin/remote-del &&
8989
git update-ref refs/heads/origin/remote-del two &&
@@ -94,7 +94,7 @@ test_expect_failure 'delete @{upstream} expansion matches -r option' '
9494
expect_branch refs/heads/remotes/origin/remote-del two
9595
'
9696

97-
test_expect_failure 'disallow deleting remote branch via @{-1}' '
97+
test_expect_success 'disallow deleting remote branch via @{-1}' '
9898
git update-ref refs/remotes/origin/previous one &&
9999
100100
git checkout -b origin/previous two &&
@@ -114,7 +114,7 @@ test_expect_failure 'create branch named "@"' '
114114
expect_branch refs/heads/@ one
115115
'
116116

117-
test_expect_failure 'delete branch named "@"' '
117+
test_expect_success 'delete branch named "@"' '
118118
git update-ref refs/heads/@ two &&
119119
git branch -D @ &&
120120
expect_deleted refs/heads/@

0 commit comments

Comments
 (0)