Skip to content

Commit a75f606

Browse files
committed
filesystem: track content with file_content option
1 parent cdd846e commit a75f606

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

hooks/filesystem.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import time
33
import stat
44
import subprocess
5+
import shutil
56
from enum import Enum
67
from tempfile import NamedTemporaryFile
78
from pathlib import Path
@@ -253,12 +254,17 @@ class GitFilesystemHook(Hook):
253254
def __init__(self, parameters):
254255
super().__init__(parameters)
255256
self.repo_path = Path(self.configuration['repo'])
257+
self.file_content = self.configuration.get('file_content', False)
256258
self.repo = Repo(str(self.repo_path))
257259
# repo must be clean
258260
if self.repo.is_dirty():
259261
raise RuntimeError("Repository is dirty. Aborting.")
260262

261-
self.context.subscribe('filesystem_new_inode', self.process_new_inode)
263+
if self.file_content:
264+
self.context.subscribe('filesystem_new_file', self.process_new_file)
265+
else:
266+
# only capture filesystem tree
267+
self.context.subscribe('filesystem_new_inode', self.process_new_inode)
262268
self.context.subscribe('filesystem_capture_end', self.fs_capture_end)
263269

264270
def process_new_inode(self, event):
@@ -275,7 +281,26 @@ def process_new_inode(self, event):
275281
# git add
276282
self.repo.git.add(str(filepath))
277283

284+
def process_new_file(self, event):
285+
local_tmp_filepath = event.filepath
286+
inode = event.inode
287+
288+
local_git_filepath = self.repo_path / inode.path.relative_to('/')
289+
# test if exists
290+
if not local_git_filepath.exists():
291+
if InodeType(inode.inode_type) == InodeType.DIR:
292+
local_git_filepath.mkdir(parents=True, exist_ok=True)
293+
else:
294+
# everything else is treated as file
295+
local_git_filepath.parent.mkdir(parents=True, exist_ok=True)
296+
# copy file content
297+
shutil.copyfile(local_tmp_filepath, local_git_filepath)
298+
299+
# git add
300+
self.repo.git.add(str(local_git_filepath))
301+
278302
def fs_capture_end(self, event):
279303
# commit
280304
domain_name = self.configuration['domain_name']
281-
self.repo.git.commit('-m', domain_name)
305+
self.logger.info('Creating new commit \'%s\'', domain_name)
306+
self.repo.git.commit('-m', domain_name)

0 commit comments

Comments
 (0)