Skip to content

Repository files navigation

import-resolve-cli

CI PyPI Python License: MIT

Resolve Git merge conflicts that only touch Python import lines.

$ git merge feature-branch
CONFLICT (content): Merge conflict in app.py

$ import-resolve-cli
[ok] app.py: resolved 1 import conflict

Install

pip install import-resolve-cli

Python 3.9+, no runtime dependencies.

Example

Two branches each added an import. Git left this:

import json
<<<<<<< HEAD
import os
=======
import math
>>>>>>> feature-a

print(json.dumps({'ok': True}))

After import-resolve-cli:

import json
import math
import os

print(json.dumps({'ok': True}))

Same example as files in docs/examples/.

Usage

import-resolve-cli                 # every conflicted .py file reported by git
import-resolve-cli app.py utils.py # specific files
import-resolve-cli --dry-run       # print the diff, write nothing
import-resolve-cli --check         # exit 1 if any conflict markers remain

Exit codes: 0 done, 1 conflicts left for you (or file errors), 2 usage error.

Errors are printed to stderr; status lines go to stdout.

Auto mode (optional)

Registers a merge driver for this repo only (.git/config + .git/info/attributes, nothing global or versioned):

import-resolve-cli --install-hook
import-resolve-cli --uninstall-hook

Safety

A conflict block is only rewritten if every non-blank line on both sides is a single-line import / from or a comment. Anything else is left alone:

[skip] app.py:14: left untouched for safety (non-import line: 'value = compute()')

Also skipped: multiline imports, \ continuations, indented imports.

Other notes:

  • the merged block must parse with ast.parse
  • writes are atomic (tempfile + os.replace) and keep the original file mode
  • LF/CRLF and UTF-8 BOM are preserved
  • binary files, non-UTF-8 files, and files over 5 MB are skipped
  • incomplete conflict markers are left in place and still fail --check
  • no network calls

Conflicts further down the same file stay as Git left them.

Out of scope

Out of scope Why
Business-logic conflicts Needs human intervention
Multiline import blocks Too easy to corrupt; skipped with a warning
Full isort/ruff style Run your formatter after; we sort simply
JS/TS imports Possible later
Config files No config by design

Compared to alternatives

Approach Limitation
Manual edit (GitHub docs) Slow when the only conflict is imports
git checkout --ours / --theirs Drops the other side's imports
isort / ruff / Black Fail while conflict markers are still in the file
LLM merge helpers Need an API key; results vary
Mergiraf Full structural merge driver; more setup

import-resolve-cli only handles import conflicts, with one command and no config.

Roadmap

  • JS/TS imports
  • --stage to git add resolved files

Development

pip install -e ".[dev]"
pytest
mypy
ruff check src tests
ruff format --check src tests

License

MIT © Ikrame Ih

About

Resolve Git import merge conflicts in one command · Python, stdlib only

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages