1
0
mirror of https://github.com/oskar456/dzonegit.git synced 2024-05-11 05:55:41 +00:00

Add smudge filter to replace $UNIXTIME directive on checkout

This commit is contained in:
Ondřej Caletka
2018-08-20 15:25:27 +02:00
parent 17f771bca6
commit 806976ca6e
4 changed files with 44 additions and 1 deletions

View File

@ -15,6 +15,7 @@ Main features
- check if zone file compiles properly using `named-compilezone(8)`_ - check if zone file compiles properly using `named-compilezone(8)`_
- autodetect zone name from file name or ``$ORIGIN`` directive - autodetect zone name from file name or ``$ORIGIN`` directive
- enforce updating serial number when zone content is changed - enforce updating serial number when zone content is changed
- optional ``smudge`` filter to replace ``$UNIXTIME`` directive with current UNIX time
- both ``pre-commit`` and ``pre-receive``/``update`` hooks to enforce similar checks in the remote repository - both ``pre-commit`` and ``pre-receive``/``update`` hooks to enforce similar checks in the remote repository
- ``post-receive`` hook to checkout the working copy from a bare repository, generate config snippets for various DNS server software and reload them - ``post-receive`` hook to checkout the working copy from a bare repository, generate config snippets for various DNS server software and reload them
- only Python 3.5+ standard library is used - only Python 3.5+ standard library is used
@ -53,6 +54,29 @@ Full instalation and usage
documentation on how to add custom hooks`_ documentation on how to add custom hooks`_
- on the server, set up the configuration options for each repository - on the server, set up the configuration options for each repository
Support for $UNIXTIME directive
-------------------------------
If you want to use ``$UNIXTIME`` in your zone files instead of serial number,
you have to install a `smudge` filter on the server, that will replace the
directive with current unix time on checkout. First, set up the filter in
the Git configuration:
.. code-block:: shell
$ git config --global filter.dzonegit.smudge $(which dzonegit-smudge-serial)
$ git config --global filter.dzonegit.clean cat
Then, apply the filter on all zone files using ``.gitattributes`` file inside
the repository:
.. code-block::
*.zone filter=dzonegit
Configuration options Configuration options
--------------------- ---------------------

View File

@ -509,6 +509,15 @@ def post_receive(stdin=sys.stdin):
subprocess.run(cmd) subprocess.run(cmd)
def smudge_serial(
bstdin=sys.stdin.buffer,
bstdout=sys.stdout.buffer,
unixtime=None,
):
"""Replace all $UNIXTIME directives with current unix time."""
bstdout.write(unixtime_directive(bstdin.read(), unixtime))
def get_action(argv=sys.argv): def get_action(argv=sys.argv):
name = Path(argv[0]).name name = Path(argv[0]).name
if "pre-commit" in name: if "pre-commit" in name:
@ -519,6 +528,8 @@ def get_action(argv=sys.argv):
return pre_receive return pre_receive
if "post-receive" in name: if "post-receive" in name:
return post_receive return post_receive
if "smudge" in name:
return smudge_serial
def main(): def main():

View File

@ -23,6 +23,7 @@ setup(
"dzonegit-pre-receive = dzonegit:pre_receive", "dzonegit-pre-receive = dzonegit:pre_receive",
"dzonegit-post-receive = dzonegit:post_receive", "dzonegit-post-receive = dzonegit:post_receive",
"dzonegit-update = dzonegit:update", "dzonegit-update = dzonegit:update",
"dzonegit-smudge-serial = dzonegit:smudge_serial",
], ],
}, },
classifiers=[ classifiers=[

View File

@ -4,7 +4,7 @@ import subprocess
import time import time
import datetime import datetime
import os import os
from io import StringIO from io import StringIO, BytesIO
from pathlib import Path from pathlib import Path
import dzonegit import dzonegit
@ -94,6 +94,13 @@ ns.example.com. 60 IN A 192.0.2.1
assert r.serial == str(123456) assert r.serial == str(123456)
def test_smudge_serial():
bstdin = BytesIO(b"something $UNIXTIME something")
bstdout = BytesIO()
dzonegit.smudge_serial(bstdin, bstdout, 123456)
assert b"something 123456 something" == bstdout.getvalue()
def test_is_serial_increased(): def test_is_serial_increased():
assert dzonegit.is_serial_increased(1234567890, "2018010100") assert dzonegit.is_serial_increased(1234567890, "2018010100")
assert dzonegit.is_serial_increased("2018010100", "4018010100") assert dzonegit.is_serial_increased("2018010100", "4018010100")