History walks start from one or more tips (resolved ref names or expressions), follow parent links, optionally subtract another set of tips, then sort the result. grit-lib exposes the same machinery the CLI uses through rev_parse and rev_list.
Rev-parse
resolve_revision turns a single spec (HEAD, main, v1.0, abc1234, main^, HEAD~3, tag peelers, and more) into an ObjectId. For range endpoints and log-style DWIM, resolve_revision_for_range_end matches Git’s A..B left/right rules.
Common helpers that do not need a full repository walk:
split_double_dot_range— splitmain..featureinto two tokens (ignores...and path segments).abbreviate_ref_name— shortenrefs/heads/maintomainfor display.
Rev-list
rev_list takes positive and negative revision specs plus RevListOptions. It returns a RevListResult whose commits field is the final oid list (after skip, max-count, and reverse).
| Option | Meaning |
|---|---|
ordering |
OrderingMode — default date order, topo, author-date variants. |
first_parent |
Follow only the first parent at merges. |
max_count / skip |
Limit how many commits are returned. |
reverse |
Reverse the selected list after sorting. |
For a range main..feature, pass feature as a positive spec and main as a negative spec (or split with split_double_dot_range as the example does). That matches git rev-list main..feature.
Merge base
merge_bases_first_vs_rest finds minimal common ancestors between one commit and one or more others — the library equivalent of git merge-base A B. For diff-style “pick exactly one base or fail”, see merge_base_for_diff_two_commits.