<commit_before>#!/usr/bin/env bash

EXCLUSION_LIST=~/dotfiles/terminal/bash/functions/deploy-exclusions.txt

source ~/dotfiles/terminal/bash/functions/helper_functions.sh

# Retrieve the local PWD, building it if necessary
__get_local_pwd() {
	local temp_dir="$1"
	if [ -f ./_config.yml ]; then
		# If local PWD is a Jekyll project, use site built by Jekyll
		local local_pwd="$temp_dir"/_site
		JEKYLL_ENV=production \
			bundle exec \
			jekyll build --destination "$local_pwd" --quiet
		echo "$local_pwd"
	elif git rev-parse --git-dir &> /dev/null; then
		# If local PWD is a Git directory, use archive created by Git
		local local_pwd="$temp_<PASSWORD>"/"$(basename "$PWD")"
		local local_pwd_archive="$local_pwd".zip
		# A commit containing the current state of the repository, including
		# uncommitted working directory changes
		local stash_id="$(git stash create)"
		git archive "${stash_id:-HEAD}" --output="$local_pwd_archive" -0 .
		mkdir "$local_pwd"
		unzip -q "$local_pwd_archive" -d "$local_pwd"
		echo "$local_pwd"
	else
		echo "$PWD"
	fi
}

# Upload the contents of the given local PWD to the given remote PWD
__upload() {
	local local_pwd="$1"
	local remote_pwd="$2"
	if [ -z "$SSH_USER" ]; then
		>&2 echo "$(__get_script_name): environment has no SSH user set"
		return 1
	elif [ -z "$SSH_HOSTNAME" ]; then
		>&2 echo "$(__get_script_name): environment has no SSH hostname set"
		return 1
	elif [ -z "$SSH_PORT" ]; then
		>&2 echo "$(__get_script_name): environment has no SSH port set"
		return 1
	else
		pushd "$local_pwd" > /dev/null
		rsync \
			--archive \
			--checksum \
			--compress \
			--exclude-from "$EXCLUSION_LIST" \
			--human-readable \
			--rsh "ssh -p $SSH_PORT" \
			--verbose \
			"$local_pwd"/ \
			"$SSH_USER"@"$SSH_HOSTNAME":"$remote_pwd"
		popd > /dev/null
	fi
}

deploy() {
	if __source_env; then
		local remote_pwd="$(__get_remote_pwd)"
	fi
	if [ -n "$remote_pwd" ]; then
		# Create a temporary directory in case it's needed (and create it
		# here so we can delete it after everything is done)
		local temp_dir="$(mktemp -d)"
		local local_pwd="$(__get_local_pwd "$temp_dir")"
		__upload "$local_pwd" "$remote_pwd"
		rm -rf "$temp_dir"
	else
		return 1
	fi
}<commit_msg>Swtich from temp dir to cache dir for deploy location<commit_after>#!/usr/bin/env bash

# The list of file/directory patterns to exclude from deployment
DEPLOY_EXCLUSION_LIST=~/dotfiles/terminal/bash/functions/deploy-exclusions.txt
# The cache location for deployed directories
DEPLOY_CACHE_DIR=~/.deploy-cache

source ~/dotfiles/terminal/bash/functions/helper_functions.sh

# Get the ID for the current directory for identification in the deploy cache
__get_cache_id() {
	echo "$PWD" | openssl sha1
}

# Retrieve the local PWD, building it if necessary
__get_local_pwd() {
	if [ -f ./_config.yml ]; then
		# If local PWD is a Jekyll project, use site built by Jekyll
		local local_pwd="$DEPLOY_CACHE_DIR"/"$(__get_cache_id)"
		JEKYLL_ENV=production \
			bundle exec \
			jekyll build --destination "$local_pwd" --quiet
		echo "$local_pwd"
	elif git rev-parse --git-dir &> /dev/null; then
		# If local PWD is a Git directory, use archive created by Git
		local local_pwd="$DEPLOY_CACHE_DIR"/"$(__get_cache_id)"
		local local_pwd_archive="$local_pwd".zip
		# A commit containing the current state of the repository, including
		# uncommitted working directory changes
		local stash_id="$(git stash create)"
		git archive "${stash_id:-HEAD}" --output="$local_pwd_archive" -0 .
		mkdir "$local_pwd"
		unzip -q "$local_pwd_archive" -d "$local_pwd"
		echo "$local_pwd"
	else
		echo "$PWD"
	fi
}

# Upload the contents of the given local PWD to the given remote PWD
__upload() {
	local local_pwd="$1"
	local remote_pwd="$2"
	if [ -z "$SSH_USER" ]; then
		>&2 echo "$(__get_script_name): environment has no SSH user set"
		return 1
	elif [ -z "$SSH_HOSTNAME" ]; then
		>&2 echo "$(__get_script_name): environment has no SSH hostname set"
		return 1
	elif [ -z "$SSH_PORT" ]; then
		>&2 echo "$(__get_script_name): environment has no SSH port set"
		return 1
	else
		pushd "$local_pwd" > /dev/null
		rsync \
			--archive \
			--checksum \
			--compress \
			--exclude-from "$DEPLOY_EXCLUSION_LIST" \
			--human-readable \
			--rsh "ssh -p $SSH_PORT" \
			--verbose \
			"$local_pwd"/ \
			"$SSH_USER"@"$SSH_HOSTNAME":"$remote_pwd"
		popd > /dev/null
	fi
}

deploy() {
	if __source_env; then
		local remote_pwd="$(__get_remote_pwd)"
	fi
	if [ -n "$remote_pwd" ]; then
		mkdir -p "$DEPLOY_CACHE_DIR"
		local local_pwd="$(__get_local_pwd)"
		__upload "$local_pwd" "$remote_pwd"
	else
		return 1
	fi
}
{'fraction_non_alphanumeric': 0.12803819444444445, 'fraction_numerical': 0.004774305555555556, 'mean_word_length': 3.801041666666667, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '107303', 'n_tokens_mistral': 1844, 'n_tokens_neox': 1661, 'n_words': 485}