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 <jk@jk.gs> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jan Krüger committed
Dec 30, 2009 at 20:57 UTC
f517f1f2e9c9e3abe0fde1b3c9a7bb20fdb5ba55
3 files changed
+53
-3
Documentation/git-push.txt
+4
@@ -91,6 +91,10 @@ nor in any Push line of the corresponding remotes file---see below).
91
will be tab-separated and sent to stdout instead of stderr. The full
92
symbolic names of the refs will be given.
93
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
+
98
--tags::
99
All refs under `$GIT_DIR/refs/tags` are pushed, in
100
addition to refspecs explicitly listed on the command
builtin-push.c
+23
-3
@@ -15,6 +15,7 @@ static const char * const push_usage[] = {
15
};
16
17
static int thin;
18
+static int deleterefs;
19
static const char *receivepack;
20
21
static const char **refspec;
@@ -39,11 +40,24 @@ static void set_refspecs(const char **refs, int nr)
40
if (nr <= ++i)
41
die("tag shorthand without <tag>");
42
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
+ }
50
strcat(tag, refs[i]);
51
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");
61
add_refspec(ref);
62
}
63
}
@@ -179,6 +193,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
193
OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
194
OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
195
(TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
196
+ OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
197
OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
198
OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
199
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)
207
git_config(git_default_config, NULL);
208
argc = parse_options(argc, argv, prefix, options, push_usage, 0);
209
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
+
215
if (tags)
216
add_refspec("refs/tags/*");
217
t/t5516-fetch-push.sh
+26
@@ -546,6 +546,32 @@ test_expect_success 'allow deleting an invalid remote ref' '
546
547
'
548
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
+
575
test_expect_success 'warn on push to HEAD of non-bare repository' '
576
mk_test heads/master
577
(cd testrepo &&