Skip to content

Instantly share code, notes, and snippets.

@roni-estein
Created October 17, 2021 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roni-estein/190c52f9c03c6ad6a883c9d0f0320c4a to your computer and use it in GitHub Desktop.
Save roni-estein/190c52f9c03c6ad6a883c9d0f0320c4a to your computer and use it in GitHub Desktop.
Safe Nah Command Setup for MacOS
# NAHS
# Where I'm keeoing my nah related base functions.
# Main nah command
nah(){
git ls-files --exclude-standard -om | xargs -I {} bash -c 'safe-nah {}'
git clean -df
}
# git ls-files --exclude-standard -om
# get a list of your directory, like git status that lists the changed and untracked files but not the ignored ones.
# | xargs -I {} bash -c 'safe-nah {}'
# send it, broken up line by line to safe-nah
# list the safe nah backups you have for this projet
# located in .nahs/<project-name>/<timestamp> directory
nah-list(){
loc=$(pwd)
project=$(pwd | xargs basename)
backupdir="$loc/.nahs/$project/"
if [ -d "$backupdir" ] ; then
echo "Listing from newest to oldest"
ls -1t $backupdir
else
echo "No nahs yet..."
fi
unset loc
unset project
unset backupdir
}
# get rid of the older-nahs kicking around if you want
alias nah-purge='rm -rf .nahs; echo Purged all NAH history'
# The wise and powerful undo! It will undo your last nah, or if you specify another one in existance, it will do that one!
nah-undo(){
file=$1
project=$(pwd | xargs basename)
loc=$(pwd)
dir="$loc/.nahs/"
backupdir="$dir$project/"
if [[ -d "$dir" && -d "$backupdir" && $(ls -A "$backupdir") ]] ; then
if [ -z $1 ] ; then
current=$(ls -Art "$backupdir" | tail -n 1)
prefix=".nahs/$project/$current"
find "$prefix" -type f | xargs -I {} bash -c "safe-nah-restore $((${#prefix}+2)) {}"
rm -rf "$prefix"
else
if [[ "$(ls -A $1)" ]] ; then
prefix=".nahs/$project/$1"
find "$prefix" -type f | xargs -I {} bash -c "safe-nah-restore $((${#prefix}+2)) {}"
rm -rf "$prefix"
else
echo "No such backup exists: $1"
fi
fi
else
echo "nothing to restore"
fi
unset file
unset project
unset loc
unset dir
unset backup
unset prefix
unset current
}
# a test to make sure this all works perfectly
test-nah(){
git clean -df
reload-commands # an alias for source ~/.aliases with some checks
nah-purge
cp -R ../stb/app/Models app #stb is jsut another laravel project in the same folder. Change it for something on your machine
gts # an alias for git status
nah
nah-undo
gts
}

Save Nah Command Setup for MacOS

This came about after a complete disaster. I hope you emply one of the following strategies or something different as long as it accomplishes saving you from this evil fate!

I had a terrible ear infaction spreading into my jaw and eyes, it was killing me! While working late with a client, I "nah'd" in another clients directory and destroyed what was a week of work. There are so many things that combined together to cause this disaster. But in the end, a millisecond after I have hit enter I knew I was in trouble and after purchasing several recovery apps, all garbage I realized I was boned and the best hting I could do was a 2 pronged attack.

  1. Avoid working when I'm clearly too messed up.
  2. Make a safer version of nah, becasue anything that happens once, will happen again!

There are 2 strategies I employed when trying this out...

  1. make nah delete files to the trash, where you can restore.
  2. make nah purge all the files as expected but move them in an organized fashion to a safe timestamped backup that can be cleared.

Scenerio 1:

Install the trash-cli from homebrew

brew install trash

In an aliases file, I use .aliases and import it into bash or zsh but you can drop it into .bash_aliases or .zshrc .bash_profile basically whatever you are running

alias nah='git ls-files --exclude-standard -om | xargs trash -F; git reset --hard'

Doing this will send you nah to the Trash Bin where you can restore your files.

This gives you a 1 step backup. Delete your trashes or nah somewhere else and you are screwed. It would have saved me, but it's more high risk than I want.

The alternative, scenario adds a more robust system.

Scenario 2

Make some scrits to save your hide!

Nothing to install here, but you will need to actually go outside you aliases files and make full on bash scripts. You need to do this becasue xargs does not install a full terminal but only a minimal shell, so your aliases and functions will not be available. However, scripts in your path will be avilable.

This version will allow you to nah to your hearts content. It will move all the appropriate files to a hidden .nahs folder in your project directory. you can restore

I opt for easy to remember lcations. I added a shell-scriptsdirectory right in my home directory. It's easy to find and remeber what its for. It's not in my path but you can link it to any shell available path like /usr/local/bin or ~/.composer/vendor/bin or whatever you like. Make sure the files are shell executable.

The folowwing files are needed.

  1. A place for your root commands .aliases for me.
  2. safe-nah.sh in my shell scripts directory
  3. safe-nah-restore.sh in my shell scripts directory
  4. Add the .nahs/ directory to your global gitignore file.
#!/bin/bash
if [ $# = 2 ] ; then
file="$(echo $2 | cut -c$1-)"
echo "$2"
dir="$(dirname $file)"
mkdir -p "$dir"
mv -f "$2" $file
else
echo bad input
fi
unset dir
unset file
#!/bin/bash
dir=$(dirname $1)
t=$(date +"%s")
loc=$(pwd | xargs basename)
backupdir=."nahs/$loc/$t/$dir"
mkdir -p "$backupdir"
mv $1 "$backupdir"
git reset --hard
unset dir
unset t
unset loc
unset backupdir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment