#! /bin/sh # patchview, Copyright (C) 2005 Randy Dunlap # License: GPL v2. # # uses patchutils (lsdiff) and tkdiff PROG=patchview VERSION=002 # usage: help message and exit function usage() { echo "usage: $PROG [-f] patchfile srctree {ver. $VERSION}" echo " -f : force tkdiff even if 'patch' has errors" echo " -s : single tkdiff even if patchfile contains multiple files" exit 1 } # strip_filename: # input: filename being patched # returns 'base', which is directory level(s) (path) without filename function strip_filename() { fn=$1 len=${#fn} fx=$((len - 1)) base="" while [ 1 -eq 1 ]; do if [ ${fn:$fx:1} == "/" ]; then ##fx=$((fx - 1)) base=${fn:0:$fx} break fi fx=$((fx - 1)) if [ $fx -eq 0 ]; then break fi done } force=0 single=0 VIEWER="tkdiff" # or maybe "sh -c colordiff" would work while [ -n "$1" ] do case $1 in -f) force=1 ;; -s) single=1 ;; -*) if [ "${1#-}" = '?' ]; then usage fi ;; *) # Accept filename or report a warning if [ -z "${patchfile}" ]; then patchfile=$1 srctree=$2 break else usage fi ;; esac # Shift argument 2 into argument 1's slot. Loop to check the argument. shift done if [ "$patchfile" = "" -o "$srctree" = "" ]; then usage fi # setup temp working directory if [ -d ~/tmp ]; then TMPDIR=~/tmp else TMPDIR=/tmp fi if [ ! -d ${TMPDIR} ]; then mkdir ${TMPDIR} || echo "failed mktemp for patch files dir." fi WORKDIR=`mktemp -d -p ${TMPDIR} src.XXXXXX` || \ { echo "failed mktemp for patch files dir."; exit 1; } pfiles=`lsdiff --strip 1 $patchfile` for pf in $pfiles ; do strip_filename $pf # sets 'base' ##echo pf: $pf + basename: $base if [ "$base" != "" ]; then if [ ! -e $WORKDIR/$base ]; then mkdir -p $WORKDIR/$base fi fi if [ -e $srctree/$pf ]; then cp -a $srctree/$pf $WORKDIR/$pf fi done patch -p1 -bs -d $WORKDIR < $patchfile sts=$? if [ $sts -ne 0 ] && [ $force -eq 0 ]; then echo "patch errors, use '$PROG -f' to force continuation" rm -rf $WORKDIR exit 1 fi for pf in $pfiles ; do $VIEWER $WORKDIR/$pf.orig $WORKDIR/$pf & if [ ${single} -eq 1 ]; then wait # for viewer to exit fi done if [ ${single} -eq 0 ]; then wait # for all viewers to exit fi rm -rf $WORKDIR