1# This shell scriplet is meant to be included by other shell scripts
2# to set up some variables pointing at the normal git directories and
3# a few helper shell functions.
4
5# Having this variable in your environment would break scripts because
6# you would cause "cd" to be taken to unexpected places.  If you
7# like CDPATH, define it for your interactive shell sessions without
8# exporting it.
9# But we protect ourselves from such a user mistake nevertheless.
10unset CDPATH
11
12# Similarly for IFS, but some shells (e.g. FreeBSD 7.2) are buggy and
13# do not equate an unset IFS with IFS with the default, so here is
14# an explicit SP HT LF.
15IFS='
16'
17
18git_broken_path_fix () {
19	case ":$PATH:" in
20	*:$1:*) : ok ;;
21	*)
22		PATH=$(
23			SANE_TOOL_PATH="$1"
24			IFS=: path= sep=
25			set x $PATH
26			shift
27			for elem
28			do
29				case "$SANE_TOOL_PATH:$elem" in
30				(?*:/bin | ?*:/usr/bin)
31					path="$path$sep$SANE_TOOL_PATH"
32					sep=:
33					SANE_TOOL_PATH=
34				esac
35				path="$path$sep$elem"
36				sep=:
37			done
38			echo "$path"
39		)
40		;;
41	esac
42}
43
44# @@BROKEN_PATH_FIX@@
45
46# Source git-sh-i18n for gettext support.
47. "$(git --exec-path)/git-sh-i18n"
48
49die () {
50	die_with_status 1 "$@"
51}
52
53die_with_status () {
54	status=$1
55	shift
56	printf >&2 '%s\n' "$*"
57	exit "$status"
58}
59
60GIT_QUIET=
61
62say () {
63	if test -z "$GIT_QUIET"
64	then
65		printf '%s\n' "$*"
66	fi
67}
68
69if test -n "$OPTIONS_SPEC"; then
70	usage() {
71		"$0" -h
72		exit 1
73	}
74
75	parseopt_extra=
76	[ -n "$OPTIONS_KEEPDASHDASH" ] &&
77		parseopt_extra="--keep-dashdash"
78	[ -n "$OPTIONS_STUCKLONG" ] &&
79		parseopt_extra="$parseopt_extra --stuck-long"
80
81	eval "$(
82		echo "$OPTIONS_SPEC" |
83			git rev-parse --parseopt $parseopt_extra -- "$@" ||
84		echo exit $?
85	)"
86else
87	dashless=$(basename -- "$0" | sed -e 's/-/ /')
88	usage() {
89		die "$(eval_gettext "usage: \$dashless \$USAGE")"
90	}
91
92	if [ -z "$LONG_USAGE" ]
93	then
94		LONG_USAGE="$(eval_gettext "usage: \$dashless \$USAGE")"
95	else
96		LONG_USAGE="$(eval_gettext "usage: \$dashless \$USAGE
97
98$LONG_USAGE")"
99	fi
100
101	case "$1" in
102		-h)
103		echo "$LONG_USAGE"
104		case "$0" in *git-legacy-stash) exit 129;; esac
105		exit
106	esac
107fi
108
109# Set the name of the end-user facing command in the reflog when the
110# script may update refs.  When GIT_REFLOG_ACTION is already set, this
111# will not overwrite it, so that a scripted Porcelain (e.g. "git
112# rebase") can set it to its own name (e.g. "rebase") and then call
113# another scripted Porcelain (e.g. "git am") and a call to this
114# function in the latter will keep the name of the end-user facing
115# program (e.g. "rebase") in GIT_REFLOG_ACTION, ensuring whatever it
116# does will be record as actions done as part of the end-user facing
117# operation (e.g. "rebase").
118#
119# NOTE NOTE NOTE: consequently, after assigning a specific message to
120# GIT_REFLOG_ACTION when calling a "git" command to record a custom
121# reflog message, do not leave that custom value in GIT_REFLOG_ACTION,
122# after you are done.  Other callers of "git" commands that rely on
123# writing the default "program name" in reflog expect the variable to
124# contain the value set by this function.
125#
126# To use a custom reflog message, do either one of these three:
127#
128# (a) use a single-shot export form:
129#     GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: preparing frotz" \
130#         git command-that-updates-a-ref
131#
132# (b) save the original away and restore:
133#     SAVED_ACTION=$GIT_REFLOG_ACTION
134#     GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: preparing frotz"
135#     git command-that-updates-a-ref
136#     GIT_REFLOG_ACITON=$SAVED_ACTION
137#
138# (c) assign the variable in a subshell:
139#     (
140#         GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: preparing frotz"
141#         git command-that-updates-a-ref
142#     )
143set_reflog_action() {
144	if [ -z "${GIT_REFLOG_ACTION:+set}" ]
145	then
146		GIT_REFLOG_ACTION="$*"
147		export GIT_REFLOG_ACTION
148	fi
149}
150
151git_editor() {
152	if test -z "${GIT_EDITOR:+set}"
153	then
154		GIT_EDITOR="$(git var GIT_EDITOR)" || return $?
155	fi
156
157	eval "$GIT_EDITOR" '"$@"'
158}
159
160git_pager() {
161	if test -t 1
162	then
163		GIT_PAGER=$(git var GIT_PAGER)
164	else
165		GIT_PAGER=cat
166	fi
167	for vardef in @@PAGER_ENV@@
168	do
169		var=${vardef%%=*}
170		eval ": \"\${$vardef}\" && export $var"
171	done
172
173	eval "$GIT_PAGER" '"$@"'
174}
175
176sane_grep () {
177	GREP_OPTIONS= LC_ALL=C grep @@SANE_TEXT_GREP@@ "$@"
178}
179
180sane_egrep () {
181	GREP_OPTIONS= LC_ALL=C egrep @@SANE_TEXT_GREP@@ "$@"
182}
183
184is_bare_repository () {
185	git rev-parse --is-bare-repository
186}
187
188cd_to_toplevel () {
189	cdup=$(git rev-parse --show-toplevel) &&
190	cd "$cdup" || {
191		gettextln "Cannot chdir to \$cdup, the toplevel of the working tree" >&2
192		exit 1
193	}
194}
195
196require_work_tree_exists () {
197	if test "z$(git rev-parse --is-bare-repository)" != zfalse
198	then
199		program_name=$0
200		die "$(eval_gettext "fatal: \$program_name cannot be used without a working tree.")"
201	fi
202}
203
204require_work_tree () {
205	test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = true || {
206		program_name=$0
207		die "$(eval_gettext "fatal: \$program_name cannot be used without a working tree.")"
208	}
209}
210
211require_clean_work_tree () {
212	git rev-parse --verify HEAD >/dev/null || exit 1
213	git update-index -q --ignore-submodules --refresh
214	err=0
215
216	if ! git diff-files --quiet --ignore-submodules
217	then
218		action=$1
219		case "$action" in
220		"rewrite branches")
221			gettextln "Cannot rewrite branches: You have unstaged changes." >&2
222			;;
223		*)
224			eval_gettextln "Cannot \$action: You have unstaged changes." >&2
225			;;
226		esac
227		err=1
228	fi
229
230	if ! git diff-index --cached --quiet --ignore-submodules HEAD --
231	then
232		if test $err = 0
233		then
234			action=$1
235			eval_gettextln "Cannot \$action: Your index contains uncommitted changes." >&2
236		else
237		    gettextln "Additionally, your index contains uncommitted changes." >&2
238		fi
239		err=1
240	fi
241
242	if test $err = 1
243	then
244		test -n "$2" && echo "$2" >&2
245		exit 1
246	fi
247}
248
249# Generate a sed script to parse identities from a commit.
250#
251# Reads the commit from stdin, which should be in raw format (e.g., from
252# cat-file or "--pretty=raw").
253#
254# The first argument specifies the ident line to parse (e.g., "author"), and
255# the second specifies the environment variable to put it in (e.g., "AUTHOR"
256# for "GIT_AUTHOR_*"). Multiple pairs can be given to parse author and
257# committer.
258pick_ident_script () {
259	while test $# -gt 0
260	do
261		lid=$1; shift
262		uid=$1; shift
263		printf '%s' "
264		/^$lid /{
265			s/'/'\\\\''/g
266			h
267			s/^$lid "'\([^<]*\) <[^>]*> .*$/\1/'"
268			s/.*/GIT_${uid}_NAME='&'/p
269
270			g
271			s/^$lid "'[^<]* <\([^>]*\)> .*$/\1/'"
272			s/.*/GIT_${uid}_EMAIL='&'/p
273
274			g
275			s/^$lid "'[^<]* <[^>]*> \(.*\)$/@\1/'"
276			s/.*/GIT_${uid}_DATE='&'/p
277		}
278		"
279	done
280	echo '/^$/q'
281}
282
283# Create a pick-script as above and feed it to sed. Stdout is suitable for
284# feeding to eval.
285parse_ident_from_commit () {
286	LANG=C LC_ALL=C sed -ne "$(pick_ident_script "$@")"
287}
288
289# Parse the author from a commit given as an argument. Stdout is suitable for
290# feeding to eval to set the usual GIT_* ident variables.
291get_author_ident_from_commit () {
292	encoding=$(git config i18n.commitencoding || echo UTF-8)
293	git show -s --pretty=raw --encoding="$encoding" "$1" -- |
294	parse_ident_from_commit author AUTHOR
295}
296
297# Clear repo-local GIT_* environment variables. Useful when switching to
298# another repository (e.g. when entering a submodule). See also the env
299# list in git_connect()
300clear_local_git_env() {
301	unset $(git rev-parse --local-env-vars)
302}
303
304# Generate a virtual base file for a two-file merge. Uses git apply to
305# remove lines from $1 that are not in $2, leaving only common lines.
306create_virtual_base() {
307	sz0=$(wc -c <"$1")
308	@@DIFF@@ -u -La/"$1" -Lb/"$1" "$1" "$2" | git apply --no-add
309	sz1=$(wc -c <"$1")
310
311	# If we do not have enough common material, it is not
312	# worth trying two-file merge using common subsections.
313	expr $sz0 \< $sz1 \* 2 >/dev/null || : >"$1"
314}
315
316
317# Platform specific tweaks to work around some commands
318case $(uname -s) in
319*MINGW*)
320	# Windows has its own (incompatible) sort and find
321	sort () {
322		/usr/bin/sort "$@"
323	}
324	find () {
325		/usr/bin/find "$@"
326	}
327	# git sees Windows-style pwd
328	pwd () {
329		builtin pwd -W
330	}
331	is_absolute_path () {
332		case "$1" in
333		[/\\]* | [A-Za-z]:*)
334			return 0 ;;
335		esac
336		return 1
337	}
338	;;
339*)
340	is_absolute_path () {
341		case "$1" in
342		/*)
343			return 0 ;;
344		esac
345		return 1
346	}
347esac
348
349# Make sure we are in a valid repository of a vintage we understand,
350# if we require to be in a git repository.
351git_dir_init () {
352	GIT_DIR=$(git rev-parse --git-dir) || exit
353	if [ -z "$SUBDIRECTORY_OK" ]
354	then
355		test -z "$(git rev-parse --show-cdup)" || {
356			exit=$?
357			gettextln "You need to run this command from the toplevel of the working tree." >&2
358			exit $exit
359		}
360	fi
361	test -n "$GIT_DIR" && GIT_DIR=$(cd "$GIT_DIR" && pwd) || {
362		gettextln "Unable to determine absolute path of git directory" >&2
363		exit 1
364	}
365	: "${GIT_OBJECT_DIRECTORY="$(git rev-parse --git-path objects)"}"
366}
367
368if test -z "$NONGIT_OK"
369then
370	git_dir_init
371fi
372
373peel_committish () {
374	case "$1" in
375	:/*)
376		peeltmp=$(git rev-parse --verify "$1") &&
377		git rev-parse --verify "${peeltmp}^0"
378		;;
379	*)
380		git rev-parse --verify "${1}^0"
381		;;
382	esac
383}
384