Skip to content

Commit 84d52ca

Browse files
committed
Merge branch 'jk/push-to-delete'
* jk/push-to-delete: builtin-push: add --delete as syntactic sugar for :foo
2 parents 9c787f3 + f517f1f commit 84d52ca

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
}
@@ -196,6 +210,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
196210
OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
197211
OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
198212
(TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
213+
OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
199214
OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
200215
OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
201216
OPT_BIT( 0, "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
@@ -209,6 +224,11 @@ int cmd_push(int argc, const char **argv, const char *prefix)
209224
git_config(git_default_config, NULL);
210225
argc = parse_options(argc, argv, prefix, options, push_usage, 0);
211226

227+
if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
228+
die("--delete is incompatible with --all, --mirror and --tags");
229+
if (deleterefs && argc < 2)
230+
die("--delete doesn't make sense without any refs");
231+
212232
if (tags)
213233
add_refspec("refs/tags/*");
214234

t/t5516-fetch-push.sh

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

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

0 commit comments

Comments
 (0)