Blame git-sh-setup.sh

Packit 4511e4
# This shell scriplet is meant to be included by other shell scripts
Packit 4511e4
# to set up some variables pointing at the normal git directories and
Packit 4511e4
# a few helper shell functions.
Packit 4511e4
Packit 4511e4
# Having this variable in your environment would break scripts because
Packit 4511e4
# you would cause "cd" to be taken to unexpected places.  If you
Packit 4511e4
# like CDPATH, define it for your interactive shell sessions without
Packit 4511e4
# exporting it.
Packit 4511e4
# But we protect ourselves from such a user mistake nevertheless.
Packit 4511e4
unset CDPATH
Packit 4511e4
Packit 4511e4
# Similarly for IFS, but some shells (e.g. FreeBSD 7.2) are buggy and
Packit 4511e4
# do not equate an unset IFS with IFS with the default, so here is
Packit 4511e4
# an explicit SP HT LF.
Packit 4511e4
IFS=' 	
Packit 4511e4
'
Packit 4511e4
Packit 4511e4
git_broken_path_fix () {
Packit 4511e4
	case ":$PATH:" in
Packit 4511e4
	*:$1:*) : ok ;;
Packit 4511e4
	*)
Packit 4511e4
		PATH=$(
Packit 4511e4
			SANE_TOOL_PATH="$1"
Packit 4511e4
			IFS=: path= sep=
Packit 4511e4
			set x $PATH
Packit 4511e4
			shift
Packit 4511e4
			for elem
Packit 4511e4
			do
Packit 4511e4
				case "$SANE_TOOL_PATH:$elem" in
Packit 4511e4
				(?*:/bin | ?*:/usr/bin)
Packit 4511e4
					path="$path$sep$SANE_TOOL_PATH"
Packit 4511e4
					sep=:
Packit 4511e4
					SANE_TOOL_PATH=
Packit 4511e4
				esac
Packit 4511e4
				path="$path$sep$elem"
Packit 4511e4
				sep=:
Packit 4511e4
			done
Packit 4511e4
			echo "$path"
Packit 4511e4
		)
Packit 4511e4
		;;
Packit 4511e4
	esac
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
# @@BROKEN_PATH_FIX@@
Packit 4511e4
Packit 4511e4
# Source git-sh-i18n for gettext support.
Packit 4511e4
. "$(git --exec-path)/git-sh-i18n"
Packit 4511e4
Packit 4511e4
die () {
Packit 4511e4
	die_with_status 1 "$@"
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
die_with_status () {
Packit 4511e4
	status=$1
Packit 4511e4
	shift
Packit 4511e4
	printf >&2 '%s\n' "$*"
Packit 4511e4
	exit "$status"
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
GIT_QUIET=
Packit 4511e4
Packit 4511e4
say () {
Packit 4511e4
	if test -z "$GIT_QUIET"
Packit 4511e4
	then
Packit 4511e4
		printf '%s\n' "$*"
Packit 4511e4
	fi
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
if test -n "$OPTIONS_SPEC"; then
Packit 4511e4
	usage() {
Packit 4511e4
		"$0" -h
Packit 4511e4
		exit 1
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	parseopt_extra=
Packit 4511e4
	[ -n "$OPTIONS_KEEPDASHDASH" ] &&
Packit 4511e4
		parseopt_extra="--keep-dashdash"
Packit 4511e4
	[ -n "$OPTIONS_STUCKLONG" ] &&
Packit 4511e4
		parseopt_extra="$parseopt_extra --stuck-long"
Packit 4511e4
Packit 4511e4
	eval "$(
Packit 4511e4
		echo "$OPTIONS_SPEC" |
Packit 4511e4
			git rev-parse --parseopt $parseopt_extra -- "$@" ||
Packit 4511e4
		echo exit $?
Packit 4511e4
	)"
Packit 4511e4
else
Packit 4511e4
	dashless=$(basename -- "$0" | sed -e 's/-/ /')
Packit 4511e4
	usage() {
Packit 4511e4
		die "$(eval_gettext "usage: \$dashless \$USAGE")"
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	if [ -z "$LONG_USAGE" ]
Packit 4511e4
	then
Packit 4511e4
		LONG_USAGE="$(eval_gettext "usage: \$dashless \$USAGE")"
Packit 4511e4
	else
Packit 4511e4
		LONG_USAGE="$(eval_gettext "usage: \$dashless \$USAGE
Packit 4511e4
Packit 4511e4
$LONG_USAGE")"
Packit 4511e4
	fi
Packit 4511e4
Packit 4511e4
	case "$1" in
Packit 4511e4
		-h)
Packit 4511e4
		echo "$LONG_USAGE"
Packit 4511e4
		exit
Packit 4511e4
	esac
Packit 4511e4
fi
Packit 4511e4
Packit 4511e4
# Set the name of the end-user facing command in the reflog when the
Packit 4511e4
# script may update refs.  When GIT_REFLOG_ACTION is already set, this
Packit 4511e4
# will not overwrite it, so that a scripted Porcelain (e.g. "git
Packit 4511e4
# rebase") can set it to its own name (e.g. "rebase") and then call
Packit 4511e4
# another scripted Porcelain (e.g. "git am") and a call to this
Packit 4511e4
# function in the latter will keep the name of the end-user facing
Packit 4511e4
# program (e.g. "rebase") in GIT_REFLOG_ACTION, ensuring whatever it
Packit 4511e4
# does will be record as actions done as part of the end-user facing
Packit 4511e4
# operation (e.g. "rebase").
Packit 4511e4
#
Packit 4511e4
# NOTE NOTE NOTE: consequently, after assigning a specific message to
Packit 4511e4
# GIT_REFLOG_ACTION when calling a "git" command to record a custom
Packit 4511e4
# reflog message, do not leave that custom value in GIT_REFLOG_ACTION,
Packit 4511e4
# after you are done.  Other callers of "git" commands that rely on
Packit 4511e4
# writing the default "program name" in reflog expect the variable to
Packit 4511e4
# contain the value set by this function.
Packit 4511e4
#
Packit 4511e4
# To use a custom reflog message, do either one of these three:
Packit 4511e4
#
Packit 4511e4
# (a) use a single-shot export form:
Packit 4511e4
#     GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: preparing frotz" \
Packit 4511e4
#         git command-that-updates-a-ref
Packit 4511e4
#
Packit 4511e4
# (b) save the original away and restore:
Packit 4511e4
#     SAVED_ACTION=$GIT_REFLOG_ACTION
Packit 4511e4
#     GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: preparing frotz"
Packit 4511e4
#     git command-that-updates-a-ref
Packit 4511e4
#     GIT_REFLOG_ACITON=$SAVED_ACTION
Packit 4511e4
#
Packit 4511e4
# (c) assign the variable in a subshell:
Packit 4511e4
#     (
Packit 4511e4
#         GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: preparing frotz"
Packit 4511e4
#         git command-that-updates-a-ref
Packit 4511e4
#     )
Packit 4511e4
set_reflog_action() {
Packit 4511e4
	if [ -z "${GIT_REFLOG_ACTION:+set}" ]
Packit 4511e4
	then
Packit 4511e4
		GIT_REFLOG_ACTION="$*"
Packit 4511e4
		export GIT_REFLOG_ACTION
Packit 4511e4
	fi
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
git_editor() {
Packit 4511e4
	if test -z "${GIT_EDITOR:+set}"
Packit 4511e4
	then
Packit 4511e4
		GIT_EDITOR="$(git var GIT_EDITOR)" || return $?
Packit 4511e4
	fi
Packit 4511e4
Packit 4511e4
	eval "$GIT_EDITOR" '"$@"'
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
git_pager() {
Packit 4511e4
	if test -t 1
Packit 4511e4
	then
Packit 4511e4
		GIT_PAGER=$(git var GIT_PAGER)
Packit 4511e4
	else
Packit 4511e4
		GIT_PAGER=cat
Packit 4511e4
	fi
Packit 4511e4
	for vardef in @@PAGER_ENV@@
Packit 4511e4
	do
Packit 4511e4
		var=${vardef%%=*}
Packit 4511e4
		eval ": \"\${$vardef}\" && export $var"
Packit 4511e4
	done
Packit 4511e4
Packit 4511e4
	eval "$GIT_PAGER" '"$@"'
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
sane_grep () {
Packit 4511e4
	GREP_OPTIONS= LC_ALL=C grep @@SANE_TEXT_GREP@@ "$@"
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
sane_egrep () {
Packit 4511e4
	GREP_OPTIONS= LC_ALL=C egrep @@SANE_TEXT_GREP@@ "$@"
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
is_bare_repository () {
Packit 4511e4
	git rev-parse --is-bare-repository
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
cd_to_toplevel () {
Packit 4511e4
	cdup=$(git rev-parse --show-toplevel) &&
Packit 4511e4
	cd "$cdup" || {
Packit 4511e4
		gettextln "Cannot chdir to \$cdup, the toplevel of the working tree" >&2
Packit 4511e4
		exit 1
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
require_work_tree_exists () {
Packit 4511e4
	if test "z$(git rev-parse --is-bare-repository)" != zfalse
Packit 4511e4
	then
Packit 4511e4
		program_name=$0
Packit 4511e4
		die "$(eval_gettext "fatal: \$program_name cannot be used without a working tree.")"
Packit 4511e4
	fi
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
require_work_tree () {
Packit 4511e4
	test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = true || {
Packit 4511e4
		program_name=$0
Packit 4511e4
		die "$(eval_gettext "fatal: \$program_name cannot be used without a working tree.")"
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
require_clean_work_tree () {
Packit 4511e4
	git rev-parse --verify HEAD >/dev/null || exit 1
Packit 4511e4
	git update-index -q --ignore-submodules --refresh
Packit 4511e4
	err=0
Packit 4511e4
Packit 4511e4
	if ! git diff-files --quiet --ignore-submodules
Packit 4511e4
	then
Packit 4511e4
		action=$1
Packit 4511e4
		case "$action" in
Packit 4511e4
		rebase)
Packit 4511e4
			gettextln "Cannot rebase: You have unstaged changes." >&2
Packit 4511e4
			;;
Packit 4511e4
		"rewrite branches")
Packit 4511e4
			gettextln "Cannot rewrite branches: You have unstaged changes." >&2
Packit 4511e4
			;;
Packit 4511e4
		"pull with rebase")
Packit 4511e4
			gettextln "Cannot pull with rebase: You have unstaged changes." >&2
Packit 4511e4
			;;
Packit 4511e4
		*)
Packit 4511e4
			eval_gettextln "Cannot \$action: You have unstaged changes." >&2
Packit 4511e4
			;;
Packit 4511e4
		esac
Packit 4511e4
		err=1
Packit 4511e4
	fi
Packit 4511e4
Packit 4511e4
	if ! git diff-index --cached --quiet --ignore-submodules HEAD --
Packit 4511e4
	then
Packit 4511e4
		if test $err = 0
Packit 4511e4
		then
Packit 4511e4
			action=$1
Packit 4511e4
			case "$action" in
Packit 4511e4
			rebase)
Packit 4511e4
				gettextln "Cannot rebase: Your index contains uncommitted changes." >&2
Packit 4511e4
				;;
Packit 4511e4
			"pull with rebase")
Packit 4511e4
				gettextln "Cannot pull with rebase: Your index contains uncommitted changes." >&2
Packit 4511e4
				;;
Packit 4511e4
			*)
Packit 4511e4
				eval_gettextln "Cannot \$action: Your index contains uncommitted changes." >&2
Packit 4511e4
				;;
Packit 4511e4
			esac
Packit 4511e4
		else
Packit 4511e4
		    gettextln "Additionally, your index contains uncommitted changes." >&2
Packit 4511e4
		fi
Packit 4511e4
		err=1
Packit 4511e4
	fi
Packit 4511e4
Packit 4511e4
	if test $err = 1
Packit 4511e4
	then
Packit 4511e4
		test -n "$2" && echo "$2" >&2
Packit 4511e4
		exit 1
Packit 4511e4
	fi
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
# Generate a sed script to parse identities from a commit.
Packit 4511e4
#
Packit 4511e4
# Reads the commit from stdin, which should be in raw format (e.g., from
Packit 4511e4
# cat-file or "--pretty=raw").
Packit 4511e4
#
Packit 4511e4
# The first argument specifies the ident line to parse (e.g., "author"), and
Packit 4511e4
# the second specifies the environment variable to put it in (e.g., "AUTHOR"
Packit 4511e4
# for "GIT_AUTHOR_*"). Multiple pairs can be given to parse author and
Packit 4511e4
# committer.
Packit 4511e4
pick_ident_script () {
Packit 4511e4
	while test $# -gt 0
Packit 4511e4
	do
Packit 4511e4
		lid=$1; shift
Packit 4511e4
		uid=$1; shift
Packit 4511e4
		printf '%s' "
Packit 4511e4
		/^$lid /{
Packit 4511e4
			s/'/'\\\\''/g
Packit 4511e4
			h
Packit 4511e4
			s/^$lid "'\([^<]*\) <[^>]*> .*$/\1/'"
Packit 4511e4
			s/.*/GIT_${uid}_NAME='&'/p
Packit 4511e4
Packit 4511e4
			g
Packit 4511e4
			s/^$lid "'[^<]* <\([^>]*\)> .*$/\1/'"
Packit 4511e4
			s/.*/GIT_${uid}_EMAIL='&'/p
Packit 4511e4
Packit 4511e4
			g
Packit 4511e4
			s/^$lid "'[^<]* <[^>]*> \(.*\)$/@\1/'"
Packit 4511e4
			s/.*/GIT_${uid}_DATE='&'/p
Packit 4511e4
		}
Packit 4511e4
		"
Packit 4511e4
	done
Packit 4511e4
	echo '/^$/q'
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
# Create a pick-script as above and feed it to sed. Stdout is suitable for
Packit 4511e4
# feeding to eval.
Packit 4511e4
parse_ident_from_commit () {
Packit 4511e4
	LANG=C LC_ALL=C sed -ne "$(pick_ident_script "$@")"
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
# Parse the author from a commit given as an argument. Stdout is suitable for
Packit 4511e4
# feeding to eval to set the usual GIT_* ident variables.
Packit 4511e4
get_author_ident_from_commit () {
Packit 4511e4
	encoding=$(git config i18n.commitencoding || echo UTF-8)
Packit 4511e4
	git show -s --pretty=raw --encoding="$encoding" "$1" -- |
Packit 4511e4
	parse_ident_from_commit author AUTHOR
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
# Clear repo-local GIT_* environment variables. Useful when switching to
Packit 4511e4
# another repository (e.g. when entering a submodule). See also the env
Packit 4511e4
# list in git_connect()
Packit 4511e4
clear_local_git_env() {
Packit 4511e4
	unset $(git rev-parse --local-env-vars)
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
# Generate a virtual base file for a two-file merge. Uses git apply to
Packit 4511e4
# remove lines from $1 that are not in $2, leaving only common lines.
Packit 4511e4
create_virtual_base() {
Packit 4511e4
	sz0=$(wc -c <"$1")
Packit 4511e4
	@@DIFF@@ -u -La/"$1" -Lb/"$1" "$1" "$2" | git apply --no-add
Packit 4511e4
	sz1=$(wc -c <"$1")
Packit 4511e4
Packit 4511e4
	# If we do not have enough common material, it is not
Packit 4511e4
	# worth trying two-file merge using common subsections.
Packit 4511e4
	expr $sz0 \< $sz1 \* 2 >/dev/null || : >"$1"
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
Packit 4511e4
# Platform specific tweaks to work around some commands
Packit 4511e4
case $(uname -s) in
Packit 4511e4
*MINGW*)
Packit 4511e4
	# Windows has its own (incompatible) sort and find
Packit 4511e4
	sort () {
Packit 4511e4
		/usr/bin/sort "$@"
Packit 4511e4
	}
Packit 4511e4
	find () {
Packit 4511e4
		/usr/bin/find "$@"
Packit 4511e4
	}
Packit 4511e4
	# git sees Windows-style pwd
Packit 4511e4
	pwd () {
Packit 4511e4
		builtin pwd -W
Packit 4511e4
	}
Packit 4511e4
	is_absolute_path () {
Packit 4511e4
		case "$1" in
Packit 4511e4
		[/\\]* | [A-Za-z]:*)
Packit 4511e4
			return 0 ;;
Packit 4511e4
		esac
Packit 4511e4
		return 1
Packit 4511e4
	}
Packit 4511e4
	;;
Packit 4511e4
*)
Packit 4511e4
	is_absolute_path () {
Packit 4511e4
		case "$1" in
Packit 4511e4
		/*)
Packit 4511e4
			return 0 ;;
Packit 4511e4
		esac
Packit 4511e4
		return 1
Packit 4511e4
	}
Packit 4511e4
esac
Packit 4511e4
Packit 4511e4
# Make sure we are in a valid repository of a vintage we understand,
Packit 4511e4
# if we require to be in a git repository.
Packit 4511e4
git_dir_init () {
Packit 4511e4
	GIT_DIR=$(git rev-parse --git-dir) || exit
Packit 4511e4
	if [ -z "$SUBDIRECTORY_OK" ]
Packit 4511e4
	then
Packit 4511e4
		test -z "$(git rev-parse --show-cdup)" || {
Packit 4511e4
			exit=$?
Packit 4511e4
			gettextln "You need to run this command from the toplevel of the working tree." >&2
Packit 4511e4
			exit $exit
Packit 4511e4
		}
Packit 4511e4
	fi
Packit 4511e4
	test -n "$GIT_DIR" && GIT_DIR=$(cd "$GIT_DIR" && pwd) || {
Packit 4511e4
		gettextln "Unable to determine absolute path of git directory" >&2
Packit 4511e4
		exit 1
Packit 4511e4
	}
Packit 4511e4
	: "${GIT_OBJECT_DIRECTORY="$(git rev-parse --git-path objects)"}"
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
if test -z "$NONGIT_OK"
Packit 4511e4
then
Packit 4511e4
	git_dir_init
Packit 4511e4
fi
Packit 4511e4
Packit 4511e4
peel_committish () {
Packit 4511e4
	case "$1" in
Packit 4511e4
	:/*)
Packit 4511e4
		peeltmp=$(git rev-parse --verify "$1") &&
Packit 4511e4
		git rev-parse --verify "${peeltmp}^0"
Packit 4511e4
		;;
Packit 4511e4
	*)
Packit 4511e4
		git rev-parse --verify "${1}^0"
Packit 4511e4
		;;
Packit 4511e4
	esac
Packit 4511e4
}