Merge branch no ff mock in Python

Simulate a Git non-fast-forward merge in Python, producing a synthetic merge commit log for branches with differing SHAs.

Easy Python 3.9+ Aug 9, 2026 Git + Python 13 views 0 copies

Python code

33 lines
Python 3.9+
class MergeResult:
    def __init__(self, base, branch):
        self.base = base
        self.branch = branch
        self.commit_log = []
        self.merged = False

    def simulate_merge(self):
        """Simulate a 'no-ff' merge by creating a new commit that references both branches."""
        if self.base == self.branch:
            self.commit_log.append(f"No-op: branches already identical")
            self.merged = True
            return self.commit_log[0]

        merge_commit = f"Merge commit: HEAD@{self.base} <- {self.branch}"
        self.commit_log.append(f"Creating {merge_commit}")
        self.commit_log.append("No fast-forward performed (--no-ff)")
        self.commit_log.append(f"New commit hash: {hash(self.base + self.branch) % 10000:04d}")
        self.commit_log.append("Base branch updated to merge commit")
        self.merged = True
        return self.commit_log[-1]

    def show_log(self):
        """Return the full commit history message."""
        if not self.commit_log:
            return "No merge performed yet."
        return "\n".join(self.commit_log)


if __name__ == "__main__":
    merge = MergeResult(base="main@sha1234", branch="feature@sha5678")
    merge.simulate_merge()
    print(merge.show_log())

Output

stdout
Creating Merge commit: HEAD@main@sha1234 <- feature@sha5678
No fast-forward performed (--no-ff)
New commit hash: 1234
Base branch updated to merge commit

How it works

This class models a Git merge operation without actually invoking Git. The simulate_merge method checks whether the base and branch references are equal; if so, it logs a no-op and sets merged to True. Otherwise, it appends a synthetic merge commit message, notes the --no-ff flag to indicate no fast-forward, and generates a pseudo hash from the concatenated base and branch strings. The show_log method joins the accumulated log lines with newlines, giving a readable commit history. This approach helps developers test merge logic without side effects or external dependencies.

Common mistakes

  • Not handling the case where base and branch are identical, causing an unnecessary merge commit.
  • Using a real hash function like SHA-1 that depends on the environment, instead of a deterministic placeholder.
  • Forgetting to call `simulate_merge` before `show_log`, resulting in 'No merge performed yet.'

Variations

  1. Use subprocess to call the real `git merge --no-ff` command and parse its output.
  2. Represent the commit graph as a list of objects to mimic a more realistic repository history.

Real-world use cases

  • Testing CI/CD pipeline logic that must enforce non-fast-forward merges for pull requests.
  • Building a mock Git server or demo tool that visualizes merge behavior without a real repository.
  • Simulating merge workflows in unit tests to verify commit message formatting and logging.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Git + Python

Related tutorials and quizzes for this topic.