OXYGENHUB

Coverage / src/looks.ts 96.0%

72 of 75 lines run, 3 of 3 functions, 20 of 45 branches · run #23 on main · run missed

10×
// How an operation of the op log reads: a friendlier label, its kind, and
20×
// the codicon and colour the Operations view gives it (see style.ts).
31×
4
export interface OperationLook {
5
  /** What happened, in words: a commit's subject, "Pushed to earth". */
6
  label: string;
7
  /** The kind, shown beside it: "commit", "push". */
8
  kind: string;
9
  icon: string;
10
  color: string;
11
}
121×
130×
/** The first line of a quoted `-m` message in an operation's command. */
142×
function message(command: string): string | undefined {
152×
  const m = /-m\s+"((?:[^"\\]|\\.)*)"/.exec(command);
162×
  if (!m) return undefined;
172×
  const text = m[1].replace(/\\n/g, "\n").replace(/\\"/g, '"').replace(/\\\\/g, "\\");
182×
  return text.split("\n")[0].trim() || undefined;
192×
}
201×
211×
/** How an operation of the op log looks, from its command. */
221×
export function operationLook(command: string, snapshot: boolean): OperationLook {
2311×
  const cmd = command.replace(/^oxy\s+/, "").replace(/\s*\((?:snapshot|merged with a concurrent operation)\)\s*/g, " ").trim();
2411×
  const words = cmd.split(/\s+/);
2511×
  const [verb, sub] = words;
2611×
  const arg = (i: number) => (words[i] ?? "").replace(/^"|"$/g, "");
2711×
  if (snapshot) {
281×
    return { label: `Snapshot for ${verb}`, kind: "snapshot", icon: "circle-small", color: "descriptionForeground" };
291×
  }
3010×
  switch (verb) {
3111×
    case "commit":
3211×
      return { label: message(cmd) ?? "Committed", kind: "commit", icon: "git-commit", color: "oxygen.commit" };
3311×
    case "amend":
3411×
    case "split":
3511×
    case "squash":
3611×
    case "describe":
3711×
      return { label: message(cmd) ?? `${verb[0].toUpperCase()}${verb.slice(1)}ed`, kind: verb, icon: "edit", color: "oxygen.commit" };
3811×
    case "push":
3911×
      return { label: sub ? `Pushed to ${sub}` : "Pushed", kind: "push", icon: "cloud-upload", color: "oxygen.push" };
4011×
    case "pull":
4111×
      return { label: sub ? `Pulled from ${sub}` : "Pulled", kind: "pull", icon: "cloud-download", color: "oxygen.pull" };
4211×
    case "fetch":
4311×
      return { label: sub ? `Fetched ${sub}` : "Fetched", kind: "fetch", icon: "sync", color: "oxygen.pull" };
4411×
    case "clone":
4511×
    case "init":
4611×
      return { label: verb === "init" ? "Repository created" : "Cloned", kind: verb, icon: "repo-clone", color: "oxygen.pull" };
4711×
    case "merge":
4811×
      return { label: sub ? `Merged ${sub}` : "Merged", kind: "merge", icon: "git-merge", color: "oxygen.merge" };
4911×
    case "rebase":
5011×
    case "resolve":
5111×
      return { label: message(cmd) ?? `${verb[0].toUpperCase()}${verb.slice(1)}d`, kind: verb, icon: "git-pull-request", color: "oxygen.merge" };
5211×
    case "checkout":
5311×
      return { label: "Checked out", kind: "checkout", icon: "arrow-swap", color: "oxygen.switch" };
5411×
    case "bookmark":
5511×
    case "tag": {
562×
      const name = arg(2);
572×
      if (sub === "switch") return { label: `Switched to ${name}`, kind: "switch", icon: "arrow-swap", color: "oxygen.switch" };
581×
      const done: Record<string, string> = { create: "created", set: "moved", delete: "deleted", move: "moved" };
592×
      return { label: `${verb === "tag" ? "Tag" : "Bookmark"} ${name} ${done[sub] ?? sub}`.trim(), kind: verb, icon: verb === "tag" ? "tag" : "bookmark", color: "oxygen.bookmark" };
602×
    }
6111×
    case "undo":
6211×
    case "discard":
6311×
    case "abandon":
6411×
    case "restore":
6511×
      return { label: `${verb[0].toUpperCase()}${verb.slice(1)}`, kind: verb, icon: "discard", color: "oxygen.undo" };
6611×
    case "seal":
6711×
    case "sign":
6811×
    case "policy":
6911×
      return { label: cmd, kind: verb, icon: "shield", color: "oxygen.key" };
7011×
    case "submodule":
7111×
      return { label: `Subrepos ${sub ?? ""}`.trim(), kind: "subrepo", icon: "repo", color: "oxygen.subrepo" };
7211×
    default:
7311×
      return { label: cmd, kind: verb ?? "", icon: "circle-outline", color: "descriptionForeground" };
7411×
  }
7511×
}