git-wad

Manage files via git but not their content
git clone git://git.meso-star.fr/git-wad.git
Log | Files | Refs | README | LICENSE

commit 854453899d45d4f6a3bdfcca42274677667b25d5
parent f1178c0ea6069fc90eb955e5cb1cd457be318f7c
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Sat, 25 Nov 2023 18:55:22 +0100

Source code review

Several changes have been made in the course of this code review. Among
other things, this commit has made the dd command silent, added comments
and corrected the log function. We've also prefixed the configuration
variables with "GIT_WAD_" to emphasize that they are environment
variables for git-wad.

Diffstat:
Mgit-wad | 100++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
1 file changed, 63 insertions(+), 37 deletions(-)

diff --git a/git-wad b/git-wad @@ -18,9 +18,9 @@ set -e # Options -HEADER="git-wad" -OBJDIR=".git/wad" -VERBOSE=1 +GIT_WAD_HEADER="#\$# git-wad" +GIT_WAD_OBJDIR=".git/wad" +GIT_WAD_VERBOSE=1 ######################################################################## # Helper functions @@ -30,70 +30,96 @@ synopsis() >&2 printf "usage: git-wad\n" } -log() +log() # str [, arg...] { - if [ -n "${VERBOSE}" ] && [ ! "${VERBOSE}" -eq 0 ]; then - exec >&2 printf "$@" + if [ -n "${GIT_WAD_VERBOSE}" ] && [ ! "${GIT_WAD_VERBOSE}" -eq 0 ]; then + # shellcheck disable=SC2059 + >&2 printf "$@" fi } +sizeof_header() +{ + printf "%s" "${GIT_WAD_HEADER}" | wc -c +} + encode() # digest, size { - printf "%s %s %d" "${HEADER}" "$1" "$2" + printf "%s %s %d" "${GIT_WAD_HEADER}" "$1" "$2" } decode() # stdin { - header="$(dd ibs=1 count="$(printf "%s" "${HEADER}" | wc -c || true)")" + header_size="$(sizeof_header)" + header="$(dd ibs=1 count="${header_size}" 2> /dev/null)" + + if [ "${header}" = "${GIT_WAD_HEADER}" ]; then + # Remove the space after the header and before the digest + cat - | sed "1s/^ //d" - if [ "${header}" != "${HEADER}" ]; then + else # It is not a WAD printf "%s" "${header}" cat - - else - # Remove the space before the digest - cat - | sed "s/^ //d" fi } -clean() +clean() # stdin { - tmpfile="$(mktemp -p "${OBJDIR}")" + tmpfile="$(mktemp -p "${GIT_WAD_OBJDIR}")" digest=$(cat - | tee "${tmpfile}" | sha256sum | cut -d' ' -f1) size=$(wc -c < "${tmpfile}") - wad=$(encode "${digest}" "${size}") + header_size="$(sizeof_header)" + header="$(dd ibs=1 count="${header_size}" < "${tmpfile}" 2> /dev/null)" - # TODO check that the file is not an un-smudged wad file + # Do not clean input stream if it is an un-smudged WAD + if [ "${header}" = "${GIT_WAD_HEADER}" ]; then + cat "${tmpfile}" + return 0 + fi - if [ -f "${OBJDIR}/${digest}" ]; then - log "git-wad:filter-clean: cache already exists %s\n" "${OBJDIR}/${digest}" + # The input stream is already managed by git-wad + if [ -f "${GIT_WAD_OBJDIR}/${digest}" ]; then + log "git-wad:filter-clean: cache already exists %s\n"\ + "${GIT_WAD_OBJDIR}/${digest}" rm "${tmpfile}" + + # Store input stream to a file whose name is the stream digest else chmod 444 "${tmpfile}" - mv "${tmpfile}" "${OBJDIR}/${digest}" - log "git-wad:filter-clean: caching to %s\n" "${OBJDIR}/${digest}" + mv "${tmpfile}" "${GIT_WAD_OBJDIR}/${digest}" + log "git-wad:filter-clean: caching to %s\n" \ + "${GIT_WAD_OBJDIR}/${digest}" fi - echo "${wad}" + + encode "${digest}" "${size}" } smudge() { - # shellcheck disable=SC2310 - if digest_size="$(decode)"; then - log "git-wad:filter-smudge: not a managed file" - exit 1 - fi + header_size="$(sizeof_header)" + header="$(dd ibs=1 count="${header_size}")" - digest="$(echo "${digest_size}" | cut -d' ' -f1)" - size="$(echo "${digest_size}" | cut -d' ' -f2)" - object="${OBJDIR}/${digest}" + if [ "${header}" != "${GIT_WAD_HEADER}" ]; then # It is not a WAD + log "git-wad:filter-smudge: not a managed file" + printf "%s" "${header}" + cat - - if [ -f "${object}" ]; then - log "git-wad:filter-smudge: restoring from %s\n" "${object}" - cat "${object}" - else - log "git-wad:filter-smudge: wad object is missing %s\n" "${object}" - encode "${digest}" "${size}" + else # It is a WAD + # The sed directive remove the space before the digest + digest_size="$(cat - | sed "1s/^ //d")" + + digest="$(echo "${digest_size}" | cut -d' ' -f1)" + size="$(echo "${digest_size}" | cut -d' ' -f2)" + object="${GIT_WAD_OBJDIR}/${digest}" + + if [ -f "${object}" ]; then + log "git-wad:filter-smudge: restoring from %s\n" "${object}" + cat "${object}" + else + log "git-wad:filter-smudge: WAD object is missing %s\n" "${object}" + encode "${digest}" "${size}" + fi fi } @@ -102,8 +128,8 @@ smudge() ######################################################################## sub_cmd="$1" -if ! [ -d "${OBJDIR}" ]; then - mkdir -p "${OBJDIR}" +if ! [ -d "${GIT_WAD_OBJDIR}" ]; then + mkdir -p "${GIT_WAD_OBJDIR}" fi case "${sub_cmd}" in