def merge_squash( repo: pygit2.Repository, ours_branch: pygit2.Branch, theirs_branch: pygit2.Branch, message: str, ) -> None: """ Performs a merge of the `theirs_branch` into `ours_branch` sqaushing the commits """ merge_state, _ = repo.merge_analysis(theirs_branch.target, ours_branch.name) if merge_state & pygit2.GIT_MERGE_ANALYSIS_UP_TO_DATE: return if not (merge_state & pygit2.GIT_MERGE_ANALYSIS_FASTFORWARD): raise ValueError(theirs_branch) index: pygit2.Index = repo.merge_trees( ancestor=ours_branch, ours=ours_branch, theirs=theirs_branch ) tree = index.write_tree(repo=repo) repo.create_commit( ours_branch.name, repo.default_signature, repo.default_signature, message, tree, [ours_branch.target], )
def finish(repo: pygit2.Repository, branch_name: str, message: str) -> None: master = repo.branches.local['master'] branch = repo.branches.local[branch_name] if not branch.is_head(): raise ValueError(branch) merge_state, _ = repo.merge_analysis(branch.target, master.name) if merge_state & pygit2.GIT_MERGE_ANALYSIS_UP_TO_DATE: repo.checkout(refname=master) return if not (merge_state & pygit2.GIT_MERGE_ANALYSIS_FASTFORWARD): raise ValueError(branch) index: pygit2.Index = repo.merge_trees(ancestor=master, ours=master, theirs=branch) tree = index.write_tree(repo=repo) repo.create_commit( master.name, repo.default_signature, repo.default_signature, message, tree, [master.target], ) repo.checkout(refname=master) branch.delete()
def preview_merge(repo: pygit2.Repository, us: pygit2.Oid, them: pygit2.Oid) -> MergeResult: base = repo.merge_base(us, them) if base is None: return MergeResult(MergeStatus.no_common_ancestor) merged_index = repo.merge_trees(base, us, them) if merged_index.conflicts is None: return MergeResult(MergeStatus.no_conflicts) conflicts = set(_parse_conflicts(repo, merged_index.conflicts)) return MergeResult(MergeStatus.conflicts_with_me, conflicts)