git-compat-util.h: avoid using c99 flex array feature with Sun compiler 5.8
[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
0e0aea5a
JH
14# @@PATH@@:$PATH
15
b33e9666 16die() {
8098a178 17 echo >&2 "$@"
b33e9666
LT
18 exit 1
19}
20
bac199b7
PH
21if test -n "$OPTIONS_SPEC"; then
22 usage() {
5d292756
JH
23 "$0" -h
24 exit 1
bac199b7
PH
25 }
26
e817e3e8
PH
27 parseopt_extra=
28 [ -n "$OPTIONS_KEEPDASHDASH" ] &&
29 parseopt_extra="--keep-dashdash"
30
31 eval "$(
cbea86fd 32 echo "$OPTIONS_SPEC" |
e817e3e8
PH
33 git rev-parse --parseopt $parseopt_extra -- "$@" ||
34 echo exit $?
35 )"
bac199b7 36else
1b1dd23f 37 dashless=$(basename "$0" | sed -e 's/-/ /')
bac199b7 38 usage() {
1b1dd23f 39 die "Usage: $dashless $USAGE"
bac199b7
PH
40 }
41
42 if [ -z "$LONG_USAGE" ]
43 then
1b1dd23f 44 LONG_USAGE="Usage: $dashless $USAGE"
bac199b7 45 else
1b1dd23f 46 LONG_USAGE="Usage: $dashless $USAGE
bac199b7
PH
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
d025524d 57
f9474132
SP
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
ef0c2abf 66git_editor() {
08874658
DK
67 : "${GIT_EDITOR:=$(git config core.editor)}"
68 : "${GIT_EDITOR:=${VISUAL:-${EDITOR}}}"
ef0c2abf
AR
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
08874658 79 eval "${GIT_EDITOR:=vi}" '"$@"'
ef0c2abf
AR
80}
81
4b441f47 82is_bare_repository () {
5be60078 83 git rev-parse --is-bare-repository
4b441f47
JH
84}
85
9fde9401 86cd_to_toplevel () {
5be60078 87 cdup=$(git rev-parse --show-cdup)
9fde9401
JH
88 if test ! -z "$cdup"
89 then
2c3c395e
MC
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"
9fde9401
JH
98 exit 1
99 }
100 fi
101}
102
7eff28a9 103require_work_tree () {
e90fdc39 104 test $(git rev-parse --is-inside-work-tree) = true ||
7eff28a9
SP
105 die "fatal: $0 cannot be used without a working tree."
106}
107
0cae2346
JS
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)
077b725f 131 git show -s --pretty=raw --encoding="$encoding" "$1" -- |
0cae2346
JS
132 LANG=C LC_ALL=C sed -ne "$pick_author_script"
133}
134
22c90717
JH
135# Make sure we are in a valid repository of a vintage we understand,
136# if we require to be in a git repository.
70087cdb 137if test -z "$NONGIT_OK"
d025524d 138then
4d6d6d2d 139 GIT_DIR=$(git rev-parse --git-dir) || exit
22c90717
JH
140 if [ -z "$SUBDIRECTORY_OK" ]
141 then
22c90717
JH
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 }
22c90717
JH
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
b2bc9a30 151 }
22c90717 152 : ${GIT_OBJECT_DIRECTORY="$GIT_DIR/objects"}
d025524d 153fi
87bddba9
JS
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