jgit.sh: Implement pager support

If the command is either `diff` or `log`, there is often a lot of
lines of output. Run these commands through $GIT_PAGER, $PAGER, or
`less` in order to make it easier to browse the output on a terminal.

Change-Id: I18b87ea4acf404b94788f2ac2101812bd13e6a0f
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
diff --git a/org.eclipse.jgit.pgm/jgit.sh b/org.eclipse.jgit.pgm/jgit.sh
index e6a2119..9ff59d6 100644
--- a/org.eclipse.jgit.pgm/jgit.sh
+++ b/org.eclipse.jgit.pgm/jgit.sh
@@ -41,6 +41,20 @@
 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
+cmd=
+for a in "$@"
+do
+	case "$a" in
+	-*) continue ;;
+	*)  cmd=$a; break; ;;
+	esac
+done
+
+use_pager=
+case "$cmd" in
+diff)     use_pager=1 ;;
+log)      use_pager=1 ;;
+esac
 
 this_script=`which "$0" 2>/dev/null`
 [ $? -gt 0 -a -f "$0" ] && this_script="$0"
@@ -58,7 +72,7 @@
 	cp=`cygpath --windows --mixed --path "$cp"`
 	;;
 Darwin)
-	if test -e /System/Library/Frameworks/JavaVM.framework
+	if [ -e /System/Library/Frameworks/JavaVM.framework ]
 	then
 		java_args='
 			-Dcom.apple.mrj.application.apple.menu.about.name=JGit
@@ -74,10 +88,25 @@
 export CLASSPATH
 
 java=java
-if test -n "$JAVA_HOME"
+if [ -n "$JAVA_HOME" ]
 then
 	java="$JAVA_HOME/bin/java"
 fi
 
-exec "$java" $java_args org.eclipse.jgit.pgm.Main "$@"
-exit 1
+if [ -n "$use_pager" ]
+then
+	use_pager=${GIT_PAGER:-${PAGER:-less}}
+	[ cat = "$use_pager" ] && use_pager=
+fi
+
+if [ -n "$use_pager" ]
+then
+	LESS=${LESS:-FSRX}
+	export LESS
+
+	"$java" $java_args org.eclipse.jgit.pgm.Main "$@" | $use_pager
+	exit
+else
+  exec "$java" $java_args org.eclipse.jgit.pgm.Main "$@"
+  exit 1
+fi