Skip to content

Instantly share code, notes, and snippets.

@l0ki000
Last active August 29, 2015 14:22
Show Gist options
  • Save l0ki000/88b274066fd46f71f172 to your computer and use it in GitHub Desktop.
Save l0ki000/88b274066fd46f71f172 to your computer and use it in GitHub Desktop.
script that is intended to get everything new from local dir and prepare commit to git repo, i.e all "untracked files" or "changed but not updated"
#!/bin/bash
# script is intended to get everything new from local dir and prepare commit to git repo
# i.e all untracked files or changed but not updated
# bash 0 is true
# 1 is false (yeah, feel the pain!)
ask_user()
{
echo $1;
select yn in "Yes" "No"; do
# handling here via REPLY both digits and words answer, as nobody don't know actually how to speak to bash script
case "$REPLY" in
[1Yy]* ) return 0;;
[2Nn]* ) return 1;;
* ) echo $REPLY; echo "Did you say something?";;
esac
done
}
GIT_HOME="./.git"
AC_HOME="./.accurev"
if [[ ! -e $GIT_HOME || ! -d $GIT_HOME ]]; then
echo "Can't find local initialized git repository at: $GIT_HOME";
exit 1;
fi;
if [[ ! -e $AC_HOME || ! -d $AC_HOME ]]; then
echo "Can't find local initialized accurev workspace at: $AC_HOME";
exit 1;
fi;
# check for git version, script was created and debugged with git 1.6.0.2
git --version | grep -Eq "1\.6\.[0-9.]+$"
if [[ $? != 0 ]]; then
echo "Your git version is not checked well with this script. Use with caution. You have been warned.";
read -s -n 1 -p "Press any key to continue . . .";
fi;
# setting line separator
OIFS=$IFS
IFS=$'\n'
# get all the files
declare -a GIT_STATUS
GIT_STATUS=( $(git status) );
#echo GIT_STATUS: "${GIT_STATUS[*]}"
# get all files that are untracked
declare -a GIT_NOT_UPDATED
GIT_NOT_UPDATED=$(echo -e "${GIT_STATUS[*]}" | sed '1,/Changed but not updated:/ d')
#echo GIT_NOT_UPDATED: "${GIT_NOT_UPDATED[*]}"
declare -a GIT_MODIFIED
declare -a GIT_REMOVED
declare -a GIT_UNTRACKED
GIT_UNTRACKED=( $(echo -e "${GIT_STATUS[*]}" | sed '1,/Untracked files:/ d') )
#echo -e GIT_UNTRACKED: "${#GIT_UNTRACKED[*]}"
#echo -e GIT_UNTRACKED: "${GIT_UNTRACKED[*]}"
# removing git hints, which are among list of files
GIT_HINTS=("*to include in what will be committed)"
"no changes added to commit*")
for del in ${GIT_HINTS[@]}
do
GIT_UNTRACKED=( "${GIT_UNTRACKED[*]/$del}" )
done
GIT_UNTRACKED=( $GIT_UNTRACKED )
#echo -e GIT_UNTRACKED: "${#GIT_UNTRACKED[*]}"
#echo -e GIT_UNTRACKED: "${GIT_UNTRACKED[*]}"
for i in ${GIT_NOT_UPDATED[@]} ; do
FILE=$(echo "$i" | grep -E "^#\s+(modified|new file):" | sed -r 's/^#\s+(modified|new file):\s+(.*)$/\2/')
if [[ "$FILE" != "" ]]; then
GIT_MODIFIED+=("$FILE"$'\n')
continue
fi;
FILE=$(echo "$i" | grep -E "^#\s+deleted:" | sed -r 's/^#\s+(deleted):\s+(.*)$/\2/')
if [[ "$FILE" != "" ]]; then
GIT_REMOVED+=("$FILE"$'\n')
continue
fi;
done;
for i in ${GIT_UNTRACKED[@]} ; do
FILE=$(echo "$i" | grep -E "^#\s+[^\0]+" | sed -r 's/^#\s+(.*)$/\1/')
if [[ "$FILE" != "" ]]; then
GIT_UNTRACKED_PRETTY+=("$FILE"$'\n')
fi;
done;
if [[ "${#GIT_MODIFIED[@]}" != 0 ]]; then
echo "===================================================================================="
echo -e "Files to be added/modified to git: ${#GIT_MODIFIED[@]}"
echo "===================================================================================="
echo -e "${GIT_MODIFIED[@]}"
if ask_user "Do you want me to add changes in all this files to git commit?" ; then
echo All files are adding to git commit, right now
for i in ${GIT_MODIFIED[@]}; do
git add `echo $i`
done;
else
echo "As you wish, my lord - skipping"
fi;
fi;
if [[ "${#GIT_REMOVED[@]}" != 0 ]]; then
echo "===================================================================================="
echo -e "Files to be commited as removed to git: ${#GIT_REMOVED[@]}"
echo "===================================================================================="
echo -e "${GIT_REMOVED[@]}"
if ask_user "Do you want me to add removal of all this files to git commit?" ; then
echo All files removal are adding to git commit, right now
for i in ${GIT_REMOVED[@]}; do
git rm `echo $i`
done;
else
echo "As you wish, my lord - skipping"
fi;
fi;
if [[ "${#GIT_UNTRACKED_PRETTY[@]}" != 0 ]]; then
echo "===================================================================================="
echo -e "Files detected as untracked by git: ${#GIT_UNTRACKED_PRETTY[@]}"
echo "===================================================================================="
echo -e "${GIT_UNTRACKED_PRETTY[@]}"
if ask_user "Do you want me to add all this files to git commit?" ; then
echo All files are adding to git commit, right now
for i in ${GIT_UNTRACKED_PRETTY[@]}; do
git add `echo $i`
done;
else
echo "As you wish, my lord - skipping"
fi;
fi;
echo -e "===================================================================================="
echo -e "Now you should be all set to perform git commit"
echo -e "===================================================================================="
# setting separator back
IFS=$OIFS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment