Skip to content

Commit fe46fa9

Browse files
committed
request-pull: update the "pull" command generation logic
The old code that insisted on asking for the tip of a branch to be pulled were not updated when we started allowing for a tag to be pulled. When a tag points at an older part of the history and there is no branch that points at the tagged commit, the script failed to say which ref is to be pulled. Signed-off-by: Junio C Hamano <[email protected]>
1 parent d050464 commit fe46fa9

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

git-request-pull.sh

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,40 @@ headrev=$(git rev-parse --verify "$head"^0) || exit
5757
merge_base=$(git merge-base $baserev $headrev) ||
5858
die "fatal: No commits in common between $base and $head"
5959

60-
find_matching_branch="/^$headrev "'refs\/heads\//{
61-
s/^.* refs\/heads\///
62-
p
63-
q
64-
}'
65-
branch=$(git ls-remote "$url" | sed -n -e "$find_matching_branch")
60+
# $head is the token given from the command line. If a ref with that
61+
# name exists at the remote and their values match, we should use it.
62+
# Otherwise find a ref that matches $headrev.
63+
find_matching_ref='
64+
sub abbr {
65+
my $ref = shift;
66+
if ($ref =~ s|refs/heads/||) {
67+
return $ref;
68+
} elsif ($ref =~ s|refs/tags/||) {
69+
return "tag $ref";
70+
} else {
71+
return $ref;
72+
}
73+
}
74+
75+
my ($exact, $found);
76+
while (<STDIN>) {
77+
my ($sha1, $ref, $deref) = /^(\S+)\s+(\S+?)(\^\{\})?$/;
78+
next unless ($sha1 eq $ARGV[1]);
79+
$found = abbr($ref);
80+
if ($ref =~ m|/\Q$ARGV[0]\E$|) {
81+
$exact = $found;
82+
last;
83+
}
84+
}
85+
if ($exact) {
86+
print "$exact\n";
87+
} elsif ($found) {
88+
print "$found\n";
89+
}
90+
'
91+
92+
ref=$(git ls-remote "$url" | perl -e "$find_matching_ref" "$head" "$headrev")
93+
6694
url=$(git ls-remote --get-url "$url")
6795

6896
git show -s --format='The following changes since commit %H:
@@ -71,7 +99,7 @@ git show -s --format='The following changes since commit %H:
7199
72100
are available in the git repository at:
73101
' $baserev &&
74-
echo " $url${branch+ $branch}" &&
102+
echo " $url${ref+ $ref}" &&
75103
git show -s --format='
76104
for you to fetch changes up to %H:
77105
@@ -81,7 +109,7 @@ for you to fetch changes up to %H:
81109

82110
if test -n "$branch_name"
83111
then
84-
echo "(from the branch description for $branch local branch)"
112+
echo "(from the branch description for $branch_name local branch)"
85113
echo
86114
git config "branch.$branch_name.description"
87115
fi &&
@@ -101,7 +129,7 @@ fi &&
101129
git shortlog ^$baserev $headrev &&
102130
git diff -M --stat --summary $patch $merge_base..$headrev || status=1
103131

104-
if test -z "$branch"
132+
if test -z "$ref"
105133
then
106134
echo "warn: No branch of $url is at:" >&2
107135
git show -s --format='warn: %h: %s' $headrev >&2

t/t5150-request-pull.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ test_expect_success 'setup: two scripts for reading pull requests' '
6767
6868
cat <<-\EOT >read-request.sed &&
6969
#!/bin/sed -nf
70+
# Note that a request could ask for "tag $tagname"
7071
/ in the git repository at:$/!d
7172
n
7273
/^$/ n
74+
s/ tag \([^ ]*\)$/ tag--\1/
7375
s/^[ ]*\(.*\) \([^ ]*\)/please pull\
7476
\1\
7577
\2/p
@@ -178,6 +180,7 @@ test_expect_success 'request names an appropriate branch' '
178180
read branch
179181
} <digest &&
180182
{
183+
test "$branch" = tag--full ||
181184
test "$branch" = master ||
182185
test "$branch" = for-upstream
183186
}

0 commit comments

Comments
 (0)