This repository has been archived by the owner on Mar 27, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split section out into it's own file
- Loading branch information
Showing
5 changed files
with
92 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
from doc import Document | ||
from section import Section | ||
|
||
__all__ = [ 'Document', 'Section' ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
MASTER_REF = 'refs/heads/master' | ||
SECTION_REF_PREFIX = 'refs/heads/sections/' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import pygit2 | ||
from gitutils import CommitBlob | ||
from .constants import SECTION_REF_PREFIX | ||
|
||
class Section(object): | ||
''' | ||
Represents a Section in a document | ||
''' | ||
|
||
def __init__( self, name, headCommit, repo ): | ||
''' | ||
Constructor | ||
Args: | ||
name The name of the section | ||
headCommit The head commit of the section | ||
repo The repository object | ||
''' | ||
self.name = name | ||
self.headCommit = headCommit | ||
self.repo = repo | ||
|
||
def CurrentContent( self ): | ||
''' | ||
Returns the current content of the section | ||
''' | ||
oid = self.headCommit.tree[ 0 ].oid | ||
blob = self.repo[ oid ] | ||
return blob.data | ||
|
||
def ContentHistory( self ): | ||
''' | ||
Generator function that returns the history of this section | ||
This niavely assumes there's only one parent commit | ||
on each commit, which will do for now. | ||
''' | ||
current = self.headCommit | ||
while True: | ||
oid = current.tree[ 0 ].oid | ||
yield self.repo[ oid ].data | ||
if current.parents: | ||
# current has at least one parent | ||
current = current.parents[ 0 ] | ||
else: | ||
break | ||
|
||
def SetContent( self, newContent ): | ||
''' | ||
Adds a new version of the section | ||
Args: | ||
newContent The new content of the section | ||
''' | ||
newId = CommitBlob( | ||
self.repo, | ||
newContent, | ||
self.name, | ||
'Updating section', | ||
[ self.headCommit ], | ||
SECTION_REF_PREFIX + self.name | ||
) | ||
self.headCommit = self.repo[ newId ] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters