Last active
December 30, 2015 13:29
-
-
Save schnell18/7836292 to your computer and use it in GitHub Desktop.
Shell script to archive full source of last changed files.
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
#!/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: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add random temp file using Bash's builtin $RANDOM variable as inspired from Bash Shell Generate Random Numbers