Simulate a rebase replaying changes onto a new base, including the way it stops at a conflict.
What to build
A program that replays a series of commits onto a base snapshot and reports what happens.
Requirements
- For each commit in order, check every change: if the current version is OLD, apply it (NEW, or remove the file for
-); if it is already NEW, leave it; otherwise it is a conflict. A commit is applied only if none of its changes conflict; a commit all of whose changes were already present is reported as already applied. - Print
applied ID as rK MESSAGE(K counts applied commits from 1) orID already applied. At the first conflicting commit printCONFLICT (content): Merge conflict in PATHfor each conflicting path (sorted), thenerror: could not apply ID... MESSAGEand stop. Finally printsnapshot:and the files aspath=content(sorted, or(empty)).
Input
Line 1: k, then k lines path content (the base). Then c; each commit is a line ID n MESSAGE… followed by n change lines.
Output
The lines described above.
Constraints
c ≤ 50; no spaces in paths or contents.
Example
Input:
1
app.py v1
2
f1 1 Add search
search.py - s1
f2 1 Improve app
app.py v1 v2
Output:
applied f1 as r1 Add search
applied f2 as r2 Improve app
snapshot: app.py=v2 search.py=s1
Why it works
A rebase reapplies each commit's change to a new base, which works where the file is still as the commit found it and conflicts where the new base changed the same file differently.
Examples
1 app.py v1 2 f1 1 Add search search.py - s1 f2 1 Improve app app.py v1 v2
applied f1 as r1 Add search applied f2 as r2 Improve app snapshot: app.py=v2 search.py=s1
1 app.py v9 1 f1 1 Change app app.py v1 v2
CONFLICT (content): Merge conflict in app.py error: could not apply f1... Change app snapshot: app.py=v9
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Each commit to replay lists its changes as `path OLD NEW` (`-` means absent); apply a change when the current version equals OLD.
Hint 2
If the current version already equals NEW, the change is already there; otherwise it is a conflict and the rebase stops.
Solution
Show one correct solution (try it yourself first)
snapshot = dict(input().split() for _ in range(int(input())))
applied = 0
stopped = False
for _ in range(int(input())):
oid, n, message = input().split(maxsplit=2)
changes = [input().split() for _ in range(int(n))]
if stopped:
continue
conflicts, pending, fresh = [], [], False
for path, old, new in changes:
current = snapshot.get(path, "-")
if current == old:
pending.append((path, new))
fresh = True
elif current != new:
conflicts.append(path)
if conflicts:
for path in sorted(conflicts):
print(f"CONFLICT (content): Merge conflict in {path}")
print(f"error: could not apply {oid}... {message}")
stopped = True
continue
for path, new in pending:
if new == "-":
snapshot.pop(path, None)
else:
snapshot[path] = new
if fresh:
applied += 1
print(f"applied {oid} as r{applied} {message}")
else:
print(f"{oid} already applied")
print("snapshot: " + (" ".join(f"{p}={c}" for p, c in sorted(snapshot.items())) or "(empty)"))