Skip to main content

Object representation of a Git merge conflict.

Project description

conflict-parser

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.1.tar.gz (7.3 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.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: conflict_parser-0.0.1.tar.gz
  • Upload date:
  • Size: 7.3 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.1.tar.gz
Algorithm Hash digest
SHA256 23a3035ac50cf0530b9488efbd75d78ac241cd563b0eac8032c636816661e6b1
MD5 3111bdf1c1feefd2d7f25d4c2b56cfc0
BLAKE2b-256 802bcaabb84429f79e6d24e9450956ef5be0960742bfc5879e83c954df566c1c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for conflict_parser-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cceb0d625b5d21572654c91ecae4b07ca9465bffc26e107bc58ea0649602a42f
MD5 818d94f26c35a38aabc5c74b5459fc28
BLAKE2b-256 0630c976deb271a36da8f0ae5062fe1ef5f432967def43ce867a0c947e630c5d

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