Skip to content

Instantly share code, notes, and snippets.

@steve-jansen
Last active September 19, 2018 21:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steve-jansen/5117693 to your computer and use it in GitHub Desktop.
Save steve-jansen/5117693 to your computer and use it in GitHub Desktop.
A custom script for git to interactively run "git difftool" against each modified and untracked file, then prompt to stage (add) the change, undo (checkout) the change, or ignore (skip) the change.
#!/bin/bash
# query the root of the repo because git status prints
# relative paths within the repo
if ! pushd "`git rev-parse --show-toplevel`" >/dev/null
then
echo "git rev-parse --show-toplevel failed to return the root dir of the repo"
exit 1
else
gitroot=`pwd`
popd >/dev/null
fi
line=' '
#Set the field separator to new line
IFS=$'\n'
list=`git status --porcelain | grep '^.[M?]'| cut -c 4- | awk 'BEGIN { FS=" -> "}; {print $NF}' | sed s/\"//g`
if [ -z "$list" ]
then
echo "nothing to commit, working directory clean"
exit 0
fi
echo git-diff-add commands:
echo " a: add file"
echo " c: checkout/revert file"
echo " s: skip file"
echo
# query git for any modified or untracked files
# filter only for modified files
# skip the the first 4 chars of the output, which give the status before the filepath
# only take the new file name for renames, which are formatted as "oldfile -> newfile"
# strip any double quotes, since we will quote the paths
for f in $list
do
git difftool -y "$gitroot/$f"
len=${#f}
if [ "$len" -lt "53" ]
then
# pad string to be 50 chars wide
f2="$f ${line:$len}"
else
# prefix with "..." and take the right 53 chars to make a 50 char string
f2="...${f:(-53)}"
fi
printf "%s [a|c|s]> " "$f2"
read -n 1 prompt
case $prompt in
[ayAY] | add | ADD | yes | YES)
git add "$f" && printf " >>> staged\n"
;;
[cC] | checkout | CHECKOUT)
git checkout "$f" && printf " >>> checkout\n"
;;
*)
printf " >>> skipped\n"
;;
esac;
done;
#!/bin/bash
# return code constants
readonly ERR_SUCCESS=0
readonly ERR_CANCELLED=1
# path constants
readonly me=git-publish
#local variables
readonly branch=`git branch | grep "\*" | cut -d " " -f 2-9`;
squash=0
echo "$me: running git-update..."
echo
# run the custom git-update script to bring the personal/feature/topic branch up to the latest from origin/master
git update
# prompt to run an optional cmd line build
echo
read -p "$me: build command to run (optional) > " prompt
echo
if [ ! -z "$prompt" ]
then
# run the cmd line build
$prompt
echo
fi
# if we are not already in master, off to either git merge or git merge --squash the branch into master
if [ "$branch" != "master" ]
then
git checkout master
echo
read -p "$me: squash merge $branch into a single master commit? [y/n]> " prompt
echo
case $prompt in
[ayAY] | yes | YES)
echo "$me: squashing..."
echo
git merge --squash "$branch"
git commit -v
squash=1
;;
*)
git merge "$branch"
;;
esac;
fi
echo
echo "$me: displaying the two most recent commits in master..."
echo
git log -n 2
echo
read -p "$me: push to origin/master? [y/n]> " prompt
echo
case $prompt in
[ayAY] | yes | YES)
git push origin master
;;
*)
echo "$me: cancelled, exiting..."
exit $ERR_CANCELLED
;;
esac;
if [ "$branch" != "master" ]
then
if [ "$squash" -eq "1" ]
then
echo
echo "$me: recreating squashed branch $branch (`git branch -v | grep "$branch " | awk '{print $2}'`)..."
echo
git branch -f "$branch" master
fi
git checkout "$branch";
fi
#!/bin/bash
stash() {
# check if we have uncommited changes to stash
git status --porcelain | grep "^." >/dev/null;
if [ $? -eq 0 ]
then
if git stash save -u "git-update on `date`";
then
stash=1;
fi
fi
}
unstash() {
# check if we have uncommited change to restore from the stash
if [ $stash -eq 1 ]
then
git stash pop;
fi
}
stash=0;
stash;
branch=`git branch | grep "\*" | cut -d " " -f 2-9`;
if [ "$branch" == "master" ]
then
git pull origin master;
else
git checkout master;
git pull origin master;
git checkout "$branch";
git rebase master;
fi
unstash;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment