2016-03-02 16:25:41 +00:00
|
|
|
#!/bin/sh
|
2016-02-28 15:10:55 +00:00
|
|
|
#
|
|
|
|
# Copyright (C) 2014 Joey Hess <id@joeyh.name>
|
|
|
|
# Copyright (C) 2016 Klaus Ethgen <Klaus@Ethgen.ch>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-04-17 20:10:20 +00:00
|
|
|
#
|
2014-03-02 22:01:07 +00:00
|
|
|
# This script can be used to add git-annex metadata to files when they're
|
2014-04-17 20:10:20 +00:00
|
|
|
# committed. It is typically installed as .git/hooks/pre-commit-annex
|
|
|
|
#
|
|
|
|
# You can also run this script by hand, passing it the names of files
|
|
|
|
# already checked into git-annex, and it will extract/refresh the git-annex
|
|
|
|
# metadata from the files.
|
2014-03-02 22:01:07 +00:00
|
|
|
|
2016-02-28 15:10:55 +00:00
|
|
|
tool="$(git config metadata.tool || :)"
|
|
|
|
if [ -z "$tool" ]; then
|
|
|
|
tool=extract
|
|
|
|
fi
|
|
|
|
case "$tool" in
|
|
|
|
exiftool)
|
|
|
|
tool_exec="exiftool -unknown -zip -veryShort -ignoreMinorErrors -use MWG -dateFormat '%Y-%m-%dT%H:%M:%S'"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
tool_exec="$tool"
|
|
|
|
;;
|
|
|
|
esac
|
2014-03-02 22:01:07 +00:00
|
|
|
|
2016-02-28 15:10:55 +00:00
|
|
|
extract_fields="$(git config metadata.extract || :)"
|
|
|
|
if [ -n "$extract_fields" ]; then
|
|
|
|
tools=extract
|
|
|
|
extract_want="^($(echo "$extract_fields" | sed -e 's/ /|/g' -e 's/_/ /g'))"
|
|
|
|
fi
|
|
|
|
exiftool_fields="$(git config metadata.exiftool || :)"
|
|
|
|
if [ -n "$exiftool_fields" ]; then
|
|
|
|
tools="exiftool $tools"
|
|
|
|
exiftool_want="^($(echo "$exiftool_fields" | sed -e 's/ /|/g' -e 's/_/ /g'))"
|
|
|
|
fi
|
|
|
|
if [ -z "$tools" ]; then
|
2014-03-02 22:01:07 +00:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2016-02-28 15:10:55 +00:00
|
|
|
case "$(git config --bool metadata.overwrite || :)" in
|
2014-03-02 22:01:07 +00:00
|
|
|
true)
|
2016-02-28 15:10:55 +00:00
|
|
|
equal="="
|
2014-03-02 22:01:07 +00:00
|
|
|
;;
|
|
|
|
*)
|
2016-02-28 15:10:55 +00:00
|
|
|
equal="?="
|
2014-03-02 22:01:07 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
if git rev-parse --verify HEAD >/dev/null 2>&1; then
|
2016-02-28 15:10:55 +00:00
|
|
|
against="HEAD"
|
2014-03-02 22:01:07 +00:00
|
|
|
else
|
|
|
|
# Initial commit: diff against an empty tree object
|
2016-02-28 15:10:55 +00:00
|
|
|
against="4b825dc642cb6eb9a060e54bf8d69288fbee4904"
|
2014-03-02 22:01:07 +00:00
|
|
|
fi
|
|
|
|
|
2016-02-28 15:10:55 +00:00
|
|
|
addmeta() {
|
|
|
|
file="$1"
|
|
|
|
field="$2"
|
|
|
|
value="$3"
|
2016-02-28 15:56:55 +00:00
|
|
|
afield="$(echo "$field" | tr ' ' '_')"
|
2016-02-28 15:10:55 +00:00
|
|
|
git -c annex.alwayscommit=false annex metadata \
|
|
|
|
--set "$afield$equal$value" --quiet -- "$file"
|
|
|
|
}
|
2014-04-17 20:10:20 +00:00
|
|
|
|
2016-02-28 15:10:55 +00:00
|
|
|
process() {
|
2014-03-02 22:01:07 +00:00
|
|
|
if [ -e "$f" ]; then
|
2015-06-15 19:27:17 +00:00
|
|
|
echo "adding metadata for $f"
|
2016-02-28 15:10:55 +00:00
|
|
|
for tool in $tools; do
|
|
|
|
case "$tool" in
|
|
|
|
exiftool)
|
|
|
|
tool_exec="exiftool -unknown -zip -veryShort -ignoreMinorErrors -use MWG -dateFormat '%Y-%m-%dT%H:%M:%S'"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
tool_exec="$tool"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
LC_ALL=C $tool_exec "./$f" | eval egrep --text -i \""\$${tool}_want"\" | while read line; do
|
|
|
|
case "$tool" in
|
|
|
|
extract)
|
|
|
|
field="${line%% - *}"
|
|
|
|
value="${line#* - }"
|
|
|
|
;;
|
|
|
|
exiftool)
|
|
|
|
field="${line%%: *}"
|
|
|
|
value="${line#*: }"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
if [ -n "$value" ]; then
|
|
|
|
addmeta "$f" "$field" "$value"
|
|
|
|
fi
|
|
|
|
done
|
2014-03-02 22:01:07 +00:00
|
|
|
done
|
|
|
|
fi
|
2014-04-17 20:10:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if [ -n "$*" ]; then
|
2016-02-28 15:10:55 +00:00
|
|
|
for f in "$@"; do
|
2014-04-17 20:10:20 +00:00
|
|
|
process "$f"
|
|
|
|
done
|
|
|
|
else
|
2016-03-02 16:25:41 +00:00
|
|
|
git diff-index --name-only --cached $against | while read f; do
|
2014-04-17 20:10:20 +00:00
|
|
|
process "$f"
|
|
|
|
done
|
|
|
|
fi
|