request-pull: warn if the remote object is not the same as the local one

In some cases, git request-pull might be invoked with remote and local objects that differ even though they point to the same commit. For example, the remote object might be a lightweight tag vs. an annotated tag on the local side; or the user might have reworded the tag locally and forgotten to push it. When this happens git-request-pull will not warn, because it only checks that "git ls-remote" returns an SHA1 that matches the local commit (known as $headrev in the script). This patch makes git-request-pull retrieve the tag object SHA1 while processing the "git ls-remote" output, so that it can be matched against the local object. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Paolo Bonzini committed May 28, 2019 at 12:15 UTC 0454220d66581f28b9688bc1b687f52cb9561798
2 files changed +63 -15
git-request-pull.sh
+28 -15
@@ -65,6 +65,8 @@ test -z "$head" && die "fatal: Not a valid revision: $local"
65 headrev=$(git rev-parse --verify --quiet "$head"^0)
66 test -z "$headrev" && die "fatal: Ambiguous revision: $local"
67
68 +local_sha1=$(git rev-parse --verify --quiet "$head")
69 +
70 # Was it a branch with a description?
71 branch_name=${head#refs/heads/}
72 if test "z$branch_name" = "z$headref" ||
@@ -77,42 +79,53 @@ merge_base=$(git merge-base $baserev $headrev) ||
79 die "fatal: No commits in common between $base and $head"
80
81 # $head is the refname from the command line.
80 -# If a ref with the same name as $head exists at the remote
81 -# and their values match, use that.
82 -#
83 -# Otherwise find a random ref that matches $headrev.
82 +# Find a ref with the same name as $head that exists at the remote
83 +# and points to the same commit as the local object.
84 find_matching_ref='
85 my ($head,$headrev) = (@ARGV);
86 my $pattern = qr{/\Q$head\E$};
87 - my ($found);
87 + my ($remote_sha1, $found);
88
89 while (<STDIN>) {
90 chomp;
91 my ($sha1, $ref, $deref) = /^(\S+)\s+([^^]+)(\S*)$/;
92 - next unless ($sha1 eq $headrev);
92
94 - if ($ref eq $head) {
95 - $found = $ref;
96 - }
97 - if ($ref =~ $pattern) {
98 - $found = $ref;
99 - }
93 if ($sha1 eq $head) {
101 - $found = $sha1;
94 + $found = $remote_sha1 = $sha1;
95 + break;
96 + }
97 +
98 + if ($ref eq $head || $ref =~ $pattern) {
99 + if ($deref eq "") {
100 + # Remember the matching object on the remote side
101 + $remote_sha1 = $sha1;
102 + }
103 + if ($sha1 eq $headrev) {
104 + $found = $ref;
105 + break;
106 + }
107 }
108 }
109 if ($found) {
105 - print "$found\n";
110 + $remote_sha1 = $headrev if ! defined $remote_sha1;
111 + print "$remote_sha1 $found\n";
112 }
113 '
114
109 -ref=$(git ls-remote "$url" | @@PERL@@ -e "$find_matching_ref" "${remote:-HEAD}" "$headrev")
115 +set fnord $(git ls-remote "$url" | @@PERL@@ -e "$find_matching_ref" "${remote:-HEAD}" "$headrev")
116 +remote_sha1=$2
117 +ref=$3
118
119 if test -z "$ref"
120 then
121 echo "warn: No match for commit $headrev found at $url" >&2
122 echo "warn: Are you sure you pushed '${remote:-HEAD}' there?" >&2
123 status=1
124 +elif test "$local_sha1" != "$remote_sha1"
125 +then
126 + echo "warn: $head found at $url but points to a different object" >&2
127 + echo "warn: Are you sure you pushed '${remote:-HEAD}' there?" >&2
128 + status=1
129 fi
130
131 # Special case: turn "for_linus" to "tags/for_linus" when it is correct
t/t5150-request-pull.sh
+35
@@ -264,4 +264,39 @@ test_expect_success 'request-pull quotes regex metacharacters properly' '
264
265 '
266
267 +test_expect_success 'pull request with mismatched object' '
268 +
269 + rm -fr downstream.git &&
270 + git init --bare downstream.git &&
271 + (
272 + cd local &&
273 + git checkout initial &&
274 + git merge --ff-only master &&
275 + git push origin HEAD:refs/tags/full &&
276 + test_must_fail git request-pull initial "$downstream_url" tags/full \
277 + 2>../err
278 + ) &&
279 + grep "points to a different object" err &&
280 + grep "Are you sure you pushed" err
281 +
282 +'
283 +
284 +test_expect_success 'pull request with stale object' '
285 +
286 + rm -fr downstream.git &&
287 + git init --bare downstream.git &&
288 + (
289 + cd local &&
290 + git checkout initial &&
291 + git merge --ff-only master &&
292 + git push origin refs/tags/full &&
293 + git tag -f -m"Thirty-one days" full &&
294 + test_must_fail git request-pull initial "$downstream_url" tags/full \
295 + 2>../err
296 + ) &&
297 + grep "points to a different object" err &&
298 + grep "Are you sure you pushed" err
299 +
300 +'
301 +
302 test_done