Skip to content

Commit

Permalink
git symbolic-ref --delete $symref
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
jherland authored and gitster committed Oct 21, 2012
1 parent 87a5461 commit 9ab55da
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
10 changes: 9 additions & 1 deletion Documentation/git-symbolic-ref.txt
Expand Up @@ -3,13 +3,14 @@ git-symbolic-ref(1)

NAME
----
git-symbolic-ref - Read and modify symbolic refs
git-symbolic-ref - Read, modify and delete symbolic refs

SYNOPSIS
--------
[verse]
'git symbolic-ref' [-m <reason>] <name> <ref>
'git symbolic-ref' [-q] [--short] <name>
'git symbolic-ref' --delete [-q] <name>

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

Given `--delete` and an additional argument, deletes the given
symbolic ref.

A symbolic ref is a regular file that stores a string that
begins with `ref: refs/`. For example, your `.git/HEAD` is
a regular file whose contents is `ref: refs/heads/master`.

OPTIONS
-------

-d::
--delete::
Delete the symbolic ref <name>.

-q::
--quiet::
Do not issue an error message if the <name> is not a
Expand Down
33 changes: 23 additions & 10 deletions builtin/symbolic-ref.c
Expand Up @@ -5,12 +5,11 @@

static const char * const git_symbolic_ref_usage[] = {
N_("git symbolic-ref [options] name [ref]"),
N_("git symbolic-ref -d [-q] name"),
NULL
};

static int shorten;

static void check_symref(const char *HEAD, int quiet)
static int check_symref(const char *HEAD, int quiet, int shorten, int print)
{
unsigned char sha1[20];
int flag;
Expand All @@ -22,20 +21,24 @@ static void check_symref(const char *HEAD, int quiet)
if (!quiet)
die("ref %s is not a symbolic ref", HEAD);
else
exit(1);
return 1;
}
if (print) {
if (shorten)
refname = shorten_unambiguous_ref(refname, 0);
puts(refname);
}
if (shorten)
refname = shorten_unambiguous_ref(refname, 0);
puts(refname);
return 0;
}

int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
{
int quiet = 0;
int quiet = 0, delete = 0, shorten = 0, ret = 0;
const char *msg = NULL;
struct option options[] = {
OPT__QUIET(&quiet,
N_("suppress error message for non-symbolic (detached) refs")),
OPT_BOOL('d', "delete", &delete, N_("delete symbolic ref")),
OPT_BOOL(0, "short", &shorten, N_("shorten ref output")),
OPT_STRING('m', NULL, &msg, N_("reason"), N_("reason of the update")),
OPT_END(),
Expand All @@ -46,9 +49,19 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
git_symbolic_ref_usage, 0);
if (msg &&!*msg)
die("Refusing to perform update with empty message");

if (delete) {
if (argc != 1)
usage_with_options(git_symbolic_ref_usage, options);
ret = check_symref(argv[0], 1, 0, 0);
if (ret)
die("Cannot delete %s, not a symbolic ref", argv[0]);
return delete_ref(argv[0], NULL, REF_NODEREF);
}

switch (argc) {
case 1:
check_symref(argv[0], quiet);
ret = check_symref(argv[0], quiet, shorten, 1);
break;
case 2:
if (!strcmp(argv[0], "HEAD") &&
Expand All @@ -59,5 +72,5 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
default:
usage_with_options(git_symbolic_ref_usage, options);
}
return 0;
return ret;
}
30 changes: 30 additions & 0 deletions t/t1401-symbolic-ref.sh
Expand Up @@ -33,4 +33,34 @@ test_expect_success 'symbolic-ref refuses bare sha1' '
'
reset_to_sane

test_expect_success 'symbolic-ref deletes HEAD' '
git symbolic-ref -d HEAD &&
test_path_is_file .git/refs/heads/foo &&
test_path_is_missing .git/HEAD
'
reset_to_sane

test_expect_success 'symbolic-ref deletes dangling HEAD' '
git symbolic-ref HEAD refs/heads/missing &&
git symbolic-ref -d HEAD &&
test_path_is_missing .git/refs/heads/missing &&
test_path_is_missing .git/HEAD
'
reset_to_sane

test_expect_success 'symbolic-ref fails to delete missing FOO' '
echo "fatal: Cannot delete FOO, not a symbolic ref" >expect &&
test_must_fail git symbolic-ref -d FOO >actual 2>&1 &&
test_cmp expect actual
'
reset_to_sane

test_expect_success 'symbolic-ref fails to delete real ref' '
echo "fatal: Cannot delete refs/heads/foo, not a symbolic ref" >expect &&
test_must_fail git symbolic-ref -d refs/heads/foo >actual 2>&1 &&
test_path_is_file .git/refs/heads/foo &&
test_cmp expect actual
'
reset_to_sane

test_done

0 comments on commit 9ab55da

Please sign in to comment.