Tower
Tower

Reputation: 102755

How to programmatically add chunks to Git repository?

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?

Upvotes: 2

Views: 471

Answers (1)

FauxFaux
FauxFaux

Reputation: 2515

I went and looked at how git add -p works. You don't even want to know.

Effectively, it parses the git diff output (actually git 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 runs git apply --cached with 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 git tools is described; you can see how it's built on just the commands I showed above.

Upvotes: 4

Related Questions