Metadata-Version: 2.4
Name: gitstory-cli
Version: 0.2.2
Summary: "Why is this code like this" narrative assembler: blame + commit messages + linked PRs/issues in one command.
Author-email: "Calas Automations Inc." <office@calasautomations.com>
License: MIT
Project-URL: Homepage, https://calasautomations.com
Project-URL: Repository, https://github.com/calasdan-sketch/gitstory
Project-URL: Issues, https://github.com/calasdan-sketch/gitstory/issues
Project-URL: Changelog, https://github.com/calasdan-sketch/gitstory/blob/main/CHANGELOG.md
Keywords: git,blame,history,code-archaeology,commit-messages,developer-tools
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Version Control :: Git
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

<!-- Image URLs are absolute on purpose: GitHub resolves relative paths, PyPI does not. -->
![gitstory: why is this code like this? One command.](https://raw.githubusercontent.com/calasdan-sketch/gitstory/main/docs/brand/gitstory-banner.png)

**Point gitstory at a line range and it quotes what git knows about why that code is there: who wrote it, when, the commit messages, and linked PRs.**

```sh
pip install git+https://github.com/calasdan-sketch/gitstory.git
```

Needs Python 3.10 or newer and git. Not on PyPI yet; once it is, `pip install gitstory-cli` will work (the command is still `gitstory`).

```text
$ gitstory app.py 2-3 --no-gh
gitstory: app.py:2-3

SUMMARY:
  First written by Calas Automations on 2026-11-27: add retry helper
  Last changed by Calas Automations on 2026-11-27: fix: retry 5 times, the upstream API drops the first calls
  2 commit(s) in this range's history. Original reason: subject line only, no extended message.

LINES (blame, -w -C):
  2: fc5fbbb Calas Automations 2026-11-27 —     for _ in range(5):
  3: cf33c41 Calas Automations 2026-11-27 —         try:

WHY (from the commit messages — quoted, not reconstructed):
  [fc5fbbb] fix: retry 5 times, the upstream API drops the first calls
Three tries was not enough in production.
  [cf33c41] add retry helper (no extended message — reason undocumented beyond the subject line)
  (... WHO WROTE IT, WHAT IT REPLACED, WHAT BREAKS sections omitted here; see the full example below)
```

*Real output from a throwaway two-commit repo, trimmed for length (the omitted sections are marked). A longer, untrimmed run on a real repo is [further down](#full-example).*

- **Assembles the evidence.** Blame, line history, commit messages and linked PRs/issues, in one short narrative with a cited short hash on every claim.
- **Never invents a why.** A commit message of "fix stuff" gives "reason undocumented", not a made-up story. Inference is labeled.
- **Local and read-only.** No LLM calls, no account, no telemetry; it never writes to your repo. The only network use is optional read-only `gh`.

![License](https://img.shields.io/badge/license-MIT-blue)
![Python](https://img.shields.io/badge/python-%3E%3D3.10-blue)

## Quickstart

```sh
gitstory src/app.py 42-58        # a line range
gitstory src/app.py:42           # one line
gitstory auth.py::Auth.login     # a Python function or class
```

`gitstory --help` shows these examples. Add `--json` for structured output, `--blame-only` if history is slow.

## Limits up front

- Team tier (shared narratives, IDE plugin) — later, not v0.2.0.
- LLM narration — flagged, not built (hallucinated whys).
- Native GitHub API calls — gh only, best-effort.
- History beyond the line range (no whole-file archaeology mode).
- Windows: the test suite passes on Windows (gh stubs run via .cmd
  wrappers); live use on Windows is not verified.
- Not tested here: live `gh pr view` on a `#N` reference written in a
  commit message, live review quoting, and submodules. More detail in
  [Live-tested vs fixture-driven](#live-tested-vs-fixture-driven).

## Why this exists

Every developer, every week, stares at a weird block and asks why. The
answer is scattered across blame, PRs, issues, and commit messages —
assembling it by hand takes 20 minutes and usually gets skipped, so the
weird code stays weird (or gets "fixed" and regresses).

gitstory assembles it for you: one command for a line range — blame +
linked PRs/issues + commit messages, into a short narrative: who, when,
why, what it replaced, what breaks if you touch it.

### Full example

```
gitstory shortleash/cli.py 10-12
```

```text
gitstory: shortleash/cli.py:10-12

SUMMARY:
  First written by Calas Automations on 2026-10-15: initial commit
  Last changed by Odin on 2026-10-19: v0.3.0: panel-approved improvements
  2 commit(s) in this range's history. Original reason: subject line only, no extended message.

LINES (blame, -w -C):
  10: 256b601 Odin 2026-10-19 — import json
  11: fc33d73 Calas Automations 2026-10-15 — import os
  12: fc33d73 Calas Automations 2026-10-15 — import signal

WHO WROTE IT, AND WHEN:
  256b601 Odin 2026-10-19 (line(s) 10): v0.3.0: panel-approved improvements
  fc33d73 Calas Automations 2026-10-15 (line(s) 11, 12): initial commit

WHY (from the commit messages — quoted, not reconstructed):
  [256b601] v0.3.0: panel-approved improvements
- advisor: pre-run go/no-go (baseline + fleet pool, exit 0/3, audit log)
- loop fingerprinting: identical-args / error-retry / output-geyser tripwires (default on)
- cross-harness telemetry parsers: codex, opencode, gemini (+claude), auto-detect, --harness
- downgrade: graceful step-down at --downgrade-at of --budget, receipt-chained, budget-breach only
- fleet budget pools: single-host ledger, exit-2 refusal, atomic debit, fleet status
- sim: --loop mode, distinct task descriptions, --model/--resume flags
  … (message truncated — full text in --json or --full)
  [fc33d73] initial commit (no extended message — reason undocumented beyond the subject line)

WHAT IT REPLACED:
  [256b601] these lines were added new — no prior version in this file (range history)
  [fc33d73] these lines were added new — no prior version in this file (range history)

WHAT CHANGED SINCE FIRST APPEARANCE (kind labels are message-pattern inference, not fact):
  256b601 Odin 2026-10-19 [change, classified by message pattern] [current form]: v0.3.0: panel-approved improvements
  first appearance: fc33d73 Calas Automations 2026-10-15: initial commit

WHAT BREAKS IF YOU TOUCH IT:
  - introducing commit(s) 256b601, fc33d73 also changed test file tests/test_cli.py [related by name to this file — evidence that tests exist alongside this code, not proof they cover these lines]
  - introducing commit 256b601 also changed 6 other test file(s) (unrelated by name to this file — count only, not listed; full list in --json)
  - introducing commit fc33d73 also changed 7 other test file(s) (unrelated by name to this file — count only, not listed; full list in --json)
  - file shortleash/__main__.py imports this module [heuristic: git grep for import statements — evidence of coupling, not proof these lines are covered]

LINKED PRS / ISSUES:
  none referenced in the commit messages.

Method: blame -w -C + line history + commit messages; deterministic template assembly, no LLM. INFERENCE is limited to message-pattern classification, labeled above.
```

The example above is real (one author name redacted): run against the shortleash repo on this
machine (hashes 256b601, fc33d73 are that repo's actual commits).

## Install (local)

```sh
python3 -m venv .venv
.venv/bin/pip install -e .
.venv/bin/gitstory <file> <line-range>
```

No dependencies beyond Python 3.10+ and git. No account, no network
(except optional gh enrichment), no telemetry, no phone-home.

## Usage

```
gitstory <file> <line-range> [--json] [--full] [--no-gh] [--blame-only] [--no-ignore-revs]
```

- `<line-range>`: `42` for one line, `42-58` for a range. A range may
  span multiple commits; each line is attributed to its own
  introducing commit. `gitstory a.py:40-58` and `gitstory a.py -L 40,58`
  mean the same as `gitstory a.py 40-58`. The `file:range` form splits
  on the last colon followed by digits, so `C:\repo\a.py:3` is file
  `C:\repo\a.py`, line 3.
- Python symbols: `gitstory auth.py::login` or
  `gitstory auth.py::Auth.login` resolves the function or class to its
  lines (decorators included) with Python's `ast`, from the
  working-tree file, and prints the resolved range first. Python only;
  a non-`.py` file or an unknown name exits 2.
- The repo is found from the FILE's location, not the caller's cwd:
  `gitstory /other/repo/src.py 10` from anywhere uses /other/repo.
- `--json`: structured evidence (blame entries, introducing commits,
  history, changes, breaks, references, agent trailers) instead of
  narrative text.
- `--full`: append the raw evidence dump (blame porcelain, line
  history, diffs) to the text narrative.
- `--no-gh`: skip gh PR/issue enrichment entirely.
- `--blame-only`: skip `git log -L` history (faster on long
  histories; narrative uses blame only, and says so).
- Formatting commits: if `blame.ignoreRevsFile` is configured, or the
  repo has a `.git-blame-ignore-revs` file, blame skips the revisions
  listed there and SUMMARY says how many and from which file. This
  applies to blame (LINES, WHO WROTE IT); the `git log -L` history
  still lists those commits. `--no-ignore-revs` turns it off.

Exit codes: 0 narrative produced, 2 error (not a repo, file not
tracked, bad range, range outside the file, git failure).

## What the narrative contains

- **Summary** — first written / last changed / commit count / whether
  the original reason is documented, in three lines, before the
  evidence. The fast answer; the rest is the proof.
- **Lines** — blame for the range (`-w` ignores whitespace, `-C`
  attributes lines moved from another file in the same commit to their
  origin; both labeled).
- **Who / when** — per introducing commit: author, date, subject,
  short hash.
- **Why** — the commit message, quoted. A message of "fix stuff" with
  no body yields "reason undocumented in history", not a reconstructed
  story. Text output folds long bodies (first 6 lines + a pointer to
  `--json`/`--full`); subjects cap at 120 chars in text only. Reason
  trailers in the message's last paragraph (`Why:`, `Why-Rationale:`,
  `Why-Alternatives:`, `Reason:`, `Rationale:` — the "git why"
  convention) are also quoted verbatim as `[trailer Why] …`, and the
  summary says "documented in trailer" when the first commit has one.
- **What it replaced** — the removed lines from the range-scoped diff
  of the introducing commit (or "added new" when there was no prior
  version; capped at 20 lines in text, full diff in `--json`/`--full`).
  Falls back to the commit's diff for the file, or for the path the
  code moved from, labeled as such. A previous filename equal to this
  file is normal continuation, NOT a move — only a different previous
  filename renders as "moved into this file from …".
- **What changed since first appearance** — the line range's history,
  newest first, from first appearance to now, with blame commits marked
  `[current form]`. Kind labels are message-pattern inference, stated
  in the section header.
- **What breaks if you touch it** — evidence only: later fix/revert
  touches in the range's history (classified by message pattern,
  labeled), test files changed in the introducing commits (related by
  name listed individually and deduplicated across commits, the rest
  collapsed to a count; full list in `--json`), files that import this
  module (heuristic `git grep` for import statements, word-bounded so
  `import cli` never matches `import click`; capped at 5 in text, full
  list in `--json`), and later touches mentioning breakage. If nothing
  in history says what breaks, it prints "unknown from history" — that
  absence is itself a finding.
- **Linked PRs / issues** — `#N` and `owner/repo#N` references parsed
  from commit messages. When `gh` is available AND the repo has a
  remote, PR titles/states/URLs are fetched (PR first, then issue
  fallback); GitHub Enterprise hosts work via `GH_HOST`. For a resolved
  PR, text output quotes the first 6 non-blank lines of the PR body
  (`[PR #N body]`) and up to 3 non-empty review bodies, each cut to 200
  characters (`[review by X]`) — quoted verbatim, never paraphrased;
  `--json` carries the full body and every review. An introducing
  commit whose message has no `#N` is looked up with
  `gh api repos/{owner}/{repo}/commits/{sha}/pulls`; a PR found that
  way is labelled `[found via commit->PR lookup for <sha>]`, and a failed
  or empty lookup prints an "unresolved" note, never a guessed link.
  These lookups go first and share the cap below. Enrichment is
  capped at 10 gh lookups (the rest shown unresolved with a note).
  When gh or the remote is absent, references are listed unresolved
  and the narrative says so — never a fabricated link. `--no-gh`
  disables enrichment.
- **Agent-authorship note** — only when commit metadata says so
  (e.g. a Co-Authored-By trailer naming an AI tool), quoted verbatim,
  with a count of trailer-carrying commits. Never guessed from code
  style.

## Honesty contract (this IS the product's trust)

- NEVER invent a why. The narrative distinguishes EVIDENCE (commit
  message text, blame, diffs, PR titles) from INFERENCE, and inference
  is labeled and kept to message-pattern classification only.
- Every claim traces to a cited short hash.
- Subjects are first lines, not git's paragraph-collapsed `%s`: a
  message with no blank line after the first line would otherwise
  collapse into one space-joined "subject" with an empty body.
- Blame `previous` is parent-commit bookkeeping, not move evidence:
  only a previous filename different from this file indicates a
  rename or copy origin. Lines copied from a file untouched in the
  copy commit attribute to the copy commit (verified: single `-C`
  and `-C -C` agree there) — git cannot see that copy, and the
  narrative does not pretend otherwise.
- No LLM calls. LLM narration risks hallucinated whys and breaks the
  $0 contract — flagged for a later version, not built.
- No network calls except optional read-only `gh` enrichment.
- gitstory never writes to the repo. All git invocations are reads
  (`blame`, `log -L`, `show`, `ls-files`, `rev-parse`, `remote`,
  `grep`).
- Non-UTF-8 or binary content renders with replacement characters,
  never a traceback.

## Live-tested vs fixture-driven

- **Live-tested:** the example above (real shortleash history, real
  blame ranges, real multi-commit ranges); a single-commit repo
  (adr-drift) end-to-end; a venv install check (`gitstory --version`
  → 0.2.0, real CLI run from the installed entry point); a
  never-modifies-the-repo check (`git status` clean before and after).
- **Fixture-driven:** empty-history files, uncommitted changes in
  range, ranges spanning a rename, single lines, files not in git,
  moved code (blame `-C` attribution + diff fallback), PR references
  with gh present (stub `gh` binary in PATH) and absent, AI trailers,
  fix/revert classification, `--json` structure, exit codes,
  non-UTF-8 files, merge commits (`--first-parent` file lists),
  Enterprise remotes (`GH_HOST`), the 10-reference enrichment cap,
  importer word boundaries, repo-from-file resolution.
- **Live gh, once:** on 2026-11-07, on Windows, `gitstory
  pre_commit_hooks/check_case_conflict.py:72 --blame-only` in a clone
  of pre-commit/pre-commit-hooks found PR #677 (MERGED) via the
  commit->PR lookup and quoted its body. That is one run of one path.
- **Not tested here:** live `gh pr view` on a `#N` reference written
  in a commit message, and live review quoting (the stub path proves
  the mechanics). Also: submodules.

## What's new in v0.2.0

- SUMMARY lead: first written / last changed / commit count / reason
  documented, in three lines before the evidence.
- Text output stays SHORT: WHY bodies fold after 6 lines, subjects
  cap at 120 chars, replaced lines cap at 20 — full text always in
  `--json`/`--full`.
- Breaks section strengthened: related test files deduplicated across
  commits, and importer evidence (`git grep`, word-bounded) added.
- Two honesty bugs fixed, both caught live: (1) blame `previous`
  misread as move evidence — a plain added import rendered as a
  "move"; now only a different previous filename counts. (2) git's
  `%s` collapses no-blank-line messages into one space-joined
  subject with an empty body; gitstory now splits raw messages on
  the first line itself.
- Robustness: non-UTF-8/binary content renders instead of
  trace-backing; `-L` history parser tolerates empty diffs and
  missing blank lines instead of silently dropping records; merge
  commits list files via `--first-parent`.
- Usability: repo found from the file's location (not the cwd);
  `--blame-only` skips slow `-L` history; Enterprise remotes via
  `GH_HOST`; gh enrichment capped at 10 references; timeout errors
  suggest a smaller range or `--blame-only`.
- `-C -C` tested and NOT adopted: identical attribution to `-C` on
  move-with-delete and pure-copy fixtures, at higher cost.

Changes: [CHANGELOG.md](./CHANGELOG.md). Panel review notes: [REVIEW.md](./REVIEW.md). License: MIT, see [LICENSE](./LICENSE).

---

Made by Calas Automations (CAL-us). Need help? Call Cara 431-244-9026.
