Skip to content

Commit e10734d

Browse files
committed
fixup! built-in add -p: implement the hunk splitting feature
While in the vicinity of this code, let's document what the rather convoluted conditions in the hunk-splitting loop actually _mean_: while they look simple enough, it took a bit of brain-twisting even for the developer who wrote this code to understand after a few months what is going on in this part. And if even the person who wrote this has a hard time understanding the code... then it needs commenting ;-) Signed-off-by: Johannes Schindelin <[email protected]>
1 parent a3ed9a3 commit e10734d

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

add-patch.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,10 @@ static int split_hunk(struct add_p_state *s, struct file_diff *file_diff,
857857
if (!ch)
858858
BUG("buffer overrun while splitting hunks");
859859

860+
/*
861+
* Is this the first context line after a chain of +/- lines?
862+
* Then record the start of the next split hunk.
863+
*/
860864
if ((marker == '-' || marker == '+') && ch == ' ') {
861865
first = 0;
862866
hunk[1].start = current;
@@ -865,6 +869,16 @@ static int split_hunk(struct add_p_state *s, struct file_diff *file_diff,
865869
context_line_count = 0;
866870
}
867871

872+
/*
873+
* Was the previous line a +/- one? Alternatively, is this the
874+
* first line (and not a +/- one)?
875+
*
876+
* Then just increment the appropriate counter and continue
877+
* with the next line.
878+
*
879+
* Otherwise this is the first of a chain of +/- lines.
880+
* neither the first of a chain of context lines?
881+
*/
868882
if (marker != ' ' || (ch != '-' && ch != '+')) {
869883
next_hunk_line:
870884
/* Comment lines are attached to the previous line */

0 commit comments

Comments
 (0)