Git: Is this a valid branch name?

Apply Git's rules for branch names.

What to build

A program that checks branch names against Git's main naming rules.

Requirements

  • Rules, checked in this order: no space or any of ~ ^ : ? * [ \ (reason contains a forbidden character); does not start with - (starts with -); no .. (contains ..); no @{ (contains @{); no empty part, so no // and no / at either end (has an empty part); no part starts with . (a part starts with .); no part ends with .lock (a part ends with .lock); does not end with . (ends with .).
  • For each name print valid NAME or invalid NAME: REASON.

Input

Line 1: n. Then n names, one per line (a name may contain spaces).

Output

n lines.

Constraints

n ≤ 100; names of 1 to 100 characters.

Example

Input:

4
fix-login
feature/search
-oops
has space

Output:

valid fix-login
valid feature/search
invalid -oops: starts with -
invalid has space: contains a forbidden character

Why it works

These characters and patterns have special meanings in Git's revision syntax or file system, so names containing them would be ambiguous or unsafe.

Examples

Example 1 · Input
4
fix-login
feature/search
-oops
has space
Expected output
valid fix-login
valid feature/search
invalid -oops: starts with -
invalid has space: contains a forbidden character
Example 2 · Input
3
bad..name
.hidden
ends.lock
Expected output
invalid bad..name: contains ..
invalid .hidden: a part starts with .
invalid ends.lock: a part ends with .lock

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

Hints

Hint 1

Check each rule in the order listed and report the first one that fails.

Hint 2

Split the name at "/" to check its parts.

Solution

Show one correct solution (try it yourself first)
for _ in range(int(input())):
    name = input()
    parts = name.split("/")
    if any(ch in name for ch in " ~^:?*[\\"):
        reason = "contains a forbidden character"
    elif name.startswith("-"):
        reason = "starts with -"
    elif ".." in name:
        reason = "contains .."
    elif "@{" in name:
        reason = "contains @{"
    elif any(part == "" for part in parts):
        reason = "has an empty part"
    elif any(part.startswith(".") for part in parts):
        reason = "a part starts with ."
    elif any(part.endswith(".lock") for part in parts):
        reason = "a part ends with .lock"
    elif name.endswith("."):
        reason = "ends with ."
    else:
        reason = None
    print(f"valid {name}" if reason is None else f"invalid {name}: {reason}")

Lesson: Working with branches →