读取 git diff 快照(暂存和未暂存的修改)
(cwd: str)
| 203 | |
| 204 | |
| 205 | def 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 | # ============================================================ |
no test coverage detected