Linus Torvalds | b33e966 | 2005-07-08 10:57:21 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
Junio C Hamano | ae2b0f1 | 2005-11-24 00:12:11 -0800 | [diff] [blame] | 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. |
Linus Torvalds | b33e966 | 2005-07-08 10:57:21 -0700 | [diff] [blame] | 7 | |
Junio C Hamano | 365527a | 2005-09-12 19:47:07 -0700 | [diff] [blame] | 8 | # Having this variable in your environment would break scripts because |
Junio C Hamano | e598c51 | 2007-09-15 16:32:23 -0700 | [diff] [blame] | 9 | # you would cause "cd" to be taken to unexpected places. If you |
Junio C Hamano | 365527a | 2005-09-12 19:47:07 -0700 | [diff] [blame] | 10 | # like CDPATH, define it for your interactive shell sessions without |
| 11 | # exporting it. |
| 12 | unset CDPATH |
| 13 | |
Junio C Hamano | 61dbb3c | 2009-06-08 09:41:49 -0700 | [diff] [blame] | 14 | git_broken_path_fix () { |
| 15 | case ":$PATH:" in |
| 16 | *:$1:*) : ok ;; |
| 17 | *) |
| 18 | PATH=$( |
| 19 | SANE_TOOL_PATH="$1" |
| 20 | IFS=: path= sep= |
| 21 | set x $PATH |
| 22 | shift |
| 23 | for elem |
| 24 | do |
| 25 | case "$SANE_TOOL_PATH:$elem" in |
| 26 | (?*:/bin | ?*:/usr/bin) |
| 27 | path="$path$sep$SANE_TOOL_PATH" |
| 28 | sep=: |
| 29 | SANE_TOOL_PATH= |
| 30 | esac |
| 31 | path="$path$sep$elem" |
| 32 | sep=: |
| 33 | done |
| 34 | echo "$path" |
| 35 | ) |
| 36 | ;; |
| 37 | esac |
| 38 | } |
| 39 | |
| 40 | # @@BROKEN_PATH_FIX@@ |
Junio C Hamano | 0e0aea5 | 2009-06-05 18:36:15 -0500 | [diff] [blame] | 41 | |
Linus Torvalds | b33e966 | 2005-07-08 10:57:21 -0700 | [diff] [blame] | 42 | die() { |
Junio C Hamano | 8098a17 | 2005-09-30 14:26:57 -0700 | [diff] [blame] | 43 | echo >&2 "$@" |
Linus Torvalds | b33e966 | 2005-07-08 10:57:21 -0700 | [diff] [blame] | 44 | exit 1 |
| 45 | } |
| 46 | |
Stephen Boyd | e064c17 | 2009-06-16 15:32:59 -0700 | [diff] [blame] | 47 | GIT_QUIET= |
| 48 | |
| 49 | say () { |
| 50 | if test -z "$GIT_QUIET" |
| 51 | then |
| 52 | printf '%s\n' "$*" |
| 53 | fi |
| 54 | } |
| 55 | |
Pierre Habouzit | bac199b | 2007-11-04 11:30:54 +0100 | [diff] [blame] | 56 | if test -n "$OPTIONS_SPEC"; then |
| 57 | usage() { |
Junio C Hamano | 5d29275 | 2007-12-20 12:58:06 -0800 | [diff] [blame] | 58 | "$0" -h |
| 59 | exit 1 |
Pierre Habouzit | bac199b | 2007-11-04 11:30:54 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Pierre Habouzit | e817e3e | 2007-11-08 10:32:11 +0100 | [diff] [blame] | 62 | parseopt_extra= |
| 63 | [ -n "$OPTIONS_KEEPDASHDASH" ] && |
| 64 | parseopt_extra="--keep-dashdash" |
| 65 | |
| 66 | eval "$( |
Junio C Hamano | cbea86f | 2007-11-07 23:04:38 -0800 | [diff] [blame] | 67 | echo "$OPTIONS_SPEC" | |
Pierre Habouzit | e817e3e | 2007-11-08 10:32:11 +0100 | [diff] [blame] | 68 | git rev-parse --parseopt $parseopt_extra -- "$@" || |
| 69 | echo exit $? |
| 70 | )" |
Pierre Habouzit | bac199b | 2007-11-04 11:30:54 +0100 | [diff] [blame] | 71 | else |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 72 | dashless=$(basename "$0" | sed -e 's/-/ /') |
Pierre Habouzit | bac199b | 2007-11-04 11:30:54 +0100 | [diff] [blame] | 73 | usage() { |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 74 | die "Usage: $dashless $USAGE" |
Pierre Habouzit | bac199b | 2007-11-04 11:30:54 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | if [ -z "$LONG_USAGE" ] |
| 78 | then |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 79 | LONG_USAGE="Usage: $dashless $USAGE" |
Pierre Habouzit | bac199b | 2007-11-04 11:30:54 +0100 | [diff] [blame] | 80 | else |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 81 | LONG_USAGE="Usage: $dashless $USAGE |
Pierre Habouzit | bac199b | 2007-11-04 11:30:54 +0100 | [diff] [blame] | 82 | |
| 83 | $LONG_USAGE" |
| 84 | fi |
| 85 | |
| 86 | case "$1" in |
| 87 | -h|--h|--he|--hel|--help) |
| 88 | echo "$LONG_USAGE" |
| 89 | exit |
| 90 | esac |
| 91 | fi |
Fredrik Kuivinen | d025524 | 2005-12-11 10:55:49 +0100 | [diff] [blame] | 92 | |
Shawn O. Pearce | f947413 | 2006-12-28 02:34:48 -0500 | [diff] [blame] | 93 | set_reflog_action() { |
| 94 | if [ -z "${GIT_REFLOG_ACTION:+set}" ] |
| 95 | then |
| 96 | GIT_REFLOG_ACTION="$*" |
| 97 | export GIT_REFLOG_ACTION |
| 98 | fi |
| 99 | } |
| 100 | |
Adam Roben | ef0c2ab | 2007-07-19 22:09:35 -0700 | [diff] [blame] | 101 | git_editor() { |
Jonathan Nieder | b4479f0 | 2009-10-30 20:42:34 -0500 | [diff] [blame] | 102 | if test -z "${GIT_EDITOR:+set}" |
| 103 | then |
| 104 | GIT_EDITOR="$(git var GIT_EDITOR)" || return $? |
| 105 | fi |
| 106 | |
| 107 | eval "$GIT_EDITOR" '"$@"' |
Adam Roben | ef0c2ab | 2007-07-19 22:09:35 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Jonathan Nieder | f6dff11 | 2010-02-14 23:04:13 -0600 | [diff] [blame] | 110 | git_pager() { |
| 111 | if test -t 1 |
| 112 | then |
| 113 | GIT_PAGER=$(git var GIT_PAGER) |
| 114 | else |
| 115 | GIT_PAGER=cat |
| 116 | fi |
| 117 | : ${LESS=-FRSX} |
| 118 | export LESS |
| 119 | |
| 120 | eval "$GIT_PAGER" '"$@"' |
| 121 | } |
| 122 | |
Junio C Hamano | e1622bf | 2009-11-23 15:56:32 -0800 | [diff] [blame] | 123 | sane_grep () { |
| 124 | GREP_OPTIONS= LC_ALL=C grep "$@" |
| 125 | } |
| 126 | |
| 127 | sane_egrep () { |
| 128 | GREP_OPTIONS= LC_ALL=C egrep "$@" |
| 129 | } |
| 130 | |
Junio C Hamano | 4b441f4 | 2007-01-07 02:17:52 -0800 | [diff] [blame] | 131 | is_bare_repository () { |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 132 | git rev-parse --is-bare-repository |
Junio C Hamano | 4b441f4 | 2007-01-07 02:17:52 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Junio C Hamano | 9fde940 | 2007-01-12 12:44:08 -0800 | [diff] [blame] | 135 | cd_to_toplevel () { |
Steven Drake | 91dc602 | 2010-01-12 11:34:34 +1300 | [diff] [blame] | 136 | cdup=$(git rev-parse --show-toplevel) && |
| 137 | cd "$cdup" || { |
| 138 | echo >&2 "Cannot chdir to $cdup, the toplevel of the working tree" |
| 139 | exit 1 |
| 140 | } |
Junio C Hamano | 9fde940 | 2007-01-12 12:44:08 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Shawn O. Pearce | 7eff28a | 2006-12-30 23:32:38 -0500 | [diff] [blame] | 143 | require_work_tree () { |
Gabriel Filion | ab62677 | 2010-02-16 23:18:50 -0500 | [diff] [blame] | 144 | test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = true || |
Shawn O. Pearce | 7eff28a | 2006-12-30 23:32:38 -0500 | [diff] [blame] | 145 | die "fatal: $0 cannot be used without a working tree." |
| 146 | } |
| 147 | |
Ramkumar Ramachandra | 92c62a3 | 2010-10-19 20:09:28 +0530 | [diff] [blame] | 148 | require_clean_work_tree () { |
| 149 | git rev-parse --verify HEAD >/dev/null || exit 1 |
| 150 | git update-index -q --ignore-submodules --refresh |
| 151 | err=0 |
| 152 | |
| 153 | if ! git diff-files --quiet --ignore-submodules |
| 154 | then |
| 155 | echo >&2 "Cannot $1: You have unstaged changes." |
| 156 | err=1 |
| 157 | fi |
| 158 | |
| 159 | if ! git diff-index --cached --quiet --ignore-submodules HEAD -- |
| 160 | then |
| 161 | if [ $err = 0 ] |
| 162 | then |
| 163 | echo >&2 "Cannot $1: Your index contains uncommitted changes." |
| 164 | else |
| 165 | echo >&2 "Additionally, your index contains uncommitted changes." |
| 166 | fi |
| 167 | err=1 |
| 168 | fi |
| 169 | |
| 170 | if [ $err = 1 ] |
| 171 | then |
| 172 | test -n "$2" && echo >&2 "$2" |
| 173 | exit 1 |
| 174 | fi |
| 175 | } |
| 176 | |
Johannes Schindelin | 0cae234 | 2007-06-25 01:04:11 +0100 | [diff] [blame] | 177 | get_author_ident_from_commit () { |
| 178 | pick_author_script=' |
| 179 | /^author /{ |
| 180 | s/'\''/'\''\\'\'\''/g |
| 181 | h |
| 182 | s/^author \([^<]*\) <[^>]*> .*$/\1/ |
Johannes Schindelin | 0cae234 | 2007-06-25 01:04:11 +0100 | [diff] [blame] | 183 | s/.*/GIT_AUTHOR_NAME='\''&'\''/p |
| 184 | |
| 185 | g |
| 186 | s/^author [^<]* <\([^>]*\)> .*$/\1/ |
Johannes Schindelin | 0cae234 | 2007-06-25 01:04:11 +0100 | [diff] [blame] | 187 | s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p |
| 188 | |
| 189 | g |
| 190 | s/^author [^<]* <[^>]*> \(.*\)$/\1/ |
Johannes Schindelin | 0cae234 | 2007-06-25 01:04:11 +0100 | [diff] [blame] | 191 | s/.*/GIT_AUTHOR_DATE='\''&'\''/p |
| 192 | |
| 193 | q |
| 194 | } |
| 195 | ' |
| 196 | encoding=$(git config i18n.commitencoding || echo UTF-8) |
Junio C Hamano | 077b725 | 2008-02-13 13:13:21 -0800 | [diff] [blame] | 197 | git show -s --pretty=raw --encoding="$encoding" "$1" -- | |
Johannes Schindelin | 0cae234 | 2007-06-25 01:04:11 +0100 | [diff] [blame] | 198 | LANG=C LC_ALL=C sed -ne "$pick_author_script" |
| 199 | } |
| 200 | |
Giuseppe Bilotta | 7d750f0 | 2010-02-25 00:34:16 +0100 | [diff] [blame] | 201 | # Clear repo-local GIT_* environment variables. Useful when switching to |
| 202 | # another repository (e.g. when entering a submodule). See also the env |
| 203 | # list in git_connect() |
| 204 | clear_local_git_env() { |
| 205 | unset $(git rev-parse --local-env-vars) |
| 206 | } |
| 207 | |
Junio C Hamano | 22c9071 | 2007-12-12 17:34:30 -0800 | [diff] [blame] | 208 | # Make sure we are in a valid repository of a vintage we understand, |
| 209 | # if we require to be in a git repository. |
Christian Couder | 70087cd | 2007-12-15 05:57:28 +0100 | [diff] [blame] | 210 | if test -z "$NONGIT_OK" |
Fredrik Kuivinen | d025524 | 2005-12-11 10:55:49 +0100 | [diff] [blame] | 211 | then |
Lars Hjemli | 4d6d6d2 | 2008-02-18 11:44:19 +0100 | [diff] [blame] | 212 | GIT_DIR=$(git rev-parse --git-dir) || exit |
Junio C Hamano | 22c9071 | 2007-12-12 17:34:30 -0800 | [diff] [blame] | 213 | if [ -z "$SUBDIRECTORY_OK" ] |
| 214 | then |
Junio C Hamano | 22c9071 | 2007-12-12 17:34:30 -0800 | [diff] [blame] | 215 | test -z "$(git rev-parse --show-cdup)" || { |
| 216 | exit=$? |
| 217 | echo >&2 "You need to run this command from the toplevel of the working tree." |
| 218 | exit $exit |
| 219 | } |
Junio C Hamano | 22c9071 | 2007-12-12 17:34:30 -0800 | [diff] [blame] | 220 | fi |
| 221 | test -n "$GIT_DIR" && GIT_DIR=$(cd "$GIT_DIR" && pwd) || { |
| 222 | echo >&2 "Unable to determine absolute path of git directory" |
| 223 | exit 1 |
David Kastrup | b2bc9a3 | 2007-08-11 15:36:28 +0200 | [diff] [blame] | 224 | } |
Junio C Hamano | 22c9071 | 2007-12-12 17:34:30 -0800 | [diff] [blame] | 225 | : ${GIT_OBJECT_DIRECTORY="$GIT_DIR/objects"} |
Fredrik Kuivinen | d025524 | 2005-12-11 10:55:49 +0100 | [diff] [blame] | 226 | fi |
Johannes Sixt | 87bddba | 2007-11-30 22:06:30 +0100 | [diff] [blame] | 227 | |
| 228 | # Fix some commands on Windows |
| 229 | case $(uname -s) in |
| 230 | *MINGW*) |
| 231 | # Windows has its own (incompatible) sort and find |
| 232 | sort () { |
| 233 | /usr/bin/sort "$@" |
| 234 | } |
| 235 | find () { |
| 236 | /usr/bin/find "$@" |
| 237 | } |
Pat Thoyts | 5e9677c | 2010-09-30 14:24:07 +0100 | [diff] [blame] | 238 | is_absolute_path () { |
| 239 | case "$1" in |
| 240 | [/\\]* | [A-Za-z]:*) |
| 241 | return 0 ;; |
| 242 | esac |
| 243 | return 1 |
| 244 | } |
Johannes Sixt | 87bddba | 2007-11-30 22:06:30 +0100 | [diff] [blame] | 245 | ;; |
Pat Thoyts | 5e9677c | 2010-09-30 14:24:07 +0100 | [diff] [blame] | 246 | *) |
| 247 | is_absolute_path () { |
| 248 | case "$1" in |
| 249 | /*) |
| 250 | return 0 ;; |
| 251 | esac |
| 252 | return 1 |
| 253 | } |
Johannes Sixt | 87bddba | 2007-11-30 22:06:30 +0100 | [diff] [blame] | 254 | esac |