1
0
Fork 0
repo2html/hashcache

83 lines
2.5 KiB
Bash
Executable File

#!/bin/bash
#
# Hash cache storage and retrieval operations
#
# Copyright (C) 2012 Mike Gerwitz
#
# This file is part of repo2html.
#
# 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/>.
# #
# intended to be configured from a configuration file; lowercase to help
# mitigate global env var conflicts
cachefile="${hashcache:-${path_root:+$path_root/}.hashcache}"
# 'cause it rhymes and 'cause it's useful
_cache()
{
hash="$1"
value="$2"
title="$3"
echo "$hash $value $title" >>"$cachefile"
}
# retrieves the value associated with the given hash
_retrieve()
{
hash="$1"
# if multiple identical hashes exist, then the last one takes precedence
# (ideally, such a thing should never happen, but it's best to be prepared);
# note that we *do not* check for a space after the hash, which allows for
# partial hashes to be matched (common practice for Git due to its long
# hashes); this may cause problems with other version control systems---such
# as SVN---that use revision numbers, in which case this implementation will
# have to be changed to accept a flag for whether or not to accept partial
# matches, or it must operate under the assumption that the commits are always
# processed in reverse order and (as such) the tail in this pipeline will
# always return the intended result
grep "^$hash" "$cachefile" \
| tail -n1 \
| cut -d' ' -f2
}
# clears the cache file
_wipe()
{
>"$cachefile"
}
# the hash is required, but the value to set it to is optional
hash="${1?No hash provided}"
set="$2"
title="$3"
# poor man's argument parsing (we don't need no stinkin' options [yet]); "clear"
# is not a valid hash currently, though I suppose a sadistic repository could
# allow such, and we're supposed to be repo-agnostic
if [ "$hash" == 'clear' ]; then
_wipe
elif [ "$set" ]; then
_cache "$hash" "$set" "$title"
else
result="$( _retrieve "$hash" "$set" )"
echo "$result"
# exit with negative status if we have no result
[ -n "$result" ]
fi