Skip to content

Instantly share code, notes, and snippets.

@ViBiOh
Last active January 15, 2024 19:26
Show Gist options
  • Select an option

  • Save ViBiOh/d72d01a82c604a37afb7a2165a72ffba to your computer and use it in GitHub Desktop.

Select an option

Save ViBiOh/d72d01a82c604a37afb7a2165a72ffba to your computer and use it in GitHub Desktop.

Revisions

  1. ViBiOh revised this gist Jun 17, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git.sh
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ head_sync() {

    local branch
    branch="$(git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||')"
    ssh -A "${1}" "cd ${2} && git reset HEAD . && git clean -f && git status -u -s | awk '{print $2}' | xargs rm -rf && git checkout -- . && git fetch && git checkout ${branch} && git pull"
    ssh -A "${1}" "cd '${2}' && git reset HEAD . && git clean -f && git status -u -s | awk '{print \$2}' | xargs rm -rf && git checkout -- . && git fetch && git checkout '${branch}' && git pull"
    }

    git_sync() {
  2. ViBiOh revised this gist Apr 28, 2020. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions git.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    #!/usr/bin/env bash

    git_is_inside() {
    git rev-parse --is-inside-work-tree 2>&1
    }

    git_root() {
    if [[ $(git_is_inside) != "true" ]]; then
    pwd
  3. ViBiOh revised this gist Apr 20, 2020. 1 changed file with 38 additions and 42 deletions.
    80 changes: 38 additions & 42 deletions git.sh
    Original file line number Diff line number Diff line change
    @@ -1,60 +1,55 @@
    #!/usr/bin/env bash

    git_root() {
    git rev-parse --is-inside-work-tree > /dev/null 2>&1 && cd $(git rev-parse --show-toplevel)
    if [[ $(git_is_inside) != "true" ]]; then
    pwd
    return
    fi

    git rev-parse --show-toplevel
    }

    head_sync() {
    if [[ "${#}" -ne 2 ]]; then
    echo "Usage: head_sync [REMOTE_SERVER] [REMOTE_PATH]"
    if [[ ${#} -ne 2 ]]; then
    printf "%bUsage: head_sync [REMOTE_SERVER] [REMOTE_PATH]%b\n" "${RED}" "${RESET}"
    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}" "cd ${REMOTE_PATH_PREFIX} && git reset HEAD . && git clean -f && git checkout -- . && git fetch && git checkout ${branch} && git pull"
    local branch
    branch="$(git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||')"
    ssh -A "${1}" "cd ${2} && git reset HEAD . && git clean -f && git status -u -s | awk '{print $2}' | xargs rm -rf && git checkout -- . && git fetch && git checkout ${branch} && git pull"
    }

    git_sync() {
    if [[ "${#}" -lt 2 ]]; then
    echo "Usage: git_sync [REMOTE_SERVER] [REMOTE_PATH] [DRY]?"
    if [[ ${#} -lt 2 ]]; then
    printf "%bUsage: git_sync [REMOTE_SERVER] [REMOTE_PATH] [DRY]?%b\n" "${RED}" "${RESET}"
    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'

    git_root
    isGit=$?
    if [[ ${isGit} -ne 0 ]]; then
    return ${isGit}
    fi

    local REMOTE_PATH_PREFIX="~/${2}"
    if [[ "${2:0:1}" == "/" ]]; then
    if [[ ${2:0:1} == "/" ]]; then
    REMOTE_PATH_PREFIX="${2}"
    fi

    local dry=false
    if [[ "${3,,}" == "dry" ]]; then
    if [[ $(echo "${3}" | tr "[:upper:]" "[:lower:]") == "dry" ]]; then
    dry=true
    fi

    if [[ "${dry}" == true ]]; then
    echo -e "${BLUE}Dry run of syncing files...${RESET}\n"
    if [[ ${dry} == true ]]; then
    printf "%bDry run of syncing files...%b\n" "${BLUE}" "${RESET}"
    else
    echo -e "${BLUE}Syncing files...${RESET}\n"
    printf "%bSyncing files at %s...%b\n" "${BLUE}" "$(date +'%H:%M:%S')" "${RESET}"
    fi

    local toSync=()
    local toDelete=()
    declare -a toSync
    declare -a toDelete

    local IFS=$'\n'
    for gitFile in $(git status --porcelain); do
    @@ -63,53 +58,54 @@ git_sync() {

    case "${trimmedPrefix:0:1}" in
    "M" | "A" | "?")
    toSync=("${toSync[@]}" "${gitFile:3}")
    toSync+=("${gitFile:3}")
    ;;

    "D")
    toDelete=("${toDelete[@]}" "${REMOTE_PATH_PREFIX}/${gitFile:3}")
    toDelete+=("${REMOTE_PATH_PREFIX}/${gitFile:3}")
    ;;

    "R")
    local originFile=$(echo -n "${gitFile}" | awk '{print $2}')
    local destinationFile=$(echo -n "${gitFile}" | awk '{print $4}')
    local originFile
    originFile="$(echo "${gitFile}" | awk '{print $2}')"
    local destinationFile
    destinationFile="$(echo "${gitFile}" | awk '{print $4}')"

    toDelete=("${toDelete[@]}" "${REMOTE_PATH_PREFIX}/${originFile}")
    toSync=("${toSync[@]}" "${destinationFile}")
    toDelete+=("${REMOTE_PATH_PREFIX}/${originFile}")
    toSync+=("${destinationFile}")
    ;;

    *)
    echo -e "${BLUE} ¯\_(ツ)_/¯ Don't know how to handle ${gitFile}${RESET}"
    printf "%b¯\_(ツ)_/¯ Don't know how to handle ${gitFile}%b\n" "${BLUE}" "${RESET}"
    esac
    done

    if ! ${dry}; then
    echo -e "${YELLOW}Cleaning remote${RESET}\n"
    printf "%bCleaning remote%b\n" "${YELLOW}" "${RESET}"
    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"
    if [[ ${#toDelete[@]} -ne 0 ]]; then
    ! ${dry} && ssh "${1}" "rm -rf ${toDelete[*]}"
    printf "%b- Deleted\n%s%b\n" "${RED}" "${toDelete[*]}" "${RESET}"
    fi

    if [[ "${#toSync[@]}" -ne 0 ]]; then
    if [[ ${#toSync[@]} -ne 0 ]]; then
    ! ${dry} && rsync -raR "${toSync[@]}" "${1}:${REMOTE_PATH_PREFIX}/"
    echo -e "${GREEN} + Copied\n${toSync[*]}${RESET}\n"
    printf "%b+ Copied\n%s%b\n" "${GREEN}" "${toSync[*]}" "${RESET}"
    fi

    echo -e "${BLUE}Done!${RESET}"
    echo
    printf "%bDone at %s!%b\n\n" "${BLUE}" "$(date +'%H:%M:%S')" "${RESET}"
    }

    watch_sync() {
    if [[ -z "${NO_HEAD_SYNC:-}" ]]; then
    if [[ -z ${NO_HEAD_SYNC:-} ]]; then
    head_sync "${@}"
    fi

    git_sync "${@}"

    fswatch -0 -o --exclude=.git/ . | while read -d "" event
    fswatch -0 -o --exclude=.git/ . | while read -r -d ""
    do
    git_sync "${@}"
    done
  4. ViBiOh revised this gist Mar 1, 2019. 1 changed file with 1 addition and 9 deletions.
    10 changes: 1 addition & 9 deletions git.sh
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,7 @@
    #!/usr/bin/env bash

    git_root() {
    if ! $(git rev-parse --is-inside-work-tree 2>/dev/null); then
    return 1
    else
    local gitRootDir=$(git rev-parse --show-cdup)

    if [[ -n "${gitRootDir}" ]]; then
    cd "${gitRootDir}"
    fi
    fi
    git rev-parse --is-inside-work-tree > /dev/null 2>&1 && cd $(git rev-parse --show-toplevel)
    }

    head_sync() {
  5. ViBiOh revised this gist Feb 22, 2019. 1 changed file with 17 additions and 13 deletions.
    30 changes: 17 additions & 13 deletions git.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,17 @@
    #!/usr/bin/env bash

    git_root() {
    if ! $(git rev-parse --is-inside-work-tree 2>/dev/null); then
    return 1
    else
    local gitRootDir=$(git rev-parse --show-cdup)

    if [[ -n "${gitRootDir}" ]]; then
    cd "${gitRootDir}"
    fi
    fi
    }

    head_sync() {
    if [[ "${#}" -ne 2 ]]; then
    echo "Usage: head_sync [REMOTE_SERVER] [REMOTE_PATH]"
    @@ -15,18 +27,6 @@ head_sync() {
    ssh "${1}" "cd ${REMOTE_PATH_PREFIX} && git reset HEAD . && git clean -f && git checkout -- . && git fetch && git checkout ${branch} && git pull"
    }

    git_root() {
    if ! $(git rev-parse --is-inside-work-tree 2>/dev/null); then
    return 1
    else
    local gitRootDir=$(git rev-parse --show-cdup)

    if [[ -n "${gitRootDir}" ]]; then
    cd "${gitRootDir}"
    fi
    fi
    }

    git_sync() {
    if [[ "${#}" -lt 2 ]]; then
    echo "Usage: git_sync [REMOTE_SERVER] [REMOTE_PATH] [DRY]?"
    @@ -110,7 +110,11 @@ git_sync() {
    echo
    }

    function watch_sync() {
    watch_sync() {
    if [[ -z "${NO_HEAD_SYNC:-}" ]]; then
    head_sync "${@}"
    fi

    git_sync "${@}"

    fswatch -0 -o --exclude=.git/ . | while read -d "" event
  6. ViBiOh revised this gist Feb 14, 2019. 1 changed file with 32 additions and 25 deletions.
    57 changes: 32 additions & 25 deletions git.sh
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,35 @@
    #!/usr/bin/env bash

    function head_sync() {
    if [ "${#}" -ne 2 ]; then
    echo Usage: head_sync [REMOTE_SERVER] [REMOTE_PATH]
    head_sync() {
    if [[ "${#}" -ne 2 ]]; then
    echo "Usage: head_sync [REMOTE_SERVER] [REMOTE_PATH]"
    return 1
    fi

    local REMOTE_PATH_PREFIX="~/${2}"
    if [ "${2:0:1}" == "/" ]; then
    if [[ "${2:0:1}" == "/" ]]; then
    REMOTE_PATH_PREFIX="${2}"
    fi

    local branch=`git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||'`
    local branch=$(git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||')
    ssh "${1}" "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] [DRY]?
    git_root() {
    if ! $(git rev-parse --is-inside-work-tree 2>/dev/null); then
    return 1
    else
    local gitRootDir=$(git rev-parse --show-cdup)

    if [[ -n "${gitRootDir}" ]]; then
    cd "${gitRootDir}"
    fi
    fi
    }

    git_sync() {
    if [[ "${#}" -lt 2 ]]; then
    echo "Usage: git_sync [REMOTE_SERVER] [REMOTE_PATH] [DRY]?"
    return 1
    fi

    @@ -27,28 +39,23 @@ function git_sync() {
    local BLUE='\033[0;34m'
    local RESET='\033[0m'

    if ! `git rev-parse --is-inside-work-tree 2>/dev/null`; then
    echo -e "${RED}You're not inside a git folder${RESET}"
    return 1
    else
    local gitRootDir=`git rev-parse --show-cdup`

    if [ -n "${gitRootDir}" ]; then
    cd "${gitRootDir}"
    fi
    git_root
    isGit=$?
    if [[ ${isGit} -ne 0 ]]; then
    return ${isGit}
    fi

    local REMOTE_PATH_PREFIX="~/${2}"
    if [ "${2:0:1}" == "/" ]; then
    if [[ "${2:0:1}" == "/" ]]; then
    REMOTE_PATH_PREFIX="${2}"
    fi

    local dry=false
    if [ "${3,,}" == "dry" ]; then
    if [[ "${3,,}" == "dry" ]]; then
    dry=true
    fi

    if ${dry}; then
    if [[ "${dry}" == true ]]; then
    echo -e "${BLUE}Dry run of syncing files...${RESET}\n"
    else
    echo -e "${BLUE}Syncing files...${RESET}\n"
    @@ -58,7 +65,7 @@ function git_sync() {
    local toDelete=()

    local IFS=$'\n'
    for gitFile in `git status --porcelain`; do
    for gitFile in $(git status --porcelain); do
    local prefix="${gitFile:0:2}"
    local trimmedPrefix="${prefix#[[:space:]]}"

    @@ -72,8 +79,8 @@ function git_sync() {
    ;;

    "R")
    local originFile=`echo -n "${gitFile}" | awk '{print $2}'`
    local destinationFile=`echo -n "${gitFile}" | awk '{print $4}'`
    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}")
    @@ -89,12 +96,12 @@ function git_sync() {
    ssh "${1}" "cd ${REMOTE_PATH_PREFIX} && git clean -f && git checkout -- ."
    fi

    if [ "${#toDelete[@]}" -ne 0 ]; then
    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
    if [[ "${#toSync[@]}" -ne 0 ]]; then
    ! ${dry} && rsync -raR "${toSync[@]}" "${1}:${REMOTE_PATH_PREFIX}/"
    echo -e "${GREEN} + Copied\n${toSync[*]}${RESET}\n"
    fi
  7. ViBiOh revised this gist Dec 19, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git.sh
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ function head_sync() {
    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"
    ssh "${1}" "cd ${REMOTE_PATH_PREFIX} && git reset HEAD . && git clean -f && git checkout -- . && git fetch && git checkout ${branch} && git pull"
    }

    function git_sync() {
  8. ViBiOh revised this gist Mar 19, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git.sh
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ function git_sync() {
    echo -e "${RED}You're not inside a git folder${RESET}"
    return 1
    else
    gitRootDir=`git rev-parse --show-cdup`
    local gitRootDir=`git rev-parse --show-cdup`

    if [ -n "${gitRootDir}" ]; then
    cd "${gitRootDir}"
  9. ViBiOh revised this gist Mar 19, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions git.sh
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    function head_sync() {
    if [ "${#}" -ne 2 ]; then
    echo Usage: head_sync [REMOTE_SERVER] [REMOTE_PATH_FROM_HOME]
    echo Usage: head_sync [REMOTE_SERVER] [REMOTE_PATH]
    return 1
    fi

    @@ -17,7 +17,7 @@ function head_sync() {

    function git_sync() {
    if [ "${#}" -lt 2 ]; then
    echo Usage: git_sync [REMOTE_SERVER] [REMOTE_PATH_FROM_HOME] [DRY]?
    echo Usage: git_sync [REMOTE_SERVER] [REMOTE_PATH] [DRY]?
    return 1
    fi

  10. ViBiOh revised this gist Mar 19, 2018. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions git.sh
    Original file line number Diff line number Diff line change
    @@ -27,11 +27,15 @@ function git_sync() {
    local BLUE='\033[0;34m'
    local RESET='\033[0m'

    if ! `git rev-parse --is-inside-work-tree`; then
    if ! `git rev-parse --is-inside-work-tree 2>/dev/null`; then
    echo -e "${RED}You're not inside a git folder${RESET}"
    return 1
    else
    cd `git rev-parse --show-cdup`
    gitRootDir=`git rev-parse --show-cdup`

    if [ -n "${gitRootDir}" ]; then
    cd "${gitRootDir}"
    fi
    fi

    local REMOTE_PATH_PREFIX="~/${2}"
  11. ViBiOh created this gist Mar 19, 2018.
    109 changes: 109 additions & 0 deletions git.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,109 @@
    #!/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
    }