git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: git add -p—splitting hunks, limit is too large

From
Jeff King <peff@peff.net>
Date
Sep 2, 2016, 19:14 UTC
Message-ID
<20160902191425.ki7nfhlqgnihoqpw@sigill.intra.peff.net>
In-Reply-To
<CAEtDOuV+0CKRSu9mJa27+yQKJ-QRyNmwecrZKaaCh0St+VagZg@mail.gmail.com>
On Fri, Sep 02, 2016 at 03:36:58PM +0100, Beau Martinez wrote:
> I'd like to inquire as to why `git add -p` can only split hunks so
> much. The limit is too large; why can't you split until each hunk is
> only a line? I often have to run `edit` and split them manually
> myself.
There's some previous discussion in this thread:
  http://public-inbox.org/git/200805232221.45406.trast@student.ethz.ch/t/#u
and further back, this message:
  http://public-inbox.org/git/7vbq8v7cdx.fsf@gitster.siamese.dyndns.org/

I think one problem is that in a given contiguous hunk, not all of the lines are independent, because edits are represented as a pair of -/+ lines. E.g., if the preimage is:

  one
  two
  four
and the postimage is:
  one
  two modified
  three
  four
your diff will be:
   one
  -two
  +two modified
  +three
   four
The ideal split is two groups:
  -two
  +two modified
  +three

So you could possibly achieve that by specifying the exact line to split at. But let's imagine "two" was the missing item, and we modified "three". Then your diff is:

   one
  -three
  +two
  +three modified
   four

Now the related lines are non-adjacent! I don't think there's a general solution, and of course it can get arbitrarily complicated, with many interleaved pairs. I don't think we can rely on figuring out which lines form a pair. In this toy example it's obvious, but in real diffs the lines might not bear any resemblance.

Splitting to single lines means you need to remember to add the matched pairs, which might be arbitrarily far apart. That's not really any different than dumping the hunk in your editor, but I find there that it's easy to rearrange and group things as appropriate.

> I'd like to contribute a patch to change it, although my C is rusty.
> Are there resources that will help me to do this?

The good news (or maybe the bad) is that "add -p" is implemented entirely in Perl. :) It's in git-add--interactive.perl.

-Peff
Previous: Beau MartinezNext: Christian Neukirchen
Message 2 of 5 in “git add -p—splitting hunks, limit is too large”
  1. Beau MartinezSep 2, 2016
  2. Jeff KingSep 2, 2016
  3. Christian NeukirchenSep 2, 2016
  4. Johannes SchindelinSep 4, 2016
  5. Johannes SchindelinSep 4, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.