git-sh-setup.sh: use "git rev-parse --show-cdup" to check for SUBDIRECTORY_OK
[git/git.git] / git-sh-setup.sh
CommitLineData
b33e9666
LT
1#!/bin/sh
2#
ae2b0f15
JH
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.
b33e9666 7
365527ad 8# Having this variable in your environment would break scripts because
e598c517 9# you would cause "cd" to be taken to unexpected places. If you
365527ad
JH
10# like CDPATH, define it for your interactive shell sessions without
11# exporting it.
12unset CDPATH
13
b33e9666 14die() {
8098a178 15 echo >&2 "$@"
b33e9666
LT
16 exit 1
17}
18
d025524d
FK
19usage() {
20 die "Usage: $0 $USAGE"
21}
22
f9474132
SP
23set_reflog_action() {
24 if [ -z "${GIT_REFLOG_ACTION:+set}" ]
25 then
26 GIT_REFLOG_ACTION="$*"
27 export GIT_REFLOG_ACTION
28 fi
29}
30
ef0c2abf 31git_editor() {
08874658
DK
32 : "${GIT_EDITOR:=$(git config core.editor)}"
33 : "${GIT_EDITOR:=${VISUAL:-${EDITOR}}}"
ef0c2abf
AR
34 case "$GIT_EDITOR,$TERM" in
35 ,dumb)
36 echo >&2 "No editor specified in GIT_EDITOR, core.editor, VISUAL,"
37 echo >&2 "or EDITOR. Tried to fall back to vi but terminal is dumb."
38 echo >&2 "Please set one of these variables to an appropriate"
39 echo >&2 "editor or run $0 with options that will not cause an"
40 echo >&2 "editor to be invoked (e.g., -m or -F for git-commit)."
41 exit 1
42 ;;
43 esac
08874658 44 eval "${GIT_EDITOR:=vi}" '"$@"'
ef0c2abf
AR
45}
46
4b441f47 47is_bare_repository () {
5be60078 48 git rev-parse --is-bare-repository
4b441f47
JH
49}
50
9fde9401 51cd_to_toplevel () {
5be60078 52 cdup=$(git rev-parse --show-cdup)
9fde9401
JH
53 if test ! -z "$cdup"
54 then
55 cd "$cdup" || {
56 echo >&2 "Cannot chdir to $cdup, the toplevel of the working tree"
57 exit 1
58 }
59 fi
60}
61
7eff28a9 62require_work_tree () {
e90fdc39 63 test $(git rev-parse --is-inside-work-tree) = true ||
7eff28a9
SP
64 die "fatal: $0 cannot be used without a working tree."
65}
66
0cae2346
JS
67get_author_ident_from_commit () {
68 pick_author_script='
69 /^author /{
70 s/'\''/'\''\\'\'\''/g
71 h
72 s/^author \([^<]*\) <[^>]*> .*$/\1/
73 s/'\''/'\''\'\'\''/g
74 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
75
76 g
77 s/^author [^<]* <\([^>]*\)> .*$/\1/
78 s/'\''/'\''\'\'\''/g
79 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
80
81 g
82 s/^author [^<]* <[^>]*> \(.*\)$/\1/
83 s/'\''/'\''\'\'\''/g
84 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
85
86 q
87 }
88 '
89 encoding=$(git config i18n.commitencoding || echo UTF-8)
90 git show -s --pretty=raw --encoding="$encoding" "$1" |
91 LANG=C LC_ALL=C sed -ne "$pick_author_script"
92}
93
d025524d
FK
94if [ -z "$LONG_USAGE" ]
95then
96 LONG_USAGE="Usage: $0 $USAGE"
97else
98 LONG_USAGE="Usage: $0 $USAGE
99
100$LONG_USAGE"
101fi
102
103case "$1" in
cad1ed95 104 -h|--h|--he|--hel|--help)
d025524d
FK
105 echo "$LONG_USAGE"
106 exit
107esac
108
c8769f76 109# Make sure we are in a valid repository of a vintage we understand.
d025524d
FK
110if [ -z "$SUBDIRECTORY_OK" ]
111then
112 : ${GIT_DIR=.git}
6b9ff1e3 113 test -z "$(git rev-parse --show-cdup)" || {
120b0dfb
JH
114 exit=$?
115 echo >&2 "You need to run this command from the toplevel of the working tree."
116 exit $exit
117 }
d025524d 118else
b2bc9a30
DK
119 GIT_DIR=$(git rev-parse --git-dir) || {
120 exit=$?
121 echo >&2 "Failed to find a valid git directory."
122 exit $exit
123 }
d025524d 124fi
b2bc9a30
DK
125
126test -n "$GIT_DIR" && GIT_DIR=$(cd "$GIT_DIR" && pwd) || {
127 echo >&2 "Unable to determine absolute path of git directory"
128 exit 1
129}
130
c8769f76 131: ${GIT_OBJECT_DIRECTORY="$GIT_DIR/objects"}