Skip to content

The agent that grows with you

fix(update): make intent-to-add entries stashable so `hermes update` can run

neron82db64d1f10989
diff --git a/hermes_cli/plugins_cmd.py b/hermes_cli/plugins_cmd.py
index b09b20f303a..c3f8c831502 100644
--- a/hermes_cli/plugins_cmd.py
+++ b/hermes_cli/plugins_cmd.py
@@ -2021,9 +2021,16 @@ def _reapply_stash(git_exe: str, target: Path) -> bool:
def _autostash_dirty_tree(git_exe: str, target: Path) -> tuple[bool, str]:
"""Stash local edits before a pull. Returns ``(stash_created, error)``; a non-empty error means
the tree is dirty but nothing was saved, so the pull must not run."""
- status = _run_plugin_git(git_exe, target, "status", "--porcelain")
+ status = _run_plugin_git(git_exe, target, "status", "--porcelain", "-z")
if status.returncode != 0 or not status.stdout.strip():
return False, ""
+ # `git add -N` entries make `git stash push` fail outright (see update_cmd_stash), so promote them
+ # to real staged adds first; the checkout's own local edits are otherwise unstashable.
+ from hermes_cli.update_cmd_stash import _intent_to_add_paths
+
+ intent_to_add = _intent_to_add_paths(status.stdout)
+ if intent_to_add:
+ _run_plugin_git(git_exe, target, "add", "--", *intent_to_add)
pre_stash = _stash_ref(git_exe, target)
push = _run_plugin_git(
git_exe, target, "stash", "push", "--include-untracked", "-m", "hermes-plugin-update-autostash")
diff --git a/hermes_cli/update_cmd_stash.py b/hermes_cli/update_cmd_stash.py
index 70be0cb7a00..87614fe3014 100644
--- a/hermes_cli/update_cmd_stash.py
+++ b/hermes_cli/update_cmd_stash.py
@@ -34,6 +34,22 @@ def _git_quiet(git_cmd: list[str], args: list[str], cwd: Path, **kwargs):
return None
+def _intent_to_add_paths(porcelain_z: str) -> tuple[str, ...]:
+ """Paths recorded with ``git add -N`` (intent-to-add), read from NUL-separated porcelain output.
+
+ Such an entry carries the empty blob with zeroed stat data, so it is never "uptodate" and
+ ``git stash push`` rejects the whole stash with ``Entry 'X' not uptodate. Cannot merge.`` - a dead
+ end for the user, because that is the state an editor leaves behind when it shows a new file in
+ diffs. Porcelain marks it ``" A"`` (in the worktree, absent from the index); a real staged add is
+ ``"A "`` and an untracked file ``"??"``. Rename records carry a second, prefix-less field, which
+ the leading ``" A"`` test cannot match.
+ """
+ return tuple(
+ record[3:] for record in porcelain_z.split("\0")
+ if len(record) > 3 and record[0] == " " and record[1] == "A"
+ )
+
+
def _git_paths_z(git_cmd: list[str], args: list[str], cwd: Path):
"""NUL-separated path listing as a set, or None when git failed (surrogateescape keeps odd filenames)."""
result = _git_quiet(git_cmd, args, cwd, text=True, encoding="utf-8", errors="surrogateescape")
@@ -58,7 +74,7 @@ def _print_first_line(text: str) -> None:
def _stash_local_changes_if_needed(git_cmd: list[str], cwd: Path) -> Optional[str]:
from hermes_cli.update_cmd import _git_run
- status = _git_run(git_cmd, ["status", "--porcelain"], cwd, check=True)
+ status = _git_run(git_cmd, ["status", "--porcelain", "-z"], cwd, check=True)
if not status.stdout.strip():
return None
# Unmerged index entries (interrupted merge/rebase) make `git stash` fail with
@@ -67,6 +83,17 @@ def _stash_local_changes_if_needed(git_cmd: list[str], cwd: Path) -> Optional[st
print("→ Clearing unmerged index entries from a previous conflict...")
subprocess.run(git_cmd + ["reset"], cwd=cwd, capture_output=True)
+ # Intent-to-add entries are the other index state `git stash push` refuses (see
+ # _intent_to_add_paths). Promoting them to real staged adds keeps their content and is lossless
+ # for the user's work; after the restore they come back as staged additions, which is the closest
+ # `git stash` can represent.
+ intent_to_add = _intent_to_add_paths(status.stdout)
+ if intent_to_add:
+ print(f"→ Making {len(intent_to_add)} intent-to-add file(s) stashable...")
+ add = _git_run(git_cmd, ["add", "--", *intent_to_add], cwd)
+ if add.returncode != 0:
+ _print_nonempty(add.stderr)
+
stash_name = datetime.now(timezone.utc).strftime(f"{_AUTOSTASH_NAME_PREFIX}%Y%m%d-%H%M%S")
print("→ Local changes detected — stashing before update...")
prev_stash = _git_run(git_cmd, ["rev-parse", "--verify", "refs/stash"], cwd).stdout.strip()
@@ -81,6 +108,8 @@ def _stash_local_changes_if_needed(git_cmd: list[str], cwd: Path) -> Optional[st
print("✗ Could not stash local changes — update aborted.")
_print_first_line(push.stderr)
print(" Commit, stash, or clean up your local changes manually, then re-run `hermes update`.")
+ print(" (An index entry from `git add -N` is the usual cause of this error; `git add` the")
+ print(" paths it names, or `git reset` them, and the update will proceed.)")
raise subprocess.CalledProcessError(push.returncode, push.args, output=push.stdout, stderr=push.stderr)
# Non-zero but entry created: push saved everything yet couldn't delete some untracked files
# (e.g. root-owned dir). Not a failure — continue.
diff --git a/tests/hermes_cli/test_plugins_cmd.py b/tests/hermes_cli/test_plugins_cmd.py
index 18dbb7ed2b1..600ae1bd3bc 100644
--- a/tests/hermes_cli/test_plugins_cmd.py
+++ b/tests/hermes_cli/test_plugins_cmd.py
@@ -911,3 +911,37 @@ def test_portable_manifest_is_visible_to_plugin_cli(tmp_path):
"Portable test plugin",
"portable.test",
)
+
+
+def test_autostash_dirty_tree_promotes_intent_to_add_entries(tmp_path):
+ """A plugin checkout holding `git add -N` entries must still autostash.
+
+ Same class as the `hermes update` autostash: an intent-to-add entry is never "uptodate", so
+ `git stash push` refuses it. A plugin install is patched in place often enough that this state is
+ ordinary rather than exotic, and the failure would abort the plugin update with a confusing error.
+ """
+ import subprocess
+
+ from hermes_cli.plugins_cmd import _autostash_dirty_tree
+
+ def git(*args, check=True):
+ return subprocess.run(
+ ["git", *args], cwd=tmp_path, capture_output=True, text=True, check=check
+ )
+
+ git("init", "-q", "-b", "main")
+ git("config", "user.email", "[email protected]")
+ git("config", "user.name", "t")
+ (tmp_path / "README.md").write_text("plugin\n", encoding="utf-8")
+ git("add", "-A")
+ git("commit", "-qm", "init")
+
+ local = tmp_path / "local_patch.py"
+ local.write_text("PATCHED = True\n", encoding="utf-8")
+ git("add", "-N", "local_patch.py")
+ assert " A local_patch.py" in git("status", "--porcelain").stdout.splitlines()
+
+ stashed, error = _autostash_dirty_tree("git", tmp_path)
+
+ assert (stashed, error) == (True, ""), "the plugin autostash must not be blocked by i-t-a entries"
+ assert git("status", "--porcelain").stdout == ""
diff --git a/tests/hermes_cli/test_update_autostash.py b/tests/hermes_cli/test_update_autostash.py
index 265148d67dd..1ab59e69a3f 100644
--- a/tests/hermes_cli/test_update_autostash.py
+++ b/tests/hermes_cli/test_update_autostash.py
@@ -736,6 +736,49 @@ def test_update_autostash_survives_undeletable_untracked_dir(tmp_path):
os.chmod(pkg, 0o755)
+def test_autostash_survives_intent_to_add_entries(tmp_path):
+ """An index entry from `git add -N` must not block the update autostash.
+
+ Reported: `hermes update` aborted with "Entry 'tests/...' not uptodate. Cannot merge." because
+ `git add -N` records a path with the empty blob and zeroed stat data, which `git stash push`
+ refuses outright. Editors that show new files in diffs leave exactly that state behind, and the
+ update must not require the user to repair their index by hand.
+ """
+ import subprocess
+
+ def git(*args, check=True):
+ return subprocess.run(
+ ["git", *args], cwd=tmp_path, capture_output=True, text=True, check=check
+ )
+
+ git("init", "-q", "-b", "main")
+ git("config", "user.email", "[email protected]")
+ git("config", "user.name", "t")
+ (tmp_path / "tracked.txt").write_text("v1\n", encoding="utf-8")
+ git("add", "-A")
+ git("commit", "-qm", "init")
+
+ # The reported shape: a new local file recorded with `git add -N`, alongside a normal edit.
+ (tmp_path / "tracked.txt").write_text("v2 local\n", encoding="utf-8")
+ local_test = tmp_path / "tests" / "test_live_custom_provider_poll.py"
+ local_test.parent.mkdir()
+ body = "def test_poll():\n assert True\n"
+ local_test.write_text(body, encoding="utf-8")
+ git("add", "-N", "tests/test_live_custom_provider_poll.py")
+ # Precondition: git reports it as " A" (present in the worktree, absent from the index) - the
+ # intent-to-add shape that `git stash push` refuses.
+ assert " A tests/test_live_custom_provider_poll.py" in git("status", "--porcelain").stdout.splitlines()
+
+ stash_ref = hermes_main._stash_local_changes_if_needed(["git"], tmp_path)
+
+ assert stash_ref, "the update must be able to stash an intent-to-add entry"
+ # The stash must have taken everything, so the pull cannot be blocked by a dirty tree.
+ assert git("status", "--porcelain").stdout == ""
+ assert hermes_main._restore_stashed_changes(["git"], tmp_path, stash_ref, prompt_user=False)
+ assert local_test.read_text(encoding="utf-8") == body
+ assert (tmp_path / "tracked.txt").read_text(encoding="utf-8") == "v2 local\n"
+
+
def test_restore_rejects_invalid_python_and_keeps_clean_updated_tree(
monkeypatch, tmp_path, capsys
):