This repository was archived by the owner on Jan 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_zip_clean.sh
executable file
·85 lines (74 loc) · 2.5 KB
/
filter_zip_clean.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
#
# This is a simple filter that decompresses ZIP-files (stores all files
# uncompressed inside a new ZIP file). This can increase the storage efficiency
# inside a git-repository, since
#
# a) the entire file is compressed by git, and compressing already compressed
# files tends to not be very efficient.
#
# and
#
# b) without the compression, it will be easier for git to determine
# similarities between files when making a pack. This is especially
# important for OpenDocument files that are changed frequently.
#
# The disadvantage is, that the file inside your working copy will be bigger
# than before. ALSO: It does not seem to work with .jar-files.
#
# When the --odf option is passed on the commandline, the script will also
# remove superfluous files inside a OpenDocument file (cache, thumbnails).
#
# filter_zip_clean.sh relies on the Info-ZIP Zip and UnZip programs
# ("sudo apt-get install zip unzip") and will not work if it they are not
# available.
#
# To use the script add the following to your .gitconfig:
#
# [filter "zip"]
# clean = "/path/to/script/filter_zip_clean.sh"
#
# [filter "odf"]
# clean = "/path/to/script/filter_zip_clean.sh --odf"
#
# and add glob-patterns for zip and OpenDocument files to the .gitattributes
# file at the root of a git working copy:
#
# *.zip filter=zip
# *.od[tpsgb] filter=odf
#
set -e
# Check for Info-ZIP, and exit if it's not found
ZIP_VERSION=`zip -v | head -1`
if ! [[ $ZIP_VERSION == *Info-ZIP* ]]
then
echo "filter_zip_clean.sh needs Info-ZIP Zip installed to work! (\"sudo apt-get install zip\")"
exit 1
fi
UNZIP_VERSION=`unzip -v | head -1`
if ! [[ $UNZIP_VERSION == *Info-ZIP* ]]
then
echo "filter_zip_clean.sh needs Info-ZIP UnZip installed to work! (\"sudo apt-get install unzip\")"
exit 1
fi
# Get working directory to restore later
CUR_PWD=`pwd`
UNZIP_TMPDIR=`mktemp -d zipfilter.XXXXXXXXXX`
ZIP_TMPFILE=`mktemp -u zipfilter.XXXXXXXXXX` # The manpage claims this is unsafe? Why?
# Unzip input to temporary directory
unzip -qq -d "${UNZIP_TMPDIR}" /dev/stdin
# Remove unneccessary files if input is an ODF-file
if [ "$1" = "--odf" ]
then
rm -f -- "${UNZIP_TMPDIR}/layout-cache"
rm -rf -- "${UNZIP_TMPDIR}/Thumbnails/"
fi
# Re-zip everything and restore working directory
cd "${UNZIP_TMPDIR}"
zip -r -0 "${ZIP_TMPFILE}.zip" .
cd "${CUR_PWD}"
# Hand the re-zipped file back to git
cp -f -- "${ZIP_TMPFILE}.zip" /dev/stdout
# Clean up behind ourselves
rm -rf -- "${UNZIP_TMPDIR}"
rm -f -- "${ZIP_TMPFILE}.zip"