refactor: simplify path normalization
This commit is contained in:
@@ -20,7 +20,6 @@ MAX_PROGRESS_CMD_LEN = 300
|
|||||||
MAX_FILE_CHANGES_INLINE = 3
|
MAX_FILE_CHANGES_INLINE = 3
|
||||||
|
|
||||||
FILE_CHANGE_VERB = {"add": "added", "delete": "deleted", "update": "updated"}
|
FILE_CHANGE_VERB = {"add": "added", "delete": "deleted", "update": "updated"}
|
||||||
_ABSOLUTE_PATH_PREFIXES = ("Users/", "home/", "private/", "var/", "etc/", "opt/", "usr/")
|
|
||||||
|
|
||||||
|
|
||||||
def format_changed_file_path(path: str, *, base_dir: Path | None = None) -> str:
|
def format_changed_file_path(path: str, *, base_dir: Path | None = None) -> str:
|
||||||
@@ -28,10 +27,6 @@ def format_changed_file_path(path: str, *, base_dir: Path | None = None) -> str:
|
|||||||
if raw.startswith("./"):
|
if raw.startswith("./"):
|
||||||
raw = raw[2:]
|
raw = raw[2:]
|
||||||
|
|
||||||
# Some clients may accidentally prefix "~" onto an absolute path (e.g. "~" + "/Users/…").
|
|
||||||
if raw.startswith("~/") and raw[2:].startswith(_ABSOLUTE_PATH_PREFIXES):
|
|
||||||
raw = "/" + raw[2:]
|
|
||||||
|
|
||||||
base = Path.cwd() if base_dir is None else base_dir
|
base = Path.cwd() if base_dir is None else base_dir
|
||||||
try:
|
try:
|
||||||
raw_path = Path(raw)
|
raw_path = Path(raw)
|
||||||
@@ -109,7 +104,11 @@ def format_file_change_title(action: Action, *, command_width: int | None) -> st
|
|||||||
if not isinstance(path, str) or not path:
|
if not isinstance(path, str) or not path:
|
||||||
continue
|
continue
|
||||||
kind = raw.get("kind")
|
kind = raw.get("kind")
|
||||||
verb = FILE_CHANGE_VERB.get(kind, "updated") if isinstance(kind, str) else "updated"
|
verb = (
|
||||||
|
FILE_CHANGE_VERB.get(kind, "updated")
|
||||||
|
if isinstance(kind, str)
|
||||||
|
else "updated"
|
||||||
|
)
|
||||||
rendered.append(f"{verb} {format_changed_file_path(path)}")
|
rendered.append(f"{verb} {format_changed_file_path(path)}")
|
||||||
|
|
||||||
if rendered:
|
if rendered:
|
||||||
|
|||||||
Reference in New Issue
Block a user