from blitzdb import Object

from checkmate.lib.git.repository import Repository

class GitProject(Object):
    
    def initialize(self):
        if not hasattr(self,'path'):
            raise Exception("You must specify a file path for the repository!")
        self._repository = Repository(self.path)

    @property
    def repository(self):
        return self._repository

    def get_snapshots(self,branch = 'master'):
        commits = self.repository.get_commits(branch = branch)
        snapshots = []
        for commit in commits:
            snapshot = GitSnapshot(commit)
            snapshot.pk = snapshot.sha
            snapshot.project = self
            snapshots.append(snapshot)
        return snapshots

class GitSnapshot(Object):
    
    def get_file_revisions(self):
        files = self.project.repository.get_files_in_commit(self.sha)
        file_revisions = []
        for file_obj in files:
            file_revision = GitFileRevision(file_obj)
            file_revision.snapshot = self
            file_revision.pk = file_revision.sha
            file_revisions.append(file_revision)
        return file_revisions

    def get_diffs(self,snapshot = None):
        return self.project.repository.get_diffs(self.sha,snapshot.sha if snapshot else None)

class GitFileRevision(Object):
    
    def get_file_content(self):
        return self.snapshot.project.repository.get_file_content(self.snapshot.sha,self.path)