780258135c773180530af4f3ddae95cfe53aec7e
   1#!/bin/sh
   2#
   3# This is included in commands that either have to be run from the toplevel
   4# of the repository, or with GIT_DIR environment variable properly.
   5# If the GIT_DIR does not look like the right correct git-repository,
   6# it dies.
   7
   8# Having this variable in your environment would break scripts because
   9# you would cause "cd" to be taken to unexpected places.  If you
  10# like CDPATH, define it for your interactive shell sessions without
  11# exporting it.
  12unset CDPATH
  13
  14# @@PATH@@:$PATH
  15
  16die() {
  17        echo >&2 "$@"
  18        exit 1
  19}
  20
  21if test -n "$OPTIONS_SPEC"; then
  22        usage() {
  23                "$0" -h
  24                exit 1
  25        }
  26
  27        parseopt_extra=
  28        [ -n "$OPTIONS_KEEPDASHDASH" ] &&
  29                parseopt_extra="--keep-dashdash"
  30
  31        eval "$(
  32                echo "$OPTIONS_SPEC" |
  33                        git rev-parse --parseopt $parseopt_extra -- "$@" ||
  34                echo exit $?
  35        )"
  36else
  37        dashless=$(basename "$0" | sed -e 's/-/ /')
  38        usage() {
  39                die "Usage: $dashless $USAGE"
  40        }
  41
  42        if [ -z "$LONG_USAGE" ]
  43        then
  44                LONG_USAGE="Usage: $dashless $USAGE"
  45        else
  46                LONG_USAGE="Usage: $dashless $USAGE
  47
  48$LONG_USAGE"
  49        fi
  50
  51        case "$1" in
  52                -h|--h|--he|--hel|--help)
  53                echo "$LONG_USAGE"
  54                exit
  55        esac
  56fi
  57
  58set_reflog_action() {
  59        if [ -z "${GIT_REFLOG_ACTION:+set}" ]
  60        then
  61                GIT_REFLOG_ACTION="$*"
  62                export GIT_REFLOG_ACTION
  63        fi
  64}
  65
  66git_editor() {
  67        : "${GIT_EDITOR:=$(git config core.editor)}"
  68        : "${GIT_EDITOR:=${VISUAL:-${EDITOR}}}"
  69        case "$GIT_EDITOR,$TERM" in
  70        ,dumb)
  71                echo >&2 "No editor specified in GIT_EDITOR, core.editor, VISUAL,"
  72                echo >&2 "or EDITOR. Tried to fall back to vi but terminal is dumb."
  73                echo >&2 "Please set one of these variables to an appropriate"
  74                echo >&2 "editor or run $0 with options that will not cause an"
  75                echo >&2 "editor to be invoked (e.g., -m or -F for git-commit)."
  76                exit 1
  77                ;;
  78        esac
  79        eval "${GIT_EDITOR:=vi}" '"$@"'
  80}
  81
  82is_bare_repository () {
  83        git rev-parse --is-bare-repository
  84}
  85
  86cd_to_toplevel () {
  87        cdup=$(git rev-parse --show-cdup)
  88        if test ! -z "$cdup"
  89        then
  90                # The "-P" option says to follow "physical" directory
  91                # structure instead of following symbolic links.  When cdup is
  92                # "../", this means following the ".." entry in the current
  93                # directory instead textually removing a symlink path element
  94                # from the PWD shell variable.  The "-P" behavior is more
  95                # consistent with the C-style chdir used by most of Git.
  96                cd -P "$cdup" || {
  97                        echo >&2 "Cannot chdir to $cdup, the toplevel of the working tree"
  98                        exit 1
  99                }
 100        fi
 101}
 102
 103require_work_tree () {
 104        test $(git rev-parse --is-inside-work-tree) = true ||
 105        die "fatal: $0 cannot be used without a working tree."
 106}
 107
 108get_author_ident_from_commit () {
 109        pick_author_script='
 110        /^author /{
 111                s/'\''/'\''\\'\'\''/g
 112                h
 113                s/^author \([^<]*\) <[^>]*> .*$/\1/
 114                s/'\''/'\''\'\'\''/g
 115                s/.*/GIT_AUTHOR_NAME='\''&'\''/p
 116
 117                g
 118                s/^author [^<]* <\([^>]*\)> .*$/\1/
 119                s/'\''/'\''\'\'\''/g
 120                s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
 121
 122                g
 123                s/^author [^<]* <[^>]*> \(.*\)$/\1/
 124                s/'\''/'\''\'\'\''/g
 125                s/.*/GIT_AUTHOR_DATE='\''&'\''/p
 126
 127                q
 128        }
 129        '
 130        encoding=$(git config i18n.commitencoding || echo UTF-8)
 131        git show -s --pretty=raw --encoding="$encoding" "$1" -- |
 132        LANG=C LC_ALL=C sed -ne "$pick_author_script"
 133}
 134
 135# Make sure we are in a valid repository of a vintage we understand,
 136# if we require to be in a git repository.
 137if test -z "$NONGIT_OK"
 138then
 139        GIT_DIR=$(git rev-parse --git-dir) || exit
 140        if [ -z "$SUBDIRECTORY_OK" ]
 141        then
 142                test -z "$(git rev-parse --show-cdup)" || {
 143                        exit=$?
 144                        echo >&2 "You need to run this command from the toplevel of the working tree."
 145                        exit $exit
 146                }
 147        fi
 148        test -n "$GIT_DIR" && GIT_DIR=$(cd "$GIT_DIR" && pwd) || {
 149                echo >&2 "Unable to determine absolute path of git directory"
 150                exit 1
 151        }
 152        : ${GIT_OBJECT_DIRECTORY="$GIT_DIR/objects"}
 153fi
 154
 155# Fix some commands on Windows
 156case $(uname -s) in
 157*MINGW*)
 158        # Windows has its own (incompatible) sort and find
 159        sort () {
 160                /usr/bin/sort "$@"
 161        }
 162        find () {
 163                /usr/bin/find "$@"
 164        }
 165        ;;
 166esac