def find_common_parent(self, commit_one, commit_two): """Find the common parent between the two commits if one exists""" one = Commit(self) one.checkout(commit_one) two = Commit(self) two.checkout(commit_two) listone = one.getAncestors() listtwo = two.getAncestors() def compare(a, b): common = None for index in range(len(a)): if a[index] is not b[index]: return common common = a[index] return common if len(listone) < len(listtwo): common = compare(listone, listtwo) else: common = compare(listtwo, listone) if not common: raise NoCommonParent("The commits %s and %s do not share a common parent" % (commit_one, commit_two)) return common
def checkout(self, branch_name): try: commit_id = self.branches[branch_name] except KeyError: raise BranchNotFound("Could not find the %s branch" % branch_name) try: commit = self.checkouts[commit_id] except KeyError: commit = Commit(self) commit.checkout(commit_id) self.checkouts[commit_id] = commit branch = Branch(self, branch_name, commit) return branch
def commit(self, msg): commit = Commit(self.repository) commit.parent = self.parent commit.changelog = self.changelog commit.schema = self.schema commit.msg = msg commit.commit_id = hashlib.sha1(json.dumps(commit.toDict())).hexdigest() commit.checkedout = True self.repository.commits[commit.commit_id] = commit.toDict() self.repository.checkouts[commit.commit_id] = commit self.repository.branches[self.name] = commit.commit_id self.parent = commit self.reset()
def migrate(self, commit_one, commit_two): """Migrate from one commit to another""" parent = self.find_common_parent(commit_one, commit_two) c1 = Commit(self) c1.checkout(commit_one) c2 = Commit(self) c2.checkout(commit_two) log = c1.rollback(parent) log.extend(c2.rollforward(parent)) return log
def commit(self, branch_name, changes, msg): """Commit the given changes to the given branch_name""" old_commit = self.checkout_branch(branch_name) for change in changes: old_commit.schema.verify(change) new_commit = Commit() new_commit.msg = msg new_commit.parent = old_commit new_commit.schema = copy.deepcopy(old_commit.schema) for change in changes: change = new_commit.schema.make_change_reversible(change) change_id = hashlib.sha1(json.dumps(change)).hexdigest() self.changes[change_id] = change new_commit.schema.add(change) new_commit.changelog.append(change_id) new_commit_dict = new_commit.to_dict() new_commit_id = hashlib.sha1(json.dumps(new_commit_dict)).hexdigest() self.commits[new_commit_id] = new_commit_dict self.branches[branch_name] = new_commit_id
def checkout_commit(self, commit_id): commit = Commit() try: commit_dict = self.commits[commit_id] except KeyError: raise CommitNotFound("Could not find the commit %s" % commit_id) commit.commit_id = commit_id commit.msg = commit_dict['msg'] commit.changelog = commit_dict['changelog'] if 'parent' in commit_dict: commit.parent = self.checkout_commit(commit_dict['parent']) commit.schema = copy.deepcopy(commit.parent.schema) else: commit.parent = None commit.schema = Schema() for change_id in commit.changelog: change = self.changes[change_id] commit.schema.add(change) return commit