Git: The three reset modes

See exactly what each kind of reset changes.

What to build

A program that simulates edits, commits and resets for a one-file project.

Requirements

  • Commands: edit TEXT (working tree), add (index = working tree), commit (a new commit with the index's content right after HEAD; any later commits are discarded), reset MODE K (MODE is soft, mixed or hard; move HEAD back K commits), show. Commits are numbered c1, c2, … by position.
  • show prints HEAD=cN(CONTENT) index=X work=Y. At the start there is one commit c1 whose content is the initial text, and the index and working tree hold that text.

Input

Line 1: the initial text. Line 2: m. Then m commands.

Output

One line per show.

Constraints

m ≤ 100; texts have no spaces; resets never go before c1.

Example

Input:

v1
6
edit v2
add
commit
show
reset soft 1
show

Output:

HEAD=c2(v2) index=v2 work=v2
HEAD=c1(v1) index=v2 work=v2

Why it works

Each mode resets one more of the three places: the branch, then the index, then the working tree.

Examples

Example 1 · Input
v1
6
edit v2
add
commit
show
reset soft 1
show
Expected output
HEAD=c2(v2) index=v2 work=v2
HEAD=c1(v1) index=v2 work=v2
Example 2 · Input
a
6
edit b
add
commit
edit c
reset hard 1
show
Expected output
HEAD=c1(a) index=a work=a

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

Hints

Hint 1

Keep the list of commit contents, the position of HEAD, the index and the working tree.

Hint 2

soft moves HEAD only; mixed also sets the index to the commit's content; hard also sets the working tree.

Solution

Show one correct solution (try it yourself first)
text = input().strip()
commits, head, index, work = [text], 0, text, text
for _ in range(int(input())):
    parts = input().split()
    if parts[0] == "edit":
        work = parts[1]
    elif parts[0] == "add":
        index = work
    elif parts[0] == "commit":
        commits = commits[: head + 1] + [index]
        head += 1
    elif parts[0] == "reset":
        head -= int(parts[2])
        if parts[1] in ("mixed", "hard"):
            index = commits[head]
        if parts[1] == "hard":
            work = commits[head]
    else:
        print(f"HEAD=c{head + 1}({commits[head]}) index={index} work={work}")

Lesson: Undoing changes: restore, reset, revert and the reflog →