request-pull: resurrect "pretty refname" feature
[git] / git-request-pull.sh
1 #!/bin/sh
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
7 USAGE='<start> <url> [<end>]'
8 LONG_USAGE='Summarizes the changes between two commits to the standard output,
9 and includes the given URL in the generated summary.'
10 SUBDIRECTORY_OK='Yes'
11 OPTIONS_KEEPDASHDASH=
12 OPTIONS_SPEC='git request-pull [options] start url [end]
13 --
14 p    show patch text as well
15 '
16
17 . git-sh-setup
18
19 GIT_PAGER=
20 export GIT_PAGER
21
22 patch=
23 while   case "$#" in 0) break ;; esac
24 do
25         case "$1" in
26         -p)
27                 patch=-p ;;
28         --)
29                 shift; break ;;
30         -*)
31                 usage ;;
32         *)
33                 break ;;
34         esac
35         shift
36 done
37
38 base=$1 url=$2 status=0
39
40 test -n "$base" && test -n "$url" || usage
41
42 baserev=$(git rev-parse --verify --quiet "$base"^0)
43 if test -z "$baserev"
44 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. It can also be of
51 # the format 'local-name:remote-name'.
52 #
53 local=${3%:*}
54 local=${local:-HEAD}
55 remote=${3#*:}
56 pretty_remote=${remote#refs/}
57 pretty_remote=${pretty_remote#heads/}
58 head=$(git symbolic-ref -q "$local")
59 head=${head:-$(git show-ref --heads --tags "$local" | cut -d' ' -f2)}
60 head=${head:-$(git rev-parse --quiet --verify "$local")}
61
62 # None of the above? Bad.
63 test -z "$head" && die "fatal: Not a valid revision: $local"
64
65 # This also verifies that the resulting head is unique:
66 # "git show-ref" could have shown multiple matching refs..
67 headrev=$(git rev-parse --verify --quiet "$head"^0)
68 test -z "$headrev" && die "fatal: Ambiguous revision: $local"
69
70 # Was it a branch with a description?
71 branch_name=${head#refs/heads/}
72 if test "z$branch_name" = "z$headref" ||
73         ! git config "branch.$branch_name.description" >/dev/null
74 then
75         branch_name=
76 fi
77
78 merge_base=$(git merge-base $baserev $headrev) ||
79 die "fatal: No commits in common between $base and $head"
80
81 # $head is the refname from the command line.
82 # If a ref with the same name as $head exists at the remote
83 # and their values match, use that.
84 #
85 # Otherwise find a random ref that matches $headrev.
86 find_matching_ref='
87         my ($head,$headrev) = (@ARGV);
88         my ($found);
89
90         while (<STDIN>) {
91                 chomp;
92                 my ($sha1, $ref, $deref) = /^(\S+)\s+([^^]+)(\S*)$/;
93                 my ($pattern);
94                 next unless ($sha1 eq $headrev);
95
96                 $pattern="/$head\$";
97                 if ($ref eq $head) {
98                         $found = $ref;
99                 }
100                 if ($ref =~ /$pattern/) {
101                         $found = $ref;
102                 }
103                 if ($sha1 eq $head) {
104                         $found = $sha1;
105                 }
106         }
107         if ($found) {
108                 print "$found\n";
109         }
110 '
111
112 ref=$(git ls-remote "$url" | @@PERL@@ -e "$find_matching_ref" "${remote:-HEAD}" "$headrev")
113
114 if test -z "$ref"
115 then
116         echo "warn: No match for commit $headrev found at $url" >&2
117         echo "warn: Are you sure you pushed '${remote:-HEAD}' there?" >&2
118         status=1
119 fi
120
121 url=$(git ls-remote --get-url "$url")
122
123 git show -s --format='The following changes since commit %H:
124
125   %s (%ci)
126
127 are available in the git repository at:
128 ' $merge_base &&
129 echo "  $url $pretty_remote" &&
130 git show -s --format='
131 for you to fetch changes up to %H:
132
133   %s (%ci)
134
135 ----------------------------------------------------------------' $headrev &&
136
137 if test $(git cat-file -t "$head") = tag
138 then
139         git cat-file tag "$head" |
140         sed -n -e '1,/^$/d' -e '/^-----BEGIN PGP /q' -e p
141         echo
142         echo "----------------------------------------------------------------"
143 fi &&
144
145 if test -n "$branch_name"
146 then
147         echo "(from the branch description for $branch_name local branch)"
148         echo
149         git config "branch.$branch_name.description"
150         echo "----------------------------------------------------------------"
151 fi &&
152
153 git shortlog ^$baserev $headrev &&
154 git diff -M --stat --summary $patch $merge_base..$headrev || status=1
155
156 exit $status