Git: Simulate branches and commits

Model how commits and branch commands move branch pointers.

What to build

A program that runs branch and commit commands and prints what Git would show.

Requirements

  • Commands: commit MESSAGE… (new id c1, c2, …; parent is the current branch's tip; prints [BRANCH ID] MESSAGE), branch NAME (create at the current tip), switch NAME, switch -c NAME (create and switch), branch (list * NAME TIP for the current branch and NAME TIP for the others, sorted by name) and log (ID MESSAGE from the tip back through parents).
  • Errors: creating a branch that exists prints fatal: a branch named 'NAME' already exists; creating a branch before the first commit prints fatal: not a valid object name: 'main'; switching to a missing branch prints fatal: invalid reference: NAME. The repository starts on main with no commits.

Input

Line 1: m. Then m commands.

Output

The lines printed by the commands.

Constraints

m ≤ 200.

Example

Input:

7
commit First
commit Second
switch -c feature
commit Third
branch
switch main
log

Output:

[main c1] First
[main c2] Second
[feature c3] Third
* feature c3
  main c2
c2 Second
c1 First

Why it works

A branch is only a name for a commit, so each command just creates a commit or moves or reads these names.

Examples

Example 1 · Input
7
commit First
commit Second
switch -c feature
commit Third
branch
switch main
log
Expected output
[main c1] First
[main c2] Second
[feature c3] Third
* feature c3
  main c2
c2 Second
c1 First
Example 2 · Input
3
branch dev
switch nowhere
commit Init
Expected output
fatal: not a valid object name: 'main'
fatal: invalid reference: nowhere
[main c1] Init

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

Hints

Hint 1

Keep a dictionary of commits (id → parent and message), a dictionary of branches (name → tip) and the current branch name.

Hint 2

A new repository is on branch main with no commits, so main has no tip until the first commit.

Solution

Show one correct solution (try it yourself first)
commits, branches, current = {}, {}, "main"
for _ in range(int(input())):
    words = input().split()
    if words[0] == "commit":
        oid = f"c{len(commits) + 1}"
        message = " ".join(words[1:])
        commits[oid] = (branches.get(current), message)
        branches[current] = oid
        print(f"[{current} {oid}] {message}")
    elif words[0] == "log":
        oid = branches.get(current)
        while oid:
            print(oid, commits[oid][1])
            oid = commits[oid][0]
    elif words == ["branch"]:
        for name in sorted(branches):
            print(("* " if name == current else "  ") + f"{name} {branches[name]}")
    else:
        create = words[0] == "branch" or words[1] == "-c"
        name = words[-1]
        if create:
            if name in branches:
                print(f"fatal: a branch named '{name}' already exists")
                continue
            if current not in branches:
                print(f"fatal: not a valid object name: '{current}'")
                continue
            branches[name] = branches[current]
        if words[0] == "switch":
            if name not in branches:
                print(f"fatal: invalid reference: {name}")
                continue
            current = name

Lesson: Working with branches →