Skip to content

Instantly share code, notes, and snippets.

@ntpeters
Last active January 2, 2020 09:12

Revisions

  1. ntpeters revised this gist Feb 22, 2016. 1 changed file with 41 additions and 8 deletions.
    49 changes: 41 additions & 8 deletions dotstat.sh
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,9 @@

    # This is a simple script to check the current local/remote status of your
    # dotfiles, and prompt to update them if needed.
    # This script uses updot (https://github.com/ntpeters/updot) for syncing
    # dotfiles, but could easily be updated to call anything else.
    #
    # Update the contents of the 'sync_dotfiles' function below to change the
    # method for performing dotfile updates.
    #
    # You must set the 'LOCAL_DOTFILES_REPOSITORY' environment variable
    # to point to the path of your local dotfiles repo prior to executing this script.
    @@ -12,6 +13,27 @@
    # the status of local/remote git repos posted by Neil Mayhew on StackOverflow:
    # http://stackoverflow.com/a/3278427/1428743

    # Syncs dotfile changes
    # NOTE TO USER:
    # Modify this function to use your desired mothod of syncing dotfiles
    function sync_dotfiles() {
    # Currently using updot (https://github.com/ntpeters/updot) for dotfile sync

    # Ensure Python is installed
    if ! prog_loc="$(type -p "python")" || [ -z "$prog_loc" ] || [ "$prog_loc" == *"not found"* ]; then
    echo "ERROR: Python does not exist in path! Updot requires Python. Please ensure Python is installed, and try again."
    return

    fi

    # Try executing updot from its default location
    if [ -f $HOME/.updot/updot.py ]; then
    python $HOME/.updot/updot.py
    else
    echo "ERROR: updot not found! Aborting sync!"
    fi
    }

    # Checks the status of local and remote dotfiles repos to determine if they
    # need to be synced up.
    function check_dotfiles_status() {
    @@ -21,6 +43,12 @@ function check_dotfiles_status() {
    return
    fi

    # Check if git is available
    if ! prog_loc="$(type -p "git")" || [ -z "$prog_loc" ] || [ "$prog_loc" == *"not found"* ]; then
    echo "ERROR: Git does not exist in path! Please ensure git is installed, and try again."
    return
    fi

    # Check for active internet connection
    echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1
    if [ $? -eq 1 ]; then
    @@ -66,16 +94,21 @@ function check_dotfiles_status() {
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo
    # Try executing updot from its default location
    if [ -f $HOME/.updot/updot.py ]; then
    python $HOME/.updot/updot.py
    else
    echo "ERROR: updot not found! Aborting sync!"
    fi
    # Execute the defined method of syncing dotfiles
    sync_dotfiles
    fi
    fi
    }

    # Check if other shell instances are currently running
    # Only check dotfile status on initial shell open
    cur_shell_instances=$(pgrep -f `ps -p $$ -ocomm= | cut -d'/' -f 3` | wc -l | tr -d ' ')
    default_shell_instances=$(pgrep -f ${SHELL##*/} | wc -l | tr -d ' ')
    total_shell_instances=$(( $cur_shell_instances + $default_shell_instances ))
    if (( $total_shell_instances > 1 )); then
    exit 1
    fi

    # Ensure a path to the local dotfiles repo has been provided
    if [ -z ${LOCAL_DOTFILES_REPOSITORY+x} ]; then
    echo "ERROR: Must set the 'LOCAL_DOTFILES_REPOSITORY' variable with path to your dotfiles directory!"
  2. ntpeters revised this gist Jul 18, 2015. 1 changed file with 13 additions and 6 deletions.
    19 changes: 13 additions & 6 deletions dotstat.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    # dotfiles, and prompt to update them if needed.
    # This script uses updot (https://github.com/ntpeters/updot) for syncing
    # dotfiles, but could easily be updated to call anything else.
    #
    #
    # You must set the 'LOCAL_DOTFILES_REPOSITORY' environment variable
    # to point to the path of your local dotfiles repo prior to executing this script.
    #
    @@ -17,18 +17,25 @@
    function check_dotfiles_status() {
    # Make sure the path we were given is a git repo
    if [ ! -d ./.git ]; then
    echo "ERROR: Provided directory is not a git repository!"
    echo "ERROR: Provided dotfile directory is not a git repository!"
    return
    fi

    # Check for active internet connection
    echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1
    if [ $? -eq 1 ]; then
    echo "ERROR: Failed to get status of remote dotfiles. No active internet connection detected!"
    return
    fi

    # Get remote branch info
    $(git fetch > /dev/null 2>&1)

    # Retrieve hashes from git
    local LOCAL=$(git rev-parse @)
    local REMOTE=$(git rev-parse @\{u\})
    local BASE=$(git merge-base @ @\{u\})

    # Check for uncommitted changes
    $(! git diff-index --quiet HEAD --)
    local UNCOMMITTED=$?
    @@ -38,7 +45,7 @@ function check_dotfiles_status() {

    # Determine status of dotfiles
    if [ -z $LOCAL ] || [ -z $REMOTE ] || [ -z $BASE ]; then
    echo "ERROR: Failed to get status of dotfiles!"
    echo "ERROR: Failed to get status of local dotfiles!"
    update=false
    elif [ $UNCOMMITTED = 0 ]; then
    echo "Local dotfiles have uncommitted changes."
  3. ntpeters revised this gist Jul 9, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions dotstat.sh
    Original file line number Diff line number Diff line change
    @@ -60,8 +60,8 @@ function check_dotfiles_status() {
    if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo
    # Try executing updot from its default location
    if [ -f $HOME/.updot/updot ]; then
    python $HOME/.updot/updot
    if [ -f $HOME/.updot/updot.py ]; then
    python $HOME/.updot/updot.py
    else
    echo "ERROR: updot not found! Aborting sync!"
    fi
  4. ntpeters revised this gist Jul 9, 2015. 1 changed file with 2 additions and 5 deletions.
    7 changes: 2 additions & 5 deletions dotstat.sh
    Original file line number Diff line number Diff line change
    @@ -59,11 +59,8 @@ function check_dotfiles_status() {
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo
    # See if updot is on our path, otherwise try executing from
    # its default location
    if ! updot_loc="$(type -p "updot")" || [ -z "$updot_loc" ]; then
    updot
    elif [ -f $HOME/.updot/updot ]; then
    # Try executing updot from its default location
    if [ -f $HOME/.updot/updot ]; then
    python $HOME/.updot/updot
    else
    echo "ERROR: updot not found! Aborting sync!"
  5. ntpeters revised this gist Jul 8, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion dotstat.sh
    Original file line number Diff line number Diff line change
    @@ -61,7 +61,7 @@ function check_dotfiles_status() {
    echo
    # See if updot is on our path, otherwise try executing from
    # its default location
    if ! updot_loc="$(type -p "updot")" || [ -z "$updot_loc"]; then
    if ! updot_loc="$(type -p "updot")" || [ -z "$updot_loc" ]; then
    updot
    elif [ -f $HOME/.updot/updot ]; then
    python $HOME/.updot/updot
  6. ntpeters revised this gist May 15, 2015. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion dotstat.sh
    Original file line number Diff line number Diff line change
    @@ -59,9 +59,12 @@ function check_dotfiles_status() {
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo
    # Ensure updot is in our path
    # See if updot is on our path, otherwise try executing from
    # its default location
    if ! updot_loc="$(type -p "updot")" || [ -z "$updot_loc"]; then
    updot
    elif [ -f $HOME/.updot/updot ]; then
    python $HOME/.updot/updot
    else
    echo "ERROR: updot not found! Aborting sync!"
    fi
  7. ntpeters revised this gist May 15, 2015. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions dotstat.sh
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,9 @@
    # dotfiles, and prompt to update them if needed.
    # This script uses updot (https://github.com/ntpeters/updot) for syncing
    # dotfiles, but could easily be updated to call anything else.
    #
    # You must set the 'LOCAL_DOTFILES_REPOSITORY' environment variable
    # to point to the path of your local dotfiles repo prior to executing this script.
    #
    # The core portion of this script is taken from a general solution for checking
    # the status of local/remote git repos posted by Neil Mayhew on StackOverflow:
    @@ -12,6 +15,12 @@
    # Checks the status of local and remote dotfiles repos to determine if they
    # need to be synced up.
    function check_dotfiles_status() {
    # Make sure the path we were given is a git repo
    if [ ! -d ./.git ]; then
    echo "ERROR: Provided directory is not a git repository!"
    return
    fi

    # Get remote branch info
    $(git fetch > /dev/null 2>&1)

  8. ntpeters revised this gist May 15, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions dotstat.sh
    Original file line number Diff line number Diff line change
    @@ -12,10 +12,14 @@
    # Checks the status of local and remote dotfiles repos to determine if they
    # need to be synced up.
    function check_dotfiles_status() {
    # Get remote branch info
    $(git fetch > /dev/null 2>&1)

    # Retrieve hashes from git
    local LOCAL=$(git rev-parse @)
    local REMOTE=$(git rev-parse @\{u\})
    local BASE=$(git merge-base @ @\{u\})

    # Check for uncommitted changes
    $(! git diff-index --quiet HEAD --)
    local UNCOMMITTED=$?
  9. ntpeters revised this gist May 15, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions dotstat.sh
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,9 @@ function check_dotfiles_status() {
    local LOCAL=$(git rev-parse @)
    local REMOTE=$(git rev-parse @\{u\})
    local BASE=$(git merge-base @ @\{u\})
    # Check for uncommitted changes
    $(! git diff-index --quiet HEAD --)
    local UNCOMMITTED=$?

    # Flag to determine if an update may need to be done
    local update=true
    @@ -24,6 +27,8 @@ function check_dotfiles_status() {
    if [ -z $LOCAL ] || [ -z $REMOTE ] || [ -z $BASE ]; then
    echo "ERROR: Failed to get status of dotfiles!"
    update=false
    elif [ $UNCOMMITTED = 0 ]; then
    echo "Local dotfiles have uncommitted changes."
    elif [ $LOCAL = $REMOTE ]; then
    echo "Dotfiles up-to-date."
    update=false
  10. ntpeters revised this gist May 14, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions dotstat.sh
    Original file line number Diff line number Diff line change
    @@ -14,8 +14,8 @@
    function check_dotfiles_status() {
    # Retrieve hashes from git
    local LOCAL=$(git rev-parse @)
    local REMOTE=$(git rev-parse @{u})
    local BASE=$(git merge-base @ @{u})
    local REMOTE=$(git rev-parse @\{u\})
    local BASE=$(git merge-base @ @\{u\})

    # Flag to determine if an update may need to be done
    local update=true
  11. ntpeters revised this gist May 14, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion dotstat.sh
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    # This script uses updot (https://github.com/ntpeters/updot) for syncing
    # dotfiles, but could easily be updated to call anything else.
    #
    # The core portion of this script is taken from a general solution for checkin
    # The core portion of this script is taken from a general solution for checking
    # the status of local/remote git repos posted by Neil Mayhew on StackOverflow:
    # http://stackoverflow.com/a/3278427/1428743

  12. ntpeters renamed this gist May 14, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  13. ntpeters renamed this gist May 14, 2015. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions gistfile1.txt → gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ function check_dotfiles_status() {
    elif [ $REMOTE = $BASE ]; then
    echo "Local dotfiles have changed."
    else
    echo "WARNING: Local and remote dotfiles have diverged. You may experi
    echo "WARNING: Local and remote dotfiles have diverged. You may experience merge conflicts!"
    fi

    # See if the user wants to sync dotfiles
    @@ -53,12 +53,11 @@ function check_dotfiles_status() {

    # Ensure a path to the local dotfiles repo has been provided
    if [ -z ${LOCAL_DOTFILES_REPOSITORY+x} ]; then
    echo "ERROR: Must set the 'LOCAL_DOTFILES_REPOSITORY' variable with path to your dotfiles
    echo "ERROR: Must set the 'LOCAL_DOTFILES_REPOSITORY' variable with path to your dotfiles directory!"
    else
    # Ensure path to dotfiles repo has expanded tilde for home dir
    # Not sure how portable this method is...
    local_dotfiles_repo=$(eval "echo $LOCAL_DOTFILES_REPOSITORY")
    # Check if dotfiles need to be updated
    ( cd ${local_dotfiles_repo} && check_dotfiles_status )
    fi
    gitcheck.sh lines 22-63/63 (END)
    fi
  14. ntpeters created this gist May 14, 2015.
    64 changes: 64 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    #!/usr/bin/env/bash

    # This is a simple script to check the current local/remote status of your
    # dotfiles, and prompt to update them if needed.
    # This script uses updot (https://github.com/ntpeters/updot) for syncing
    # dotfiles, but could easily be updated to call anything else.
    #
    # The core portion of this script is taken from a general solution for checkin
    # the status of local/remote git repos posted by Neil Mayhew on StackOverflow:
    # http://stackoverflow.com/a/3278427/1428743

    # Checks the status of local and remote dotfiles repos to determine if they
    # need to be synced up.
    function check_dotfiles_status() {
    # Retrieve hashes from git
    local LOCAL=$(git rev-parse @)
    local REMOTE=$(git rev-parse @{u})
    local BASE=$(git merge-base @ @{u})

    # Flag to determine if an update may need to be done
    local update=true

    # Determine status of dotfiles
    if [ -z $LOCAL ] || [ -z $REMOTE ] || [ -z $BASE ]; then
    echo "ERROR: Failed to get status of dotfiles!"
    update=false
    elif [ $LOCAL = $REMOTE ]; then
    echo "Dotfiles up-to-date."
    update=false
    elif [ $LOCAL = $BASE ]; then
    echo "Remote dotfiles have changed."
    elif [ $REMOTE = $BASE ]; then
    echo "Local dotfiles have changed."
    else
    echo "WARNING: Local and remote dotfiles have diverged. You may experi
    fi
    # See if the user wants to sync dotfiles
    if $update; then
    read -p "Sync dotfiles now? [y/n] " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo
    # Ensure updot is in our path
    if ! updot_loc="$(type -p "updot")" || [ -z "$updot_loc"]; then
    updot
    else
    echo "ERROR: updot not found! Aborting sync!"
    fi
    fi
    fi
    }
    # Ensure a path to the local dotfiles repo has been provided
    if [ -z ${LOCAL_DOTFILES_REPOSITORY+x} ]; then
    echo "ERROR: Must set the 'LOCAL_DOTFILES_REPOSITORY' variable with path to your dotfiles
    else
    # Ensure path to dotfiles repo has expanded tilde for home dir
    # Not sure how portable this method is...
    local_dotfiles_repo=$(eval "echo $LOCAL_DOTFILES_REPOSITORY")
    # Check if dotfiles need to be updated
    ( cd ${local_dotfiles_repo} && check_dotfiles_status )
    fi
    gitcheck.sh lines 22-63/63 (END)