[git] Break git sync into separate command

This commit is contained in:
Nemo 2019-08-13 18:01:11 +05:30
parent 1159aeb4fd
commit 09287e7612
1 changed files with 29 additions and 22 deletions

View File

@ -1,4 +1,5 @@
#!/bin/sh #!/bin/sh
set -eu
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then
echo "Please run with outliner [export|import] arguments" echo "Please run with outliner [export|import] arguments"
@ -6,50 +7,56 @@ if [ $# -eq 0 ]; then
fi fi
setup_git() { setup_git() {
chmod 0400 "$HOME/.ssh/id_rsa"
if [ -f "$HOME/.ssh/id_rsa" ]; then if [ -f "$HOME/.ssh/id_rsa" ]; then
chmod 600 "$HOME.ssh/id_rsa" chmod 0400 "$HOME/.ssh/id_rsa"
if [ ! -d "$HOME/.ssh/id_rsa.pub" ]; then if [ ! -d "$HOME/.ssh/id_rsa.pub" ]; then
ssh-keygen -y -f "$HOME/.ssh/id_rsa" > "$HOME/.ssh/id_rsa.pub" ssh-keygen -y -f "$HOME/.ssh/id_rsa" > "$HOME/.ssh/id_rsa.pub"
fi fi
echo "[+] Using SSH key for git pushes" echo "[+] Using SSH key for git pushes"
else else
echo "[E] Git credentials not available, quitting" echo "[E] Git credentials not available, quitting"
exit 0 exit 1
fi fi
eval $(ssh-agent)
ssh-add "$HOME/.ssh/id_rsa"
} }
update_git_repo() { update_git_config() {
BRANCH=${GIT_BRANCH:-master} EMAIL=${GIT_EMAIL:-outliner@example.invalid}
# Clone the branch in a temporary directory git config --global user.email "$EMAIL"
tmp_dir=$(mktemp -d) git config --global user.name "Outliner Backup"
git clone "$GIT_REMOTE_URL" "$tmp_dir" git remote add origin "$GIT_REMOTE_URL"
cp -r "$1/*" "$tmp_dir/"
cd "$tmp_dir" || exit 1
git add .
git commit -m "Updates: $(date)"
git push origin "$BRANCH"
} }
case $1 in case $1 in
export) export)
shift shift
bundle exec outliner-export "$@" bundle exec outliner-export "$@"
if [ -z "$GIT_REMOTE_URL" ]; then
echo "GIT_REMOTE_URL not set"
else
setup_git
update_git_repo "$2"
fi
;; ;;
import) import)
shift shift
bundle exec outliner-import "$@" bundle exec outliner-import "$@"
;; ;;
sync)
tmp_dir=$(mktemp -d)
if [ -z "$GIT_REMOTE_URL" ]; then
echo "[E] GIT_REMOTE_URL not set"
exit 1
else
setup_git
bundle exec outliner-export "$tmp_dir"
cd "$tmp_dir"
git init
update_git_config
git add .
git commit --message "Backup: $(date)"
BRANCH=${GIT_BRANCH:-master}
git checkout -b "$BRANCH"
git push origin --force "$BRANCH"
fi
;;
*) *)
echo "Invalid command, please check README" echo "Invalid command, please check README"
;; ;;