| 165 | |
| 166 | |
| 167 | def get_session_diff( |
| 168 | cwd: str | None = None, |
| 169 | *, |
| 170 | staged: bool = False, |
| 171 | paths: list[str] | None = None, |
| 172 | ) -> DiffResult: |
| 173 | args = ["diff"] |
| 174 | if staged: |
| 175 | args.append("--cached") |
| 176 | args.append("--stat") |
| 177 | if paths: |
| 178 | args.append("--") |
| 179 | args.extend(paths) |
| 180 | |
| 181 | stat_output = _run_git_ok(args, cwd) |
| 182 | |
| 183 | diff_args = ["diff"] |
| 184 | if staged: |
| 185 | diff_args.append("--cached") |
| 186 | if paths: |
| 187 | diff_args.append("--") |
| 188 | diff_args.extend(paths) |
| 189 | |
| 190 | diff_output = _run_git_ok(diff_args, cwd) |
| 191 | |
| 192 | files_changed = 0 |
| 193 | insertions = 0 |
| 194 | deletions = 0 |
| 195 | if stat_output: |
| 196 | lines = stat_output.strip().splitlines() |
| 197 | if lines: |
| 198 | summary = lines[-1] |
| 199 | import re |
| 200 | files_match = re.search(r"(\d+)\s+file", summary) |
| 201 | ins_match = re.search(r"(\d+)\s+insertion", summary) |
| 202 | del_match = re.search(r"(\d+)\s+deletion", summary) |
| 203 | if files_match: |
| 204 | files_changed = int(files_match.group(1)) |
| 205 | if ins_match: |
| 206 | insertions = int(ins_match.group(1)) |
| 207 | if del_match: |
| 208 | deletions = int(del_match.group(1)) |
| 209 | |
| 210 | return DiffResult( |
| 211 | diff_text=diff_output, |
| 212 | files_changed=files_changed, |
| 213 | insertions=insertions, |
| 214 | deletions=deletions, |
| 215 | ) |
| 216 | |
| 217 | |
| 218 | def get_diff_against_branch( |