Merge branch 'pb/request-pull-verify-remote-ref'

"git request-pull" learned to warn when the ref we ask them to pull
from in the local repository and in the published repository are
different.

* pb/request-pull-verify-remote-ref:
  request-pull: warn if the remote object is not the same as the local one
  request-pull: quote regex metacharacters in local ref
This commit is contained in:
Junio C Hamano 2019-06-17 10:15:20 -07:00
commit dedc046421
2 changed files with 82 additions and 17 deletions

View File

@ -65,6 +65,8 @@ test -z "$head" && die "fatal: Not a valid revision: $local"
headrev=$(git rev-parse --verify --quiet "$head"^0)
test -z "$headrev" && die "fatal: Ambiguous revision: $local"
local_sha1=$(git rev-parse --verify --quiet "$head")
# Was it a branch with a description?
branch_name=${head#refs/heads/}
if test "z$branch_name" = "z$headref" ||
@ -77,43 +79,53 @@ merge_base=$(git merge-base $baserev $headrev) ||
die "fatal: No commits in common between $base and $head"
# $head is the refname from the command line.
# If a ref with the same name as $head exists at the remote
# and their values match, use that.
#
# Otherwise find a random ref that matches $headrev.
# Find a ref with the same name as $head that exists at the remote
# and points to the same commit as the local object.
find_matching_ref='
my ($head,$headrev) = (@ARGV);
my ($found);
my $pattern = qr{/\Q$head\E$};
my ($remote_sha1, $found);
while (<STDIN>) {
chomp;
my ($sha1, $ref, $deref) = /^(\S+)\s+([^^]+)(\S*)$/;
my ($pattern);
next unless ($sha1 eq $headrev);
$pattern="/$head\$";
if ($ref eq $head) {
$found = $ref;
}
if ($ref =~ /$pattern/) {
$found = $ref;
}
if ($sha1 eq $head) {
$found = $sha1;
$found = $remote_sha1 = $sha1;
break;
}
if ($ref eq $head || $ref =~ $pattern) {
if ($deref eq "") {
# Remember the matching object on the remote side
$remote_sha1 = $sha1;
}
if ($sha1 eq $headrev) {
$found = $ref;
break;
}
}
}
if ($found) {
print "$found\n";
$remote_sha1 = $headrev if ! defined $remote_sha1;
print "$remote_sha1 $found\n";
}
'
ref=$(git ls-remote "$url" | @@PERL@@ -e "$find_matching_ref" "${remote:-HEAD}" "$headrev")
set fnord $(git ls-remote "$url" | @@PERL@@ -e "$find_matching_ref" "${remote:-HEAD}" "$headrev")
remote_sha1=$2
ref=$3
if test -z "$ref"
then
echo "warn: No match for commit $headrev found at $url" >&2
echo "warn: Are you sure you pushed '${remote:-HEAD}' there?" >&2
status=1
elif test "$local_sha1" != "$remote_sha1"
then
echo "warn: $head found at $url but points to a different object" >&2
echo "warn: Are you sure you pushed '${remote:-HEAD}' there?" >&2
status=1
fi
# Special case: turn "for_linus" to "tags/for_linus" when it is correct

View File

@ -246,4 +246,57 @@ test_expect_success 'request-pull ignores OPTIONS_KEEPDASHDASH poison' '
'
test_expect_success 'request-pull quotes regex metacharacters properly' '
rm -fr downstream.git &&
git init --bare downstream.git &&
(
cd local &&
git checkout initial &&
git merge --ff-only master &&
git tag -mrelease v2.0 &&
git push origin refs/tags/v2.0:refs/tags/v2-0 &&
test_must_fail git request-pull initial "$downstream_url" tags/v2.0 \
2>../err
) &&
grep "No match for commit .*" err &&
grep "Are you sure you pushed" err
'
test_expect_success 'pull request with mismatched object' '
rm -fr downstream.git &&
git init --bare downstream.git &&
(
cd local &&
git checkout initial &&
git merge --ff-only master &&
git push origin HEAD:refs/tags/full &&
test_must_fail git request-pull initial "$downstream_url" tags/full \
2>../err
) &&
grep "points to a different object" err &&
grep "Are you sure you pushed" err
'
test_expect_success 'pull request with stale object' '
rm -fr downstream.git &&
git init --bare downstream.git &&
(
cd local &&
git checkout initial &&
git merge --ff-only master &&
git push origin refs/tags/full &&
git tag -f -m"Thirty-one days" full &&
test_must_fail git request-pull initial "$downstream_url" tags/full \
2>../err
) &&
grep "points to a different object" err &&
grep "Are you sure you pushed" err
'
test_done