request-pull: more strictly match local/remote branches

The current 'request-pull' will try to find matching commit on the given remote, and rewrite the "please pull" line to match that remote ref. That may be very helpful if your local tree doesn't match the layout of the remote branches, but for the common case it's been a recurring disaster, when "request-pull" is done against a delayed remote update, and it rewrites the target branch randomly to some other branch name that happens to have the same expected SHA1 (or more commonly, leaves it blank). To avoid that recurring problem, this changes "git request-pull" so that it matches the ref name to be pulled against the *local* repository, and then warns if the remote repository does not have that exact same branch or tag name and content. This means that git request-pull will never rewrite the ref-name you gave it. If the local branch name is "xyzzy", that is the only branch name that request-pull will ask the other side to fetch. If the remote has that branch under a different name, that's your problem and git request-pull will not try to fix it up (but git request-pull will warn about the fact that no exact matching branch is found, and you can edit the end result to then have the remote name you want if it doesn't match your local one). The new "find local ref" code will also complain loudly if you give an ambiguous refname (eg you have both a tag and a branch with that same name, and you don't specify "heads/name" or "tags/name"). Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Linus Torvalds committed Jan 22, 2014 at 12:32 UTC 024d34cb0813e098c5c0e4ccdacb6a461d34cb6b
1 file changed +43 -67
git-request-pull.sh
+43 -67
@@ -35,20 +35,7 @@ do
35 shift
36 done
37
38 -base=$1 url=$2 head=${3-HEAD} status=0 branch_name=
39 -
40 -headref=$(git symbolic-ref -q "$head")
41 -if git show-ref -q --verify "$headref"
42 -then
43 - branch_name=${headref#refs/heads/}
44 - if test "z$branch_name" = "z$headref" ||
45 - ! git config "branch.$branch_name.description" >/dev/null
46 - then
47 - branch_name=
48 - fi
49 -fi
50 -
51 -tag_name=$(git describe --exact "$head^0" 2>/dev/null)
38 +base=$1 url=$2 status=0
39
40 test -n "$base" && test -n "$url" || usage
41
@@ -58,55 +45,68 @@ then
45 die "fatal: Not a valid revision: $base"
46 fi
47
48 +#
49 +# $3 must be a symbolic ref, a unique ref, or
50 +# a SHA object expression
51 +#
52 +head=$(git symbolic-ref -q "${3-HEAD}")
53 +head=${head:-$(git show-ref "${3-HEAD}" | cut -d' ' -f2)}
54 +head=${head:-$(git rev-parse --quiet --verify "$3")}
55 +
56 +# None of the above? Bad.
57 +test -z "$head" && die "fatal: Not a valid revision: $3"
58 +
59 +# This also verifies that the resulting head is unique:
60 +# "git show-ref" could have shown multiple matching refs..
61 headrev=$(git rev-parse --verify --quiet "$head"^0)
62 -if test -z "$headrev"
62 +test -z "$headrev" && die "fatal: Ambiguous revision: $3"
63 +
64 +# Was it a branch with a description?
65 +branch_name=${head#refs/heads/}
66 +if test "z$branch_name" = "z$headref" ||
67 + ! git config "branch.$branch_name.description" >/dev/null
68 then
64 - die "fatal: Not a valid revision: $head"
69 + branch_name=
70 fi
71
72 +prettyhead=${head#refs/}
73 +prettyhead=${prettyhead#heads/}
74 +
75 merge_base=$(git merge-base $baserev $headrev) ||
76 die "fatal: No commits in common between $base and $head"
77
70 -# $head is the token given from the command line, and $tag_name, if
71 -# exists, is the tag we are going to show the commit information for.
72 -# If that tag exists at the remote and it points at the commit, use it.
73 -# Otherwise, if a branch with the same name as $head exists at the remote
74 -# and their values match, use that instead.
78 +# $head is the refname from the command line.
79 +# If a ref with the same name as $head exists at the remote
80 +# and their values match, use that.
81 #
82 # Otherwise find a random ref that matches $headrev.
83 find_matching_ref='
78 - sub abbr {
79 - my $ref = shift;
80 - if ($ref =~ s|^refs/heads/|| || $ref =~ s|^refs/tags/|tags/|) {
81 - return $ref;
82 - } else {
83 - return $ref;
84 - }
85 - }
86 -
87 - my ($tagged, $branch, $found);
84 + my ($exact,$found);
85 while (<STDIN>) {
89 - my ($sha1, $ref, $deref) = /^(\S+)\s+(\S+?)(\^\{\})?$/;
86 + my ($sha1, $ref, $deref) = /^(\S+)\s+([^^]+)(\S*)$/;
87 next unless ($sha1 eq $ARGV[1]);
91 - $found = abbr($ref);
92 - if ($deref && $ref eq "tags/$ARGV[2]") {
93 - $tagged = $found;
94 - last;
88 + if ($ref eq $ARGV[0]) {
89 + $exact = $ref;
90 }
96 - if ($ref =~ m|/\Q$ARGV[0]\E$|) {
97 - $exact = $found;
91 + if ($sha1 eq $ARGV[0]) {
92 + $found = $sha1;
93 }
94 }
100 - if ($tagged) {
101 - print "$tagged\n";
102 - } elsif ($exact) {
95 + if ($exact) {
96 print "$exact\n";
97 } elsif ($found) {
98 print "$found\n";
99 }
100 '
101
109 -ref=$(git ls-remote "$url" | @@PERL@@ -e "$find_matching_ref" "$head" "$headrev" "$tag_name")
102 +ref=$(git ls-remote "$url" | @@PERL@@ -e "$find_matching_ref" "$head" "$headrev")
103 +
104 +if test -z "$ref"
105 +then
106 + echo "warn: No match for $prettyhead found at $url" >&2
107 + echo "warn: Are you sure you pushed '$prettyhead' there?" >&2
108 + status=1
109 +fi
110
111 url=$(git ls-remote --get-url "$url")
112
@@ -116,7 +116,7 @@ git show -s --format='The following changes since commit %H:
116
117 are available in the git repository at:
118 ' $merge_base &&
119 -echo " $url${ref+ $ref}" &&
119 +echo " $url $prettyhead" &&
120 git show -s --format='
121 for you to fetch changes up to %H:
122
@@ -129,34 +129,10 @@ then
129 echo "(from the branch description for $branch_name local branch)"
130 echo
131 git config "branch.$branch_name.description"
132 -fi &&
133 -
134 -if test -n "$tag_name"
135 -then
136 - if test -z "$ref" || test "$ref" != "tags/$tag_name"
137 - then
138 - echo >&2 "warn: You locally have $tag_name but it does not (yet)"
139 - echo >&2 "warn: appear to be at $url"
140 - echo >&2 "warn: Do you want to push it there, perhaps?"
141 - fi
142 - git cat-file tag "$tag_name" |
143 - sed -n -e '1,/^$/d' -e '/^-----BEGIN PGP /q' -e p
144 - echo
145 -fi &&
146 -
147 -if test -n "$branch_name" || test -n "$tag_name"
148 -then
132 echo "----------------------------------------------------------------"
133 fi &&
134
135 git shortlog ^$baserev $headrev &&
136 git diff -M --stat --summary $patch $merge_base..$headrev || status=1
137
155 -if test -z "$ref"
156 -then
157 - echo "warn: No branch of $url is at:" >&2
158 - git show -s --format='warn: %h: %s' $headrev >&2
159 - echo "warn: Are you sure you pushed '$head' there?" >&2
160 - status=1
161 -fi
138 exit $status