Skip to content

Commit

Permalink
feat: added get_parent_revision()
Browse files Browse the repository at this point in the history
  • Loading branch information
redimp committed Aug 13, 2024
1 parent 008a562 commit 521a8a4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
22 changes: 20 additions & 2 deletions otterwiki/gitstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,16 @@ def log(self, filename=None, fail_on_git_error=False):
return []
else:
try:
rawlog = self.repo.git.log("--name-only", "-z", "--follow", filename)
rawlog = self.repo.git.log("--name-only", "-z", "--follow", "--", filename)
except (git.exc.GitCommandError) as e:
raise StorageNotFound(str(e))

rawlog = rawlog.strip("\x00").split("\x00\x00")
# clean up artifacts
rawlog = [entry for entry in rawlog.strip("\x00").split("\x00\x00") if len(entry)>0]
# raise Exception of no log entry has been found
if len(rawlog)<1:
raise StorageNotFound

return [self._get_metadata_of_log(entry) for entry in rawlog]

def log_slow(self, filename=None):
Expand Down Expand Up @@ -361,5 +366,18 @@ def show_commit(self, revision):
diff = self.repo.git.show(revision, format="%b")
return metadata, diff

def get_parent_revision(self, filename, revision):
"""
Walk the log for the given file to find the revision of the commit before the given one.
"""
log = self.log(filename)
for i, entry in enumerate(log):
if entry['revision'] == revision:
try:
return log[i+1]['revision']
except IndexError:
raise StorageNotFound
raise StorageNotFound


storage = None
29 changes: 29 additions & 0 deletions tests/test_gitstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,33 @@ def test_show_commit(storage):
assert "\n-aaa" in diff
assert "\n+bbb" in diff


def test_get_parent_revision(storage):
content = "kdfjlhg gdklfjghdf gkl;djshfg dgf;lkjhs glkshjad\n"
filename1 = "test_parentrevision1.md"
filename2 = "test_parentrevision_random.md"
filename1a = "test_parentrevision2.md"
# commit the first file, the one that will be renamed
author = ("Example Author", "[email protected]")
assert True == storage.store(
filename1, content=content, author=author, message="operation 1"
)
# commit another file
assert True == storage.store(
filename2, content=content, author=author, message="operation 2"
)
# rename
storage.rename(filename1, filename1a, author=author, message="operation 3")
# fetch the log
log = storage.log()
# get the revision of the commits matching the operations we did
revision1 = [x['revision'] for x in log if x['message']=="operation 1"][0]
revision3 = [x['revision'] for x in log if x['message']=="operation 3"][0]
# now we check the parent revision of the renamed file, with the revison of the
# commit that did the renaming as child
parent_revision = storage.get_parent_revision(filename=filename1, revision=revision3)
assert parent_revision == revision1



# vim: set et ts=8 sts=4 sw=4 ai:

0 comments on commit 521a8a4

Please sign in to comment.