Skip to content

Commit f517f1f

Browse files
jastgitster
authored andcommitted
builtin-push: add --delete as syntactic sugar for :foo
Refspecs without a source side have been reported as confusing by many. As an alternative, this adds support for commands like: git push origin --delete somebranch git push origin --delete tag sometag Specifically, --delete will prepend a colon to all colon-less refspecs given on the command line, and will refuse to accept refspecs with colons to prevent undue confusion. Signed-off-by: Jan Krüger <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 902f235 commit f517f1f

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

Documentation/git-push.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ nor in any Push line of the corresponding remotes file---see below).
9191
will be tab-separated and sent to stdout instead of stderr. The full
9292
symbolic names of the refs will be given.
9393

94+
--delete::
95+
All listed refs are deleted from the remote repository. This is
96+
the same as prefixing all refs with a colon.
97+
9498
--tags::
9599
All refs under `$GIT_DIR/refs/tags` are pushed, in
96100
addition to refspecs explicitly listed on the command

builtin-push.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ static const char * const push_usage[] = {
1515
};
1616

1717
static int thin;
18+
static int deleterefs;
1819
static const char *receivepack;
1920

2021
static const char **refspec;
@@ -39,11 +40,24 @@ static void set_refspecs(const char **refs, int nr)
3940
if (nr <= ++i)
4041
die("tag shorthand without <tag>");
4142
len = strlen(refs[i]) + 11;
42-
tag = xmalloc(len);
43-
strcpy(tag, "refs/tags/");
43+
if (deleterefs) {
44+
tag = xmalloc(len+1);
45+
strcpy(tag, ":refs/tags/");
46+
} else {
47+
tag = xmalloc(len);
48+
strcpy(tag, "refs/tags/");
49+
}
4450
strcat(tag, refs[i]);
4551
ref = tag;
46-
}
52+
} else if (deleterefs && !strchr(ref, ':')) {
53+
char *delref;
54+
int len = strlen(ref)+1;
55+
delref = xmalloc(len);
56+
strcpy(delref, ":");
57+
strcat(delref, ref);
58+
ref = delref;
59+
} else if (deleterefs)
60+
die("--delete only accepts plain target ref names");
4761
add_refspec(ref);
4862
}
4963
}
@@ -179,6 +193,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
179193
OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
180194
OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
181195
(TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
196+
OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
182197
OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
183198
OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
184199
OPT_BIT( 0, "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
@@ -192,6 +207,11 @@ int cmd_push(int argc, const char **argv, const char *prefix)
192207
git_config(git_default_config, NULL);
193208
argc = parse_options(argc, argv, prefix, options, push_usage, 0);
194209

210+
if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
211+
die("--delete is incompatible with --all, --mirror and --tags");
212+
if (deleterefs && argc < 2)
213+
die("--delete doesn't make sense without any refs");
214+
195215
if (tags)
196216
add_refspec("refs/tags/*");
197217

t/t5516-fetch-push.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,32 @@ test_expect_success 'allow deleting an invalid remote ref' '
546546
547547
'
548548

549+
test_expect_success 'allow deleting a ref using --delete' '
550+
mk_test heads/master &&
551+
(cd testrepo && git config receive.denyDeleteCurrent warn) &&
552+
git push testrepo --delete master &&
553+
(cd testrepo && test_must_fail git rev-parse --verify refs/heads/master)
554+
'
555+
556+
test_expect_success 'allow deleting a tag using --delete' '
557+
mk_test heads/master &&
558+
git tag -a -m dummy_message deltag heads/master &&
559+
git push testrepo --tags &&
560+
(cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
561+
git push testrepo --delete tag deltag &&
562+
(cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
563+
'
564+
565+
test_expect_success 'push --delete without args aborts' '
566+
mk_test heads/master &&
567+
test_must_fail git push testrepo --delete
568+
'
569+
570+
test_expect_success 'push --delete refuses src:dest refspecs' '
571+
mk_test heads/master &&
572+
test_must_fail git push testrepo --delete master:foo
573+
'
574+
549575
test_expect_success 'warn on push to HEAD of non-bare repository' '
550576
mk_test heads/master
551577
(cd testrepo &&

0 commit comments

Comments
 (0)