@@ -457,11 +457,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
457
457
eol = pend ;
458
458
459
459
if (starts_with (p , "diff " )) {
460
- s -> file_diff_nr ++ ;
461
- ALLOC_GROW (s -> file_diff , s -> file_diff_nr ,
460
+ ALLOC_GROW_BY (s -> file_diff , s -> file_diff_nr , 1 ,
462
461
file_diff_alloc );
463
462
file_diff = s -> file_diff + s -> file_diff_nr - 1 ;
464
- memset (file_diff , 0 , sizeof (* file_diff ));
465
463
hunk = & file_diff -> head ;
466
464
hunk -> start = p - plain -> buf ;
467
465
if (colored_p )
@@ -483,11 +481,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
483
481
*/
484
482
hunk -> splittable_into ++ ;
485
483
486
- file_diff -> hunk_nr ++ ;
487
- ALLOC_GROW (file_diff -> hunk , file_diff -> hunk_nr ,
484
+ ALLOC_GROW_BY (file_diff -> hunk , file_diff -> hunk_nr , 1 ,
488
485
file_diff -> hunk_alloc );
489
486
hunk = file_diff -> hunk + file_diff -> hunk_nr - 1 ;
490
- memset (hunk , 0 , sizeof (* hunk ));
491
487
492
488
hunk -> start = p - plain -> buf ;
493
489
if (colored )
@@ -511,7 +507,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
511
507
if (file_diff -> mode_change )
512
508
BUG ("double mode change?\n\n%.*s" ,
513
509
(int )(eol - plain -> buf ), plain -> buf );
514
- if (file_diff -> hunk_nr ++ )
510
+ if (file_diff -> hunk_nr )
515
511
BUG ("mode change in the middle?\n\n%.*s" ,
516
512
(int )(eol - plain -> buf ), plain -> buf );
517
513
@@ -520,9 +516,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
520
516
* is _part of_ the header "hunk".
521
517
*/
522
518
file_diff -> mode_change = 1 ;
523
- ALLOC_GROW (file_diff -> hunk , file_diff -> hunk_nr ,
519
+ ALLOC_GROW_BY (file_diff -> hunk , file_diff -> hunk_nr , 1 ,
524
520
file_diff -> hunk_alloc );
525
- memset (file_diff -> hunk , 0 , sizeof (struct hunk ));
526
521
file_diff -> hunk -> start = p - plain -> buf ;
527
522
if (colored_p )
528
523
file_diff -> hunk -> colored_start =
@@ -1357,6 +1352,15 @@ static int patch_update_file(struct add_p_state *s,
1357
1352
struct child_process cp = CHILD_PROCESS_INIT ;
1358
1353
int colored = !!s -> colored .len , quit = 0 ;
1359
1354
enum prompt_mode_type prompt_mode_type ;
1355
+ enum {
1356
+ ALLOW_GOTO_PREVIOUS_HUNK = 1 << 0 ,
1357
+ ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK = 1 << 1 ,
1358
+ ALLOW_GOTO_NEXT_HUNK = 1 << 2 ,
1359
+ ALLOW_GOTO_NEXT_UNDECIDED_HUNK = 1 << 3 ,
1360
+ ALLOW_SEARCH_AND_GOTO = 1 << 4 ,
1361
+ ALLOW_SPLIT = 1 << 5 ,
1362
+ ALLOW_EDIT = 1 << 6
1363
+ } permitted = 0 ;
1360
1364
1361
1365
if (!file_diff -> hunk_nr )
1362
1366
return 0 ;
@@ -1393,22 +1397,35 @@ static int patch_update_file(struct add_p_state *s,
1393
1397
fputs (s -> buf .buf , stdout );
1394
1398
1395
1399
strbuf_reset (& s -> buf );
1396
- if (undecided_previous >= 0 )
1400
+ if (undecided_previous >= 0 ) {
1401
+ permitted |= ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK ;
1397
1402
strbuf_addstr (& s -> buf , ",k" );
1398
- if (hunk_index )
1403
+ }
1404
+ if (hunk_index ) {
1405
+ permitted |= ALLOW_GOTO_PREVIOUS_HUNK ;
1399
1406
strbuf_addstr (& s -> buf , ",K" );
1400
- if (undecided_next >= 0 )
1407
+ }
1408
+ if (undecided_next >= 0 ) {
1409
+ permitted |= ALLOW_GOTO_NEXT_UNDECIDED_HUNK ;
1401
1410
strbuf_addstr (& s -> buf , ",j" );
1402
- if (hunk_index + 1 < file_diff -> hunk_nr )
1411
+ }
1412
+ if (hunk_index + 1 < file_diff -> hunk_nr ) {
1413
+ permitted |= ALLOW_GOTO_NEXT_HUNK ;
1403
1414
strbuf_addstr (& s -> buf , ",J" );
1404
- if (file_diff -> hunk_nr > 1 )
1415
+ }
1416
+ if (file_diff -> hunk_nr > 1 ) {
1417
+ permitted |= ALLOW_SEARCH_AND_GOTO ;
1405
1418
strbuf_addstr (& s -> buf , ",g,/" );
1406
- if (hunk -> splittable_into > 1 )
1419
+ }
1420
+ if (hunk -> splittable_into > 1 ) {
1421
+ permitted |= ALLOW_SPLIT ;
1407
1422
strbuf_addstr (& s -> buf , ",s" );
1423
+ }
1408
1424
if (hunk_index + 1 > file_diff -> mode_change &&
1409
- !file_diff -> deleted )
1425
+ !file_diff -> deleted ) {
1426
+ permitted |= ALLOW_EDIT ;
1410
1427
strbuf_addstr (& s -> buf , ",e" );
1411
-
1428
+ }
1412
1429
if (file_diff -> deleted )
1413
1430
prompt_mode_type = PROMPT_DELETION ;
1414
1431
else if (file_diff -> added )
@@ -1457,30 +1474,30 @@ static int patch_update_file(struct add_p_state *s,
1457
1474
break ;
1458
1475
}
1459
1476
} else if (s -> answer .buf [0 ] == 'K' ) {
1460
- if (hunk_index )
1477
+ if (permitted & ALLOW_GOTO_PREVIOUS_HUNK )
1461
1478
hunk_index -- ;
1462
1479
else
1463
1480
err (s , _ ("No previous hunk" ));
1464
1481
} else if (s -> answer .buf [0 ] == 'J' ) {
1465
- if (hunk_index + 1 < file_diff -> hunk_nr )
1482
+ if (permitted & ALLOW_GOTO_NEXT_HUNK )
1466
1483
hunk_index ++ ;
1467
1484
else
1468
1485
err (s , _ ("No next hunk" ));
1469
1486
} else if (s -> answer .buf [0 ] == 'k' ) {
1470
- if (undecided_previous >= 0 )
1487
+ if (permitted & ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK )
1471
1488
hunk_index = undecided_previous ;
1472
1489
else
1473
1490
err (s , _ ("No previous hunk" ));
1474
1491
} else if (s -> answer .buf [0 ] == 'j' ) {
1475
- if (undecided_next >= 0 )
1492
+ if (permitted & ALLOW_GOTO_NEXT_UNDECIDED_HUNK )
1476
1493
hunk_index = undecided_next ;
1477
1494
else
1478
1495
err (s , _ ("No next hunk" ));
1479
1496
} else if (s -> answer .buf [0 ] == 'g' ) {
1480
1497
char * pend ;
1481
1498
unsigned long response ;
1482
1499
1483
- if (file_diff -> hunk_nr < 2 ) {
1500
+ if (!( permitted & ALLOW_SEARCH_AND_GOTO ) ) {
1484
1501
err (s , _ ("No other hunks to goto" ));
1485
1502
continue ;
1486
1503
}
@@ -1517,7 +1534,7 @@ static int patch_update_file(struct add_p_state *s,
1517
1534
regex_t regex ;
1518
1535
int ret ;
1519
1536
1520
- if (file_diff -> hunk_nr < 2 ) {
1537
+ if (!( permitted & ALLOW_SEARCH_AND_GOTO ) ) {
1521
1538
err (s , _ ("No other hunks to search" ));
1522
1539
continue ;
1523
1540
}
@@ -1562,15 +1579,15 @@ static int patch_update_file(struct add_p_state *s,
1562
1579
hunk_index = i ;
1563
1580
} else if (s -> answer .buf [0 ] == 's' ) {
1564
1581
size_t splittable_into = hunk -> splittable_into ;
1565
- if (splittable_into < 2 )
1582
+ if (!( permitted & ALLOW_SPLIT ) )
1566
1583
err (s , _ ("Sorry, cannot split this hunk" ));
1567
1584
else if (!split_hunk (s , file_diff ,
1568
1585
hunk - file_diff -> hunk ))
1569
1586
color_fprintf_ln (stdout , s -> s .header_color ,
1570
1587
_ ("Split into %d hunks." ),
1571
1588
(int )splittable_into );
1572
1589
} else if (s -> answer .buf [0 ] == 'e' ) {
1573
- if (hunk_index + 1 == file_diff -> mode_change )
1590
+ if (!( permitted & ALLOW_EDIT ) )
1574
1591
err (s , _ ("Sorry, cannot edit this hunk" ));
1575
1592
else if (edit_hunk_loop (s , file_diff , hunk ) >= 0 ) {
1576
1593
hunk -> use = USE_HUNK ;
0 commit comments