Skip to content

Instantly share code, notes, and snippets.

@schnell18
Last active December 30, 2015 13:29
Show Gist options
  • Save schnell18/7836292 to your computer and use it in GitHub Desktop.
Save schnell18/7836292 to your computer and use it in GitHub Desktop.
Shell script to archive full source of last changed files.
#!/bin/bash
base=$1
archive=$2
currentBranch=$(git rev-parse --abbrev-ref HEAD)
currentBranch=${currentBranch//heads\//}
if [[ -z $base ]]
then
base=bl_${currentBranch}
fi
if [[ -z $archive ]]
then
archive=${currentBranch}_$(date +%Y-%m-%d).tgz
# turn slash in the branch name to understore
# so that it can be part of archive file name
archive=${archive//\//-}
fi
# use bash built-in random number variable
mod_lst_file=/tmp/gp_mod${RANDOM}.lst
del_lst_file=/tmp/gp_del${RANDOM}.lst
# remove stale temp files
if [[ -f $mod_lst_file ]]
then
rm -f $mod_lst_file
fi
if [[ -f $del_lst_file ]]
then
rm -f $del_lst_file
fi
# IFS must be new line so that the status column and
# the file are treated as one entry rather than two
IFS=$(printf '\n')
for entry in "$(git diff --name-status $base)"
do
if [[ $entry =~ ^M|^A ]]
then
echo $entry |cut -f 2 >> $mod_lst_file
elif [[ $entry =~ ^D ]]
then
echo $entry |cut -f 2 >> $del_lst_file
fi
done
unset IFS
if [[ -f $mod_lst_file && -s $mod_lst_file ]]
then
tar -cvzf $archive -T $mod_lst_file
else
echo "No changes made"
exit 1
fi
# clean up temp files
if [[ -f $mod_lst_file ]]
then
rm -f $mod_lst_file
fi
if [[ -f $del_lst_file ]]
then
rm -f $del_lst_file
fi
# vim: set ai nu nobk expandtab sw=4 ts=4 syntax=sh:
@schnell18
Copy link
Author

Add random temp file using Bash's builtin $RANDOM variable as inspired from Bash Shell Generate Random Numbers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment