Skip to main content

Object representation of a Git merge conflict.

Project description

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 resolutiontake_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. Forkfeature branchPR
  2. Add/adjust tests for any new behaviour
  3. Follow conventional commit messages

📄 License

Released under the MIT License © 2025 Jinu Jang.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

conflict_parser-0.0.2.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

conflict_parser-0.0.2-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file conflict_parser-0.0.2.tar.gz.

File metadata

  • Download URL: conflict_parser-0.0.2.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for conflict_parser-0.0.2.tar.gz
Algorithm Hash digest
SHA256 3d441e52a33703387a69d282fddcc00fb731fde24913bd124141f8910df11e0c
MD5 2b4817e2f66c5109473b69a5ccd932a5
BLAKE2b-256 56bfdcc67246ea9b7302c530db53509caa220371bf0576ad22afa43f2d971e1c

See more details on using hashes here.

File details

Details for the file conflict_parser-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for conflict_parser-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4f5d2beb03896af9b2f545b21881c722ab3f9f75891263433f95f07d2c6334da
MD5 30bfd5dd400d9acee02f32ba0b0ec280
BLAKE2b-256 8620363363d102198cbf83ad8aee690cbb90e100e2f73566548e65bbba0879d6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page