def commit(self, branch, actions, data=None): if self.current_user is None: raise LedgerException('No User Logged in') branch = "refs/heads/%s" % branch parent = '' if branch in self.repo.refs: parent = [self.repo.refs[branch]] digest = hashlib.sha256(data).hexdigest() ctime = int(time.time()) if isinstance(actions, list): actions = ". ".join(actions) s2s = Ledger.str_to_sign(ctime = ctime, parent = parent, digest = digest, actions = actions, user = repr(self.current_user)) ase = ASymEnc(self.current_user.key) sig = ase.sign(s2s) if not ase.verify(s2s, sig): raise Exception('Bah!') msg = "Actions: %s\nSig: %s\n%s" % (actions, sig, data) commit = Commit() commit.author = commit.committer = repr(self.current_user) tzo = int(tzlocal().utcoffset(datetime.datetime.now()).total_seconds()) commit.commit_timezone = commit.author_timezone = tzo commit.commit_time = commit.author_time = ctime commit.encoding = "UTF-8" commit.message = msg # SHA of an empty tree # git hash-object -t tree /dev/null commit.tree = '4b825dc642cb6eb9a060e54bf8d69288fbee4904' if parent: commit.parents = parent object_store = self.repo.object_store object_store.add_object(commit) self.repo.refs[branch] = commit.id
def walk_branch(self, branch, verify = True): branch = "refs/heads/%s" % branch if branch not in self.repo.refs: return for tx in self.repo.get_walker(include=self.repo.refs[branch]): a = tx.commit.message.split('\n', 2) actions = a[0] sig = a[1] data = a[2] actions = actions.split(':')[1].strip() sig = sig.split(':')[1].strip() s2s = Ledger.str_to_sign(ctime = tx.commit.commit_time, parent = ','.join(tx.commit.parents), digest = hashlib.sha256(data).hexdigest(), actions = actions, user = tx.commit.author) if verify: user = self.cached_users[tx.commit.author] asc = ASymEnc(user.key) if not asc.verify(s2s, sig): raise LedgerException("Commit %s has a bad sig" % tx.commit.id) if data is None and -1 != tx.commit.message.find('Merge'): continue yield data, tx.commit