request-pull: allow "local:remote" to specify names on both ends
[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
3eb91bfc
SN
7USAGE='<start> <url> [<end>]'
8LONG_USAGE='Summarizes the changes between two commits to the standard output,
9and includes the given URL in the generated summary.'
806f36d4 10SUBDIRECTORY_OK='Yes'
50ab6558 11OPTIONS_KEEPDASHDASH=
133cfaeb
JH
12OPTIONS_SPEC='git request-pull [options] start url [end]
13--
14p show patch text as well
15'
16
806f36d4 17. git-sh-setup
ab421d2c 18
653a31c1
MM
19GIT_PAGER=
20export GIT_PAGER
21
133cfaeb
JH
22patch=
23while case "$#" in 0) break ;; esac
24do
25 case "$1" in
26 -p)
27 patch=-p ;;
28 --)
29 shift; break ;;
30 -*)
31 usage ;;
32 *)
33 break ;;
34 esac
35 shift
36done
37
024d34cb 38base=$1 url=$2 status=0
d0504645 39
3c9f1e7c 40test -n "$base" && test -n "$url" || usage
ace33bf9
DW
41
42baserev=$(git rev-parse --verify --quiet "$base"^0)
43if test -z "$baserev"
44then
45 die "fatal: Not a valid revision: $base"
46fi
47
024d34cb
LT
48#
49# $3 must be a symbolic ref, a unique ref, or
dc2eacc5
LT
50# a SHA object expression. It can also be of
51# the format 'local-name:remote-name'.
024d34cb 52#
dc2eacc5
LT
53local=${3%:*}
54local=${local:-HEAD}
55remote=${3#*:}
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
LT
85 my ($head,$headrev) = (@ARGV);
86 my ($found);
87
fe46fa9d 88 while (<STDIN>) {
dc2eacc5 89 chomp;
024d34cb 90 my ($sha1, $ref, $deref) = /^(\S+)\s+([^^]+)(\S*)$/;
dc2eacc5
LT
91 my ($pattern);
92 next unless ($sha1 eq $headrev);
93
94 $pattern="/$head\$";
95 if ($ref eq $head) {
96 $found = $ref;
97 }
98 if ($ref =~ /$pattern/) {
99 $found = $ref;
682853e6 100 }
dc2eacc5 101 if ($sha1 eq $head) {
024d34cb 102 $found = $sha1;
fe46fa9d
JH
103 }
104 }
dc2eacc5 105 if ($found) {
fe46fa9d
JH
106 print "$found\n";
107 }
108'
109
dc2eacc5 110ref=$(git ls-remote "$url" | @@PERL@@ -e "$find_matching_ref" "${remote:-HEAD}" "$headrev")
024d34cb
LT
111
112if test -z "$ref"
113then
dc2eacc5
LT
114 echo "warn: No match for commit $headrev found at $url" >&2
115 echo "warn: Are you sure you pushed '${remote:-HEAD}' there?" >&2
024d34cb
LT
116 status=1
117fi
fe46fa9d 118
1a927775 119url=$(git ls-remote --get-url "$url")
ff06c743 120
10eb0007 121git show -s --format='The following changes since commit %H:
ab421d2c 122
10eb0007
MV
123 %s (%ci)
124
cf731666 125are available in the git repository at:
b7e642ec 126' $merge_base &&
dc2eacc5 127echo " $url $remote" &&
cf731666
JH
128git show -s --format='
129for you to fetch changes up to %H:
130
131 %s (%ci)
132
133----------------------------------------------------------------' $headrev &&
ab421d2c 134
c0168147
JH
135if test -n "$branch_name"
136then
fe46fa9d 137 echo "(from the branch description for $branch_name local branch)"
c0168147
JH
138 echo
139 git config "branch.$branch_name.description"
140 echo "----------------------------------------------------------------"
141fi &&
d0504645 142
53dfac44 143git shortlog ^$baserev $headrev &&
cf731666
JH
144git diff -M --stat --summary $patch $merge_base..$headrev || status=1
145
ff06c743 146exit $status