Last active
March 28, 2020 17:07
-
-
Save jcorbin/2b6be1f57f26e6068516e6bcb663f411 to your computer and use it in GitHub Desktop.
git-memo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# Memoizes a functionally pure command using git notes. | |
# | |
# Example: | |
# | |
# $ echo 'hello word' | git memo tr 'a-z' 'n-za-m' | |
# uryyb jbeq | |
# TODO set USAGE and LONG_USAGE | |
# TODO support for argv_0 versioning: if the first command arg is a tracked | |
# file under the current git repo, then use its sha in place of command | |
# name to compute $memo_id | |
# TODO support an option for "use this pre-existing git input object" | |
# TODO support an option for "use this input file path" | |
# TODO ... either of which could be a tree/directory | |
# TODO ... which could be supported through more command calling conventions: | |
# TODO ... pass-a-zip on stdin ( ala git-archive ) | |
# TODO ... expect-a-zip on stdout | |
# TODO ... expect-a-git-sha on stdout ( git-aware command ) | |
# TODO ... run-within-dir after preparing it | |
# TODO ... and then take that, or some other $specified output directory | |
SUBDIRECTORY_OK=1 | |
. "$(git --exec-path)/git-sh-setup" | |
# soak input into git, computing its hash key | |
INPUT=$(git hash-object -w --stdin) | |
# determine the memo note ref for the given command | |
memo_id=$(echo "$@" | git hash-object -w --stdin) | |
memo_ref="memo/$memo_id" | |
# get prior cached output hash key, or run the command and soak its output | |
if ! OUTPUT=$(git notes --ref "$memo_ref" show "$INPUT" 2>/dev/null); then | |
OUTPUT=$(git cat-file blob "$INPUT" | "$@" | git hash-object -w --stdin) | |
git notes --ref "$memo_ref" add -f -m "$OUTPUT" "$INPUT" 2>/dev/null | |
fi | |
# either way, now you get output | |
git cat-file blob "$OUTPUT" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: the .sh extension is just here to hint github as to the language ; in practice, drop the extension when putting it in a bin-dir