Implement gitignore functionality in SnapshotProvider

This commit is contained in:
Kristóf Tóth 2018-08-06 15:42:51 +02:00
parent 59dce4a848
commit b7ed4c3d0f

View File

@ -5,14 +5,14 @@ import re
from subprocess import run, CalledProcessError, PIPE from subprocess import run, CalledProcessError, PIPE
from getpass import getuser from getpass import getuser
from os.path import isdir from os.path import isdir
from os.path import join as joinpath
from uuid import uuid4 from uuid import uuid4
from dateutil import parser as dateparser from dateutil import parser as dateparser
# TODO: gitignore
class SnapshotProvider: class SnapshotProvider:
def __init__(self, directory, git_dir): def __init__(self, directory, git_dir, exclude_unix_patterns=None):
self._classname = self.__class__.__name__ self._classname = self.__class__.__name__
author = f'{getuser()} via TFW {self._classname}' author = f'{getuser()} via TFW {self._classname}'
self.gitenv = { self.gitenv = {
@ -27,6 +27,8 @@ class SnapshotProvider:
self._init_repo() self._init_repo()
self.__last_valid_branch = self._branch self.__last_valid_branch = self._branch
if exclude_unix_patterns:
self.exclude = exclude_unix_patterns
def _init_repo(self): def _init_repo(self):
self._check_environment() self._check_environment()
@ -105,6 +107,24 @@ class SnapshotProvider:
kwargs['env'] = self.gitenv kwargs['env'] = self.gitenv
return run(*args, **kwargs) return run(*args, **kwargs)
@property
def exclude(self):
with open(self._exclude_path, 'r') as ofile:
return ofile.read()
@exclude.setter
def exclude(self, exclude_patterns):
with open(self._exclude_path, 'w') as ifile:
ifile.write('\n'.join(exclude_patterns))
@property
def _exclude_path(self):
return joinpath(
self.gitenv['GIT_DIR'],
'info',
'exclude'
)
def take_snapshot(self): def take_snapshot(self):
if self._head_detached: if self._head_detached:
self._checkout_new_branch_from_head() self._checkout_new_branch_from_head()