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