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 <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johan Herland committed
Oct 21, 2012 at 13:32 UTC
9ab55daa55377fa258c18ffb90211547e299eec9
3 files changed
+62
-11
Documentation/git-symbolic-ref.txt
+9
-1
@@ -3,13 +3,14 @@ git-symbolic-ref(1)
3
4
NAME
5
----
6
-git-symbolic-ref - Read and modify symbolic refs
6
+git-symbolic-ref - Read, modify and delete symbolic refs
7
8
SYNOPSIS
9
--------
10
[verse]
11
'git symbolic-ref' [-m <reason>] <name> <ref>
12
'git symbolic-ref' [-q] [--short] <name>
13
+'git symbolic-ref' --delete [-q] <name>
14
15
DESCRIPTION
16
-----------
@@ -21,6 +22,9 @@ argument to see which branch your working tree is on.
22
Given two arguments, creates or updates a symbolic ref <name> to
23
point at the given branch <ref>.
24
25
+Given `--delete` and an additional argument, deletes the given
26
+symbolic ref.
27
+
28
A symbolic ref is a regular file that stores a string that
29
begins with `ref: refs/`. For example, your `.git/HEAD` is
30
a regular file whose contents is `ref: refs/heads/master`.
@@ -28,6 +32,10 @@ a regular file whose contents is `ref: refs/heads/master`.
32
OPTIONS
33
-------
34
35
+-d::
36
+--delete::
37
+ Delete the symbolic ref <name>.
38
+
39
-q::
40
--quiet::
41
Do not issue an error message if the <name> is not a
builtin/symbolic-ref.c
+23
-10
@@ -5,12 +5,11 @@
5
6
static const char * const git_symbolic_ref_usage[] = {
7
N_("git symbolic-ref [options] name [ref]"),
8
+ N_("git symbolic-ref -d [-q] name"),
9
NULL
10
};
11
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)
13
{
14
unsigned char sha1[20];
15
int flag;
@@ -22,20 +21,24 @@ static void check_symref(const char *HEAD, int quiet)
21
if (!quiet)
22
die("ref %s is not a symbolic ref", HEAD);
23
else
25
- exit(1);
24
+ return 1;
25
+ }
26
+ if (print) {
27
+ if (shorten)
28
+ refname = shorten_unambiguous_ref(refname, 0);
29
+ puts(refname);
30
}
27
- if (shorten)
28
- refname = shorten_unambiguous_ref(refname, 0);
29
- puts(refname);
31
+ return 0;
32
}
33
34
int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
35
{
34
- int quiet = 0;
36
+ int quiet = 0, delete = 0, shorten = 0, ret = 0;
37
const char *msg = NULL;
38
struct option options[] = {
39
OPT__QUIET(&quiet,
40
N_("suppress error message for non-symbolic (detached) refs")),
41
+ OPT_BOOL('d', "delete", &delete, N_("delete symbolic ref")),
42
OPT_BOOL(0, "short", &shorten, N_("shorten ref output")),
43
OPT_STRING('m', NULL, &msg, N_("reason"), N_("reason of the update")),
44
OPT_END(),
@@ -46,9 +49,19 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
49
git_symbolic_ref_usage, 0);
50
if (msg &&!*msg)
51
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
+
62
switch (argc) {
63
case 1:
51
- check_symref(argv[0], quiet);
64
+ ret = check_symref(argv[0], quiet, shorten, 1);
65
break;
66
case 2:
67
if (!strcmp(argv[0], "HEAD") &&
@@ -59,5 +72,5 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
72
default:
73
usage_with_options(git_symbolic_ref_usage, options);
74
}
62
- return 0;
75
+ return ret;
76
}
t/t1401-symbolic-ref.sh
+30
@@ -33,4 +33,34 @@ test_expect_success 'symbolic-ref refuses bare sha1' '
33
'
34
reset_to_sane
35
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
+
66
test_done