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 <gitster@pobox.com>

Junio C Hamano committed Dec 16, 2011 at 09:00 UTC fe46fa9d267fc5465b320d0d2877de45333efeb8
2 files changed +40 -9
git-request-pull.sh
+37 -9
@@ -57,12 +57,40 @@ headrev=$(git rev-parse --verify "$head"^0) || exit
57 merge_base=$(git merge-base $baserev $headrev) ||
58 die "fatal: No commits in common between $base and $head"
59
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 +
94 url=$(git ls-remote --get-url "$url")
95
96 git show -s --format='The following changes since commit %H:
@@ -71,7 +99,7 @@ git show -s --format='The following changes since commit %H:
99
100 are available in the git repository at:
101 ' $baserev &&
74 -echo " $url${branch+ $branch}" &&
102 +echo " $url${ref+ $ref}" &&
103 git show -s --format='
104 for you to fetch changes up to %H:
105
@@ -81,7 +109,7 @@ for you to fetch changes up to %H:
109
110 if test -n "$branch_name"
111 then
84 - echo "(from the branch description for $branch local branch)"
112 + echo "(from the branch description for $branch_name local branch)"
113 echo
114 git config "branch.$branch_name.description"
115 fi &&
@@ -101,7 +129,7 @@ fi &&
129 git shortlog ^$baserev $headrev &&
130 git diff -M --stat --summary $patch $merge_base..$headrev || status=1
131
104 -if test -z "$branch"
132 +if test -z "$ref"
133 then
134 echo "warn: No branch of $url is at:" >&2
135 git show -s --format='warn: %h: %s' $headrev >&2
t/t5150-request-pull.sh
+3
@@ -67,9 +67,11 @@ test_expect_success 'setup: two scripts for reading pull requests' '
67
68 cat <<-\EOT >read-request.sed &&
69 #!/bin/sed -nf
70 + # Note that a request could ask for "tag $tagname"
71 / in the git repository at:$/!d
72 n
73 /^$/ n
74 + s/ tag \([^ ]*\)$/ tag--\1/
75 s/^[ ]*\(.*\) \([^ ]*\)/please pull\
76 \1\
77 \2/p
@@ -178,6 +180,7 @@ test_expect_success 'request names an appropriate branch' '
180 read branch
181 } <digest &&
182 {
183 + test "$branch" = tag--full ||
184 test "$branch" = master ||
185 test "$branch" = for-upstream
186 }