summaryrefslogtreecommitdiff
path: root/git_crecord/gitrepo.py
diff options
context:
space:
mode:
Diffstat (limited to 'git_crecord/gitrepo.py')
-rw-r--r--git_crecord/gitrepo.py57
1 files changed, 40 insertions, 17 deletions
diff --git a/git_crecord/gitrepo.py b/git_crecord/gitrepo.py
index c5f1843..0a3a59a 100644
--- a/git_crecord/gitrepo.py
+++ b/git_crecord/gitrepo.py
@@ -1,9 +1,14 @@
import os
import sys
+from pathlib import Path
+from typing import Optional
+
from . import util
INDEX_FILENAME = "index"
+ObjectHash = str
+
class GitTree:
def __init__(self, tree):
self._tree = tree
@@ -23,41 +28,59 @@ class GitIndex:
def __repr__(self):
return "%s(%r, %r)" % (self.__class__.__name__, self._filename, self.indextree)
- def commit(self):
- return util.systemcall(['git', 'write-tree'], onerr=RuntimeError).rstrip('\n')
+ def commit(self) -> ObjectHash:
+ return util.systemcall(
+ ['git', 'write-tree'],
+ onerr=RuntimeError,
+ encoding="ascii",
+ ).rstrip('\n')
def write(self):
GitTree(self.indextree).read()
- def backup_tree(self):
+ def backup_tree(self) -> ObjectHash:
try:
self.indextree = self.commit()
except RuntimeError as inst:
raise util.Abort('failed to read the index: %s' % inst)
return self.indextree
+
class GitRepo:
- def __init__(self, path):
+ def __init__(self, path: Optional[os.PathLike]):
try:
- self.path = util.systemcall(['git', 'rev-parse', '--show-toplevel'],
- onerr=util.Abort).rstrip('\n')
- self._controldir = util.systemcall(['git', 'rev-parse', '--git-dir']).rstrip('\n')
- if not os.path.isdir(self._controldir):
+ self.path = Path(util.systemcall(
+ ['git', 'rev-parse', '--show-toplevel'],
+ dir=path,
+ encoding="fs",
+ onerr=util.Abort
+ ).rstrip('\n'))
+ self._controldir = Path(util.systemcall(
+ ['git', 'rev-parse', '--git-dir'],
+ dir=path,
+ encoding="fs",
+ ).rstrip('\n'))
+ if not self._controldir.is_dir():
raise util.Abort
except util.Abort:
sys.exit(1)
def __repr__(self):
- return "%s(%r)" % (self.__class__.__name__, self.path)
+ return "%s(%s)" % (self.__class__.__name__, self.path)
- def controldir(self):
- return os.path.abspath(self._controldir)
+ @property
+ def controldir(self) -> Path:
+ return self._controldir.resolve()
- def index_path(self):
- return os.path.join(self.controldir(), INDEX_FILENAME)
+ @property
+ def index_path(self) -> Path:
+ return self.controldir / INDEX_FILENAME
- def open_index(self):
- return GitIndex(self.index_path())
+ def open_index(self) -> GitIndex:
+ return GitIndex(self.index_path)
- def head(self):
- return util.systemcall(['git', 'rev-parse', '--verify', '-q', 'HEAD']).rstrip('\n')
+ def head(self) -> ObjectHash:
+ return util.systemcall(
+ ['git', 'rev-parse', '--verify', '-q', 'HEAD'],
+ encoding="ascii",
+ ).rstrip('\n')