From 19d4ed6f747248e4ff8cd1c524a7431871fd55f3 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Mon, 22 Mar 2021 09:39:46 +0100 Subject: git hook to update the lesana cache when files are changed by git. --- CHANGELOG.rst | 5 +++++ lesana/collection.py | 17 +++++++++++++++-- lesana/data/post-checkout | 21 +++++++++++++++++++++ tests/test_collection.py | 18 ++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100755 lesana/data/post-checkout diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c8c2786..8c6a212 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,11 @@ Unreleased * New data type: geo (for Geo URIs). * New custom filter for templates: to_yaml. +* git hook to update the lesana cache when files are changed by git. + + This hook is installed when running ``lesana init``; you may want to + add it to existing repositories by running ``lesana init`` on them + (this is safe to do and won't change your settings or your data). 0.8.1 ===== diff --git a/lesana/collection.py b/lesana/collection.py index a4546df..87f767f 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -8,7 +8,7 @@ import ruamel.yaml import xapian import jinja2 -from pkg_resources import resource_string +from pkg_resources import resource_string, resource_filename from . import types, templating try: @@ -527,7 +527,20 @@ class Collection(object): fp.write('#Added by lesana init\n.lesana') if repo: repo.index.add(['.gitignore']) - # TODO: Add hook to index files as they are pulled + + # Add post-checkout and post-merge hook + hook_source = resource_filename('lesana', 'data/post-checkout') + hooks_dir = os.path.join( + c_dir, + '.git', + 'hooks', + ) + checkout_hook = os.path.join(hooks_dir, 'post-checkout') + merge_hook = os.path.join(hooks_dir, 'post-merge') + + shutil.copy(hook_source, checkout_hook) + if not os.path.islink(merge_hook): + os.symlink(checkout_hook, merge_hook) # If it doesn't exist, create a skeleton of settings.yaml file # then open settings.yaml for editing filepath = os.path.join(c_dir, 'settings.yaml') diff --git a/lesana/data/post-checkout b/lesana/data/post-checkout new file mode 100755 index 0000000..9c2c084 --- /dev/null +++ b/lesana/data/post-checkout @@ -0,0 +1,21 @@ +#!/bin/sh +# +# Update a lesana cache when the repository is changed with git +# +# To use this hook add it to .git/hooks under the names "post-checkout" and +# "post-merge" + +if [ $(basename $0) = "post-checkout" ] ; then + diff_cmd="git diff --name-status $1 $2" +elif [ $(basename $0) = "post-merge" ] ; then + diff_cmd="git diff --name-status --no-commit-id ORIG_HEAD HEAD" +else + echo "This hook can only work as post-checkout or post-merge" + exit 1 +fi + +for F in $( $diff_cmd | grep "^D" | cut -c 2-) ; do + lesana rm $(basename $F .yaml) +done + +lesana index $( $diff_cmd | grep "^[AM]" | cut -c 2- | xargs) diff --git a/tests/test_collection.py b/tests/test_collection.py index 2218169..2af4cfe 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -520,6 +520,24 @@ class testCollectionCreation(unittest.TestCase): self.assertTrue( os.path.isfile(os.path.join(self.tmpdir, '.gitignore')) ) + checkout_hook = os.path.join( + self.tmpdir, + '.git', + 'hooks', + 'post-checkout', + ) + merge_hook = os.path.join( + self.tmpdir, + '.git', + 'hooks', + 'post-merge', + ) + self.assertTrue(os.path.isfile(checkout_hook)) + self.assertTrue(os.path.islink(merge_hook)) + self.assertEqual( + os.path.abspath(checkout_hook), + os.path.abspath(os.readlink(merge_hook)) + ) # and then run it twice on the same directory, nothing should break collection = lesana.Collection.init(self.tmpdir) self.assertIsInstance(collection, lesana.Collection) -- cgit v1.2.3