push: require force for annotated tags
Do not allow fast-forwarding of references that point to a tag object. Updating from a tag is potentially destructive since it would likely leave the tag dangling. Disallowing updates to a tag also makes sense semantically and is consistent with the behavior of lightweight tags. Signed-off-by: Chris Rorvick <chris@rorvick.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Chris Rorvick committed
Nov 29, 2012 at 19:41 UTC
40eff1799983b958d6dbe09fb499ad505bcf6f8d
3 files changed
+35
-7
Documentation/git-push.txt
+5
-5
@@ -52,11 +52,11 @@ updated.
52
+
53
The object referenced by <src> is used to update the <dst> reference
54
on the remote side. By default this is only allowed if <dst> is not
55
-under refs/tags/, and then only if it can fast-forward <dst>. By having
56
-the optional leading `+`, you can tell git to update the <dst> ref even
57
-if it is not allowed by default (e.g., it is not a fast-forward.) This
58
-does *not* attempt to merge <src> into <dst>. See EXAMPLES below for
59
-details.
55
+a tag (annotated or lightweight), and then only if it can fast-forward
56
+<dst>. By having the optional leading `+`, you can tell git to update
57
+the <dst> ref even if it is not allowed by default (e.g., it is not a
58
+fast-forward.) This does *not* attempt to merge <src> into <dst>. See
59
+EXAMPLES below for details.
60
+
61
`tag <tag>` means the same as `refs/tags/<tag>:refs/tags/<tag>`.
62
+
remote.c
+9
-2
@@ -1281,9 +1281,16 @@ int match_push_refs(struct ref *src, struct ref **dst,
1281
1282
static inline int is_forwardable(struct ref* ref)
1283
{
1284
+ struct object *o;
1285
+
1286
if (!prefixcmp(ref->name, "refs/tags/"))
1287
return 0;
1288
1289
+ /* old object must be a commit */
1290
+ o = parse_object(ref->old_sha1);
1291
+ if (!o || o->type != OBJ_COMMIT)
1292
+ return 0;
1293
+
1294
return 1;
1295
}
1296
@@ -1323,8 +1330,8 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
1330
* to overwrite it; you would not know what you are losing
1331
* otherwise.
1332
*
1326
- * (4) if both new and old are commit-ish, and new is a
1327
- * descendant of old, it is OK.
1333
+ * (4) if old is a commit and new is a descendant of old
1334
+ * (implying new is commit-ish), it is OK.
1335
*
1336
* (5) regardless of all of the above, removing :B is
1337
* always allowed.
t/t5516-fetch-push.sh
+21
@@ -950,6 +950,27 @@ test_expect_success 'push requires --force to update lightweight tag' '
950
)
951
'
952
953
+test_expect_success 'push requires --force to update annotated tag' '
954
+ mk_test heads/master &&
955
+ mk_child child1 &&
956
+ mk_child child2 &&
957
+ (
958
+ cd child1 &&
959
+ git tag -a -m "message 1" Tag &&
960
+ git push ../child2 Tag:refs/tmp/Tag &&
961
+ git push ../child2 Tag:refs/tmp/Tag &&
962
+ >file1 &&
963
+ git add file1 &&
964
+ git commit -m "file1" &&
965
+ git tag -f -a -m "message 2" Tag &&
966
+ test_must_fail git push ../child2 Tag:refs/tmp/Tag &&
967
+ git push --force ../child2 Tag:refs/tmp/Tag &&
968
+ git tag -f -a -m "message 3" Tag HEAD~ &&
969
+ test_must_fail git push ../child2 Tag:refs/tmp/Tag &&
970
+ git push --force ../child2 Tag:refs/tmp/Tag
971
+ )
972
+'
973
+
974
test_expect_success 'push --porcelain' '
975
mk_empty &&
976
echo >.git/foo "To testrepo" &&