2018-07-19 09:06:13 +00:00
|
|
|
import re
|
2018-08-01 15:18:43 +00:00
|
|
|
from subprocess import run, CalledProcessError, PIPE
|
2018-07-17 14:04:28 +00:00
|
|
|
from getpass import getuser
|
2018-07-18 12:26:14 +00:00
|
|
|
from os.path import isdir
|
2018-08-06 13:42:51 +00:00
|
|
|
from os.path import join as joinpath
|
2018-07-19 12:26:41 +00:00
|
|
|
from uuid import uuid4
|
2018-07-17 14:04:28 +00:00
|
|
|
|
2018-08-03 09:39:55 +00:00
|
|
|
from dateutil import parser as dateparser
|
|
|
|
|
2018-07-17 14:04:28 +00:00
|
|
|
|
|
|
|
class SnapshotProvider:
|
2018-08-06 13:42:51 +00:00
|
|
|
def __init__(self, directory, git_dir, exclude_unix_patterns=None):
|
2018-07-19 13:05:43 +00:00
|
|
|
self._classname = self.__class__.__name__
|
|
|
|
author = f'{getuser()} via TFW {self._classname}'
|
2018-07-17 14:04:28 +00:00
|
|
|
self.gitenv = {
|
|
|
|
'GIT_DIR': git_dir,
|
|
|
|
'GIT_WORK_TREE': directory,
|
|
|
|
'GIT_AUTHOR_NAME': author,
|
|
|
|
'GIT_AUTHOR_EMAIL': '',
|
|
|
|
'GIT_COMMITTER_NAME': author,
|
2018-07-18 13:47:12 +00:00
|
|
|
'GIT_COMMITTER_EMAIL': '',
|
|
|
|
'GIT_PAGER': 'cat'
|
2018-07-17 14:04:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 12:56:59 +00:00
|
|
|
self._init_repo()
|
2018-07-19 09:06:13 +00:00
|
|
|
self.__last_valid_branch = self._branch
|
2018-08-06 13:42:51 +00:00
|
|
|
if exclude_unix_patterns:
|
|
|
|
self.exclude = exclude_unix_patterns
|
2018-07-19 12:56:59 +00:00
|
|
|
|
|
|
|
def _init_repo(self):
|
|
|
|
self._check_environment()
|
|
|
|
|
|
|
|
if not self._repo_is_initialized:
|
|
|
|
self._run(('git', 'init'))
|
|
|
|
|
|
|
|
if self._number_of_commits == 0:
|
2018-07-19 13:05:43 +00:00
|
|
|
try:
|
|
|
|
self._snapshot()
|
|
|
|
except CalledProcessError:
|
|
|
|
raise EnvironmentError(f'{self._classname} cannot init on empty directories!')
|
2018-07-19 12:56:59 +00:00
|
|
|
|
|
|
|
self._check_head_not_detached()
|
2018-07-18 11:42:05 +00:00
|
|
|
|
2018-07-18 12:26:14 +00:00
|
|
|
def _check_environment(self):
|
|
|
|
if not isdir(self.gitenv['GIT_DIR']) or not isdir(self.gitenv['GIT_WORK_TREE']):
|
2018-07-19 13:05:43 +00:00
|
|
|
raise EnvironmentError(f'{self._classname}: "directory" and "git_dir" must exist!')
|
2018-07-19 12:56:59 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def _repo_is_initialized(self):
|
|
|
|
return self._run(
|
|
|
|
('git', 'status'),
|
|
|
|
check=False
|
|
|
|
).returncode == 0
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _number_of_commits(self):
|
|
|
|
return int(
|
|
|
|
self._get_stdout((
|
|
|
|
'git', 'rev-list',
|
|
|
|
'--all',
|
|
|
|
'--count'
|
|
|
|
))
|
|
|
|
)
|
|
|
|
|
|
|
|
def _snapshot(self):
|
|
|
|
self._run((
|
|
|
|
'git', 'add',
|
|
|
|
'-A'
|
|
|
|
))
|
2018-08-01 15:19:31 +00:00
|
|
|
try:
|
|
|
|
self._get_stdout((
|
|
|
|
'git', 'commit',
|
|
|
|
'-m', 'Snapshot'
|
|
|
|
))
|
|
|
|
except CalledProcessError as err:
|
|
|
|
if b'nothing to commit, working tree clean' not in err.output:
|
|
|
|
raise
|
2018-07-19 12:56:59 +00:00
|
|
|
|
|
|
|
def _check_head_not_detached(self):
|
2018-07-19 09:06:13 +00:00
|
|
|
if self._head_detached:
|
2018-07-19 13:05:43 +00:00
|
|
|
raise EnvironmentError(f'{self._classname} cannot init from detached HEAD state!')
|
2018-07-18 12:26:14 +00:00
|
|
|
|
2018-07-19 09:37:51 +00:00
|
|
|
@property
|
|
|
|
def _head_detached(self):
|
|
|
|
return self._branch == 'HEAD'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _branch(self):
|
|
|
|
return self._get_stdout((
|
|
|
|
'git', 'rev-parse',
|
|
|
|
'--abbrev-ref', 'HEAD'
|
|
|
|
))
|
|
|
|
|
|
|
|
def _get_stdout(self, *args, **kwargs):
|
2018-08-01 15:18:43 +00:00
|
|
|
kwargs['stdout'] = PIPE
|
|
|
|
kwargs['stderr'] = PIPE
|
2018-07-19 09:37:51 +00:00
|
|
|
stdout_bytes = self._run(*args, **kwargs).stdout
|
|
|
|
return stdout_bytes.decode().rstrip('\n')
|
|
|
|
|
|
|
|
def _run(self, *args, **kwargs):
|
2018-07-19 12:56:59 +00:00
|
|
|
if 'check' not in kwargs:
|
|
|
|
kwargs['check'] = True
|
2018-07-19 09:37:51 +00:00
|
|
|
if 'env' not in kwargs:
|
|
|
|
kwargs['env'] = self.gitenv
|
|
|
|
return run(*args, **kwargs)
|
|
|
|
|
2018-08-06 13:42:51 +00:00
|
|
|
@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'
|
|
|
|
)
|
|
|
|
|
2018-07-17 14:04:28 +00:00
|
|
|
def take_snapshot(self):
|
2018-07-18 11:38:17 +00:00
|
|
|
if self._head_detached:
|
2018-07-18 14:26:10 +00:00
|
|
|
self._checkout_new_branch_from_head()
|
2018-07-19 12:56:59 +00:00
|
|
|
self._snapshot()
|
2018-07-17 14:04:28 +00:00
|
|
|
|
2018-07-18 14:26:10 +00:00
|
|
|
def _checkout_new_branch_from_head(self):
|
2018-08-03 09:39:55 +00:00
|
|
|
branch_name = str(uuid4())
|
2018-07-18 11:38:17 +00:00
|
|
|
self._run((
|
2018-07-19 09:31:31 +00:00
|
|
|
'git', 'branch',
|
2018-07-19 12:26:41 +00:00
|
|
|
branch_name
|
2018-07-19 09:11:56 +00:00
|
|
|
))
|
2018-07-19 12:26:41 +00:00
|
|
|
self._checkout(branch_name)
|
2018-07-18 11:38:17 +00:00
|
|
|
|
2018-07-19 14:15:54 +00:00
|
|
|
def _checkout(self, what):
|
|
|
|
self._run((
|
|
|
|
'git', 'checkout',
|
|
|
|
what
|
|
|
|
))
|
|
|
|
|
2018-07-17 14:04:28 +00:00
|
|
|
def restore_snapshot(self, date):
|
|
|
|
commit = self._get_commit_from_timestamp(date)
|
2018-08-06 12:19:18 +00:00
|
|
|
branch = self._last_valid_branch
|
|
|
|
if commit == self._latest_commit_on_branch(branch):
|
|
|
|
commit = branch
|
2018-07-18 13:47:12 +00:00
|
|
|
self._checkout(commit)
|
2018-07-17 14:04:28 +00:00
|
|
|
|
|
|
|
def _get_commit_from_timestamp(self, date):
|
2018-08-03 09:55:51 +00:00
|
|
|
commit = self._get_stdout((
|
2018-07-17 14:04:28 +00:00
|
|
|
'git', 'rev-list',
|
|
|
|
'--date=iso',
|
|
|
|
'-n', '1',
|
|
|
|
f'--before="{date.isoformat()}"',
|
2018-07-19 09:06:13 +00:00
|
|
|
self._last_valid_branch
|
2018-07-17 14:04:28 +00:00
|
|
|
))
|
2018-08-03 09:55:51 +00:00
|
|
|
if not commit:
|
|
|
|
commit = self._get_oldest_parent_of_head()
|
|
|
|
return commit
|
|
|
|
|
|
|
|
def _get_oldest_parent_of_head(self):
|
|
|
|
return self._get_stdout((
|
|
|
|
'git',
|
|
|
|
'rev-list',
|
|
|
|
'--max-parents=0',
|
|
|
|
'HEAD'
|
|
|
|
))
|
2018-07-17 14:04:28 +00:00
|
|
|
|
2018-07-19 09:37:51 +00:00
|
|
|
@property
|
|
|
|
def _last_valid_branch(self):
|
|
|
|
if not self._head_detached:
|
|
|
|
self.__last_valid_branch = self._branch
|
2018-07-19 12:25:25 +00:00
|
|
|
return self.__last_valid_branch
|
2018-07-19 09:37:51 +00:00
|
|
|
|
2018-08-06 12:19:18 +00:00
|
|
|
def _latest_commit_on_branch(self, branch):
|
|
|
|
return self._get_stdout((
|
|
|
|
'git', 'log',
|
|
|
|
'-n', '1',
|
|
|
|
'--pretty=format:%H',
|
|
|
|
branch
|
|
|
|
))
|
|
|
|
|
2018-07-18 13:47:12 +00:00
|
|
|
@property
|
|
|
|
def all_timelines(self):
|
|
|
|
return self._branches
|
|
|
|
|
2018-07-19 09:06:13 +00:00
|
|
|
@property
|
|
|
|
def _branches(self):
|
|
|
|
git_branch_output = self._get_stdout(('git', 'branch'))
|
|
|
|
regex_pattern = re.compile(r'(?:[^\S\n]|[*])') # matches '*' and non-newline whitespace chars
|
|
|
|
return re.sub(regex_pattern, '', git_branch_output).splitlines()
|
|
|
|
|
2018-07-18 13:47:12 +00:00
|
|
|
@property
|
|
|
|
def timeline(self):
|
2018-07-19 12:29:14 +00:00
|
|
|
return self._last_valid_branch
|
2018-07-18 13:47:12 +00:00
|
|
|
|
|
|
|
@timeline.setter
|
|
|
|
def timeline(self, value):
|
|
|
|
self._checkout(value)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def snapshots(self):
|
|
|
|
return self._pretty_log_branch()
|
|
|
|
|
|
|
|
def _pretty_log_branch(self):
|
|
|
|
git_log_output = self._get_stdout((
|
|
|
|
'git', 'log',
|
|
|
|
'--pretty=%H@%aI'
|
|
|
|
))
|
|
|
|
|
|
|
|
commits = []
|
|
|
|
for line in git_log_output.splitlines():
|
|
|
|
commit_hash, timestamp = line.split('@')
|
|
|
|
commits.append({
|
|
|
|
'hash': commit_hash,
|
2018-08-03 09:39:55 +00:00
|
|
|
'timestamp': dateparser.parse(timestamp)
|
2018-07-18 13:47:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return commits
|