2013-03-05 23:41:27 -05:00
|
|
|
#!/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
|
2013-06-02 11:54:58 -04:00
|
|
|
cachefile="${hashcache:-${path_root:+$path_root/}.hashcache}"
|
2013-03-05 23:41:27 -05:00
|
|
|
|
|
|
|
# 'cause it rhymes and 'cause it's useful
|
|
|
|
_cache()
|
|
|
|
{
|
|
|
|
hash="$1"
|
|
|
|
value="$2"
|
2013-06-02 11:56:00 -04:00
|
|
|
title="$3"
|
2013-03-05 23:41:27 -05:00
|
|
|
|
2013-06-02 11:56:00 -04:00
|
|
|
echo "$hash $value $title" >>"$cachefile"
|
2013-03-05 23:41:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# retrieves the value associated with the given hash
|
|
|
|
_retrieve()
|
|
|
|
{
|
|
|
|
hash="$1"
|
|
|
|
|
|
|
|
# if multiple identical hashes exist, then the last one takes precedence
|
2013-03-09 09:44:37 -05:00
|
|
|
# (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" \
|
2013-03-05 23:41:27 -05:00
|
|
|
| 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"
|
2013-06-02 11:56:00 -04:00
|
|
|
title="$3"
|
2013-03-05 23:41:27 -05:00
|
|
|
|
|
|
|
# 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
|
2013-06-02 11:56:00 -04:00
|
|
|
_cache "$hash" "$set" "$title"
|
2013-03-05 23:41:27 -05:00
|
|
|
else
|
2013-03-10 16:14:14 -04:00
|
|
|
result="$( _retrieve "$hash" "$set" )"
|
|
|
|
echo "$result"
|
|
|
|
|
|
|
|
# exit with negative status if we have no result
|
|
|
|
[ -n "$result" ]
|
2013-03-05 23:41:27 -05:00
|
|
|
fi
|