git-request-pull.shon commit t5550: do not assume the "matching" push is the default (c9704aa)
   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
   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.'
  10SUBDIRECTORY_OK='Yes'
  11OPTIONS_KEEPDASHDASH=
  12OPTIONS_SPEC='git request-pull [options] start url [end]
  13--
  14p    show patch text as well
  15'
  16
  17. git-sh-setup
  18
  19GIT_PAGER=
  20export GIT_PAGER
  21
  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
  38base=$1 url=$2 head=${3-HEAD} status=0 branch_name=
  39
  40headref=$(git symbolic-ref -q "$head")
  41if git show-ref -q --verify "$headref"
  42then
  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
  49fi
  50
  51tag_name=$(git describe --exact "$head^0" 2>/dev/null)
  52
  53test -n "$base" && test -n "$url" || usage
  54baserev=$(git rev-parse --verify "$base"^0) &&
  55headrev=$(git rev-parse --verify "$head"^0) || exit
  56
  57merge_base=$(git merge-base $baserev $headrev) ||
  58die "fatal: No commits in common between $base and $head"
  59
  60# $head is the token given from the command line, and $tag_name, if
  61# exists, is the tag we are going to show the commit information for.
  62# If that tag exists at the remote and it points at the commit, use it.
  63# Otherwise, if a branch with the same name as $head exists at the remote
  64# and their values match, use that instead.
  65#
  66# Otherwise find a random ref that matches $headrev.
  67find_matching_ref='
  68        sub abbr {
  69                my $ref = shift;
  70                if ($ref =~ s|^refs/heads/|| || $ref =~ s|^refs/tags/|tags/|) {
  71                        return $ref;
  72                } else {
  73                        return $ref;
  74                }
  75        }
  76
  77        my ($tagged, $branch, $found);
  78        while (<STDIN>) {
  79                my ($sha1, $ref, $deref) = /^(\S+)\s+(\S+?)(\^\{\})?$/;
  80                next unless ($sha1 eq $ARGV[1]);
  81                $found = abbr($ref);
  82                if ($deref && $ref eq "tags/$ARGV[2]") {
  83                        $tagged = $found;
  84                        last;
  85                }
  86                if ($ref =~ m|/\Q$ARGV[0]\E$|) {
  87                        $exact = $found;
  88                }
  89        }
  90        if ($tagged) {
  91                print "$tagged\n";
  92        } elsif ($exact) {
  93                print "$exact\n";
  94        } elsif ($found) {
  95                print "$found\n";
  96        }
  97'
  98
  99ref=$(git ls-remote "$url" | perl -e "$find_matching_ref" "$head" "$headrev" "$tag_name")
 100
 101url=$(git ls-remote --get-url "$url")
 102
 103git show -s --format='The following changes since commit %H:
 104
 105  %s (%ci)
 106
 107are available in the git repository at:
 108' $merge_base &&
 109echo "  $url${ref+ $ref}" &&
 110git show -s --format='
 111for you to fetch changes up to %H:
 112
 113  %s (%ci)
 114
 115----------------------------------------------------------------' $headrev &&
 116
 117if test -n "$branch_name"
 118then
 119        echo "(from the branch description for $branch_name local branch)"
 120        echo
 121        git config "branch.$branch_name.description"
 122fi &&
 123
 124if test -n "$tag_name"
 125then
 126        if test -z "$ref" || test "$ref" != "tags/$tag_name"
 127        then
 128                echo >&2 "warn: You locally have $tag_name but it does not (yet)"
 129                echo >&2 "warn: appear to be at $url"
 130                echo >&2 "warn: Do you want to push it there, perhaps?"
 131        fi
 132        git cat-file tag "$tag_name" |
 133        sed -n -e '1,/^$/d' -e '/^-----BEGIN PGP /q' -e p
 134        echo
 135fi &&
 136
 137if test -n "$branch_name" || test -n "$tag_name"
 138then
 139        echo "----------------------------------------------------------------"
 140fi &&
 141
 142git shortlog ^$baserev $headrev &&
 143git diff -M --stat --summary $patch $merge_base..$headrev || status=1
 144
 145if test -z "$ref"
 146then
 147        echo "warn: No branch of $url is at:" >&2
 148        git show -s --format='warn:   %h: %s' $headrev >&2
 149        echo "warn: Are you sure you pushed '$head' there?" >&2
 150        status=1
 151fi
 152exit $status