I know that I have git add -p to manually choose what chunks to add to the index. However, I am looking for a way to use the Git CLI API to programmatically add certain chunks.
Is this possible with the CLI or do I need API bindings for C# which I am using?
I went and looked at how
git add -pworks. You don’t even want to know.Effectively, it parses the
git diffoutput (actuallygit diff-files -p), and processes the diffs manually. It shows you a hunk, if you want it, it appends that to its running diff, and eventually runsgit apply --cachedwith the generated diff.I’d strongly recommend using any API available to you over trying to convince this 1,600 line patch manipulation script to do what you want.
Notable parts include the area around @hunk = coalesce_overlapping_hunks(@hunk) , where it’s just done processing the question loop for all the hunks, and is just about to compress them down into a diff for you, and around my %patch_modes = … where all the interaction with actual
gittools is described; you can see how it’s built on just the commands I showed above.