Skip to content

Commit 9ab55da

Browse files
jherlandgitster
authored andcommitted
git symbolic-ref --delete $symref
Teach symbolic-ref to delete symrefs by adding the -d/--delete option to git-symbolic-ref. Both proper and dangling symrefs are deleted by this option, but other refs - or anything else that is not a symref - is not. The symref deletion is performed by first verifying that we are given a proper symref, and then invoking delete_ref() on it with the REF_NODEREF flag. Signed-off-by: Johan Herland <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 87a5461 commit 9ab55da

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

Documentation/git-symbolic-ref.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ git-symbolic-ref(1)
33

44
NAME
55
----
6-
git-symbolic-ref - Read and modify symbolic refs
6+
git-symbolic-ref - Read, modify and delete symbolic refs
77

88
SYNOPSIS
99
--------
1010
[verse]
1111
'git symbolic-ref' [-m <reason>] <name> <ref>
1212
'git symbolic-ref' [-q] [--short] <name>
13+
'git symbolic-ref' --delete [-q] <name>
1314

1415
DESCRIPTION
1516
-----------
@@ -21,13 +22,20 @@ argument to see which branch your working tree is on.
2122
Given two arguments, creates or updates a symbolic ref <name> to
2223
point at the given branch <ref>.
2324

25+
Given `--delete` and an additional argument, deletes the given
26+
symbolic ref.
27+
2428
A symbolic ref is a regular file that stores a string that
2529
begins with `ref: refs/`. For example, your `.git/HEAD` is
2630
a regular file whose contents is `ref: refs/heads/master`.
2731

2832
OPTIONS
2933
-------
3034

35+
-d::
36+
--delete::
37+
Delete the symbolic ref <name>.
38+
3139
-q::
3240
--quiet::
3341
Do not issue an error message if the <name> is not a

builtin/symbolic-ref.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55

66
static const char * const git_symbolic_ref_usage[] = {
77
N_("git symbolic-ref [options] name [ref]"),
8+
N_("git symbolic-ref -d [-q] name"),
89
NULL
910
};
1011

11-
static int shorten;
12-
13-
static void check_symref(const char *HEAD, int quiet)
12+
static int check_symref(const char *HEAD, int quiet, int shorten, int print)
1413
{
1514
unsigned char sha1[20];
1615
int flag;
@@ -22,20 +21,24 @@ static void check_symref(const char *HEAD, int quiet)
2221
if (!quiet)
2322
die("ref %s is not a symbolic ref", HEAD);
2423
else
25-
exit(1);
24+
return 1;
25+
}
26+
if (print) {
27+
if (shorten)
28+
refname = shorten_unambiguous_ref(refname, 0);
29+
puts(refname);
2630
}
27-
if (shorten)
28-
refname = shorten_unambiguous_ref(refname, 0);
29-
puts(refname);
31+
return 0;
3032
}
3133

3234
int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
3335
{
34-
int quiet = 0;
36+
int quiet = 0, delete = 0, shorten = 0, ret = 0;
3537
const char *msg = NULL;
3638
struct option options[] = {
3739
OPT__QUIET(&quiet,
3840
N_("suppress error message for non-symbolic (detached) refs")),
41+
OPT_BOOL('d', "delete", &delete, N_("delete symbolic ref")),
3942
OPT_BOOL(0, "short", &shorten, N_("shorten ref output")),
4043
OPT_STRING('m', NULL, &msg, N_("reason"), N_("reason of the update")),
4144
OPT_END(),
@@ -46,9 +49,19 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
4649
git_symbolic_ref_usage, 0);
4750
if (msg &&!*msg)
4851
die("Refusing to perform update with empty message");
52+
53+
if (delete) {
54+
if (argc != 1)
55+
usage_with_options(git_symbolic_ref_usage, options);
56+
ret = check_symref(argv[0], 1, 0, 0);
57+
if (ret)
58+
die("Cannot delete %s, not a symbolic ref", argv[0]);
59+
return delete_ref(argv[0], NULL, REF_NODEREF);
60+
}
61+
4962
switch (argc) {
5063
case 1:
51-
check_symref(argv[0], quiet);
64+
ret = check_symref(argv[0], quiet, shorten, 1);
5265
break;
5366
case 2:
5467
if (!strcmp(argv[0], "HEAD") &&
@@ -59,5 +72,5 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
5972
default:
6073
usage_with_options(git_symbolic_ref_usage, options);
6174
}
62-
return 0;
75+
return ret;
6376
}

t/t1401-symbolic-ref.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,34 @@ test_expect_success 'symbolic-ref refuses bare sha1' '
3333
'
3434
reset_to_sane
3535

36+
test_expect_success 'symbolic-ref deletes HEAD' '
37+
git symbolic-ref -d HEAD &&
38+
test_path_is_file .git/refs/heads/foo &&
39+
test_path_is_missing .git/HEAD
40+
'
41+
reset_to_sane
42+
43+
test_expect_success 'symbolic-ref deletes dangling HEAD' '
44+
git symbolic-ref HEAD refs/heads/missing &&
45+
git symbolic-ref -d HEAD &&
46+
test_path_is_missing .git/refs/heads/missing &&
47+
test_path_is_missing .git/HEAD
48+
'
49+
reset_to_sane
50+
51+
test_expect_success 'symbolic-ref fails to delete missing FOO' '
52+
echo "fatal: Cannot delete FOO, not a symbolic ref" >expect &&
53+
test_must_fail git symbolic-ref -d FOO >actual 2>&1 &&
54+
test_cmp expect actual
55+
'
56+
reset_to_sane
57+
58+
test_expect_success 'symbolic-ref fails to delete real ref' '
59+
echo "fatal: Cannot delete refs/heads/foo, not a symbolic ref" >expect &&
60+
test_must_fail git symbolic-ref -d refs/heads/foo >actual 2>&1 &&
61+
test_path_is_file .git/refs/heads/foo &&
62+
test_cmp expect actual
63+
'
64+
reset_to_sane
65+
3666
test_done

0 commit comments

Comments
 (0)