Skip to content

Instantly share code, notes, and snippets.

@jobovy
Last active October 3, 2016 17:10
Show Gist options
  • Save jobovy/625c55492a16e5d80ae0 to your computer and use it in GitHub Desktop.
Save jobovy/625c55492a16e5d80ae0 to your computer and use it in GitHub Desktop.
Modified version of Andy Casey's (@andycasey) post-commit hook to run latexdiff between different revisions of a latex draft (see http://astrowizici.st/blog/2013/10/04/publishing-with-git); works on Macs and when the paper is in a sub-directory of the main git repository.
#!/bin/sh
# Post-commit hook for revision-awsm-ness
function gettempfilename()
{
tempfilename=$1-$RANDOM$RANDOM.tex
if [ -e $tempfilename ]
then
tempfilename=$(gettempfilename)
fi
echo $tempfilename
}
current_hash=$(git rev-parse HEAD)
current_dir=$(git diff-tree --no-commit-id --name-only -r $current_hash | xargs -I file dirname file)
num_revisions=$(git log --pretty=oneline $current_dir | grep -ic "revision [v\d+(?:\.\d+)*]")
# See if there are at least two revisions so that we can do a comparison
if [ $num_revisions -lt 2 ]
then
exit
else
# Check to see if the last named revision is actually the commit hash that just happened
current_revision=$(git log --pretty=oneline $current_dir | grep -i "revision [v\d\.]" | grep -oEi "v\d+(?:\.\d+)" | head -n 1)
most_recent_revision_hash=$(git log --pretty=oneline $current_dir | grep -i "revision [v\d+(?:\.\d+)*]" | head -n 1 | awk '{ print $1 }')
# If the last commit wasn't the one that contained the most recent revision number, then there's nothing to do.
if [[ "$current_hash" != "$most_recent_revision_hash" ]]; then
exit
fi
previous_revision=$(git log --pretty=oneline $current_dir | grep -i "revision [v\d\.]" | grep -oEi "v\d+(?:\.\d+)" | sed -n 2p)
previous_revision_hash=$(git log --pretty=oneline $current_dir | grep -i "revision [v\d+(?:\.\d+)*]" | sed -n 2p | awk '{ print $1 }')
# Use the most edited tex file in this repository as the manuscript, unless the manuscript filename was specified as an argument
most_edited_tex_file=$(git log --pretty=format: --name-only $current_dir | sort | uniq -c | sort -rg | grep ".tex$" | head -n 1 | awk '{ print $2 }')
manuscript_filename=${1:-$most_edited_tex_file}
manuscript_filename_no_ext="${manuscript_filename%.*}"
# If we can't find the manuscript filename, then exit.
if [ ! -f $manuscript_filename ]; then
echo "Manuscript file $manuscript_filename does not exist."
fi
# Get the manuscript file associated with the previous revision hash
previous_manuscript_filename=$(gettempfilename previous)
git show $previous_revision_hash:$manuscript_filename > $previous_manuscript_filename
# Use latexdiff to create a difference version
diff_ms_no_file_ext="$manuscript_filename_no_ext-revisions-$current_revision"
latexdiff $previous_manuscript_filename $manuscript_filename > $diff_ms_no_file_ext.tex
rm -f $previous_manuscript_filename
# mv this file to the original directory and cd into that directory
diff_ms_dir=$(dirname $diff_ms_no_file_ext)
diff_ms_no_file_ext_no_dir=$(basename $diff_ms_no_file_ext)
cd $diff_ms_dir
# Compile the difference file
latex $diff_ms_no_file_ext_no_dir.tex > /dev/null 2>&1
latex $diff_ms_no_file_ext_no_dir.tex > /dev/null 2>&1
latex $diff_ms_no_file_ext_no_dir.tex > /dev/null 2>&1
latex $diff_ms_no_file_ext_no_dir.tex > /dev/null 2>&1
dvipdf $diff_ms_no_file_ext_no_dir.dvi > /dev/null 2>&1
# Remove the intermediate files
ls $diff_ms_no_file_ext_no_dir.* | grep -v pdf | xargs rm -f
ls ${diff_ms_no_file_ext_no_dir}Notes.bib | grep -v pdf | xargs rm -f
ls ${diff_ms_no_file_ext_no_dir}Notess.bib | grep -v pdf | xargs rm -f
echo "Revisions to $manuscript_filename made between $previous_revision"\
"and $current_revision are highlighted in $diff_ms_no_file_ext_no_dir.pdf"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment