Skip to content

Commit dedc046

Browse files
committed
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
2 parents add59c4 + 0454220 commit dedc046

File tree

2 files changed

+82
-17
lines changed

2 files changed

+82
-17
lines changed

git-request-pull.sh

Lines changed: 29 additions & 17 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,43 +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);
86-
my ($found);
86+
my $pattern = qr{/\Q$head\E$};
87+
my ($remote_sha1, $found);
8788
8889
while (<STDIN>) {
8990
chomp;
9091
my ($sha1, $ref, $deref) = /^(\S+)\s+([^^]+)(\S*)$/;
91-
my ($pattern);
92-
next unless ($sha1 eq $headrev);
9392
94-
$pattern="/$head\$";
95-
if ($ref eq $head) {
96-
$found = $ref;
97-
}
98-
if ($ref =~ /$pattern/) {
99-
$found = $ref;
100-
}
10193
if ($sha1 eq $head) {
102-
$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+
}
103107
}
104108
}
105109
if ($found) {
106-
print "$found\n";
110+
$remote_sha1 = $headrev if ! defined $remote_sha1;
111+
print "$remote_sha1 $found\n";
107112
}
108113
'
109114

110-
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
111118

112119
if test -z "$ref"
113120
then
114121
echo "warn: No match for commit $headrev found at $url" >&2
115122
echo "warn: Are you sure you pushed '${remote:-HEAD}' there?" >&2
116123
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
117129
fi
118130

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

t/t5150-request-pull.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,57 @@ test_expect_success 'request-pull ignores OPTIONS_KEEPDASHDASH poison' '
246246
247247
'
248248

249+
test_expect_success 'request-pull quotes regex metacharacters properly' '
250+
251+
rm -fr downstream.git &&
252+
git init --bare downstream.git &&
253+
(
254+
cd local &&
255+
git checkout initial &&
256+
git merge --ff-only master &&
257+
git tag -mrelease v2.0 &&
258+
git push origin refs/tags/v2.0:refs/tags/v2-0 &&
259+
test_must_fail git request-pull initial "$downstream_url" tags/v2.0 \
260+
2>../err
261+
) &&
262+
grep "No match for commit .*" err &&
263+
grep "Are you sure you pushed" err
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+
249302
test_done

0 commit comments

Comments
 (0)