From cc8becd226cd0ad2fbaab709a01ffcf49993ca67 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Fri, 6 Nov 2026 21:59:24 +0900 Subject: [PATCH] refac --- open_terminal/utils/git.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/open_terminal/utils/git.py b/open_terminal/utils/git.py index eee739c..6b09e6b 100644 --- a/open_terminal/utils/git.py +++ b/open_terminal/utils/git.py @@ -319,8 +319,25 @@ class GitRepository: code, _, _ = await self.run("rev-parse", "--verify", "HEAD", check=False) args = ["rm", "--cached", "--", *pathspecs] if code else ["reset", "HEAD", "--", *pathspecs] elif action == "discard": - # Restore tracked working-tree changes; never delete untracked files. - args = ["restore", "--worktree", "--", *pathspecs] + status = await self.status() + changed = {} + for file in status["files"]: + changed[file["path"]] = file["status"] != "untracked" + if file.get("original_path"): + changed[file["original_path"]] = True + if any(path not in changed for path in paths): + raise HTTPException(400, "Select changed files first") + tracked = [spec for path, spec in zip(paths, pathspecs) if changed[path]] + untracked = [spec for path, spec in zip(paths, pathspecs) if not changed[path]] + if tracked: + code, _, _ = await self.run("rev-parse", "--verify", "HEAD", check=False) + if code: + await self.run("rm", "-f", "--", *tracked) + else: + await self.run("restore", "--source=HEAD", "--staged", "--worktree", "--", *tracked) + if untracked: + await self.run("clean", "-f", "--", *untracked) + return {"message": ""} elif action == "commit": if not message.strip(): raise HTTPException(400, "Enter a commit message")