Subject: Re: Management of opendocument (openoffice.org) files in git

From: Paolo Bonzini <[email-masked]>
Date: Tue, 16 Sep 2008 10:12:44 +0200

> With regards to
> 
>> And maybe -b,-qq,-X and -q,-r respectively could be added by default?
>>
>>   
> I would prefer not to do so:  if you do you get something that is
> somehow "specialised", otherwise you have a totally generic "rezipper"
> that might also find other applications (who knows).

Yeah, but regarding -b/-X, their effect can/should be undone with zip
command line options (-l, -ll, -X).  And for zip's -r option, a rezipper
that by default only rezips the top directory does not seem very useful. :-)

You're right about letting the user specify -qq/-q.  Or maybe you can
have a -q/--quiet option to rezip that adds both -qq to unzip, and -q to
zip.  This way you can use the openoffice profile both in quiet mode
(for git) and in non-quiet mode (for manual use).

Putting all of this together, it would make the filter look like this:

  [filter "opendocument"]
        clean = "rezip --quiet --zip-opts -D,-0"
        smudge = "rezip --quiet --zip-opts -D,-6"

or similarly with profiles:

  [filter "opendocument"]
        clean = "rezip --quiet -p ODF_UNCOMPRESS"
        smudge = "rezip --quiet -p ODF_COMPRESS"

After my signature you can find my attempt at making rezip more useful
as a general program.  It supports --quiet, multiple input files, and -
for stdin.  (And I learnt from you that >&$foo works).

> BTW, that is why I added the profiles, so that there was no need to type
> repetitive stuff.

Understood.

Paolo

-----8<-----------------------
#! /bin/sh
#
# (c) 2008 Sergio Callegari
#
# Rewrites a zip archive, possibly changing the compression level

USAGE='Usage: rezip OPTIONS FILE...
with options:
  -h, --help               Gives help
  --unzip-opts OPTIONS     Pass options to unzip helper to read zip file
  --zip-opts OPTIONS       Pass options to zip helper to write zip file
  -p, --profile PROFILE    Get options for helpers from profile
  -q, --quiet              Make unzip and zip quiet

Rewrites a zip archive, possibily changing the compression level.
If the archive name is unspecified or "-", then the command operates
like a filter, reading from standard input and writing to standard
output.  Options (either space- or comma-separated) can be manually
provided to the unzip process doing the read and to the zip process
doing the write.  Alternatively a profile can be used to set options
automatically.'

PROFILES="ODF_UNCOMPRESS ODF_COMPRESS"

PROFILE_UNZIP_ODF_UNCOMPRESS=
PROFILE_ZIP_ODF_UNCOMPRESS=-D,-0
PROFILE_UNZIP_ODF_COMPRESS=
PROFILE_ZIP_ODF_COMPRESS=-D,-6

die()
{
    echo "$3$USAGE

Available profiles: ${PROFILES}" >&$1
    exit $2
}

UNZIP_OPTS=""
ZIP_OPTS=""

while true ; do
    case "$1" in
        -h | --help)
            die 1 0 ;;

        # TODO: handle -p*, --profile=* and similarly for other options

        -p | --profile)
            eval UNZIP_OPTS=\$PROFILE_UNZIP_$2
            eval ZIP_OPTS=\$PROFILE_ZIP_$2
            shift ;;
        --unzip-opts)
            UNZIP_OPTS="${UNZIP_OPTS} $2"
            shift ;;
        --zip-opts)
            ZIP_OPTS="${ZIP_OPTS} $2"
            shift ;;
        -q | --quiet)
            UNZIP_QUIET=-qq
            ZIP_QUIET=-q ;;
        -*)
            die 2 1 "Invalid option: $1
" ;;
        *)
            break ;;
    esac
    shift
done

UNZIP_OPTS="$UNZIP_QUIET -b -X `echo $UNZIP_OPTS | sed 'y/,/ /'`"
ZIP_OPTS="$ZIP_QUIET -r `echo $ZIP_OPTS | sed 'y/,/ /'`"

if [ $# = 0 ] ; then
    set fnord -
    shift
fi

redir=1
for filename
do
    if [ "$filename" = - ]; then
        redir=2
        break
    fi
done

for filename
do
    workdir=`mktemp -d -t rezip.workdir.XXXXXX`

    if [ "$filename" = - ]; then
        tmpcopy=:
        filename=`mktemp rezip.zip.XXXXXX`
        cat > $filename
    else
        tmpcopy=false
    fi

    (case $filename in
        /*) ;;
        *) filename=`pwd`/$filename ;;
    esac
    cd "$workdir"
    unzip $UNZIP_OPTS "$filename" >&$redir
    zip $ZIP_OPTS "$filename" . >&$redir)
    rm -fr "$workdir"

    if $tmpcopy ; then
        cat "$filename"
        rm "$filename"
    fi
done

--------8<------------------------