Skip to content

Instantly share code, notes, and snippets.

@a3linux
Last active June 7, 2018 04:08
Show Gist options
  • Select an option

  • Save a3linux/62d46c50a18268f5231ae37b35786d93 to your computer and use it in GitHub Desktop.

Select an option

Save a3linux/62d46c50a18268f5231ae37b35786d93 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Show difference between commits
# Usage: gdiff -n 1 <filename>
usage() {
echo "Usage: $0 -n|--commits num <filename>"
echo " Show git difference between <num> commits "
exit 1
}
ARGV=`getopt -a -o n: -l commits: -- "$@"`
[ $? -ne 0 ] && usage
eval set -- "${ARGV}"
while true
do
case "$1" in
-n|--commits)
if [[ $2 =~ ^~?[0-9]+$ ]]; then
num=$2
else
echo "Accept integter ONLY for commits numbers"
exit 1
fi
shift
;;
--)
shift
break
;;
esac
shift
done
i=1
compares=""
for commit in `git log --reverse -n ${num} ${1} |grep ^commit|awk 'NR==1{print $2}; END{if (NR>1) {print $2;}}'`
do
if [ ${i} -eq 1 ]; then
compares="${commit}"
i=$(((++i)))
else
if [ ! -z ${commit} ]; then
compares="${compares}..${commit}"
else
compares="${compares}..HEAD"
fi
i=$(((++i)))
fi
done
if [ ${i} -eq 2 ]; then
compares="${compares}..HEAD"
fi
git diff ${compares} ${1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment