Implement the core commands of Git over the three trees.
What to build
A program that runs a sequence of Git-like commands and prints what they report.
Requirements
- Commands:
write PATH CONTENT(create or change a file in the working tree),remove PATH(delete it from the working tree),add PATH(copy the working-tree version into the index, or remove the path from the index if the file was deleted;error: PATH did not match any filesif it is in neither),commit MESSAGE…(nothing to commitif the index equals HEAD, otherwise[N] MESSAGEwith N the commit number from 1),status(as in the previous exercise), andlog(commit numbers and messages, newest first, asN MESSAGE, orno commits yet). - The repository starts empty: HEAD, index and working tree are all empty.
Input
Line 1: m. Then m commands.
Output
The lines printed by the commands.
Constraints
m ≤ 200.
Example
Input:
6
write a.txt hello
status
add a.txt
commit Add a.txt
status
log
Output:
?? a.txt
[1] Add a.txt
nothing to commit, working tree clean
1 Add a.txt
Why it works
Each command only moves file versions between the working tree, the index and the commits, so a few dictionaries model Git's everyday behaviour exactly.
Examples
Example 1 · Input
6 write a.txt hello status add a.txt commit Add a.txt status log
Expected output
?? a.txt [1] Add a.txt nothing to commit, working tree clean 1 Add a.txt
Example 2 · Input
3 log commit Empty add ghost.txt
Expected output
no commits yet nothing to commit error: ghost.txt did not match any files
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Keep three dictionaries (HEAD, index, working tree) and a list of commits.
Hint 2
`commit` refuses when the index equals HEAD; otherwise it copies the index into a new commit, which becomes HEAD.
Solution
Show one correct solution (try it yourself first)
head, index, work, commits = {}, {}, {}, []
def status():
lines = []
for path in sorted(set(head) | set(index)):
x = "A" if path not in head else "D" if path not in index else ("M" if head[path] != index[path] else " ")
y = " " if path not in index else "D" if path not in work else ("M" if work[path] != index[path] else " ")
if x + y != " ":
lines.append(f"{x}{y} {path}")
lines += [f"?? {path}" for path in sorted(work) if path not in index]
return lines or ["nothing to commit, working tree clean"]
for _ in range(int(input())):
command, _, rest = input().partition(" ")
if command == "write":
path, content = rest.split()
work[path] = content
elif command == "remove":
work.pop(rest, None)
elif command == "add":
if rest in work:
index[rest] = work[rest]
elif rest in index:
del index[rest]
else:
print(f"error: {rest} did not match any files")
elif command == "commit":
if index == head:
print("nothing to commit")
else:
commits.append(rest)
head = dict(index)
print(f"[{len(commits)}] {rest}")
elif command == "status":
print("\n".join(status()))
elif command == "log":
print("\n".join(f"{n} {commits[n - 1]}" for n in range(len(commits), 0, -1)) if commits else "no commits yet")