+}
+
+init() {
+ hook pre-init
+ [ ! -e "$GIT_DIR" ] || fatal "'$GIT_DIR' exists" 10
+ mkdir -p "$VCSH_BASE" || fatal "could not create '$VCSH_BASE'" 50
+ cd "$VCSH_BASE" || fatal "could not enter '$VCSH_BASE'" 11
+ git init --shared=0600
+ upgrade
+ hook post-init
+}
+
+list() {
+ for repo in "$VCSH_REPO_D"/*.git; do
+ [ -d "$repo" ] && [ -r "$repo" ] && echo "$(basename "$repo" .git)"
+ done
+}
+
+get_files() {
+ GIT_DIR=$VCSH_REPO_D/$VCSH_REPO_NAME.git; export GIT_DIR
+ git ls-files
+}
+
+list_tracked() {
+ VCSH_REPO_NAME=$2; export VCSH_REPO_NAME
+ if [ -n "$VCSH_REPO_NAME" ]; then
+ get_files | list_tracked_helper
+ else
+ for VCSH_REPO_NAME in $(list); do
+ get_files
+ done | list_tracked_helper
+ fi
+}
+
+list_tracked_helper() {
+ sed "s,^,$(printf '%s\n' "$VCSH_BASE/" | sed 's/[,\&]/\\&/g')," | sort -u
+}
+
+list_tracked_by() {
+ list_tracked '' $2
+}
+
+list_untracked() {
+ command -v 'comm' >/dev/null 2>&1 || fatal "Could not find 'comm'"
+
+ temp_file_others=$(mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX") || fatal 'Could not create temp file'
+ temp_file_untracked=$(mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX") || fatal 'Could not create temp file'
+ temp_file_untracked_copy=$(mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX") || fatal 'Could not create temp file'
+
+ # Hack in support for `vcsh list-untracked -r`...
+ directory_opt="--directory"
+ shift 1
+ while getopts "r" flag; do
+ if [ x"$1" = x'-r' ]; then
+ unset directory_opt
+ fi
+ shift 1
+ done
+ # ...and parse for a potential parameter afterwards. As we shifted things out of $* in during getops, we need to look at $1
+ VCSH_REPO_NAME=$1; export VCSH_REPO_NAME
+
+ if [ -n "$VCSH_REPO_NAME" ]; then
+ list_untracked_helper $VCSH_REPO_NAME
+ else
+ for VCSH_REPO_NAME in $(list); do
+ list_untracked_helper $VCSH_REPO_NAME
+ done
+ fi
+ cat $temp_file_untracked
+
+ unset directory_opt directory_component
+ rm -f $temp_file_others $temp_file_untracked $temp_file_untracked_copy || fatal 'Could not delete temp files'
+}
+
+list_untracked_helper() {
+ export GIT_DIR="$VCSH_REPO_D/$VCSH_REPO_NAME.git"
+ git ls-files --others "$directory_opt" | (
+ while read line; do
+ echo "$line"
+ directory_component=${line%%/*}
+ [ -d "$directory_component" ] && printf '%s/\n' "$directory_component"
+ done
+ ) | sort -u > $temp_file_others
+ if [ -z "$ran_once" ]; then
+ ran_once=1
+ cp $temp_file_others $temp_file_untracked || fatal 'Could not copy temp file'
+ fi
+ cp $temp_file_untracked $temp_file_untracked_copy || fatal 'Could not copy temp file'
+ comm -12 $temp_file_others $temp_file_untracked_copy > $temp_file_untracked
+}
+
+pull() {
+ hook pre-pull
+ for VCSH_REPO_NAME in $(list); do
+ printf '%s: ' "$VCSH_REPO_NAME"
+ GIT_DIR=$VCSH_REPO_D/$VCSH_REPO_NAME.git; export GIT_DIR
+ use
+ git pull
+ VCSH_COMMAND_RETURN_CODE=$?
+ echo
+ done
+ hook post-pull
+}
+
+push() {
+ hook pre-push
+ for VCSH_REPO_NAME in $(list); do
+ printf '%s: ' "$VCSH_REPO_NAME"
+ GIT_DIR=$VCSH_REPO_D/$VCSH_REPO_NAME.git; export GIT_DIR
+ use
+ git push
+ VCSH_COMMAND_RETURN_CODE=$?
+ echo
+ done
+ hook post-push
+}
+
+retire() {
+ unset VCSH_DIRECTORY
+}
+
+rename() {
+ git_dir_exists
+ [ -d "$GIT_DIR_NEW" ] && fatal "'$GIT_DIR_NEW' exists" 54
+ mv -f "$GIT_DIR" "$GIT_DIR_NEW" || fatal "Could not mv '$GIT_DIR' '$GIT_DIR_NEW'" 52
+
+ # Now that the repository has been renamed, we need to fix up its configuration
+ # Overwrite old name..
+ GIT_DIR=$GIT_DIR_NEW
+ VCSH_REPO_NAME=$VCSH_REPO_NAME_NEW
+ # ..and clobber all old configuration
+ upgrade
+}
+
+run() {
+ hook pre-run
+ use
+ "$@"
+ VCSH_COMMAND_RETURN_CODE=$?
+ hook post-run
+}
+
+status() {
+ if [ -t 1 ]; then
+ COLORING="-c color.status=always"
+ fi
+ if [ -n "$VCSH_REPO_NAME" ]; then
+ status_helper $VCSH_REPO_NAME
+ else
+ for VCSH_REPO_NAME in $(list); do
+ STATUS=$(status_helper $VCSH_REPO_NAME "$COLORING")
+ [ -n "$STATUS" -o -z "$VCSH_STATUS_TERSE" ] && echo "$VCSH_REPO_NAME:"
+ [ -n "$STATUS" ] && echo "$STATUS"
+ [ -z "$VCSH_STATUS_TERSE" ] && echo
+ done
+ fi
+}
+
+status_helper() {
+ GIT_DIR=$VCSH_REPO_D/$1.git; export GIT_DIR
+ VCSH_GIT_OPTIONS=$2
+ use
+ remote_tracking_branch=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2> /dev/null) && {
+ commits_behind=$(git log ..${remote_tracking_branch} --oneline | wc -l)
+ commits_ahead=$(git log ${remote_tracking_branch}.. --oneline | wc -l)
+ [ ${commits_behind} -ne 0 ] && echo "Behind $remote_tracking_branch by $commits_behind commits"
+ [ ${commits_ahead} -ne 0 ] && echo "Ahead of $remote_tracking_branch by $commits_ahead commits"
+ }
+ git ${VCSH_GIT_OPTIONS} status --short --untracked-files='no'
+ VCSH_COMMAND_RETURN_CODE=$?
+}
+
+upgrade() {
+ hook pre-upgrade
+ # fake-bare repositories are not bare, actually. Set this to false
+ # because otherwise Git complains "fatal: core.bare and core.worktree
+ # do not make sense"
+ git config core.bare false
+ # core.worktree may be absolute or relative to $GIT_DIR, depending on
+ # user preference
+ if [ ! "x$VCSH_WORKTREE" = 'xabsolute' ]; then
+ git config core.worktree "$(cd "$GIT_DIR" && GIT_WORK_TREE=$VCSH_BASE git rev-parse --show-cdup)"
+ elif [ ! "x$VCSH_WORKTREE" = 'xrelative' ]; then
+ git config core.worktree "$VCSH_BASE"
+ fi
+ [ ! "x$VCSH_GITIGNORE" = 'xnone' ] && git config core.excludesfile ".gitignore.d/$VCSH_REPO_NAME"
+ [ ! "x$VCSH_GITATTRIBUTES" = 'xnone' ] && git config core.attributesfile ".gitattributes.d/$VCSH_REPO_NAME"
+ git config vcsh.vcsh 'true'
+ use
+ [ -e "$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME" ] && git add -f "$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME"
+ [ -e "$VCSH_BASE/.gitattributes.d/$VCSH_REPO_NAME" ] && git add -f "$VCSH_BASE/.gitattributes.d/$VCSH_REPO_NAME"
+ hook post-upgrade
+}
+
+use() {
+ git_dir_exists
+ VCSH_DIRECTORY=$VCSH_REPO_NAME; export VCSH_DIRECTORY
+}
+
+which() {
+ output=$(for VCSH_REPO_NAME in $(list); do
+ get_files | grep -- "$VCSH_COMMAND_PARAMETER" | sed "s/^/$VCSH_REPO_NAME: /"
+ done | sort -u)
+ if [ -z "$output" ]; then
+ fatal "'$VCSH_COMMAND_PARAMETER' does not exist" 1
+ else
+ echo "$output"
+ fi
+}
+
+write_gitignore() {
+ # Don't do anything if the user does not want to write gitignore
+ if [ "x$VCSH_GITIGNORE" = 'xnone' ]; then
+ info "Not writing gitignore as '\$VCSH_GITIGNORE' is set to 'none'"
+ exit
+ fi
+
+ use
+ cd "$VCSH_BASE" || fatal "could not enter '$VCSH_BASE'" 11
+ OLDIFS=$IFS
+ IFS=$(printf '\n\t')