Skip to content

Commit d39cab3

Browse files
committed
Merge branch 'ab/fetch-tags-noclobber'
The rules used by "git push" and "git fetch" to determine if a ref can or cannot be updated were inconsistent; specifically, fetching to update existing tags were allowed even though tags are supposed to be unmoving anchoring points. "git fetch" was taught to forbid updates to existing tags without the "--force" option. * ab/fetch-tags-noclobber: fetch: stop clobbering existing tags without --force fetch: document local ref updates with/without --force push doc: correct lies about how push refspecs work push doc: move mention of "tag <tag>" later in the prose push doc: remove confusing mention of remote merger fetch tests: add a test for clobbering tag behavior push tests: use spaces in interpolated string push tests: make use of unused $1 in test description fetch: change "branch" to "reference" in --force -h output
2 parents 1c515bf + 0bc8d71 commit d39cab3

File tree

7 files changed

+136
-35
lines changed

7 files changed

+136
-35
lines changed

Documentation/fetch-options.txt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,16 @@ endif::git-pull[]
6868

6969
-f::
7070
--force::
71-
When 'git fetch' is used with `<rbranch>:<lbranch>`
72-
refspec, it refuses to update the local branch
73-
`<lbranch>` unless the remote branch `<rbranch>` it
74-
fetches is a descendant of `<lbranch>`. This option
75-
overrides that check.
71+
When 'git fetch' is used with `<src>:<dst>` refspec it may
72+
refuse to update the local branch as discussed
73+
ifdef::git-pull[]
74+
in the `<refspec>` part of the linkgit:git-fetch[1]
75+
documentation.
76+
endif::git-pull[]
77+
ifndef::git-pull[]
78+
in the `<refspec>` part below.
79+
endif::git-pull[]
80+
This option overrides that check.
7681

7782
-k::
7883
--keep::

Documentation/git-push.txt

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,57 @@ without any `<refspec>` on the command line. Otherwise, missing
7474
`:<dst>` means to update the same ref as the `<src>`.
7575
+
7676
The object referenced by <src> is used to update the <dst> reference
77-
on the remote side. By default this is only allowed if <dst> is not
78-
a tag (annotated or lightweight), and then only if it can fast-forward
79-
<dst>. By having the optional leading `+`, you can tell Git to update
80-
the <dst> ref even if it is not allowed by default (e.g., it is not a
81-
fast-forward.) This does *not* attempt to merge <src> into <dst>. See
82-
EXAMPLES below for details.
83-
+
84-
`tag <tag>` means the same as `refs/tags/<tag>:refs/tags/<tag>`.
85-
+
86-
Pushing an empty <src> allows you to delete the <dst> ref from
87-
the remote repository.
77+
on the remote side. Whether this is allowed depends on where in
78+
`refs/*` the <dst> reference lives as described in detail below, in
79+
those sections "update" means any modifications except deletes, which
80+
as noted after the next few sections are treated differently.
81+
+
82+
The `refs/heads/*` namespace will only accept commit objects, and
83+
updates only if they can be fast-forwarded.
84+
+
85+
The `refs/tags/*` namespace will accept any kind of object (as
86+
commits, trees and blobs can be tagged), and any updates to them will
87+
be rejected.
88+
+
89+
It's possible to push any type of object to any namespace outside of
90+
`refs/{tags,heads}/*`. In the case of tags and commits, these will be
91+
treated as if they were the commits inside `refs/heads/*` for the
92+
purposes of whether the update is allowed.
93+
+
94+
I.e. a fast-forward of commits and tags outside `refs/{tags,heads}/*`
95+
is allowed, even in cases where what's being fast-forwarded is not a
96+
commit, but a tag object which happens to point to a new commit which
97+
is a fast-forward of the commit the last tag (or commit) it's
98+
replacing. Replacing a tag with an entirely different tag is also
99+
allowed, if it points to the same commit, as well as pushing a peeled
100+
tag, i.e. pushing the commit that existing tag object points to, or a
101+
new tag object which an existing commit points to.
102+
+
103+
Tree and blob objects outside of `refs/{tags,heads}/*` will be treated
104+
the same way as if they were inside `refs/tags/*`, any update of them
105+
will be rejected.
106+
+
107+
All of the rules described above about what's not allowed as an update
108+
can be overridden by adding an the optional leading `+` to a refspec
109+
(or using `--force` command line option). The only exception to this
110+
is that no amount of forcing will make the `refs/heads/*` namespace
111+
accept a non-commit object. Hooks and configuration can also override
112+
or amend these rules, see e.g. `receive.denyNonFastForwards` in
113+
linkgit:git-config[1] and`pre-receive` and `update` in
114+
linkgit:githooks[5].
115+
+
116+
Pushing an empty <src> allows you to delete the <dst> ref from the
117+
remote repository. Deletions are always accepted without a leading `+`
118+
in the refspec (or `--force`), except when forbidden by configuration
119+
or hooks. See `receive.denyDeletes` in linkgit:git-config[1] and
120+
`pre-receive` and `update` in linkgit:githooks[5].
88121
+
89122
The special refspec `:` (or `+:` to allow non-fast-forward updates)
90123
directs Git to push "matching" branches: for every branch that exists on
91124
the local side, the remote side is updated if a branch of the same name
92125
already exists on the remote side.
126+
+
127+
`tag <tag>` means the same as `refs/tags/<tag>:refs/tags/<tag>`.
93128

94129
--all::
95130
Push all branches (i.e. refs under `refs/heads/`); cannot be

Documentation/gitrevisions.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ walk the revision graph (such as linkgit:git-log[1]), all commits which are
1919
reachable from that commit. For commands that walk the revision graph one can
2020
also specify a range of revisions explicitly.
2121

22-
In addition, some Git commands (such as linkgit:git-show[1]) also take
23-
revision parameters which denote other objects than commits, e.g. blobs
24-
("files") or trees ("directories of files").
22+
In addition, some Git commands (such as linkgit:git-show[1] and
23+
linkgit:git-push[1]) can also take revision parameters which denote
24+
other objects than commits, e.g. blobs ("files") or trees
25+
("directories of files").
2526

2627
include::revisions.txt[]
2728

Documentation/pull-fetch-param.txt

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,40 @@ name.
3333
it requests fetching everything up to the given tag.
3434
+
3535
The remote ref that matches <src>
36-
is fetched, and if <dst> is not an empty string, the local
37-
ref that matches it is fast-forwarded using <src>.
38-
If the optional plus `+` is used, the local ref
39-
is updated even if it does not result in a fast-forward
40-
update.
36+
is fetched, and if <dst> is not an empty string, an attempt
37+
is made to update the local ref that matches it.
38+
+
39+
Whether that update is allowed without `--force` depends on the ref
40+
namespace it's being fetched to, the type of object being fetched, and
41+
whether the update is considered to be a fast-forward. Generally, the
42+
same rules apply for fetching as when pushing, see the `<refspec>...`
43+
section of linkgit:git-push[1] for what those are. Exceptions to those
44+
rules particular to 'git fetch' are noted below.
45+
+
46+
Until Git version 2.20, and unlike when pushing with
47+
linkgit:git-push[1], any updates to `refs/tags/*` would be accepted
48+
without `+` in the refspec (or `--force`). The receiving promiscuously
49+
considered all tag updates from a remote to be forced fetches. Since
50+
Git version 2.20, fetching to update `refs/tags/*` work the same way
51+
as when pushing. I.e. any updates will be rejected without `+` in the
52+
refspec (or `--force`).
53+
+
54+
Unlike when pushing with linkgit:git-push[1], any updates outside of
55+
`refs/{tags,heads}/*` will be accepted without `+` in the refspec (or
56+
`--force`), whether that's swapping e.g. a tree object for a blob, or
57+
a commit for another commit that's doesn't have the previous commit as
58+
an ancestor etc.
59+
+
60+
Unlike when pushing with linkgit:git-push[1], there is no
61+
configuration which'll amend these rules, and nothing like a
62+
`pre-fetch` hook analogous to the `pre-receive` hook.
63+
+
64+
As with pushing with linkgit:git-push[1], all of the rules described
65+
above about what's not allowed as an update can be overridden by
66+
adding an the optional leading `+` to a refspec (or using `--force`
67+
command line option). The only exception to this is that no amount of
68+
forcing will make the `refs/heads/*` namespace accept a non-commit
69+
object.
4170
+
4271
[NOTE]
4372
When the remote branch you want to fetch is known to

builtin/fetch.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static struct option builtin_fetch_options[] = {
115115
N_("append to .git/FETCH_HEAD instead of overwriting")),
116116
OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
117117
N_("path to upload pack on remote end")),
118-
OPT__FORCE(&force, N_("force overwrite of local branch"), 0),
118+
OPT__FORCE(&force, N_("force overwrite of local reference"), 0),
119119
OPT_BOOL('m', "multiple", &multiple,
120120
N_("fetch from multiple remotes")),
121121
OPT_SET_INT('t', "tags", &tags,
@@ -668,12 +668,18 @@ static int update_local_ref(struct ref *ref,
668668

669669
if (!is_null_oid(&ref->old_oid) &&
670670
starts_with(ref->name, "refs/tags/")) {
671-
int r;
672-
r = s_update_ref("updating tag", ref, 0);
673-
format_display(display, r ? '!' : 't', _("[tag update]"),
674-
r ? _("unable to update local ref") : NULL,
675-
remote, pretty_ref, summary_width);
676-
return r;
671+
if (force || ref->force) {
672+
int r;
673+
r = s_update_ref("updating tag", ref, 0);
674+
format_display(display, r ? '!' : 't', _("[tag update]"),
675+
r ? _("unable to update local ref") : NULL,
676+
remote, pretty_ref, summary_width);
677+
return r;
678+
} else {
679+
format_display(display, '!', _("[rejected]"), _("would clobber existing tag"),
680+
remote, pretty_ref, summary_width);
681+
return 1;
682+
}
677683
}
678684

679685
current = lookup_commit_reference_gently(the_repository,

t/t5516-fetch-push.sh

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ test_force_push_tag () {
969969
tag_type_description=$1
970970
tag_args=$2
971971

972-
test_expect_success 'force pushing required to update lightweight tag' "
972+
test_expect_success "force pushing required to update $tag_type_description" "
973973
mk_test testrepo heads/master &&
974974
mk_child testrepo child1 &&
975975
mk_child testrepo child2 &&
@@ -1009,7 +1009,32 @@ test_force_push_tag () {
10091009
}
10101010

10111011
test_force_push_tag "lightweight tag" "-f"
1012-
test_force_push_tag "annotated tag" "-f -a -m'msg'"
1012+
test_force_push_tag "annotated tag" "-f -a -m'tag message'"
1013+
1014+
test_force_fetch_tag () {
1015+
tag_type_description=$1
1016+
tag_args=$2
1017+
1018+
test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
1019+
mk_test testrepo heads/master &&
1020+
mk_child testrepo child1 &&
1021+
mk_child testrepo child2 &&
1022+
(
1023+
cd testrepo &&
1024+
git tag testTag &&
1025+
git -C ../child1 fetch origin tag testTag &&
1026+
>file1 &&
1027+
git add file1 &&
1028+
git commit -m 'file1' &&
1029+
git tag $tag_args testTag &&
1030+
test_must_fail git -C ../child1 fetch origin tag testTag &&
1031+
git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
1032+
)
1033+
"
1034+
}
1035+
1036+
test_force_fetch_tag "lightweight tag" "-f"
1037+
test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
10131038

10141039
test_expect_success 'push --porcelain' '
10151040
mk_empty testrepo &&

t/t5612-clone-refspec.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ test_expect_success 'clone with --no-tags' '
103103
test_expect_success '--single-branch while HEAD pointing at master' '
104104
(
105105
cd dir_master &&
106-
git fetch &&
106+
git fetch --force &&
107107
git for-each-ref refs/remotes/origin |
108108
sed -e "/HEAD$/d" \
109109
-e "s|/remotes/origin/|/heads/|" >../actual
@@ -114,7 +114,7 @@ test_expect_success '--single-branch while HEAD pointing at master' '
114114
test_cmp expect actual &&
115115
(
116116
cd dir_master &&
117-
git fetch --tags &&
117+
git fetch --tags --force &&
118118
git for-each-ref refs/tags >../actual
119119
) &&
120120
git for-each-ref refs/tags >expect &&

0 commit comments

Comments
 (0)