MCPcopy Create free account
hub / github.com/Louisym/MiniCC / read_git_diff

Function read_git_diff

tutorials/06_system_prompt_builder.py:205–231  ·  view source on GitHub ↗

读取 git diff 快照(暂存和未暂存的修改)

(cwd: str)

Source from the content-addressed store, hash-verified

203
204
205def read_git_diff(cwd: str) -> Optional[str]:
206 """读取 git diff 快照(暂存和未暂存的修改)"""
207 sections = []
208
209 # 暂存的修改(git add 过的)
210 try:
211 staged = subprocess.run(
212 ["git", "diff", "--cached"],
213 cwd=cwd, capture_output=True, text=True, timeout=5,
214 )
215 if staged.returncode == 0 and staged.stdout.strip():
216 sections.append(f"Staged changes:\n{staged.stdout.strip()}")
217 except (subprocess.TimeoutExpired, FileNotFoundError):
218 pass
219
220 # 未暂存的修改
221 try:
222 unstaged = subprocess.run(
223 ["git", "diff"],
224 cwd=cwd, capture_output=True, text=True, timeout=5,
225 )
226 if unstaged.returncode == 0 and unstaged.stdout.strip():
227 sections.append(f"Unstaged changes:\n{unstaged.stdout.strip()}")
228 except (subprocess.TimeoutExpired, FileNotFoundError):
229 pass
230
231 return "\n\n".join(sections) if sections else None
232
233
234# ============================================================

Callers 1

discover_with_gitMethod · 0.70

Calls 2

runMethod · 0.80
appendMethod · 0.80

Tested by

no test coverage detected