Skip to content

Instantly share code, notes, and snippets.

@avilaHugo
Created December 22, 2021 00:50
Show Gist options
  • Select an option

  • Save avilaHugo/5c22035ba6ed44374fcb8347e60a2ab1 to your computer and use it in GitHub Desktop.

Select an option

Save avilaHugo/5c22035ba6ed44374fcb8347e60a2ab1 to your computer and use it in GitHub Desktop.
Helper script to push and pull my games save states
#!/usr/bin/env bash
set -eo pipefail
# echo errors massages and exit.
function error (){
echo "ERROR: ${*}"
exit 1
}
# echo logs
function log (){
echo "LOG: ${*}"
}
# return "true" if the repos has changes
# and "false" if not.
function check_changes (){
git --git-dir="${GIT_DIR}" --work-tree="${GIT_WORK_TREE}" diff --quiet --exit-code && echo "false" || echo "true"
}
# Run git in the desired worktree
function run_git (){
local git_cmd branch
git_cmd="${1}"
branch="${2}"
git --git-dir="${GIT_DIR}" --work-tree="${GIT_WORK_TREE}" \
"${git_cmd}" origin "$branch"
}
# Check if the repo has new changes and commit then.
function commit_save_files (){
local files_to_commit
files_to_commit=$(git \
--git-dir="${GIT_DIR}" \
--work-tree="${GIT_WORK_TREE}" \
status --porcelain=v1 | grep -Po 'states.+' | xargs echo)
git --git-dir="${GIT_DIR}" --work-tree="${GIT_WORK_TREE}" \
add "${files_to_commit[*]}"
git --git-dir="${GIT_DIR}" --work-tree="${GIT_WORK_TREE}" \
commit -m "savestate: ${files_to_commit[*]//states\//}"
}
# Print help information.
function help_info (){
cat << EOF
Sync savestates with github (huguito - 2021)
Usage:
./sync_saves.sh ( push | pull ) GIT_LOCAL_REPO
Options
push Push will commit changes made locally (if any)
and commit then to the git remote repo.
pull Pull will donwload updates changes locally from git.
EOF
exit 1
}
function main (){
[ -z "${*:1}" ] && help_info
local git_command repo_location branch
git_command="${1}"
repo_location="$(readlink -f ${2})"
branch="${3:-master}"
if [ -d "${repo_location}" ];then
local GIT_DIR GIT_WORK_TREE
GIT_DIR="$( echo "${repo_location}" | sed 's/\/$//' )/.git"
GIT_WORK_TREE="${repo_location}"
else
error "repo do not exists !!!"
fi
case "${git_command}" in
"push")
"$(check_changes)" && commit_save_files || {
echo "LOG: No files to be commited."
}
run_git "${git_command}" "$branch"
;;
"pull")
run_git "${git_command}" "$branch"
;;
*)
help_info
;;
esac
}
ARGS=("$@")
main "${ARGS[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment