request-pull: quote regex metacharacters in local ref
[git/git.git] / git-request-pull.sh
CommitLineData
53dfac44 1#!/bin/sh
ab421d2c
RA
2# Copyright 2005, Ryan Anderson <ryan@michonline.com>
3#
4# This file is licensed under the GPL v2, or a later version
5# at the discretion of Linus Torvalds.
6
806f36d4 7SUBDIRECTORY_OK='Yes'
50ab6558 8OPTIONS_KEEPDASHDASH=
51ba8ce3 9OPTIONS_STUCKLONG=
133cfaeb
JH
10OPTIONS_SPEC='git request-pull [options] start url [end]
11--
12p show patch text as well
13'
14
806f36d4 15. git-sh-setup
ab421d2c 16
653a31c1
MM
17GIT_PAGER=
18export GIT_PAGER
19
133cfaeb
JH
20patch=
21while case "$#" in 0) break ;; esac
22do
23 case "$1" in
24 -p)
25 patch=-p ;;
26 --)
27 shift; break ;;
28 -*)
29 usage ;;
30 *)
31 break ;;
32 esac
33 shift
34done
35
024d34cb 36base=$1 url=$2 status=0
d0504645 37
3c9f1e7c 38test -n "$base" && test -n "$url" || usage
ace33bf9
DW
39
40baserev=$(git rev-parse --verify --quiet "$base"^0)
41if test -z "$baserev"
42then
43 die "fatal: Not a valid revision: $base"
44fi
45
024d34cb
LT
46#
47# $3 must be a symbolic ref, a unique ref, or
dc2eacc5
LT
48# a SHA object expression. It can also be of
49# the format 'local-name:remote-name'.
024d34cb 50#
dc2eacc5
LT
51local=${3%:*}
52local=${local:-HEAD}
53remote=${3#*:}
5aae66bd
JH
54pretty_remote=${remote#refs/}
55pretty_remote=${pretty_remote#heads/}
dc2eacc5
LT
56head=$(git symbolic-ref -q "$local")
57head=${head:-$(git show-ref --heads --tags "$local" | cut -d' ' -f2)}
58head=${head:-$(git rev-parse --quiet --verify "$local")}
024d34cb
LT
59
60# None of the above? Bad.
dc2eacc5 61test -z "$head" && die "fatal: Not a valid revision: $local"
024d34cb
LT
62
63# This also verifies that the resulting head is unique:
64# "git show-ref" could have shown multiple matching refs..
ace33bf9 65headrev=$(git rev-parse --verify --quiet "$head"^0)
dc2eacc5 66test -z "$headrev" && die "fatal: Ambiguous revision: $local"
024d34cb
LT
67
68# Was it a branch with a description?
69branch_name=${head#refs/heads/}
70if test "z$branch_name" = "z$headref" ||
71 ! git config "branch.$branch_name.description" >/dev/null
ace33bf9 72then
024d34cb 73 branch_name=
ace33bf9 74fi
ab421d2c 75
3c9f1e7c 76merge_base=$(git merge-base $baserev $headrev) ||
ff06c743
SP
77die "fatal: No commits in common between $base and $head"
78
024d34cb
LT
79# $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.
682853e6
JH
82#
83# Otherwise find a random ref that matches $headrev.
fe46fa9d 84find_matching_ref='
dc2eacc5 85 my ($head,$headrev) = (@ARGV);
5731dfce 86 my $pattern = qr{/\Q$head\E$};
dc2eacc5 87 my ($found);
fe46fa9d 88
fe46fa9d 89 while (<STDIN>) {
dc2eacc5 90 chomp;
024d34cb 91 my ($sha1, $ref, $deref) = /^(\S+)\s+([^^]+)(\S*)$/;
dc2eacc5
LT
92 next unless ($sha1 eq $headrev);
93
dc2eacc5
LT
94 if ($ref eq $head) {
95 $found = $ref;
96 }
5731dfce 97 if ($ref =~ $pattern) {
dc2eacc5 98 $found = $ref;
682853e6 99 }
dc2eacc5 100 if ($sha1 eq $head) {
024d34cb 101 $found = $sha1;
fe46fa9d
JH
102 }
103 }
dc2eacc5 104 if ($found) {
fe46fa9d
JH
105 print "$found\n";
106 }
107'
108
dc2eacc5 109ref=$(git ls-remote "$url" | @@PERL@@ -e "$find_matching_ref" "${remote:-HEAD}" "$headrev")
024d34cb
LT
110
111if test -z "$ref"
112then
dc2eacc5
LT
113 echo "warn: No match for commit $headrev found at $url" >&2
114 echo "warn: Are you sure you pushed '${remote:-HEAD}' there?" >&2
024d34cb
LT
115 status=1
116fi
fe46fa9d 117
d952cbb1
JH
118# Special case: turn "for_linus" to "tags/for_linus" when it is correct
119if test "$ref" = "refs/tags/$pretty_remote"
120then
121 pretty_remote=tags/$pretty_remote
122fi
123
1a927775 124url=$(git ls-remote --get-url "$url")
ff06c743 125
10eb0007 126git show -s --format='The following changes since commit %H:
ab421d2c 127
10eb0007
MV
128 %s (%ci)
129
e66d7c37 130are available in the Git repository at:
b7e642ec 131' $merge_base &&
5aae66bd 132echo " $url $pretty_remote" &&
cf731666
JH
133git show -s --format='
134for you to fetch changes up to %H:
135
136 %s (%ci)
137
138----------------------------------------------------------------' $headrev &&
ab421d2c 139
4b14ec87 140if test $(git cat-file -t "$head") = tag
c0168147 141then
4b14ec87 142 git cat-file tag "$head" |
d0504645
JH
143 sed -n -e '1,/^$/d' -e '/^-----BEGIN PGP /q' -e p
144 echo
4b14ec87 145 echo "----------------------------------------------------------------"
d0504645
JH
146fi &&
147
c0168147 148if test -n "$branch_name"
d0504645 149then
fe46fa9d 150 echo "(from the branch description for $branch_name local branch)"
c0168147
JH
151 echo
152 git config "branch.$branch_name.description"
153 echo "----------------------------------------------------------------"
154fi &&
d0504645 155
53dfac44 156git shortlog ^$baserev $headrev &&
cf731666
JH
157git diff -M --stat --summary $patch $merge_base..$headrev || status=1
158
ff06c743 159exit $status