Git: The stash stack

Simulate saving and restoring unfinished work.

What to build

A program that simulates edits and stash commands.

Requirements

  • HEAD is a fixed snapshot. Commands: edit PATH CONTENT; stash MESSAGE… (save the changed files and reset the working tree to HEAD; No local changes to save if nothing changed); list (stash@{N}: On main: MESSAGE, newest first); apply N and pop N (copy the entry's files into the working tree; refuse with error: local changes to PATH would be overwritten for each such path, sorted, if a file in the entry already differs from HEAD; pop then removes the entry); drop N; show (the working tree as path content, sorted).
  • A missing entry prints error: stash@{N} does not exist.

Input

Line 1: k, then k lines path content (HEAD and the starting working tree). Then m and m commands.

Output

The lines printed by the commands.

Constraints

m ≤ 100; no spaces in paths or contents.

Example

Input:

1
app.py v1
6
edit app.py half
stash login form
show
list
pop 0
show

Output:

app.py v1
stash@{0}: On main: login form
app.py half

Why it works

A stash entry is just the set of changes against HEAD, kept on a stack, so saving resets the tree and restoring copies the changes back if nothing would be overwritten.

Examples

Example 1 · Input
1
app.py v1
6
edit app.py half
stash login form
show
list
pop 0
show
Expected output
app.py v1
stash@{0}: On main: login form
app.py half
Example 2 · Input
1
a 1
3
stash nothing
pop 0
list
Expected output
No local changes to save
error: stash@{0} does not exist

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

A stash entry saves the working-tree files that differ from HEAD, then the working tree is reset to HEAD.

Hint 2

stash@{0} is the front of the list; pop is apply followed by drop.

Solution

Show one correct solution (try it yourself first)
head = dict(input().split() for _ in range(int(input())))
work, stash = dict(head), []
for _ in range(int(input())):
    parts = input().split()
    command = parts[0]
    if command == "edit":
        work[parts[1]] = parts[2]
    elif command == "stash":
        changed = {p: c for p, c in work.items() if head.get(p) != c}
        if not changed:
            print("No local changes to save")
        else:
            stash.insert(0, (" ".join(parts[1:]), changed))
            work = dict(head)
    elif command == "list":
        for n, (message, _) in enumerate(stash):
            print(f"stash@{{{n}}}: On main: {message}")
    elif command == "show":
        for path in sorted(work):
            print(path, work[path])
    else:
        n = int(parts[1])
        if n >= len(stash):
            print(f"error: stash@{{{n}}} does not exist")
            continue
        if command == "drop":
            stash.pop(n)
            continue
        message, changed = stash[n]
        blocked = sorted(p for p in changed if work.get(p) != head.get(p))
        if blocked:
            for path in blocked:
                print(f"error: local changes to {path} would be overwritten")
            continue
        work.update(changed)
        if command == "pop":
            stash.pop(n)

Lesson: Stashing work and tagging releases →