From d306d0e484bcb2cf235e2458f57710d2681d801a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 18 Jul 2018 15:47:12 +0200 Subject: [PATCH] Implement properties to get info from SnapshotProvider instance --- lib/tfw/components/snapshot_provider.py | 42 ++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/lib/tfw/components/snapshot_provider.py b/lib/tfw/components/snapshot_provider.py index 53cb63e..1a11c51 100644 --- a/lib/tfw/components/snapshot_provider.py +++ b/lib/tfw/components/snapshot_provider.py @@ -4,6 +4,7 @@ from subprocess import run from getpass import getuser from os.path import isdir +from datetime import datetime class SnapshotProvider: @@ -15,7 +16,8 @@ class SnapshotProvider: 'GIT_AUTHOR_NAME': author, 'GIT_AUTHOR_EMAIL': '', 'GIT_COMMITTER_NAME': author, - 'GIT_COMMITTER_EMAIL': '' + 'GIT_COMMITTER_EMAIL': '', + 'GIT_PAGER': 'cat' } self._check_environment() @@ -58,7 +60,7 @@ class SnapshotProvider: def restore_snapshot(self, date): commit = self._get_commit_from_timestamp(date) - self._checkout_commit(commit) + self._checkout(commit) self._head_detached = True def _get_commit_from_timestamp(self, date): @@ -70,10 +72,10 @@ class SnapshotProvider: self._branch )) - def _checkout_commit(self, commit): + def _checkout(self, what): self._run(( 'git', 'checkout', - commit + what )) def _get_stdout(self, *args, **kwargs): @@ -85,3 +87,35 @@ class SnapshotProvider: if 'env' not in kwargs: kwargs['env'] = self.gitenv return run(*args, **kwargs) + + @property + def all_timelines(self): + return self._branches + + @property + def timeline(self): + return self._branch + + @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, + 'timestamp': datetime.fromisoformat(timestamp) + }) + + return commits