request-pull: resurrect for-linus -> tags/for-linus DWIM
[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 # Special case: turn "for_linus" to "tags/for_linus" when it is correct
122 if test "$ref" = "refs/tags/$pretty_remote"
123 then
124         pretty_remote=tags/$pretty_remote
125 fi
126
127 url=$(git ls-remote --get-url "$url")
128
129 git show -s --format='The following changes since commit %H:
130
131   %s (%ci)
132
133 are available in the git repository at:
134 ' $merge_base &&
135 echo "  $url $pretty_remote" &&
136 git show -s --format='
137 for you to fetch changes up to %H:
138
139   %s (%ci)
140
141 ----------------------------------------------------------------' $headrev &&
142
143 if test $(git cat-file -t "$head") = tag
144 then
145         git cat-file tag "$head" |
146         sed -n -e '1,/^$/d' -e '/^-----BEGIN PGP /q' -e p
147         echo
148         echo "----------------------------------------------------------------"
149 fi &&
150
151 if test -n "$branch_name"
152 then
153         echo "(from the branch description for $branch_name local branch)"
154         echo
155         git config "branch.$branch_name.description"
156         echo "----------------------------------------------------------------"
157 fi &&
158
159 git shortlog ^$baserev $headrev &&
160 git diff -M --stat --summary $patch $merge_base..$headrev || status=1
161
162 exit $status