Make SnapshotProvider more in line with reality

This commit is contained in:
Kristóf Tóth 2018-07-19 11:06:13 +02:00
parent fa3ce317f0
commit 85523ede32

View File

@ -1,6 +1,7 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details.
import re
from subprocess import run
from getpass import getuser
from os.path import isdir
@ -21,16 +22,14 @@ class SnapshotProvider:
}
self._check_environment()
self._head_detached = False
self._branch = 'master'
self._branches = [self._branch]
self.__last_valid_branch = self._branch
self._init_repo_if_needed()
def _check_environment(self):
if not isdir(self.gitenv['GIT_DIR']) or not isdir(self.gitenv['GIT_WORK_TREE']):
raise EnvironmentError('Directories "directory" and "git_dir" must exist!')
if self._head_detached:
raise EnvironmentError(f'{self.__class__.__name__} cannot init from detached HEAD state!')
def _init_repo_if_needed(self):
if not self._repo_is_initialized():
@ -51,9 +50,6 @@ class SnapshotProvider:
'git', 'checkout',
'-b', head_hash, head_hash
))
self._branches.append(head_hash)
self._branch = head_hash
self._head_detached = False
def _get_head_hash(self):
return self._get_stdout(('git', 'rev-parse', 'HEAD'))
@ -61,7 +57,6 @@ class SnapshotProvider:
def restore_snapshot(self, date):
commit = self._get_commit_from_timestamp(date)
self._checkout(commit)
self._head_detached = True
def _get_commit_from_timestamp(self, date):
return self._get_stdout((
@ -69,7 +64,7 @@ class SnapshotProvider:
'--date=iso',
'-n', '1',
f'--before="{date.isoformat()}"',
self._branch
self._last_valid_branch
))
def _checkout(self, what):
@ -93,10 +88,34 @@ class SnapshotProvider:
def all_timelines(self):
return self._branches
@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()
@property
def timeline(self):
return self._branch
@property
def _branch(self):
return self._get_stdout((
'git', 'rev-parse',
'--abbrev-ref', 'HEAD'
))
@property
def _last_valid_branch(self):
current_branch = self._branch
if current_branch != 'HEAD':
self.__last_valid_branch = current_branch
return self.__last_valid_branch
@property
def _head_detached(self):
return self._branch == 'HEAD'
@timeline.setter
def timeline(self, value):
self._checkout(value)