threads / patch / 29635

patchgit-latexdiff: new command in contrib, to use latexdiff and Git

Subject: [PATCH] git-latexdiff: new command in contrib, to use latexdiff and Git

## tl;dr

22 messages between Feb 15, 2012 and Feb 20, 2012. Diffs are folded; open one to read it.

replies: 21people: 7as markdown or json

Matthieu Moy· Feb 15, 2012, 15:49 UTC · lore

git-latexdiff is a wrapper around latexdiff (http://www.ctan.org/pkg/latexdiff) that allows using it to diff two revisions of a LaTeX file.

git-latexdiff is made to work on documents split accross multiple .tex files (plus possibly figures and other non-diffable files), hence could not be implemented as a per-file diff driver.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---

It seems at least one person is interested in my script, so it probably deserves to be in contrib/ ;-).

 contrib/latex/Makefile      |   22 ++++
 contrib/latex/README        |   12 +++
 contrib/latex/git-latexdiff |  222 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 256 insertions(+), 0 deletions(-)
 create mode 100644 contrib/latex/Makefile
 create mode 100644 contrib/latex/README
 create mode 100755 contrib/latex/git-latexdiff
Show changes to 3 files +256 −0

contrib/latex/Makefile, contrib/latex/README, contrib/latex/git-latexdiff

diff --git a/contrib/latex/Makefile b/contrib/latex/Makefile
new file mode 100644
index 0000000..4617906
--- /dev/null
+++ b/contrib/latex/Makefile
@@ -0,0 +1,22 @@
+-include ../../config.mak
+-include ../../config.mak.autogen
+
+ifndef SHELL_PATH
+	SHELL_PATH = /bin/sh
+endif
+
+SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH))
+gitexecdir_SQ = $(subst ','\'',$(gitexecdir))
+
+SCRIPT=git-latexdiff
+
+.PHONY: install help
+help:
+	@echo 'This is the help target of the Makefile. Current configuration:'
+	@echo '  gitexecdir = $(gitexecdir_SQ)'
+	@echo '  SHELL_PATH = $(SHELL_PATH_SQ)'
+	@echo 'Run "$(MAKE) install" to install $(SCRIPT) in gitexecdir.'
+
+install:
+	sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' $(SCRIPT) > '$(gitexecdir_SQ)/$(SCRIPT)'
+	chmod 755 '$(gitexecdir)/$(SCRIPT)'
diff --git a/contrib/latex/README b/contrib/latex/README
new file mode 100644
index 0000000..2d7fdd6
--- /dev/null
+++ b/contrib/latex/README
@@ -0,0 +1,12 @@
+git-latexdiff is a wrapper around latexdiff
+(http://www.ctan.org/pkg/latexdiff) that allows using it to diff two
+revisions of a LaTeX file.
+
+The script internally checks out the full tree for the specified
+revisions, and calls latexdiff with the --flatten option, hence this
+works if the document is split into multiple .tex files.
+
+Try "git latexdiff -h" for more information.
+
+To install, either drop git-latexdiff in your $PATH, or run "make
+install".
diff --git a/contrib/latex/git-latexdiff b/contrib/latex/git-latexdiff
new file mode 100755
index 0000000..13aeb9a
--- /dev/null
+++ b/contrib/latex/git-latexdiff
@@ -0,0 +1,222 @@
+#! /bin/sh
+
+# Author: Matthieu Moy <Matthieu.Moy@imag.fr> (2012)
+
+# Missing features (patches welcome ;-) :
+# - diff the index or the current worktree 
+# - checkout only a subdirectory of the repo
+# - hardlink temporary checkouts as much as possible
+
+usage () {
+            cat << EOF
+Usage: $(basename $0) [options] OLD [NEW]
+Call latexdiff on two Git revisions of a file.
+
+OLD and NEW are Git revision identifiers. NEW defaults to HEAD.
+
+Options:
+	--help		This help message
+	--main FILE.tex	Name of the main LaTeX file
+	--no-view	Don't display the resulting PDF file
+	--view		View the resulting PDF file
+			(default if -o is not used)
+	--no-cleanup	Don't cleanup temp dir after running
+	-o FILE, --output FILE
+			Copy resulting PDF into FILE
+			(usually ending with .pdf)
+EOF
+}
+
+die () {
+    echo "fatal: $@"
+    exit 1
+}
+
+verbose () {
+    if [ "$verbose" = 1 ]; then
+	printf "%s ..." "$@"
+    fi
+}
+
+verbose_progress () {
+    if [ "$verbose" = 1 ]; then
+	printf "." "$@"
+    fi
+}
+
+verbose_done () {
+    if [ "$verbose" = 1 ]; then
+	echo " done."
+    fi
+}
+
+old=
+new=
+main=
+view=maybe
+cleanup=1
+verbose=0
+output=
+initial_dir=$PWD
+
+while test $# -ne 0; do
+    case "$1" in
+        "--help"|"-h")
+            usage
+            exit 0
+            ;;
+	"--main")
+	    shift
+	    main=$1
+	    ;;
+	"--no-view")
+	    view=0
+	    ;;
+	"--view")
+	    view=1
+	    ;;
+	"--no-cleanup")
+	    cleanup=0
+	    ;;
+	"-o"|"--output")
+	    shift
+	    output=$1
+	    ;;
+	"--verbose"|"-v")
+	    verbose=1
+	    ;;
+        *)
+	    if [ "$1" = "" ]; then
+		echo "Empty string not allowed as argument"
+		usage
+		exit 1
+	    elif [ "$old" = "" ]; then
+		old=$1
+	    elif [ "$new" = "" ]; then
+		new=$1
+	    else
+		echo "Bad argument $1"
+		usage
+		exit 1
+	    fi
+            ;;
+    esac
+    shift
+done
+
+if [ "$new" = "" ]; then
+    new=HEAD
+fi
+
+if [ "$old" = "" ]; then
+    echo "fatal: Please, provide at least one revision to diff with."
+    usage
+    exit 1
+fi
+
+if [ "$main" = "" ]; then
+    printf "%s" "No --main provided, trying to guess ... "
+    main=$(git grep -l '^[ \t]*\\documentclass')
+    # May return multiple results, but if so the result won't be a file.
+    if [ -r "$main" ]; then
+	echo "Using $main as the main file."
+    else
+	if [ "$main" = "" ]; then
+	    echo "No candidate for main file."
+	else
+	    echo "Multiple candidates for main file:"
+	    printf "%s\n" "$main" | sed 's/^/\t/'
+	fi
+	die "Please, provide a main file with --main FILE.tex."
+    fi
+fi
+
+if [ ! -r "$main" ]; then
+    die "Cannot read $main."
+fi
+
+verbose "Creating temporary directories"
+
+git_prefix=$(git rev-parse --show-prefix)
+cd "$(git rev-parse --show-cdup)" || die "Can't cd back to repository root"
+git_dir="$(git rev-parse --git-dir)" || die "Not a git repository?"
+git_dir=$(cd "$git_dir"; pwd)
+
+main=$git_prefix/$main
+
+tmpdir=$initial_dir/git-latexdiff.$$
+mkdir "$tmpdir" || die "Cannot create temporary directory."
+
+cd "$tmpdir" || die "Cannot cd to $tmpdir"
+
+mkdir old new diff || die "Cannot create old, new and diff directories."
+
+verbose_done
+verbose "Checking out old and new version"
+
+cd old || die "Cannot cd to old/"
+git --git-dir="$git_dir" --work-tree=. checkout "$old" -- . || die "checkout failed for old/"
+verbose_progress
+cd ../new || die "Cannot cd to new/"
+git --git-dir="$git_dir" --work-tree=. checkout "$new" -- . || die "checkout failed for new/"
+verbose_progress
+cd ..
+
+verbose_done
+verbose "Running latexdiff --flatten old/$main new/$main > $main"
+
+latexdiff --flatten old/"$main" new/"$main" > diff.tex || die "latexdiff failed"
+
+mv -f diff.tex new/"$main"
+
+verbose_done
+
+mainbase=$(basename "$main" .tex)
+maindir=$(dirname "$main")
+
+verbose "Compiling result"
+
+compile_error=0
+cd new/"$maindir" || die "Can't cd to new/$maindir"
+if [ -f Makefile ]; then
+    make || compile_error=1
+else
+    pdflatex --interaction errorstopmode "$mainbase" || compile_error=1
+fi
+
+verbose_done
+
+pdffile="$mainbase".pdf
+if [ ! -r "$pdffile" ]; then
+    echo "No PDF file generated."
+    compile_error=1
+fi
+
+if [ ! -s "$pdffile" ]; then
+    echo "PDF file generated is empty."
+    compile_error=1
+fi
+
+if [ "$compile_error" = "1" ]; then
+    echo "Error during compilation. Please examine and cleanup if needed:"
+    echo "Directory: $tmpdir/new/$maindir/"
+    echo "     File: $mainbase.tex"
+    # Don't clean up to let the user diagnose.
+    exit 1
+fi
+
+if [ "$output" != "" ]; then
+    abs_pdffile="$PWD/$pdffile"
+    (cd "$initial_dir" && cp "$abs_pdffile" "$output")
+    echo "Output written on $output"
+fi
+
+if [ "$view" = 1 ] || [ "$view" = maybe ] && [ "$output" = "" ]; then
+    xpdf "$pdffile"
+fi
+
+if [ "$cleanup" = 1 ]; then
+    verbose "Cleaning-up result"
+    rm -fr "$tmpdir"
+    verbose_done
+fi
-- 
1.7.9.111.gf3fb0.dirty
Tim Haga· Feb 15, 2012, 23:33 UTC · re: Matthieu Moy · lore

Re: [PATCH] git-latexdiff: new command in contrib, to use latexdiff and Git

While testing your script on my office machine i discovered that the following might be a problem:

> +if [ "$view" = 1 ] || [ "$view" = maybe ] && [ "$output" = "" ]; then
> +    xpdf "$pdffile"
> +fi

Xpdf is not installed on all machines (e.g. it's not installed on my office machine), so maybe it would be a good idea to use a environment variable instead?

Tim
Matthieu Moy· Feb 16, 2012, 08:34 UTC · re: Tim Haga · lore

Re: [PATCH] git-latexdiff: new command in contrib, to use latexdiff and Git

Tim Haga <timhaga@ebene6.org> writes:
Show 10 quoted lines
> While testing your script on my office machine i discovered that the
> following might be a problem:
>
>> +if [ "$view" = 1 ] || [ "$view" = maybe ] && [ "$output" = "" ]; then
>> +    xpdf "$pdffile"
>> +fi
>
> Xpdf is not installed on all machines (e.g. it's not installed on my
> office machine), so maybe it would be a good idea to use a environment
> variable instead?

Right. I'm squashing this into the next version to allow configuration (environment variable or --pdf-viewer) and sensible auto-detection:

Show changes to contrib/latex/git-latexdiff +30 −2
diff --git a/contrib/latex/git-latexdiff b/contrib/latex/git-latexdiff
index 13aeb9a..85aafda 100755
--- a/contrib/latex/git-latexdiff
+++ b/contrib/latex/git-latexdiff
@@ -20,6 +20,8 @@ Options:
 	--no-view	Don't display the resulting PDF file
 	--view		View the resulting PDF file
 			(default if -o is not used)
+	--pdf-viewer CMD
+			Use CMD to view the PDF file (default: \$PDFVIEWER)
 	--no-cleanup	Don't cleanup temp dir after running
 	-o FILE, --output FILE
 			Copy resulting PDF into FILE
@@ -46,7 +48,7 @@ verbose_progress () {
 
 verbose_done () {
     if [ "$verbose" = 1 ]; then
-	echo " done."
+	echo " ${1:-done}."
     fi
 }
 
@@ -75,6 +77,10 @@ while test $# -ne 0; do
 	"--view")
 	    view=1
 	    ;;
+	"--pdf-viewer")
+	    shift
+	    PDFVIEWER="$1"
+	    ;;
 	"--no-cleanup")
 	    cleanup=0
 	    ;;
@@ -114,6 +120,28 @@ if [ "$old" = "" ]; then
     exit 1
 fi
 
+verbose "Auto-detecting PDF viewer"
+for command in xdg-open evince okular xpdf acroread; do
+    if [ "$PDFVIEWER" = "" ]; then
+	if command -v "$command" >/dev/null 2>&1; then
+	    PDFVIEWER="$command"
+	else
+	    verbose_progress
+	fi
+    fi
+done
+verbose_done "$PDFVIEWER"
+
+case "$view" in
+    maybe|1)
+	if [ "$PDFVIEWER" = "" ]; then
+	    echo "warning: could not find a PDF viewer on your system."
+	    echo "warning: Please set \$PDFVIEWER or use --pdf-viewer CMD."
+	    PDFVIEWER=false
+	fi
+	;;
+esac
+
 if [ "$main" = "" ]; then
     printf "%s" "No --main provided, trying to guess ... "
     main=$(git grep -l '^[ \t]*\\documentclass')
@@ -212,7 +240,7 @@ if [ "$output" != "" ]; then
 fi
 
 if [ "$view" = 1 ] || [ "$view" = maybe ] && [ "$output" = "" ]; then
-    xpdf "$pdffile"
+    "$PDFVIEWER" "$pdffile"
 fi
 
 if [ "$cleanup" = 1 ]; then
-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Matthieu Moy· Feb 16, 2012, 08:39 UTC · re: Matthieu Moy · lore

[PATCH v2] git-latexdiff: new command in contrib, to use latexdiff and Git

git-latexdiff is a wrapper around latexdiff (http://www.ctan.org/pkg/latexdiff) that allows using it to diff two revisions of a LaTeX file.

git-latexdiff is made to work on documents split accross multiple .tex files (plus possibly figures and other non-diffable files), hence could not be implemented as a per-file diff driver.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
Changes since v1:
- Configuration/autodetection of PDF viewer
- Reformat output of -h to match what PARSE_OPTION does for other commands
 contrib/latex/Makefile      |   22 ++++
 contrib/latex/README        |   12 ++
 contrib/latex/git-latexdiff |  249 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 283 insertions(+), 0 deletions(-)
 create mode 100644 contrib/latex/Makefile
 create mode 100644 contrib/latex/README
 create mode 100755 contrib/latex/git-latexdiff
Show changes to 3 files +283 −0

contrib/latex/Makefile, contrib/latex/README, contrib/latex/git-latexdiff

diff --git a/contrib/latex/Makefile b/contrib/latex/Makefile
new file mode 100644
index 0000000..4617906
--- /dev/null
+++ b/contrib/latex/Makefile
@@ -0,0 +1,22 @@
+-include ../../config.mak
+-include ../../config.mak.autogen
+
+ifndef SHELL_PATH
+	SHELL_PATH = /bin/sh
+endif
+
+SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH))
+gitexecdir_SQ = $(subst ','\'',$(gitexecdir))
+
+SCRIPT=git-latexdiff
+
+.PHONY: install help
+help:
+	@echo 'This is the help target of the Makefile. Current configuration:'
+	@echo '  gitexecdir = $(gitexecdir_SQ)'
+	@echo '  SHELL_PATH = $(SHELL_PATH_SQ)'
+	@echo 'Run "$(MAKE) install" to install $(SCRIPT) in gitexecdir.'
+
+install:
+	sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' $(SCRIPT) > '$(gitexecdir_SQ)/$(SCRIPT)'
+	chmod 755 '$(gitexecdir)/$(SCRIPT)'
diff --git a/contrib/latex/README b/contrib/latex/README
new file mode 100644
index 0000000..2d7fdd6
--- /dev/null
+++ b/contrib/latex/README
@@ -0,0 +1,12 @@
+git-latexdiff is a wrapper around latexdiff
+(http://www.ctan.org/pkg/latexdiff) that allows using it to diff two
+revisions of a LaTeX file.
+
+The script internally checks out the full tree for the specified
+revisions, and calls latexdiff with the --flatten option, hence this
+works if the document is split into multiple .tex files.
+
+Try "git latexdiff -h" for more information.
+
+To install, either drop git-latexdiff in your $PATH, or run "make
+install".
diff --git a/contrib/latex/git-latexdiff b/contrib/latex/git-latexdiff
new file mode 100755
index 0000000..57c2237
--- /dev/null
+++ b/contrib/latex/git-latexdiff
@@ -0,0 +1,249 @@
+#! /bin/sh
+
+# Author: Matthieu Moy <Matthieu.Moy@imag.fr> (2012)
+
+# Missing features (patches welcome ;-) :
+# - diff the index or the current worktree 
+# - checkout only a subdirectory of the repo
+# - hardlink temporary checkouts as much as possible
+
+usage () {
+            cat << EOF
+Usage: $(basename $0) [options] OLD [NEW]
+Call latexdiff on two Git revisions of a file.
+
+OLD and NEW are Git revision identifiers. NEW defaults to HEAD.
+
+Options:
+    --help                this help message
+    --main <file.tex>     name of the main LaTeX file
+    --no-view             don't display the resulting PDF file
+    --view                view the resulting PDF file
+                            (default if -o is not used)
+    --pdf-viewer <cmd>    use <cmd> to view the PDF file (default: \$PDFVIEWER)
+    --no-cleanup          don't cleanup temp dir after running
+    -o <file>, --output <file>
+                          copy resulting PDF into <file>
+                             (usually ending with .pdf)
+EOF
+}
+
+die () {
+    echo "fatal: $@"
+    exit 1
+}
+
+verbose () {
+    if [ "$verbose" = 1 ]; then
+	printf "%s ..." "$@"
+    fi
+}
+
+verbose_progress () {
+    if [ "$verbose" = 1 ]; then
+	printf "." "$@"
+    fi
+}
+
+verbose_done () {
+    if [ "$verbose" = 1 ]; then
+	echo " ${1:-done}."
+    fi
+}
+
+old=
+new=
+main=
+view=maybe
+cleanup=1
+verbose=0
+output=
+initial_dir=$PWD
+
+while test $# -ne 0; do
+    case "$1" in
+        "--help"|"-h")
+            usage
+            exit 0
+            ;;
+	"--main")
+	    shift
+	    main=$1
+	    ;;
+	"--no-view")
+	    view=0
+	    ;;
+	"--view")
+	    view=1
+	    ;;
+	"--pdf-viewer")
+	    shift
+	    PDFVIEWER="$1"
+	    ;;
+	"--no-cleanup")
+	    cleanup=0
+	    ;;
+	"-o"|"--output")
+	    shift
+	    output=$1
+	    ;;
+	"--verbose"|"-v")
+	    verbose=1
+	    ;;
+        *)
+	    if [ "$1" = "" ]; then
+		echo "Empty string not allowed as argument"
+		usage
+		exit 1
+	    elif [ "$old" = "" ]; then
+		old=$1
+	    elif [ "$new" = "" ]; then
+		new=$1
+	    else
+		echo "Bad argument $1"
+		usage
+		exit 1
+	    fi
+            ;;
+    esac
+    shift
+done
+
+if [ "$new" = "" ]; then
+    new=HEAD
+fi
+
+if [ "$old" = "" ]; then
+    echo "fatal: Please, provide at least one revision to diff with."
+    usage
+    exit 1
+fi
+
+verbose "Auto-detecting PDF viewer"
+for command in xdg-open evince okular xpdf acroread; do
+    if [ "$PDFVIEWER" = "" ]; then
+	if command -v "$command" >/dev/null 2>&1; then
+	    PDFVIEWER="$command"
+	else
+	    verbose_progress
+	fi
+    fi
+done
+verbose_done "$PDFVIEWER"
+
+case "$view" in
+    maybe|1)
+	if [ "$PDFVIEWER" = "" ]; then
+	    echo "warning: could not find a PDF viewer on your system."
+	    echo "warning: Please set \$PDFVIEWER or use --pdf-viewer CMD."
+	    PDFVIEWER=false
+	fi
+	;;
+esac
+
+if [ "$main" = "" ]; then
+    printf "%s" "No --main provided, trying to guess ... "
+    main=$(git grep -l '^[ \t]*\\documentclass')
+    # May return multiple results, but if so the result won't be a file.
+    if [ -r "$main" ]; then
+	echo "Using $main as the main file."
+    else
+	if [ "$main" = "" ]; then
+	    echo "No candidate for main file."
+	else
+	    echo "Multiple candidates for main file:"
+	    printf "%s\n" "$main" | sed 's/^/\t/'
+	fi
+	die "Please, provide a main file with --main FILE.tex."
+    fi
+fi
+
+if [ ! -r "$main" ]; then
+    die "Cannot read $main."
+fi
+
+verbose "Creating temporary directories"
+
+git_prefix=$(git rev-parse --show-prefix)
+cd "$(git rev-parse --show-cdup)" || die "Can't cd back to repository root"
+git_dir="$(git rev-parse --git-dir)" || die "Not a git repository?"
+git_dir=$(cd "$git_dir"; pwd)
+
+main=$git_prefix/$main
+
+tmpdir=$initial_dir/git-latexdiff.$$
+mkdir "$tmpdir" || die "Cannot create temporary directory."
+
+cd "$tmpdir" || die "Cannot cd to $tmpdir"
+
+mkdir old new diff || die "Cannot create old, new and diff directories."
+
+verbose_done
+verbose "Checking out old and new version"
+
+cd old || die "Cannot cd to old/"
+git --git-dir="$git_dir" --work-tree=. checkout "$old" -- . || die "checkout failed for old/"
+verbose_progress
+cd ../new || die "Cannot cd to new/"
+git --git-dir="$git_dir" --work-tree=. checkout "$new" -- . || die "checkout failed for new/"
+verbose_progress
+cd ..
+
+verbose_done
+verbose "Running latexdiff --flatten old/$main new/$main > $main"
+
+latexdiff --flatten old/"$main" new/"$main" > diff.tex || die "latexdiff failed"
+
+mv -f diff.tex new/"$main"
+
+verbose_done
+
+mainbase=$(basename "$main" .tex)
+maindir=$(dirname "$main")
+
+verbose "Compiling result"
+
+compile_error=0
+cd new/"$maindir" || die "Can't cd to new/$maindir"
+if [ -f Makefile ]; then
+    make || compile_error=1
+else
+    pdflatex --interaction errorstopmode "$mainbase" || compile_error=1
+fi
+
+verbose_done
+
+pdffile="$mainbase".pdf
+if [ ! -r "$pdffile" ]; then
+    echo "No PDF file generated."
+    compile_error=1
+fi
+
+if [ ! -s "$pdffile" ]; then
+    echo "PDF file generated is empty."
+    compile_error=1
+fi
+
+if [ "$compile_error" = "1" ]; then
+    echo "Error during compilation. Please examine and cleanup if needed:"
+    echo "Directory: $tmpdir/new/$maindir/"
+    echo "     File: $mainbase.tex"
+    # Don't clean up to let the user diagnose.
+    exit 1
+fi
+
+if [ "$output" != "" ]; then
+    abs_pdffile="$PWD/$pdffile"
+    (cd "$initial_dir" && cp "$abs_pdffile" "$output")
+    echo "Output written on $output"
+fi
+
+if [ "$view" = 1 ] || [ "$view" = maybe ] && [ "$output" = "" ]; then
+    "$PDFVIEWER" "$pdffile"
+fi
+
+if [ "$cleanup" = 1 ]; then
+    verbose "Cleaning-up result"
+    rm -fr "$tmpdir"
+    verbose_done
+fi
-- 
1.7.9.111.gf3fb0.dirty
Jakub Narebski· Feb 16, 2012, 09:15 UTC · re: Matthieu Moy · lore

Re: [PATCH v2] git-latexdiff: new command in contrib, to use latexdiff and Git

Matthieu Moy <Matthieu.Moy@imag.fr> writes:
Show 11 quoted lines
> +verbose "Auto-detecting PDF viewer"
> +for command in xdg-open evince okular xpdf acroread; do
> +    if [ "$PDFVIEWER" = "" ]; then
> +	if command -v "$command" >/dev/null 2>&1; then
> +	    PDFVIEWER="$command"
> +	else
> +	    verbose_progress
> +	fi
> +    fi
> +done
> +verbose_done "$PDFVIEWER"

Why we autodetect PDF viewer unconditionally? Why we do not stop on first detected viewer rather than last? Why not

  +if [ "$PDFVIEWER" = "" ]; then
  +	verbose "Auto-detecting PDF viewer"
  +	for command in xdg-open evince okular xpdf acroread; do
  +		if command -v "$command" >/dev/null 2>&1; then
  +			PDFVIEWER=$command
  +			break
  +		else
  +			verbose_progress
  +		fi
  +	done
  +	verbose_done "$PDFVIEWER"
  +fi
Nb. Documentation/CodingGuidelines says:
  For shell scripts specifically (not exhaustive):
  
  [...]
  
   - We prefer "test" over "[ ... ]".
I know that 'contrib/' is more relaxed...
-- 
Jakub Narebski
David Aguilar· Feb 16, 2012, 19:24 UTC · re: Matthieu Moy · lore

Re: [PATCH v2] git-latexdiff: new command in contrib, to use latexdiff and Git

On Thu, Feb 16, 2012 at 12:39 AM, Matthieu Moy <Matthieu.Moy@imag.fr> wrote:
Show 6 quoted lines
> +
> +verbose () {
> +    if [ "$verbose" = 1 ]; then
> +       printf "%s ..." "$@"
> +    fi
> +}

In addition to preferring "test" over "[", we also prefer to write these on several lines. It's probably time to update the CodingStyle document with these notes.

e.g.
if test "$verbose" = 1
then
    .... do stuff
fi
(with hard-tabs, not spaces (unlike my example))
-- 
David
Steven Michalske· Feb 16, 2012, 08:47 UTC · re: Matthieu Moy · lore

Re: [PATCH] git-latexdiff: new command in contrib, to use latexdiff and Git

On Mac OS X use the open command and the OS will use the correct viewer chosen by the user
open "$pdffile"
On Feb 16, 2012, at 12:34 AM, Matthieu Moy wrote:
Show 95 quoted lines
> Tim Haga <timhaga@ebene6.org> writes:
> 
>> While testing your script on my office machine i discovered that the
>> following might be a problem:
>> 
>>> +if [ "$view" = 1 ] || [ "$view" = maybe ] && [ "$output" = "" ]; then
>>> +    xpdf "$pdffile"
>>> +fi
>> 
>> Xpdf is not installed on all machines (e.g. it's not installed on my
>> office machine), so maybe it would be a good idea to use a environment
>> variable instead?
> 
> Right. I'm squashing this into the next version to allow configuration
> (environment variable or --pdf-viewer) and sensible auto-detection:
> 
> diff --git a/contrib/latex/git-latexdiff b/contrib/latex/git-latexdiff
> index 13aeb9a..85aafda 100755
> --- a/contrib/latex/git-latexdiff
> +++ b/contrib/latex/git-latexdiff
> @@ -20,6 +20,8 @@ Options:
> 	--no-view	Don't display the resulting PDF file
> 	--view		View the resulting PDF file
> 			(default if -o is not used)
> +	--pdf-viewer CMD
> +			Use CMD to view the PDF file (default: \$PDFVIEWER)
> 	--no-cleanup	Don't cleanup temp dir after running
> 	-o FILE, --output FILE
> 			Copy resulting PDF into FILE
> @@ -46,7 +48,7 @@ verbose_progress () {
> 
> verbose_done () {
>     if [ "$verbose" = 1 ]; then
> -	echo " done."
> +	echo " ${1:-done}."
>     fi
> }
> 
> @@ -75,6 +77,10 @@ while test $# -ne 0; do
> 	"--view")
> 	    view=1
> 	    ;;
> +	"--pdf-viewer")
> +	    shift
> +	    PDFVIEWER="$1"
> +	    ;;
> 	"--no-cleanup")
> 	    cleanup=0
> 	    ;;
> @@ -114,6 +120,28 @@ if [ "$old" = "" ]; then
>     exit 1
> fi
> 
> +verbose "Auto-detecting PDF viewer"
> +for command in xdg-open evince okular xpdf acroread; do
> +    if [ "$PDFVIEWER" = "" ]; then
> +	if command -v "$command" >/dev/null 2>&1; then
> +	    PDFVIEWER="$command"
> +	else
> +	    verbose_progress
> +	fi
> +    fi
> +done
> +verbose_done "$PDFVIEWER"
> +
> +case "$view" in
> +    maybe|1)
> +	if [ "$PDFVIEWER" = "" ]; then
> +	    echo "warning: could not find a PDF viewer on your system."
> +	    echo "warning: Please set \$PDFVIEWER or use --pdf-viewer CMD."
> +	    PDFVIEWER=false
> +	fi
> +	;;
> +esac
> +
> if [ "$main" = "" ]; then
>     printf "%s" "No --main provided, trying to guess ... "
>     main=$(git grep -l '^[ \t]*\\documentclass')
> @@ -212,7 +240,7 @@ if [ "$output" != "" ]; then
> fi
> 
> if [ "$view" = 1 ] || [ "$view" = maybe ] && [ "$output" = "" ]; then
> -    xpdf "$pdffile"
> +    "$PDFVIEWER" "$pdffile"
> fi
> 
> if [ "$cleanup" = 1 ]; then
> 
> -- 
> Matthieu Moy
> http://www-verimag.imag.fr/~moy/
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
Matthieu Moy· Feb 16, 2012, 08:59 UTC · re: Steven Michalske · lore

Re: [PATCH] git-latexdiff: new command in contrib, to use latexdiff and Git

Steven Michalske <smichalske@gmail.com> writes:
> On Mac OS X use the open command and the OS will use the correct viewer chosen by the user
>
> open "$pdffile"

Unfortunately, open also exists on my system, but does not do that at all. I'm squashing this into the next version:

Show changes to contrib/latex/git-latexdiff +7 −1
--- a/contrib/latex/git-latexdiff
+++ b/contrib/latex/git-latexdiff
@@ -120,7 +120,13 @@ if [ "$old" = "" ]; then
 fi
 
 verbose "Auto-detecting PDF viewer"
-for command in xdg-open evince okular xpdf acroread; do
+candidates="xdg-open evince okular xpdf acroread"
+if [ "$(uname)" = Darwin ]; then
+    # open exists on GNU/Linux, but does not open PDFs
+    candidates="open $candidates"
+fi
+
+for command in $candidates; do
     if [ "$PDFVIEWER" = "" ]; then
        if command -v "$command" >/dev/null 2>&1; then
            PDFVIEWER="$command"
-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Matthieu Moy· Feb 16, 2012, 12:36 UTC · re: Matthieu Moy · lore

[PATCH v3] git-latexdiff: new command in contrib, to use latexdiff and Git

git-latexdiff is a wrapper around latexdiff (http://www.ctan.org/pkg/latexdiff) that allows using it to diff two revisions of a LaTeX file.

git-latexdiff is made to work on documents split accross multiple .tex files (plus possibly figures and other non-diffable files), hence could not be implemented as a per-file diff driver.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
Changes since v2:
- Try 'open' on MacOS to view PDF file.
- Shell style issues (thanks to Jakub)
 contrib/latex/Makefile      |   22 ++++
 contrib/latex/README        |   12 ++
 contrib/latex/git-latexdiff |  255 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 289 insertions(+), 0 deletions(-)
 create mode 100644 contrib/latex/Makefile
 create mode 100644 contrib/latex/README
 create mode 100755 contrib/latex/git-latexdiff
Show changes to 3 files +289 −0

contrib/latex/Makefile, contrib/latex/README, contrib/latex/git-latexdiff

diff --git a/contrib/latex/Makefile b/contrib/latex/Makefile
new file mode 100644
index 0000000..4617906
--- /dev/null
+++ b/contrib/latex/Makefile
@@ -0,0 +1,22 @@
+-include ../../config.mak
+-include ../../config.mak.autogen
+
+ifndef SHELL_PATH
+	SHELL_PATH = /bin/sh
+endif
+
+SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH))
+gitexecdir_SQ = $(subst ','\'',$(gitexecdir))
+
+SCRIPT=git-latexdiff
+
+.PHONY: install help
+help:
+	@echo 'This is the help target of the Makefile. Current configuration:'
+	@echo '  gitexecdir = $(gitexecdir_SQ)'
+	@echo '  SHELL_PATH = $(SHELL_PATH_SQ)'
+	@echo 'Run "$(MAKE) install" to install $(SCRIPT) in gitexecdir.'
+
+install:
+	sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' $(SCRIPT) > '$(gitexecdir_SQ)/$(SCRIPT)'
+	chmod 755 '$(gitexecdir)/$(SCRIPT)'
diff --git a/contrib/latex/README b/contrib/latex/README
new file mode 100644
index 0000000..2d7fdd6
--- /dev/null
+++ b/contrib/latex/README
@@ -0,0 +1,12 @@
+git-latexdiff is a wrapper around latexdiff
+(http://www.ctan.org/pkg/latexdiff) that allows using it to diff two
+revisions of a LaTeX file.
+
+The script internally checks out the full tree for the specified
+revisions, and calls latexdiff with the --flatten option, hence this
+works if the document is split into multiple .tex files.
+
+Try "git latexdiff -h" for more information.
+
+To install, either drop git-latexdiff in your $PATH, or run "make
+install".
diff --git a/contrib/latex/git-latexdiff b/contrib/latex/git-latexdiff
new file mode 100755
index 0000000..8466f52
--- /dev/null
+++ b/contrib/latex/git-latexdiff
@@ -0,0 +1,255 @@
+#! /bin/sh
+
+# Author: Matthieu Moy <Matthieu.Moy@imag.fr> (2012)
+
+# Missing features (patches welcome ;-) :
+# - diff the index or the current worktree 
+# - checkout only a subdirectory of the repo
+# - hardlink temporary checkouts as much as possible
+
+usage () {
+            cat << EOF
+Usage: $(basename $0) [options] OLD [NEW]
+Call latexdiff on two Git revisions of a file.
+
+OLD and NEW are Git revision identifiers. NEW defaults to HEAD.
+
+Options:
+    --help                this help message
+    --main <file.tex>     name of the main LaTeX file
+    --no-view             don't display the resulting PDF file
+    --view                view the resulting PDF file
+                            (default if -o is not used)
+    --pdf-viewer <cmd>    use <cmd> to view the PDF file (default: \$PDFVIEWER)
+    --no-cleanup          don't cleanup temp dir after running
+    -o <file>, --output <file>
+                          copy resulting PDF into <file>
+                             (usually ending with .pdf)
+EOF
+}
+
+die () {
+    echo "fatal: $@"
+    exit 1
+}
+
+verbose () {
+    if [ "$verbose" = 1 ]; then
+	printf "%s ..." "$@"
+    fi
+}
+
+verbose_progress () {
+    if [ "$verbose" = 1 ]; then
+	printf "." "$@"
+    fi
+}
+
+verbose_done () {
+    if [ "$verbose" = 1 ]; then
+	echo " ${1:-done}."
+    fi
+}
+
+old=
+new=
+main=
+view=maybe
+cleanup=1
+verbose=0
+output=
+initial_dir=$PWD
+
+while test $# -ne 0; do
+    case "$1" in
+        "--help"|"-h")
+            usage
+            exit 0
+            ;;
+	"--main")
+	    shift
+	    main=$1
+	    ;;
+	"--no-view")
+	    view=0
+	    ;;
+	"--view")
+	    view=1
+	    ;;
+	"--pdf-viewer")
+	    shift
+	    PDFVIEWER="$1"
+	    ;;
+	"--no-cleanup")
+	    cleanup=0
+	    ;;
+	"-o"|"--output")
+	    shift
+	    output=$1
+	    ;;
+	"--verbose"|"-v")
+	    verbose=1
+	    ;;
+        *)
+	    if [ "$1" = "" ]; then
+		echo "Empty string not allowed as argument"
+		usage
+		exit 1
+	    elif [ "$old" = "" ]; then
+		old=$1
+	    elif [ "$new" = "" ]; then
+		new=$1
+	    else
+		echo "Bad argument $1"
+		usage
+		exit 1
+	    fi
+            ;;
+    esac
+    shift
+done
+
+if [ "$new" = "" ]; then
+    new=HEAD
+fi
+
+if [ "$old" = "" ]; then
+    echo "fatal: Please, provide at least one revision to diff with."
+    usage
+    exit 1
+fi
+
+verbose "Auto-detecting PDF viewer"
+candidates="xdg-open evince okular xpdf acroread"
+if [ "$(uname)" = Darwin ]; then
+    # open exists on GNU/Linux, but does not open PDFs
+    candidates="open $candidates"
+fi
+
+for command in $candidates; do
+    if [ "$PDFVIEWER" = "" ]; then
+	if command -v "$command" >/dev/null 2>&1; then
+	    PDFVIEWER="$command"
+	else
+	    verbose_progress
+	fi
+    fi
+done
+verbose_done "$PDFVIEWER"
+
+case "$view" in
+    maybe|1)
+	if [ "$PDFVIEWER" = "" ]; then
+	    echo "warning: could not find a PDF viewer on your system."
+	    echo "warning: Please set \$PDFVIEWER or use --pdf-viewer CMD."
+	    PDFVIEWER=false
+	fi
+	;;
+esac
+
+if [ "$main" = "" ]; then
+    printf "%s" "No --main provided, trying to guess ... "
+    main=$(git grep -l '^[ \t]*\\documentclass')
+    # May return multiple results, but if so the result won't be a file.
+    if [ -r "$main" ]; then
+	echo "Using $main as the main file."
+    else
+	if [ "$main" = "" ]; then
+	    echo "No candidate for main file."
+	else
+	    echo "Multiple candidates for main file:"
+	    printf "%s\n" "$main" | sed 's/^/\t/'
+	fi
+	die "Please, provide a main file with --main FILE.tex."
+    fi
+fi
+
+if [ ! -r "$main" ]; then
+    die "Cannot read $main."
+fi
+
+verbose "Creating temporary directories"
+
+git_prefix=$(git rev-parse --show-prefix)
+cd "$(git rev-parse --show-cdup)" || die "Can't cd back to repository root"
+git_dir="$(git rev-parse --git-dir)" || die "Not a git repository?"
+git_dir=$(cd "$git_dir"; pwd)
+
+main=$git_prefix/$main
+
+tmpdir=$initial_dir/git-latexdiff.$$
+mkdir "$tmpdir" || die "Cannot create temporary directory."
+
+cd "$tmpdir" || die "Cannot cd to $tmpdir"
+
+mkdir old new diff || die "Cannot create old, new and diff directories."
+
+verbose_done
+verbose "Checking out old and new version"
+
+cd old || die "Cannot cd to old/"
+git --git-dir="$git_dir" --work-tree=. checkout "$old" -- . || die "checkout failed for old/"
+verbose_progress
+cd ../new || die "Cannot cd to new/"
+git --git-dir="$git_dir" --work-tree=. checkout "$new" -- . || die "checkout failed for new/"
+verbose_progress
+cd ..
+
+verbose_done
+verbose "Running latexdiff --flatten old/$main new/$main > $main"
+
+latexdiff --flatten old/"$main" new/"$main" > diff.tex || die "latexdiff failed"
+
+mv -f diff.tex new/"$main"
+
+verbose_done
+
+mainbase=$(basename "$main" .tex)
+maindir=$(dirname "$main")
+
+verbose "Compiling result"
+
+compile_error=0
+cd new/"$maindir" || die "Can't cd to new/$maindir"
+if [ -f Makefile ]; then
+    make || compile_error=1
+else
+    pdflatex --interaction errorstopmode "$mainbase" || compile_error=1
+fi
+
+verbose_done
+
+pdffile="$mainbase".pdf
+if [ ! -r "$pdffile" ]; then
+    echo "No PDF file generated."
+    compile_error=1
+fi
+
+if [ ! -s "$pdffile" ]; then
+    echo "PDF file generated is empty."
+    compile_error=1
+fi
+
+if [ "$compile_error" = "1" ]; then
+    echo "Error during compilation. Please examine and cleanup if needed:"
+    echo "Directory: $tmpdir/new/$maindir/"
+    echo "     File: $mainbase.tex"
+    # Don't clean up to let the user diagnose.
+    exit 1
+fi
+
+if [ "$output" != "" ]; then
+    abs_pdffile="$PWD/$pdffile"
+    (cd "$initial_dir" && cp "$abs_pdffile" "$output")
+    echo "Output written on $output"
+fi
+
+if [ "$view" = 1 ] || [ "$view" = maybe ] && [ "$output" = "" ]; then
+    "$PDFVIEWER" "$pdffile"
+fi
+
+if [ "$cleanup" = 1 ]; then
+    verbose "Cleaning-up result"
+    rm -fr "$tmpdir"
+    verbose_done
+fi
-- 
1.7.9.111.gf3fb0.dirty
Jakub Narebski· Feb 16, 2012, 13:40 UTC · re: Matthieu Moy · lore

Re: [PATCH v3] git-latexdiff: new command in contrib, to use latexdiff and Git

Matthieu Moy <Matthieu.Moy@imag.fr> writes:
> Changes since v2:
> 
[...]
> - Shell style issues (thanks to Jakub)
[...]
Show 17 quoted lines
> +verbose "Auto-detecting PDF viewer"
> +candidates="xdg-open evince okular xpdf acroread"
> +if [ "$(uname)" = Darwin ]; then
> +    # open exists on GNU/Linux, but does not open PDFs
> +    candidates="open $candidates"
> +fi
> +
> +for command in $candidates; do
> +    if [ "$PDFVIEWER" = "" ]; then
> +	if command -v "$command" >/dev/null 2>&1; then
> +	    PDFVIEWER="$command"
> +	else
> +	    verbose_progress
> +	fi
> +    fi
> +done
> +verbose_done "$PDFVIEWER"

Eh? I don't see shell style issues fixed (loop inside conditional instead of vice-versa, "test ..." instead of "[ ... ]").

Nb. I think it would be good to put detecting PDF viewer in its own function, don't you?

-- 
Jakub Narebski
Matthieu Moy· Feb 16, 2012, 14:08 UTC · re: Jakub Narebski · lore

[PATCH v4] git-latexdiff: new command in contrib, to use latexdiff and Git

git-latexdiff is a wrapper around latexdiff (http://www.ctan.org/pkg/latexdiff) that allows using it to diff two revisions of a LaTeX file.

git-latexdiff is made to work on documents split accross multiple .tex files (plus possibly figures and other non-diffable files), hence could not be implemented as a per-file diff driver.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
Sorry, I forgot to commit before sending v3, so it was obviously wrong.
This one should contain what I promised in v2, i.e:
- Try 'open' on MacOS to view PDF file.
- Shell style issues (thanks to Jakub)

Jakub, I also forgot to send this before the patch (I got interrupted, and it seems my mental context-switch implementation loses data ;-) ):

> Why we autodetect PDF viewer unconditionally?

We don't really do that: the test is inside a test on $PDFVIEWER, so it's essentially the same. I just wrote it this way because I thought "break" wasn't POSIX, but it is actually, so I'm taking your version.

 contrib/latex/Makefile      |   22 ++++
 contrib/latex/README        |   12 ++
 contrib/latex/git-latexdiff |  256 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 290 insertions(+), 0 deletions(-)
 create mode 100644 contrib/latex/Makefile
 create mode 100644 contrib/latex/README
 create mode 100755 contrib/latex/git-latexdiff
Show changes to 3 files +290 −0

contrib/latex/Makefile, contrib/latex/README, contrib/latex/git-latexdiff

diff --git a/contrib/latex/Makefile b/contrib/latex/Makefile
new file mode 100644
index 0000000..4617906
--- /dev/null
+++ b/contrib/latex/Makefile
@@ -0,0 +1,22 @@
+-include ../../config.mak
+-include ../../config.mak.autogen
+
+ifndef SHELL_PATH
+	SHELL_PATH = /bin/sh
+endif
+
+SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH))
+gitexecdir_SQ = $(subst ','\'',$(gitexecdir))
+
+SCRIPT=git-latexdiff
+
+.PHONY: install help
+help:
+	@echo 'This is the help target of the Makefile. Current configuration:'
+	@echo '  gitexecdir = $(gitexecdir_SQ)'
+	@echo '  SHELL_PATH = $(SHELL_PATH_SQ)'
+	@echo 'Run "$(MAKE) install" to install $(SCRIPT) in gitexecdir.'
+
+install:
+	sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' $(SCRIPT) > '$(gitexecdir_SQ)/$(SCRIPT)'
+	chmod 755 '$(gitexecdir)/$(SCRIPT)'
diff --git a/contrib/latex/README b/contrib/latex/README
new file mode 100644
index 0000000..2d7fdd6
--- /dev/null
+++ b/contrib/latex/README
@@ -0,0 +1,12 @@
+git-latexdiff is a wrapper around latexdiff
+(http://www.ctan.org/pkg/latexdiff) that allows using it to diff two
+revisions of a LaTeX file.
+
+The script internally checks out the full tree for the specified
+revisions, and calls latexdiff with the --flatten option, hence this
+works if the document is split into multiple .tex files.
+
+Try "git latexdiff -h" for more information.
+
+To install, either drop git-latexdiff in your $PATH, or run "make
+install".
diff --git a/contrib/latex/git-latexdiff b/contrib/latex/git-latexdiff
new file mode 100755
index 0000000..19b9783
--- /dev/null
+++ b/contrib/latex/git-latexdiff
@@ -0,0 +1,256 @@
+#! /bin/sh
+
+# Author: Matthieu Moy <Matthieu.Moy@imag.fr> (2012)
+
+# Missing features (patches welcome ;-) :
+# - diff the index or the current worktree 
+# - checkout only a subdirectory of the repo
+# - hardlink temporary checkouts as much as possible
+
+usage () {
+            cat << EOF
+Usage: $(basename $0) [options] OLD [NEW]
+Call latexdiff on two Git revisions of a file.
+
+OLD and NEW are Git revision identifiers. NEW defaults to HEAD.
+
+Options:
+    --help                this help message
+    --main <file.tex>     name of the main LaTeX file
+    --no-view             don't display the resulting PDF file
+    --view                view the resulting PDF file
+                            (default if -o is not used)
+    --pdf-viewer <cmd>    use <cmd> to view the PDF file (default: \$PDFVIEWER)
+    --no-cleanup          don't cleanup temp dir after running
+    -o <file>, --output <file>
+                          copy resulting PDF into <file>
+                             (usually ending with .pdf)
+EOF
+}
+
+die () {
+    echo "fatal: $@"
+    exit 1
+}
+
+verbose () {
+    if test "$verbose" = 1 ; then
+	printf "%s ..." "$@"
+    fi
+}
+
+verbose_progress () {
+    if test "$verbose" = 1 ; then
+	printf "." "$@"
+    fi
+}
+
+verbose_done () {
+    if test "$verbose" = 1 ; then
+	echo " ${1:-done}."
+    fi
+}
+
+old=
+new=
+main=
+view=maybe
+cleanup=1
+verbose=0
+output=
+initial_dir=$PWD
+
+while test $# -ne 0; do
+    case "$1" in
+        "--help"|"-h")
+            usage
+            exit 0
+            ;;
+	"--main")
+	    test $# -gt 1 && shift || die "missing argument for $1"
+	    main=$1
+	    ;;
+	"--no-view")
+	    view=0
+	    ;;
+	"--view")
+	    view=1
+	    ;;
+	"--pdf-viewer")
+	    test $# -gt 1 && shift || die "missing argument for $1"
+	    PDFVIEWER="$1"
+	    ;;
+	"--no-cleanup")
+	    cleanup=0
+	    ;;
+	"-o"|"--output")
+	    test $# -gt 1 && shift || die "missing argument for $1"
+	    output=$1
+	    ;;
+	"--verbose"|"-v")
+	    verbose=1
+	    ;;
+        *)
+	    if test -z "$1" ; then
+		echo "Empty string not allowed as argument"
+		usage
+		exit 1
+	    elif test -z "$old" ; then
+		old=$1
+	    elif test -z "$new" ; then
+		new=$1
+	    else
+		echo "Bad argument $1"
+		usage
+		exit 1
+	    fi
+            ;;
+    esac
+    shift
+done
+
+if test -z "$new" ; then
+    new=HEAD
+fi
+
+if test -z "$old" ; then
+    echo "fatal: Please, provide at least one revision to diff with."
+    usage
+    exit 1
+fi
+
+if test -z "$PDFVIEWER" ; then
+    verbose "Auto-detecting PDF viewer"
+    candidates="xdg-open evince okular xpdf acroread"
+    if test "$(uname)" = Darwin ; then
+        # open exists on GNU/Linux, but does not open PDFs
+	candidates="open $candidates"
+    fi
+    
+    for command in $candidates; do
+	if command -v "$command" >/dev/null 2>&1; then
+	    PDFVIEWER="$command"
+	    break
+	else
+	    verbose_progress
+	fi
+    done
+    verbose_done "$PDFVIEWER"
+fi
+
+case "$view" in
+    maybe|1)
+	if test -z "$PDFVIEWER" ; then
+	    echo "warning: could not find a PDF viewer on your system."
+	    echo "warning: Please set \$PDFVIEWER or use --pdf-viewer CMD."
+	    PDFVIEWER=false
+	fi
+	;;
+esac
+
+if test -z "$main" ; then
+    printf "%s" "No --main provided, trying to guess ... "
+    main=$(git grep -l '^[ \t]*\\documentclass')
+    # May return multiple results, but if so the result won't be a file.
+    if test -r "$main" ; then
+	echo "Using $main as the main file."
+    else
+	if test -z "$main" ; then
+	    echo "No candidate for main file."
+	else
+	    echo "Multiple candidates for main file:"
+	    printf "%s\n" "$main" | sed 's/^/\t/'
+	fi
+	die "Please, provide a main file with --main FILE.tex."
+    fi
+fi
+
+if test ! -r "$main" ; then
+    die "Cannot read $main."
+fi
+
+verbose "Creating temporary directories"
+
+git_prefix=$(git rev-parse --show-prefix)
+cd "$(git rev-parse --show-cdup)" || die "Can't cd back to repository root"
+git_dir="$(git rev-parse --git-dir)" || die "Not a git repository?"
+git_dir=$(cd "$git_dir"; pwd)
+
+main=$git_prefix/$main
+
+tmpdir=$initial_dir/git-latexdiff.$$
+mkdir "$tmpdir" || die "Cannot create temporary directory."
+
+cd "$tmpdir" || die "Cannot cd to $tmpdir"
+
+mkdir old new diff || die "Cannot create old, new and diff directories."
+
+verbose_done
+verbose "Checking out old and new version"
+
+cd old || die "Cannot cd to old/"
+git --git-dir="$git_dir" --work-tree=. checkout "$old" -- . || die "checkout failed for old/"
+verbose_progress
+cd ../new || die "Cannot cd to new/"
+git --git-dir="$git_dir" --work-tree=. checkout "$new" -- . || die "checkout failed for new/"
+verbose_progress
+cd ..
+
+verbose_done
+verbose "Running latexdiff --flatten old/$main new/$main > $main"
+
+latexdiff --flatten old/"$main" new/"$main" > diff.tex || die "latexdiff failed"
+
+mv -f diff.tex new/"$main"
+
+verbose_done
+
+mainbase=$(basename "$main" .tex)
+maindir=$(dirname "$main")
+
+verbose "Compiling result"
+
+compile_error=0
+cd new/"$maindir" || die "Can't cd to new/$maindir"
+if test -f Makefile ; then
+    make || compile_error=1
+else
+    pdflatex --interaction errorstopmode "$mainbase" || compile_error=1
+fi
+
+verbose_done
+
+pdffile="$mainbase".pdf
+if test ! -r "$pdffile" ; then
+    echo "No PDF file generated."
+    compile_error=1
+fi
+
+if test ! -s "$pdffile" ; then
+    echo "PDF file generated is empty."
+    compile_error=1
+fi
+
+if test "$compile_error" = "1" ; then
+    echo "Error during compilation. Please examine and cleanup if needed:"
+    echo "Directory: $tmpdir/new/$maindir/"
+    echo "     File: $mainbase.tex"
+    # Don't clean up to let the user diagnose.
+    exit 1
+fi
+
+if test -n "$output" ; then
+    abs_pdffile="$PWD/$pdffile"
+    (cd "$initial_dir" && cp "$abs_pdffile" "$output")
+    echo "Output written on $output"
+fi
+
+if test "$view" = 1  || test "$view" = maybe  && test -z "$output" ; then
+    "$PDFVIEWER" "$pdffile"
+fi
+
+if test "$cleanup" = 1 ; then
+    verbose "Cleaning-up result"
+    rm -fr "$tmpdir"
+    verbose_done
+fi
-- 
1.7.9.111.gf3fb0.dirty
Matthieu Moy· Feb 16, 2012, 14:15 UTC · re: Jakub Narebski · lore

Re: [PATCH v3] git-latexdiff: new command in contrib, to use latexdiff and Git

Jakub Narebski <jnareb@gmail.com> writes:
> Nb. I think it would be good to put detecting PDF viewer in its own
> function, don't you?

I normally like functions, but for such a linear and small piece of code, I think it adds more confusion that clarity to write

do_fo () {
   actual stuff
}
do_foo
than the actual stuff alone.
-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Junio C Hamano· Feb 16, 2012, 20:10 UTC · re: Matthieu Moy · lore

Re: [PATCH] git-latexdiff: new command in contrib, to use latexdiff and Git

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
Show 12 quoted lines
> Tim Haga <timhaga@ebene6.org> writes:
>
>> While testing your script on my office machine i discovered that the
>> following might be a problem:
>>
>>> +if [ "$view" = 1 ] || [ "$view" = maybe ] && [ "$output" = "" ]; then
>>> +    xpdf "$pdffile"
>>> +fi
>>
>> Xpdf is not installed on all machines (e.g. it's not installed on my
>> office machine), so maybe it would be a good idea to use a environment
>> variable instead?

Honestly speaking, this is looking more like an "useful application for latex users who happen to use git to store their document source", and not a "useful addition for all git users", to me.

These two viewpoint suggests completely different evolution path for this program. Imagining what the first major new enhancement intended for people outside the original audience <git,latex> will be, I have this suspicion that "this new version will help people who have their documents stored in Mercurial" would be much more realistic (and the end result being useful) than "this new version will help git users who do not write their documents in latex but in asciidoc".

For that reason, I suspect that in the longer term, the tool will benefit more if I do not take this patch and the tool lives standalone.

Junio C Hamano· Feb 16, 2012, 21:04 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-latexdiff: new command in contrib, to use latexdiff and Git

Junio C Hamano <gitster@pobox.com> writes:
> Honestly speaking, this is looking more like an "useful application for
> latex users who happen to use git to store their document source", and not
> a "useful addition for all git users", to me.
Sorry, un-proofread draft escaped.

Please replace "for all git users" with "to git to help users who happen to have latex documents in their repositories."

Matthieu Moy· Feb 17, 2012, 08:10 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-latexdiff: new command in contrib, to use latexdiff and Git

Junio C Hamano <gitster@pobox.com> writes:
Show 11 quoted lines
> Honestly speaking, this is looking more like an "useful application for
> latex users who happen to use git to store their document source", and not
> a "useful addition for all git users", to me.
>
> These two viewpoint suggests completely different evolution path for this
> program.  Imagining what the first major new enhancement intended for
> people outside the original audience <git,latex> will be, I have this
> suspicion that "this new version will help people who have their documents
> stored in Mercurial" would be much more realistic (and the end result
> being useful) than "this new version will help git users who do not write
> their documents in latex but in asciidoc".

I agree that the next step may be to allow users of <whatever SCM outside Git>, but I don't think the way to do that would be to make the script generic. The script is a quick hack, and all the "clever" parts of it are calls to Git. If someone were to adapt this for Mercurial or Bzr, writting a python plugin would be a much better way to go (Mercurial already has "hg extdiff" doing the hardlinked checkouts for example, and both would allow better command-line option parsing than my "case $1 in ... esac").

I normally like code reuse very much, but trying to make a 250 lines long script generic enough to accept multiple SCMs would be more work than a rewrite.

OTOH, having this script in contrib/ has several advantages over maintaining it as a separate one-file project:

- "make install" uses Git's Makefile configuration, so it's easy to
  install.
- It makes it natural to use this mailing list for discussion. The
  script has already improved a lot since I posted it as a patch here.
-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Junio C Hamano· Feb 17, 2012, 13:31 UTC · re: Matthieu Moy · lore

Re: [PATCH] git-latexdiff: new command in contrib, to use latexdiff and Git

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
> I agree that the next step may be to allow users of <whatever SCM
> outside Git>, but I don't think the way to do that would be to make the
> script generic. The script is a quick hack, and all the "clever" parts
> of it are calls to Git.

You are not suggesting me to take and carry any future request that wants to add any quick hack that is heavily specific to Git and not portable to other SCMs to the contrib/ area only because they depend on Git, are you?

That would bloat the contrib/ area with stuff that do not belong there and we need to draw a line somewhere. The criteria I use to draw it is by answering "is this an application that merely happens to use git, or is it a way to help people who use Git?" question.

Look at what we have in the contrib/ area. I think what is common among them is that their primary benefit is to enrich user's Git experience. Bash completion for example is dependent on bash and it may be useless for Csh users, but if you are a bash user, your Git experience will be infinitely better with it regardless of what kind of payload you are tracking in your Git repository. And in my mind, "regardless of what you are tracking" is the key part that defines "the enhancement is about user's Git experience".

Matthieu Moy· Feb 17, 2012, 14:19 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-latexdiff: new command in contrib, to use latexdiff and Git

Junio C Hamano <gitster@pobox.com> writes:
Show 11 quoted lines
> Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
>
>> I agree that the next step may be to allow users of <whatever SCM
>> outside Git>, but I don't think the way to do that would be to make the
>> script generic. The script is a quick hack, and all the "clever" parts
>> of it are calls to Git.
>
> You are not suggesting me to take and carry any future request that wants
> to add any quick hack that is heavily specific to Git and not portable to
> other SCMs to the contrib/ area only because they depend on Git, are
> you?
I'm answering the remark you made:
| I have this suspicion that "this new version will help people who have
| their documents stored in Mercurial" would be much more realistic (and
| the end result being useful) than "this new version will help git users
| who do not write their documents in latex but in asciidoc".

I think the probability that a next version of git-latexdiff is to support another SCM is 0, and I tried to explain that.

Do you think I failed to address this remark?
> Look at what we have in the contrib/ area.  I think what is common among
> them is that their primary benefit is to enrich user's Git experience.

... and many of them is to enrich the user experience using Git with another tool (shell, text editor, foreign VCS).

Without git-latexdiff, you can run "git diff" on LaTeX documents, while with it, you can get a better view of the diff. To me, this is "enrich user's experience" of users running "git diff".

Git's _core_ already has some code to show diff hunks for various languages, and I don't think anyone would want to move these out because they only benefit people tracking files in these languages.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Junio C Hamano· Feb 17, 2012, 17:25 UTC · re: Matthieu Moy · lore

Re: [PATCH] git-latexdiff: new command in contrib, to use latexdiff and Git

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
Show 5 quoted lines
>> Look at what we have in the contrib/ area.  I think what is common among
>> them is that their primary benefit is to enrich user's Git experience.
>
> ... and many of them is to enrich the user experience using Git with
> another tool (shell, text editor, foreign VCS).

... where the amount of the benefit they get does not change regardless of the payload. That is what makes them tool in Git users' toolbox, not shell scripters' or p4 users' toolbox.

> Git's _core_ already has some code to show diff hunks for various
> languages,...

I would have to say that it is an oranges vs squirrels comparison. I do not see a justification to reject an addition of an entry to an array that adds a few strings of regexp to drive the mechanism that is already in core, when it gets compiled in, and it is useless by itself outside Git. Unless the language is something obscure, that is.

Read git-latexdiff that is a free-standing 200+ line script again yourself. The use of Git in the script is not more than how you would emulate what you would use "cp -r" in order to populate the old/ and new/ directories if the two versions were stored in the file system outside Git. If you rip the part that deals with "the two versions happen to be stored in Git" out, the remainder deals with parsing the command line, interfacing with latex and reporting the result, none of which is in any way Git specific. That is much larger part of the script, which I view as a clear sign that it is an application to serve the need for LaTeX users better, which happens to be written for the subset of LaTeX users who have their contents in Git.

Aren't there LaTeX tools archives that would be a much better home for this tool?

Jakub Narebski· Feb 17, 2012, 18:40 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-latexdiff: new command in contrib, to use latexdiff and Git

Junio C Hamano <gitster@pobox.com> writes:
> Aren't there LaTeX tools archives that would be a much better home for
> this tool?

There is CTAN: Comprehensive TeX Archive Network (http://ctan.org), which hosts tools such like latexmk (cousin of the general make utility), autolatex (generates Makefile), chktex, ite (interactive TeX editor),... latexdiff itself is also there.

-- 
Jakub Narebski
Matthieu Moy· Feb 20, 2012, 08:50 UTC · re: Jakub Narebski · lore

Re: [PATCH] git-latexdiff: new command in contrib, to use latexdiff and Git

Jakub Narebski <jnareb@gmail.com> writes:
Show 9 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
>
>> Aren't there LaTeX tools archives that would be a much better home for
>> this tool?
>
> There is CTAN: Comprehensive TeX Archive Network (http://ctan.org),
> which hosts tools such like latexmk (cousin of the general make
> utility), autolatex (generates Makefile), chktex, ite (interactive TeX
> editor),... latexdiff itself is also there.

It's not really the same thing: AFAIK, ctan is a good place to distribute things, but not to develop them (i.e. I don't see revision control or developpers mailing-lists there, which were the two motivations for pushing git-latexdiff in git.git).

Anyway, since no one spoke up to defend my position, I'll have to find another place to host it.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Junio C Hamano· Feb 20, 2012, 09:05 UTC · re: Matthieu Moy · lore

Re: [PATCH] git-latexdiff: new command in contrib, to use latexdiff and Git

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
Show 11 quoted lines
> Jakub Narebski <jnareb@gmail.com> writes:
>
>> There is CTAN: Comprehensive TeX Archive Network (http://ctan.org),
>> which hosts tools such like latexmk (cousin of the general make
>> utility), autolatex (generates Makefile), chktex, ite (interactive TeX
>> editor),... latexdiff itself is also there.
>
> It's not really the same thing: AFAIK, ctan is a good place to
> distribute things, but not to develop them (i.e. I don't see revision
> control or developpers mailing-lists there, which were the two
> motivations for pushing git-latexdiff in git.git).
Oh, don't get me wrong.

It's just that I agree with Jakub that a better final destination for it is CTAN next to latexdiff, not in git.git/contrib/, and when we last did something similar for emacs vc mode support, the copy I carried ended up becoming way stale than its final destination, and we had to remove our copy in order to reduce confusion. I just do not want to make the same mistake here.

I didn't intend to discourage discusson of your application here.

Carry on the discussion in a forum where you think the most people with appropriate skills and interests hang out, and if that forum is this list, that is perfectly fine. Host a development repository at GitHub, Gitorious or somesuch, if it would help people stay in sync with the current status to have a public repository.

I just do not think my tree is a good place to use as a "while we are developing" staging area.

Matthieu Moy· Feb 20, 2012, 12:00 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-latexdiff: new command in contrib, to use latexdiff and Git

Junio C Hamano <gitster@pobox.com> writes:
Show 5 quoted lines
> Carry on the discussion in a forum where you think the most people with
> appropriate skills and interests hang out, and if that forum is this list,
> that is perfectly fine. Host a development repository at GitHub, Gitorious
> or somesuch, if it would help people stay in sync with the current status
> to have a public repository.
The script is now available here:
  https://gitorious.org/git-latexdiff/git-latexdiff/trees/master

In the meantime, I discovered another script doing essentially the same : latexdiff-git

  https://github.com/cawka/latexdiff/blob/master/latexdiff-git

My feeling is that the development of latexdiff is stalled (the author's email included in the script bounces ...), and there are a few unofficial forks, more or less stalled too.

When I have time, I may try to add the features of my script to latexdiff-git (written in perl, while latexdiff is already in perl, so the choice of language is probably better). But that's most likely not going to happen soon.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

← back to recent threads