Skip to contentSkip to conversation

Autosplit option for git add -p

DISCUSSION Konstantin Tokarev 4 participants 4 messages Jun 7, 2024 – Jun 8, 2024

lore
Share & tools
lore
#0 lore raw

Hello all,

Is there any option in git to make git add -p split hunks by default, just like 's' was pressed automatically? If no, how would one implement such an option?

My use case: I've recently started working with big LaTeX document having every paragraph as a single line. As a result, hunks quickly become larger than screen, so that splitting is highly desirable.

Thanks.

#1 lore raw

Hi,

Quoted 6 lines · Konstantin Tokarev

Hello all,

Is there any option in git to make git add -p split hunks by default, just like 's' was pressed automatically? If no, how would one implement such an option?

It doesn't look like there is any option to do that. About implementing it, it depends if it would be a command line option or a config option.

Anyway I think you might want to take a look at the run_add_p() function in "add-patch.c".

Quoted 3 lines

My use case: I've recently started working with big LaTeX document having every paragraph as a single line. As a result, hunks quickly become larger than screen, so that splitting is highly desirable.

Best!

#2 lore raw

Hello,

Quoted 8 lines · Christian Couder
Quoted 3 lines · Konstantin Tokarev

Is there any option in git to make git add -p split hunks by default, just like 's' was pressed automatically? If no, how would one implement such an option?

It doesn't look like there is any option to do that. About implementing it, it depends if it would be a command line option or a config option.

To me, a new command-line option would make more sense. Though, perhaps a new configuration option could also make sense when enabled for particular repositories, but that could also trigger the main red flag, which is that a repository configuration can change the user experience, so I'm not sure it wouldn't be frowned upon.

Quoted 6 lines

Anyway I think you might want to take a look at the run_add_p() function in "add-patch.c".

Quoted 3 lines

My use case: I've recently started working with big LaTeX document having every paragraph as a single line. As a result, hunks quickly become larger than screen, so that splitting is highly desirable.

#3 lore raw
In reply to Christian Couder
Quoted 3 lines · Christian Couder

It doesn't look like there is any option to do that. About implementing it, it depends if it would be a command line option or a config option.

Having these alone make no sense to me.

I'd suggest extending the lowest level (i.e. interactive prompt response) first so that a stronger 's' splits all hunks in the current file. It might be sufficient for the use case.

On top of that, it happens to be that when changes to _all_ paths want to be split, it may prove handy to also have a command line option.

But the reason why I am hesitant to endorse a command line option that splits everything is because:

(1) it is dubious to expect the user to _know_ that it makes sense to split all changes before starting "add -p" (and friends).

(2) if the user gives such an option and then in the interactive session regrets that it was too much to split all the hunks, it is unclear how the user can easily recover (there is no "merge all hunks back to the original" operation).