Model the safety check Git makes before it overwrites your files.
What to build
A program that simulates editing files and switching between commits, refusing a switch that would destroy local changes.
Requirements
- Commits are named snapshots. The working tree starts as a copy of the first commit.
edit PATH CONTENTanddelete PATHchange the working tree.switch NAME: for every path whose content differs between the current commit and NAME (a file missing from one side counts as different), if the working copy differs from the current commit's version, the switch is refused. Otherwise every such path takes NAME's version (or is removed), other local changes are kept, and NAME becomes current. - For a refused switch print
error: local changes to P would be overwrittenfor each such path, sorted. For a successful one printswitched to NAME. Forshow, print the working tree aspath: contentlines sorted by path, or(empty).
Input
Line 1: c. For each commit: a line NAME k and k lines path content. Then a line with m and m commands.
Output
The lines produced by the commands.
Constraints
c ≤ 20; m ≤ 100.
Example
Input:
2
main 2
a.txt 1
b.txt 1
feature 2
a.txt 2
b.txt 1
3
edit b.txt local
switch feature
show
Output:
switched to feature
a.txt: 2
b.txt: local
Why it works
Git only replaces files that differ between the two commits, so local edits to other files travel with you, but an edit to a file it would have to replace would be lost, so it stops instead.
Examples
2 main 2 a.txt 1 b.txt 1 feature 2 a.txt 2 b.txt 1 3 edit b.txt local switch feature show
switched to feature a.txt: 2 b.txt: local
2 main 1 a.txt 1 feature 1 a.txt 2 2 edit a.txt mine switch feature
error: local changes to a.txt would be overwritten
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
For each file that differs between the current commit and the target, check whether the working copy still matches the current commit.
Hint 2
Keep the working tree as a dictionary; a deleted file is simply missing from it.
Solution
Show one correct solution (try it yourself first)
commits, order = {}, []
for _ in range(int(input())):
name, k = input().split()
commits[name] = dict(input().split() for _ in range(int(k)))
order.append(name)
current = order[0]
work = dict(commits[current])
for _ in range(int(input())):
parts = input().split()
if parts[0] == "edit":
work[parts[1]] = parts[2]
elif parts[0] == "delete":
work.pop(parts[1], None)
elif parts[0] == "show":
print("\n".join(f"{p}: {work[p]}" for p in sorted(work)) if work else "(empty)")
else:
target = commits[parts[1]]
here = commits[current]
differing = [p for p in sorted(set(here) | set(target)) if here.get(p) != target.get(p)]
blocked = [p for p in differing if work.get(p) != here.get(p)]
if blocked:
for p in blocked:
print(f"error: local changes to {p} would be overwritten")
continue
for p in differing:
if p in target:
work[p] = target[p]
else:
work.pop(p, None)
current = parts[1]
print("switched to", current)