Skip to main content

conflict-parser

PyPI version Python versions License: MIT Downloads

Parse, analyse, and resolve Git merge-conflict files in pure Python

conflict-parser turns any file that contains Git conflict markers (<<<<<<<, |||||||, =======, >>>>>>>) into a structured, easy-to-inspect object. No external dependencies, no shelling out to Git — just a small, single-pass state machine.


✨ Features

  • Supports both conflict styles merge (<<<<<<< / ======= / >>>>>>>) and diff3 (<<<<<<< / ||||||| / ======= / >>>>>>>)
  • Line-accurate segmentation into:
    • ContextSegment – unchanged regions
    • ConflictSegment – full metadata + separate ours/base/theirs lines
  • Round-trip safe – regenerate the exact original text
  • One-liner conflict resolution – take_ours / take_theirs
  • Pure Python ≥ 3.10, zero run-time dependencies
  • Typed everywhere (PEP 561) & 100 % test coverage via pytest

📦 Installation

pip install conflict-parser

🚀 Quick-start

from conflict_parser import MergeMetadata, MergedFile

raw = (
    "line1\n"
    "<<<<<<< HEAD\n"
    "ours1\n"
    "ours2\n"
    "=======\n"
    "theirs1\n"
    "theirs2\n"
    ">>>>>>> feature-branch\n"
    "line_after\n"
)

# 1) Parse --------------------------------------------------------------
meta = MergeMetadata(conflict_style="merge")      # or "diff3"
mf   = MergedFile.from_content("demo.txt", raw, meta)

print(len(mf.segments))           # → 3  (context / conflict / context)

# 2) Inspect ------------------------------------------------------------
for seg in mf.segments:
    if isinstance(seg, ConflictSegment):
        print(seg.ours_label, seg.theirs_label, seg.ours_lines)

# 3) Recreate the original text ----------------------------------------
assert mf.to_original_content() == raw

# 4) Resolve by keeping OUR side ---------------------------------------
clean = mf.resolve_conflicts("take_ours")

🧩 API in 60 seconds

Object What it represents
MergeMetadata Chosen conflict style (merge/diff3) & marker size
ContextSegment Uncontested block (start_line_no, lines)
ConflictSegment One conflict chunk; labels & ours/base/theirs text
MergedFile Wrapper holding ordered segments + helper methods

MergedFile helpers

Method Purpose
to_original_content() Losslessly regenerate the original conflicted file
resolve_conflicts(strategy="take_ours") Remove markers and keep either ours or theirs side

🔬 Testing

git clone https://github.com/jinu-jang/conflict-parser
cd conflict-parser
pip install -e ".[dev]"
pytest -q

All tests should pass — see tests/ for comprehensive use-cases.


🤝 Contributing

Pull requests are welcome! Please run black . && isort . && pytest before submitting.

  1. Fork → feature branch → PR
  2. Add/adjust tests for any new behaviour
  3. Follow conventional commit messages

📄 License

Released under the MIT License © 2025 Jinu Jang.

Metadata

Release files for conflict-parser 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for conflict-parser 0.0.2
File Size Uploaded
conflict_parser-0.0.2.tar.gz 8.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for conflict-parser 0.0.2
File Interpreter ABI Platform
conflict_parser-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 14.7 kB

Release files / conflict_parser-0.0.2.tar.gz

Download URL conflict_parser-0.0.2.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3d441e52a33703387a69d282fddcc00fb731fde24913bd124141f8910df11e0c
BLAKE2b-256 checksum
How to use checksums
56bfdcc67246ea9b7302c530db53509caa220371bf0576ad22afa43f2d971e1c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Release files / conflict_parser-0.0.2-py3-none-any.whl

Download URL conflict_parser-0.0.2-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f5d2beb03896af9b2f545b21881c722ab3f9f75891263433f95f07d2c6334da
BLAKE2b-256 checksum
How to use checksums
8620363363d102198cbf83ad8aee690cbb90e100e2f73566548e65bbba0879d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page