grip_hook.git¶
grip_hook.git ¶
Thin wrapper around the git CLI: repository discovery and diff collection.
LS_REMOTE_TIMEOUT
module-attribute
¶
Seconds to wait for git ls-remote before trusting the local remote-tracking refs.
COMMITS_MAX_BYTES
module-attribute
¶
Commit messages beyond this many bytes are cut before they reach the model.
EMPTY_TREES
module-attribute
¶
EMPTY_TREES = frozenset({'4b825dc642cb6eb9a060e54bf8d69288fbee4904', '6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321'})
The empty tree in SHA-1 and SHA-256 repositories: the base for a root commit.
PushedRef
dataclass
¶
One line of the pre-push hook's stdin.
Source code in src/grip_hook/git.py
parse
classmethod
¶
Parse <local ref> <local sha> <remote ref> <remote sha>; None if malformed.
DiffStats
dataclass
¶
How many lines a diff changes, to tell a diff with nothing to explain from the rest.
Source code in src/grip_hook/git.py
significant
instance-attribute
¶
Added plus removed lines once whitespace-only edits are set aside (git diff -w).
A line whose indentation changed still counts: in Python, YAML or a Makefile that
changes what the code means.
binary
class-attribute
instance-attribute
¶
A binary file changed. It has no line count, so such a diff is never trivial.
Diff
dataclass
¶
A collected diff, ready to be sent to a provider.
Source code in src/grip_hook/git.py
description
instance-attribute
¶
Human-readable description of what is being diffed, e.g. staged changes.
context
class-attribute
instance-attribute
¶
Optional text about the task the diff solves, shown to the model as data.
commits
class-attribute
instance-attribute
¶
The messages of the commits being quizzed, newest first and capped at
:data:COMMITS_MAX_BYTES, shown to the model as data. Not part of :attr:digest:
rewording a commit does not change what passed.
stats
class-attribute
instance-attribute
¶
Line counts, when git measured them. None is unknown, and never trivial.
binary_only
class-attribute
instance-attribute
¶
True when every changed file is binary: the patch has no lines to ask about.
size
class-attribute
instance-attribute
¶
Bytes in the whole patch before truncation; 0 when not known.
digest
property
¶
Stable fingerprint of the patch content, used to remember passed quizzes.
Git ¶
Run git commands inside one repository.
Source code in src/grip_hook/git.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | |
run ¶
Run git <args> and return stdout, raising :class:GitError on failure.
Source code in src/grip_hook/git.py
ok ¶
Return True when git <args> exits successfully.
has_commits ¶
root ¶
git_dir ¶
hooks_dir ¶
Where hooks live, honouring core.hooksPath.
Source code in src/grip_hook/git.py
rev_exists ¶
empty_tree ¶
unpushed_base ¶
Find a base to diff local_sha against that covers every unpushed commit.
That is the parent of the oldest commit reachable from local_sha but not from
any remote (or from remote only, when given). When that oldest commit is a root
commit the empty tree is returned so the whole history is covered. Returns None
when every commit is already on a remote.
Source code in src/grip_hook/git.py
unpushed_base_live ¶
:meth:unpushed_base against the refs remote has now, not the local copies.
Remote-tracking refs go stale: a branch deleted upstream after its pull request
merged stays in refs/remotes until git fetch --prune, so its commits look
pushed when the remote no longer has them. This asks the remote with
git ls-remote. Returns None when every commit is on the remote, or when the
remote cannot be reached (then the local refs are all there is to go on).
Source code in src/grip_hook/git.py
default_branch ¶
The branch pull requests here merge into, as a revision (origin/main).
That is what <remote>/HEAD points at, for the current branch's remote (else
origin, else the first remote), then <remote>/main or <remote>/master,
then a local main or master. None when none of them exists.
Source code in src/grip_hook/git.py
branch_base ¶
(default branch, merge base) for head: where its pull request starts.
None when there is no default branch or it shares no history with head.
Source code in src/grip_hook/git.py
commit_messages ¶
The messages of the commits in base..head, newest first, merges left out.
base may be the empty tree, for a range that starts at the root commit. The
text is cut at max_bytes, keeping the newest commits. Empty when git cannot
list the range.
Source code in src/grip_hook/git.py
current_branch ¶
The checked-out branch's short name; None on a detached HEAD.
describe_commits ¶
describe_commits(base: str, head: str, *, branch: str | None = None, remote: str | None = None, unpushed: bool = False) -> str
3 commits on feat/x โ origin: what base..head holds, said for people.
When base is the empty tree (a root commit) the count is the whole history and
the text says so instead of showing the tree's hash.
Source code in src/grip_hook/git.py
staged_files ¶
Paths with changes staged for the next commit.
unstaged_files ¶
Tracked paths changed in the working tree but not staged.
untracked_files ¶
Paths git does not track yet, leaving out what .gitignore ignores.
count_commits ¶
git rev-list --count <revs>; 0 when a revision does not resolve (no commits).
staged_diff ¶
range_diff ¶
range_diff(base: str, head: str, exclude: tuple[str, ...] = (), max_bytes: int = 200000, description: str | None = None, *, commits: bool = False) -> Diff
Changes between two revisions (base may be the empty tree).
With commits, the messages of the commits in the range come along
(:meth:commit_messages).
Source code in src/grip_hook/git.py
merge_base_diff ¶
merge_base_diff(base: str, head: str, exclude: tuple[str, ...] = (), max_bytes: int = 200000, *, commits: bool = False) -> Diff
Changes on head since it forked from base: what git means by base...head.
With commits, the messages of the commits since the fork come along. Raises
:class:GitError when a revision is unknown or the two share no history.
Source code in src/grip_hook/git.py
worktree_diff ¶
Everything since base: commits plus staged and unstaged changes.
Source code in src/grip_hook/git.py
push_diff ¶
push_diff(refs: list[PushedRef], remote: str | None = None, exclude: tuple[str, ...] = (), max_bytes: int = 200000, *, commits: bool = False) -> Diff
Combined diff of everything a git push would send upstream.
Source code in src/grip_hook/git.py
matches_path ¶
Whether the repository path path matches one of the globs in patterns.
Read the way exclude is: a pattern without / matches at any depth (*.md),
one with / from the repository root (docs/**). * and ? stay within one
directory, ** spans several, and a pattern that matches a directory (docs,
docs/) matches everything inside it.