Last active
January 15, 2024 19:26
-
-
Save ViBiOh/d72d01a82c604a37afb7a2165a72ffba to your computer and use it in GitHub Desktop.
Git sync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| function head_sync() { | |
| if [ "${#}" -ne 2 ]; then | |
| echo Usage: head_sync [REMOTE_SERVER] [REMOTE_PATH_FROM_HOME] | |
| return 1 | |
| fi | |
| local REMOTE_PATH_PREFIX="~/${2}" | |
| if [ "${2:0:1}" == "/" ]; then | |
| REMOTE_PATH_PREFIX="${2}" | |
| fi | |
| local branch=`git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||'` | |
| ssh "${1}" "PS1=$ source ~/.bashrc && cd ${REMOTE_PATH_PREFIX} && git reset HEAD . && git clean -f && git checkout -- . && git fetch && git checkout ${branch} && git pull" | |
| } | |
| function git_sync() { | |
| if [ "${#}" -lt 2 ]; then | |
| echo Usage: git_sync [REMOTE_SERVER] [REMOTE_PATH_FROM_HOME] [DRY]? | |
| return 1 | |
| fi | |
| local RED='\033[0;31m' | |
| local GREEN='\033[0;32m' | |
| local YELLOW='\033[0;33m' | |
| local BLUE='\033[0;34m' | |
| local RESET='\033[0m' | |
| if ! `git rev-parse --is-inside-work-tree`; then | |
| echo -e "${RED}You're not inside a git folder${RESET}" | |
| return 1 | |
| else | |
| cd `git rev-parse --show-cdup` | |
| fi | |
| local REMOTE_PATH_PREFIX="~/${2}" | |
| if [ "${2:0:1}" == "/" ]; then | |
| REMOTE_PATH_PREFIX="${2}" | |
| fi | |
| local dry=false | |
| if [ "${3,,}" == "dry" ]; then | |
| dry=true | |
| fi | |
| if ${dry}; then | |
| echo -e "${BLUE}Dry run of syncing files...${RESET}\n" | |
| else | |
| echo -e "${BLUE}Syncing files...${RESET}\n" | |
| fi | |
| local toSync=() | |
| local toDelete=() | |
| local IFS=$'\n' | |
| for gitFile in `git status --porcelain`; do | |
| local prefix="${gitFile:0:2}" | |
| local trimmedPrefix="${prefix#[[:space:]]}" | |
| case "${trimmedPrefix:0:1}" in | |
| "M" | "A" | "?") | |
| toSync=("${toSync[@]}" "${gitFile:3}") | |
| ;; | |
| "D") | |
| toDelete=("${toDelete[@]}" "${REMOTE_PATH_PREFIX}/${gitFile:3}") | |
| ;; | |
| "R") | |
| local originFile=`echo -n "${gitFile}" | awk '{print $2}'` | |
| local destinationFile=`echo -n "${gitFile}" | awk '{print $4}'` | |
| toDelete=("${toDelete[@]}" "${REMOTE_PATH_PREFIX}/${originFile}") | |
| toSync=("${toSync[@]}" "${destinationFile}") | |
| ;; | |
| *) | |
| echo -e "${BLUE} ¯\_(ツ)_/¯ Don't know how to handle ${gitFile}${RESET}" | |
| esac | |
| done | |
| if ! ${dry}; then | |
| echo -e "${YELLOW}Cleaning remote${RESET}\n" | |
| ssh "${1}" "cd ${REMOTE_PATH_PREFIX} && git clean -f && git checkout -- ." | |
| fi | |
| if [ "${#toDelete[@]}" -ne 0 ]; then | |
| ! ${dry} && ssh "${1}" "rm -rf ${toDelete[@]}" | |
| echo -e "${RED} - Deleted\n${toDelete[*]}${RESET}\n" | |
| fi | |
| if [ "${#toSync[@]}" -ne 0 ]; then | |
| ! ${dry} && rsync -raR "${toSync[@]}" "${1}:${REMOTE_PATH_PREFIX}/" | |
| echo -e "${GREEN} + Copied\n${toSync[*]}${RESET}\n" | |
| fi | |
| echo -e "${BLUE}Done!${RESET}" | |
| echo | |
| } | |
| function watch_sync() { | |
| git_sync "${@}" | |
| fswatch -0 -o --exclude=.git/ . | while read -d "" event | |
| do | |
| git_sync "${@}" | |
| done | |
| } |
Author
@kuvaldini Thank you, I just updated it
thank you for this script, it helped me a lot!
How to use this script?
This is awesome, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Look much less verbose