Skip to content

Commit 0454220

Browse files
bonzinigitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5731dfc commit 0454220

File tree

2 files changed

+63
-15
lines changed

2 files changed

+63
-15
lines changed

git-request-pull.sh

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ test -z "$head" && die "fatal: Not a valid revision: $local"
6565
headrev=$(git rev-parse --verify --quiet "$head"^0)
6666
test -z "$headrev" && die "fatal: Ambiguous revision: $local"
6767

68+
local_sha1=$(git rev-parse --verify --quiet "$head")
69+
6870
# Was it a branch with a description?
6971
branch_name=${head#refs/heads/}
7072
if test "z$branch_name" = "z$headref" ||
@@ -77,42 +79,53 @@ merge_base=$(git merge-base $baserev $headrev) ||
7779
die "fatal: No commits in common between $base and $head"
7880

7981
# $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.
8484
find_matching_ref='
8585
my ($head,$headrev) = (@ARGV);
8686
my $pattern = qr{/\Q$head\E$};
87-
my ($found);
87+
my ($remote_sha1, $found);
8888
8989
while (<STDIN>) {
9090
chomp;
9191
my ($sha1, $ref, $deref) = /^(\S+)\s+([^^]+)(\S*)$/;
92-
next unless ($sha1 eq $headrev);
9392
94-
if ($ref eq $head) {
95-
$found = $ref;
96-
}
97-
if ($ref =~ $pattern) {
98-
$found = $ref;
99-
}
10093
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+
}
102107
}
103108
}
104109
if ($found) {
105-
print "$found\n";
110+
$remote_sha1 = $headrev if ! defined $remote_sha1;
111+
print "$remote_sha1 $found\n";
106112
}
107113
'
108114

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
110118

111119
if test -z "$ref"
112120
then
113121
echo "warn: No match for commit $headrev found at $url" >&2
114122
echo "warn: Are you sure you pushed '${remote:-HEAD}' there?" >&2
115123
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
116129
fi
117130

118131
# Special case: turn "for_linus" to "tags/for_linus" when it is correct

t/t5150-request-pull.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,39 @@ test_expect_success 'request-pull quotes regex metacharacters properly' '
264264
265265
'
266266

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+
267302
test_done

0 commit comments

Comments
 (0)