Expand description
Non-interactive git add -p alternative: pick and split unified-diff hunks by
index, range, or content id.
hunkpick reads a unified diff from stdin (or a file), splits each hunk into
minimal sub-hunks, and emits only the selected ones — suitable for piping into
git apply --cached. The crate exposes the building blocks used by the CLI:
parser— parse a unified diff into amodel::Patch;select— resolve selectors (index, range,path:*, content id) and emit the chosen sub-hunks;split— split an original hunk at context boundaries;renumber— recompute new-side line numbers of a result diff;list— enumerate addressable sub-hunks (human-readable and JSON);validate— check internal consistency of a result diff;subhunk_id— stable content ids for sub-hunks;emit— render amodel::Patchback to a unified diff;error— application errors with process exit codes.
use hunkpick::{emit, model, parser, select};
// A hunk with two changes separated by context auto-splits into two sub-hunks.
let diff = "\
--- a/f
+++ b/f
@@ -1,5 +1,5 @@
a
-b
+B
c
-d
+D
e
";
let patch: model::Patch = parser::parse(diff.as_bytes())?;
// Take only the second one; the result is a diff of its own, with the new-side
// anchors recomputed so `git apply` can locate it.
let selectors = select::parse_selectors(&["2".to_string()])?;
let out = select::select(&patch, &selectors)?;
let text = String::from_utf8(emit::emit(&out)).unwrap();
assert!(text.contains("+D"));
assert!(!text.contains("+B"));See the README for the selector grammar and command reference.
Modules§
- cli
- Command-line surface: the argument types clap derives the CLI from, and colour resolution.
- emit
- Render a
model::Patchback to a unified diff, byte-for-byte for git-canonical input. - error
- Application errors and the process exit codes they map to.
- gitenv
- The environment variables that decide which repository a
gitchild acts on. The environment variables that decide which repository agitinvocation acts on. - list
- Enumerate the addressable sub-hunks of a patch, human-readable or as JSON.
- model
- The data model of a parsed diff:
model::Patch,model::FileDiff,model::Hunk. - parser
- Parse a unified diff (git or plain) into a
model::Patch. - renumber
- Recompute the new-side line numbers of a result diff from the diff itself.
New-side (
+) line numbers of a result diff. - select
- Resolve selectors (index, range,
path:*,@id,@L) and build the result patch. - split
- Split hunks: automatically at context gaps, explicitly at given lines, or down to a subset of changed lines.
- subhunk_
id - Stable, context-free content ids for sub-hunks (the
@<id>selector form). - validate
- Consistency checks for an input or result diff, plus the optional
git apply --checkgate.