forked from git/git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-patch.c
1777 lines (1577 loc) · 50.7 KB
/
add-patch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "git-compat-util.h"
#include "add-interactive.h"
#include "advice.h"
#include "editor.h"
#include "environment.h"
#include "gettext.h"
#include "object-name.h"
#include "read-cache-ll.h"
#include "repository.h"
#include "strbuf.h"
#include "run-command.h"
#include "strvec.h"
#include "pathspec.h"
#include "color.h"
#include "compat/terminal.h"
#include "prompt.h"
enum prompt_mode_type {
PROMPT_MODE_CHANGE = 0, PROMPT_DELETION, PROMPT_ADDITION, PROMPT_HUNK,
PROMPT_MODE_MAX, /* must be last */
};
struct patch_mode {
/*
* The magic constant 4 is chosen such that all patch modes
* provide enough space for three command-line arguments followed by a
* trailing `NULL`.
*/
const char *diff_cmd[4], *apply_args[4], *apply_check_args[4];
unsigned is_reverse:1, index_only:1, apply_for_checkout:1;
const char *prompt_mode[PROMPT_MODE_MAX];
const char *edit_hunk_hint, *help_patch_text;
};
static struct patch_mode patch_mode_add = {
.diff_cmd = { "diff-files", NULL },
.apply_args = { "--cached", NULL },
.apply_check_args = { "--cached", NULL },
.prompt_mode = {
N_("Stage mode change [y,n,q,a,d%s,?]? "),
N_("Stage deletion [y,n,q,a,d%s,?]? "),
N_("Stage addition [y,n,q,a,d%s,?]? "),
N_("Stage this hunk [y,n,q,a,d%s,?]? ")
},
.edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
"will immediately be marked for staging."),
.help_patch_text =
N_("y - stage this hunk\n"
"n - do not stage this hunk\n"
"q - quit; do not stage this hunk or any of the remaining "
"ones\n"
"a - stage this hunk and all later hunks in the file\n"
"d - do not stage this hunk or any of the later hunks in "
"the file\n")
};
static struct patch_mode patch_mode_stash = {
.diff_cmd = { "diff-index", "HEAD", NULL },
.apply_args = { "--cached", NULL },
.apply_check_args = { "--cached", NULL },
.prompt_mode = {
N_("Stash mode change [y,n,q,a,d%s,?]? "),
N_("Stash deletion [y,n,q,a,d%s,?]? "),
N_("Stash addition [y,n,q,a,d%s,?]? "),
N_("Stash this hunk [y,n,q,a,d%s,?]? "),
},
.edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
"will immediately be marked for stashing."),
.help_patch_text =
N_("y - stash this hunk\n"
"n - do not stash this hunk\n"
"q - quit; do not stash this hunk or any of the remaining "
"ones\n"
"a - stash this hunk and all later hunks in the file\n"
"d - do not stash this hunk or any of the later hunks in "
"the file\n"),
};
static struct patch_mode patch_mode_reset_head = {
.diff_cmd = { "diff-index", "--cached", NULL },
.apply_args = { "-R", "--cached", NULL },
.apply_check_args = { "-R", "--cached", NULL },
.is_reverse = 1,
.index_only = 1,
.prompt_mode = {
N_("Unstage mode change [y,n,q,a,d%s,?]? "),
N_("Unstage deletion [y,n,q,a,d%s,?]? "),
N_("Unstage addition [y,n,q,a,d%s,?]? "),
N_("Unstage this hunk [y,n,q,a,d%s,?]? "),
},
.edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
"will immediately be marked for unstaging."),
.help_patch_text =
N_("y - unstage this hunk\n"
"n - do not unstage this hunk\n"
"q - quit; do not unstage this hunk or any of the remaining "
"ones\n"
"a - unstage this hunk and all later hunks in the file\n"
"d - do not unstage this hunk or any of the later hunks in "
"the file\n"),
};
static struct patch_mode patch_mode_reset_nothead = {
.diff_cmd = { "diff-index", "-R", "--cached", NULL },
.apply_args = { "--cached", NULL },
.apply_check_args = { "--cached", NULL },
.index_only = 1,
.prompt_mode = {
N_("Apply mode change to index [y,n,q,a,d%s,?]? "),
N_("Apply deletion to index [y,n,q,a,d%s,?]? "),
N_("Apply addition to index [y,n,q,a,d%s,?]? "),
N_("Apply this hunk to index [y,n,q,a,d%s,?]? "),
},
.edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
"will immediately be marked for applying."),
.help_patch_text =
N_("y - apply this hunk to index\n"
"n - do not apply this hunk to index\n"
"q - quit; do not apply this hunk or any of the remaining "
"ones\n"
"a - apply this hunk and all later hunks in the file\n"
"d - do not apply this hunk or any of the later hunks in "
"the file\n"),
};
static struct patch_mode patch_mode_checkout_index = {
.diff_cmd = { "diff-files", NULL },
.apply_args = { "-R", NULL },
.apply_check_args = { "-R", NULL },
.is_reverse = 1,
.prompt_mode = {
N_("Discard mode change from worktree [y,n,q,a,d%s,?]? "),
N_("Discard deletion from worktree [y,n,q,a,d%s,?]? "),
N_("Discard addition from worktree [y,n,q,a,d%s,?]? "),
N_("Discard this hunk from worktree [y,n,q,a,d%s,?]? "),
},
.edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
"will immediately be marked for discarding."),
.help_patch_text =
N_("y - discard this hunk from worktree\n"
"n - do not discard this hunk from worktree\n"
"q - quit; do not discard this hunk or any of the remaining "
"ones\n"
"a - discard this hunk and all later hunks in the file\n"
"d - do not discard this hunk or any of the later hunks in "
"the file\n"),
};
static struct patch_mode patch_mode_checkout_head = {
.diff_cmd = { "diff-index", NULL },
.apply_for_checkout = 1,
.apply_check_args = { "-R", NULL },
.is_reverse = 1,
.prompt_mode = {
N_("Discard mode change from index and worktree [y,n,q,a,d%s,?]? "),
N_("Discard deletion from index and worktree [y,n,q,a,d%s,?]? "),
N_("Discard addition from index and worktree [y,n,q,a,d%s,?]? "),
N_("Discard this hunk from index and worktree [y,n,q,a,d%s,?]? "),
},
.edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
"will immediately be marked for discarding."),
.help_patch_text =
N_("y - discard this hunk from index and worktree\n"
"n - do not discard this hunk from index and worktree\n"
"q - quit; do not discard this hunk or any of the remaining "
"ones\n"
"a - discard this hunk and all later hunks in the file\n"
"d - do not discard this hunk or any of the later hunks in "
"the file\n"),
};
static struct patch_mode patch_mode_checkout_nothead = {
.diff_cmd = { "diff-index", "-R", NULL },
.apply_for_checkout = 1,
.apply_check_args = { NULL },
.prompt_mode = {
N_("Apply mode change to index and worktree [y,n,q,a,d%s,?]? "),
N_("Apply deletion to index and worktree [y,n,q,a,d%s,?]? "),
N_("Apply addition to index and worktree [y,n,q,a,d%s,?]? "),
N_("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? "),
},
.edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
"will immediately be marked for applying."),
.help_patch_text =
N_("y - apply this hunk to index and worktree\n"
"n - do not apply this hunk to index and worktree\n"
"q - quit; do not apply this hunk or any of the remaining "
"ones\n"
"a - apply this hunk and all later hunks in the file\n"
"d - do not apply this hunk or any of the later hunks in "
"the file\n"),
};
static struct patch_mode patch_mode_worktree_head = {
.diff_cmd = { "diff-index", NULL },
.apply_args = { "-R", NULL },
.apply_check_args = { "-R", NULL },
.is_reverse = 1,
.prompt_mode = {
N_("Discard mode change from worktree [y,n,q,a,d%s,?]? "),
N_("Discard deletion from worktree [y,n,q,a,d%s,?]? "),
N_("Discard addition from worktree [y,n,q,a,d%s,?]? "),
N_("Discard this hunk from worktree [y,n,q,a,d%s,?]? "),
},
.edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
"will immediately be marked for discarding."),
.help_patch_text =
N_("y - discard this hunk from worktree\n"
"n - do not discard this hunk from worktree\n"
"q - quit; do not discard this hunk or any of the remaining "
"ones\n"
"a - discard this hunk and all later hunks in the file\n"
"d - do not discard this hunk or any of the later hunks in "
"the file\n"),
};
static struct patch_mode patch_mode_worktree_nothead = {
.diff_cmd = { "diff-index", "-R", NULL },
.apply_args = { NULL },
.apply_check_args = { NULL },
.prompt_mode = {
N_("Apply mode change to worktree [y,n,q,a,d%s,?]? "),
N_("Apply deletion to worktree [y,n,q,a,d%s,?]? "),
N_("Apply addition to worktree [y,n,q,a,d%s,?]? "),
N_("Apply this hunk to worktree [y,n,q,a,d%s,?]? "),
},
.edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
"will immediately be marked for applying."),
.help_patch_text =
N_("y - apply this hunk to worktree\n"
"n - do not apply this hunk to worktree\n"
"q - quit; do not apply this hunk or any of the remaining "
"ones\n"
"a - apply this hunk and all later hunks in the file\n"
"d - do not apply this hunk or any of the later hunks in "
"the file\n"),
};
struct hunk_header {
unsigned long old_offset, old_count, new_offset, new_count;
/*
* Start/end offsets to the extra text after the second `@@` in the
* hunk header, e.g. the function signature. This is expected to
* include the newline.
*/
size_t extra_start, extra_end, colored_extra_start, colored_extra_end;
unsigned suppress_colored_line_range:1;
};
struct hunk {
size_t start, end, colored_start, colored_end, splittable_into;
ssize_t delta;
enum { UNDECIDED_HUNK = 0, SKIP_HUNK, USE_HUNK } use;
struct hunk_header header;
};
struct add_p_state {
struct add_i_state s;
struct strbuf answer, buf;
/* parsed diff */
struct strbuf plain, colored;
struct file_diff {
struct hunk head;
struct hunk *hunk;
size_t hunk_nr, hunk_alloc;
unsigned deleted:1, added:1, mode_change:1,binary:1;
} *file_diff;
size_t file_diff_nr;
/* patch mode */
struct patch_mode *mode;
const char *revision;
};
static void add_p_state_clear(struct add_p_state *s)
{
size_t i;
strbuf_release(&s->answer);
strbuf_release(&s->buf);
strbuf_release(&s->plain);
strbuf_release(&s->colored);
for (i = 0; i < s->file_diff_nr; i++)
free(s->file_diff[i].hunk);
free(s->file_diff);
clear_add_i_state(&s->s);
}
__attribute__((format (printf, 2, 3)))
static void err(struct add_p_state *s, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
fputs(s->s.error_color, stderr);
vfprintf(stderr, fmt, args);
fputs(s->s.reset_color, stderr);
fputc('\n', stderr);
va_end(args);
}
static void setup_child_process(struct add_p_state *s,
struct child_process *cp, ...)
{
va_list ap;
const char *arg;
va_start(ap, cp);
while ((arg = va_arg(ap, const char *)))
strvec_push(&cp->args, arg);
va_end(ap);
cp->git_cmd = 1;
strvec_pushf(&cp->env,
INDEX_ENVIRONMENT "=%s", s->s.r->index_file);
}
static int parse_range(const char **p,
unsigned long *offset, unsigned long *count)
{
char *pend;
*offset = strtoul(*p, &pend, 10);
if (pend == *p)
return -1;
if (*pend != ',') {
*count = 1;
*p = pend;
return 0;
}
*count = strtoul(pend + 1, (char **)p, 10);
return *p == pend + 1 ? -1 : 0;
}
static int parse_hunk_header(struct add_p_state *s, struct hunk *hunk)
{
struct hunk_header *header = &hunk->header;
const char *line = s->plain.buf + hunk->start, *p = line;
char *eol = memchr(p, '\n', s->plain.len - hunk->start);
if (!eol)
eol = s->plain.buf + s->plain.len;
if (!skip_prefix(p, "@@ -", &p) ||
parse_range(&p, &header->old_offset, &header->old_count) < 0 ||
!skip_prefix(p, " +", &p) ||
parse_range(&p, &header->new_offset, &header->new_count) < 0 ||
!skip_prefix(p, " @@", &p))
return error(_("could not parse hunk header '%.*s'"),
(int)(eol - line), line);
hunk->start = eol - s->plain.buf + (*eol == '\n');
header->extra_start = p - s->plain.buf;
header->extra_end = hunk->start;
if (!s->colored.len) {
header->colored_extra_start = header->colored_extra_end = 0;
return 0;
}
/* Now find the extra text in the colored diff */
line = s->colored.buf + hunk->colored_start;
eol = memchr(line, '\n', s->colored.len - hunk->colored_start);
if (!eol)
eol = s->colored.buf + s->colored.len;
p = memmem(line, eol - line, "@@ -", 4);
if (p && (p = memmem(p + 4, eol - p - 4, " @@", 3))) {
header->colored_extra_start = p + 3 - s->colored.buf;
} else {
/* could not parse colored hunk header, leave as-is */
header->colored_extra_start = hunk->colored_start;
header->suppress_colored_line_range = 1;
}
hunk->colored_start = eol - s->colored.buf + (*eol == '\n');
header->colored_extra_end = hunk->colored_start;
return 0;
}
static int is_octal(const char *p, size_t len)
{
if (!len)
return 0;
while (len--)
if (*p < '0' || *(p++) > '7')
return 0;
return 1;
}
static void complete_file(char marker, struct hunk *hunk)
{
if (marker == '-' || marker == '+')
/*
* Last hunk ended in non-context line (i.e. it
* appended lines to the file, so there are no
* trailing context lines).
*/
hunk->splittable_into++;
}
static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
{
struct strvec args = STRVEC_INIT;
const char *diff_algorithm = s->s.interactive_diff_algorithm;
struct strbuf *plain = &s->plain, *colored = NULL;
struct child_process cp = CHILD_PROCESS_INIT;
char *p, *pend, *colored_p = NULL, *colored_pend = NULL, marker = '\0';
size_t file_diff_alloc = 0, i, color_arg_index;
struct file_diff *file_diff = NULL;
struct hunk *hunk = NULL;
int res;
strvec_pushv(&args, s->mode->diff_cmd);
if (diff_algorithm)
strvec_pushf(&args, "--diff-algorithm=%s", diff_algorithm);
if (s->revision) {
struct object_id oid;
strvec_push(&args,
/* could be on an unborn branch */
!strcmp("HEAD", s->revision) &&
repo_get_oid(the_repository, "HEAD", &oid) ?
empty_tree_oid_hex() : s->revision);
}
color_arg_index = args.nr;
/* Use `--no-color` explicitly, just in case `diff.color = always`. */
strvec_pushl(&args, "--no-color", "--ignore-submodules=dirty", "-p",
"--", NULL);
for (i = 0; i < ps->nr; i++)
strvec_push(&args, ps->items[i].original);
setup_child_process(s, &cp, NULL);
strvec_pushv(&cp.args, args.v);
res = capture_command(&cp, plain, 0);
if (res) {
strvec_clear(&args);
return error(_("could not parse diff"));
}
if (!plain->len) {
strvec_clear(&args);
return 0;
}
strbuf_complete_line(plain);
if (want_color_fd(1, -1)) {
struct child_process colored_cp = CHILD_PROCESS_INIT;
const char *diff_filter = s->s.interactive_diff_filter;
setup_child_process(s, &colored_cp, NULL);
xsnprintf((char *)args.v[color_arg_index], 8, "--color");
strvec_pushv(&colored_cp.args, args.v);
colored = &s->colored;
res = capture_command(&colored_cp, colored, 0);
strvec_clear(&args);
if (res)
return error(_("could not parse colored diff"));
if (diff_filter) {
struct child_process filter_cp = CHILD_PROCESS_INIT;
setup_child_process(s, &filter_cp,
diff_filter, NULL);
filter_cp.git_cmd = 0;
filter_cp.use_shell = 1;
strbuf_reset(&s->buf);
if (pipe_command(&filter_cp,
colored->buf, colored->len,
&s->buf, colored->len,
NULL, 0) < 0)
return error(_("failed to run '%s'"),
diff_filter);
strbuf_swap(colored, &s->buf);
}
strbuf_complete_line(colored);
colored_p = colored->buf;
colored_pend = colored_p + colored->len;
}
strvec_clear(&args);
/* parse files and hunks */
p = plain->buf;
pend = p + plain->len;
while (p != pend) {
char *eol = memchr(p, '\n', pend - p);
const char *deleted = NULL, *mode_change = NULL;
if (!eol)
eol = pend;
if (starts_with(p, "diff ") ||
starts_with(p, "* Unmerged path ")) {
complete_file(marker, hunk);
ALLOC_GROW_BY(s->file_diff, s->file_diff_nr, 1,
file_diff_alloc);
file_diff = s->file_diff + s->file_diff_nr - 1;
hunk = &file_diff->head;
hunk->start = p - plain->buf;
if (colored_p)
hunk->colored_start = colored_p - colored->buf;
marker = '\0';
} else if (p == plain->buf)
BUG("diff starts with unexpected line:\n"
"%.*s\n", (int)(eol - p), p);
else if (file_diff->deleted)
; /* keep the rest of the file in a single "hunk" */
else if (starts_with(p, "@@ ") ||
(hunk == &file_diff->head &&
(skip_prefix(p, "deleted file", &deleted)))) {
if (marker == '-' || marker == '+')
/*
* Should not happen; previous hunk did not end
* in a context line? Handle it anyway.
*/
hunk->splittable_into++;
ALLOC_GROW_BY(file_diff->hunk, file_diff->hunk_nr, 1,
file_diff->hunk_alloc);
hunk = file_diff->hunk + file_diff->hunk_nr - 1;
hunk->start = p - plain->buf;
if (colored)
hunk->colored_start = colored_p - colored->buf;
if (deleted)
file_diff->deleted = 1;
else if (parse_hunk_header(s, hunk) < 0)
return -1;
/*
* Start counting into how many hunks this one can be
* split
*/
marker = *p;
} else if (hunk == &file_diff->head &&
starts_with(p, "new file")) {
file_diff->added = 1;
} else if (hunk == &file_diff->head &&
skip_prefix(p, "old mode ", &mode_change) &&
is_octal(mode_change, eol - mode_change)) {
if (file_diff->mode_change)
BUG("double mode change?\n\n%.*s",
(int)(eol - plain->buf), plain->buf);
if (file_diff->hunk_nr)
BUG("mode change in the middle?\n\n%.*s",
(int)(eol - plain->buf), plain->buf);
/*
* Do *not* change `hunk`: the mode change pseudo-hunk
* is _part of_ the header "hunk".
*/
file_diff->mode_change = 1;
ALLOC_GROW_BY(file_diff->hunk, file_diff->hunk_nr, 1,
file_diff->hunk_alloc);
file_diff->hunk->start = p - plain->buf;
if (colored_p)
file_diff->hunk->colored_start =
colored_p - colored->buf;
} else if (hunk == &file_diff->head &&
skip_prefix(p, "new mode ", &mode_change) &&
is_octal(mode_change, eol - mode_change)) {
/*
* Extend the "mode change" pseudo-hunk to include also
* the "new mode" line.
*/
if (!file_diff->mode_change)
BUG("'new mode' without 'old mode'?\n\n%.*s",
(int)(eol - plain->buf), plain->buf);
if (file_diff->hunk_nr != 1)
BUG("mode change in the middle?\n\n%.*s",
(int)(eol - plain->buf), plain->buf);
if (p - plain->buf != file_diff->hunk->end)
BUG("'new mode' does not immediately follow "
"'old mode'?\n\n%.*s",
(int)(eol - plain->buf), plain->buf);
} else if (hunk == &file_diff->head &&
starts_with(p, "Binary files "))
file_diff->binary = 1;
if (!!file_diff->deleted + !!file_diff->added +
!!file_diff->mode_change > 1)
BUG("diff can only contain delete *or* add *or* a "
"mode change?!?\n%.*s",
(int)(eol - (plain->buf + file_diff->head.start)),
plain->buf + file_diff->head.start);
if ((marker == '-' || marker == '+') && *p == ' ')
hunk->splittable_into++;
if (marker && *p != '\\')
marker = *p;
p = eol == pend ? pend : eol + 1;
hunk->end = p - plain->buf;
if (colored) {
char *colored_eol = memchr(colored_p, '\n',
colored_pend - colored_p);
if (colored_eol)
colored_p = colored_eol + 1;
else if (p != pend)
/* non-colored has more lines? */
goto mismatched_output;
else if (colored_p == colored_pend)
/* last line has no matching colored one? */
goto mismatched_output;
else
colored_p = colored_pend;
hunk->colored_end = colored_p - colored->buf;
}
if (mode_change) {
if (file_diff->hunk_nr != 1)
BUG("mode change in hunk #%d???",
(int)file_diff->hunk_nr);
/* Adjust the end of the "mode change" pseudo-hunk */
file_diff->hunk->end = hunk->end;
if (colored)
file_diff->hunk->colored_end = hunk->colored_end;
}
}
complete_file(marker, hunk);
/* non-colored shorter than colored? */
if (colored_p != colored_pend) {
mismatched_output:
error(_("mismatched output from interactive.diffFilter"));
advise(_("Your filter must maintain a one-to-one correspondence\n"
"between its input and output lines."));
return -1;
}
return 0;
}
static size_t find_next_line(struct strbuf *sb, size_t offset)
{
char *eol;
if (offset >= sb->len)
BUG("looking for next line beyond buffer (%d >= %d)\n%s",
(int)offset, (int)sb->len, sb->buf);
eol = memchr(sb->buf + offset, '\n', sb->len - offset);
if (!eol)
return sb->len;
return eol - sb->buf + 1;
}
static void render_hunk(struct add_p_state *s, struct hunk *hunk,
ssize_t delta, int colored, struct strbuf *out)
{
struct hunk_header *header = &hunk->header;
if (hunk->header.old_offset != 0 || hunk->header.new_offset != 0) {
/*
* Generate the hunk header dynamically, except for special
* hunks (such as the diff header).
*/
const char *p;
size_t len;
unsigned long old_offset = header->old_offset;
unsigned long new_offset = header->new_offset;
if (!colored) {
p = s->plain.buf + header->extra_start;
len = header->extra_end - header->extra_start;
} else if (header->suppress_colored_line_range) {
strbuf_add(out,
s->colored.buf + header->colored_extra_start,
header->colored_extra_end -
header->colored_extra_start);
strbuf_add(out, s->colored.buf + hunk->colored_start,
hunk->colored_end - hunk->colored_start);
return;
} else {
strbuf_addstr(out, s->s.fraginfo_color);
p = s->colored.buf + header->colored_extra_start;
len = header->colored_extra_end
- header->colored_extra_start;
}
if (s->mode->is_reverse)
old_offset -= delta;
else
new_offset += delta;
strbuf_addf(out, "@@ -%lu", old_offset);
if (header->old_count != 1)
strbuf_addf(out, ",%lu", header->old_count);
strbuf_addf(out, " +%lu", new_offset);
if (header->new_count != 1)
strbuf_addf(out, ",%lu", header->new_count);
strbuf_addstr(out, " @@");
if (len)
strbuf_add(out, p, len);
else if (colored)
strbuf_addf(out, "%s\n", s->s.reset_color);
else
strbuf_addch(out, '\n');
}
if (colored)
strbuf_add(out, s->colored.buf + hunk->colored_start,
hunk->colored_end - hunk->colored_start);
else
strbuf_add(out, s->plain.buf + hunk->start,
hunk->end - hunk->start);
}
static void render_diff_header(struct add_p_state *s,
struct file_diff *file_diff, int colored,
struct strbuf *out)
{
/*
* If there was a mode change, the first hunk is a pseudo hunk that
* corresponds to the mode line in the header. If the user did not want
* to stage that "hunk", we actually have to cut it out from the header.
*/
int skip_mode_change =
file_diff->mode_change && file_diff->hunk->use != USE_HUNK;
struct hunk *head = &file_diff->head, *first = file_diff->hunk;
if (!skip_mode_change) {
render_hunk(s, head, 0, colored, out);
return;
}
if (colored) {
const char *p = s->colored.buf;
strbuf_add(out, p + head->colored_start,
first->colored_start - head->colored_start);
strbuf_add(out, p + first->colored_end,
head->colored_end - first->colored_end);
} else {
const char *p = s->plain.buf;
strbuf_add(out, p + head->start, first->start - head->start);
strbuf_add(out, p + first->end, head->end - first->end);
}
}
/* Coalesce hunks again that were split */
static int merge_hunks(struct add_p_state *s, struct file_diff *file_diff,
size_t *hunk_index, int use_all, struct hunk *merged)
{
size_t i = *hunk_index, delta;
struct hunk *hunk = file_diff->hunk + i;
/* `header` corresponds to the merged hunk */
struct hunk_header *header = &merged->header, *next;
if (!use_all && hunk->use != USE_HUNK)
return 0;
*merged = *hunk;
/* We simply skip the colored part (if any) when merging hunks */
merged->colored_start = merged->colored_end = 0;
for (; i + 1 < file_diff->hunk_nr; i++) {
hunk++;
next = &hunk->header;
/*
* Stop merging hunks when:
*
* - the hunk is not selected for use, or
* - the hunk does not overlap with the already-merged hunk(s)
*/
if ((!use_all && hunk->use != USE_HUNK) ||
header->new_offset >= next->new_offset + merged->delta ||
header->new_offset + header->new_count
< next->new_offset + merged->delta)
break;
/*
* If the hunks were not edited, and overlap, we can simply
* extend the line range.
*/
if (merged->start < hunk->start && merged->end > hunk->start) {
merged->end = hunk->end;
merged->colored_end = hunk->colored_end;
delta = 0;
} else {
const char *plain = s->plain.buf;
size_t overlapping_line_count = header->new_offset
+ header->new_count - merged->delta
- next->new_offset;
size_t overlap_end = hunk->start;
size_t overlap_start = overlap_end;
size_t overlap_next, len, j;
/*
* One of the hunks was edited: the modified hunk was
* appended to the strbuf `s->plain`.
*
* Let's ensure that at least the last context line of
* the first hunk overlaps with the corresponding line
* of the second hunk, and then merge.
*/
for (j = 0; j < overlapping_line_count; j++) {
overlap_next = find_next_line(&s->plain,
overlap_end);
if (overlap_next > hunk->end)
BUG("failed to find %d context lines "
"in:\n%.*s",
(int)overlapping_line_count,
(int)(hunk->end - hunk->start),
plain + hunk->start);
if (plain[overlap_end] != ' ')
return error(_("expected context line "
"#%d in\n%.*s"),
(int)(j + 1),
(int)(hunk->end
- hunk->start),
plain + hunk->start);
overlap_start = overlap_end;
overlap_end = overlap_next;
}
len = overlap_end - overlap_start;
if (len > merged->end - merged->start ||
memcmp(plain + merged->end - len,
plain + overlap_start, len))
return error(_("hunks do not overlap:\n%.*s\n"
"\tdoes not end with:\n%.*s"),
(int)(merged->end - merged->start),
plain + merged->start,
(int)len, plain + overlap_start);
/*
* Since the start-end ranges are not adjacent, we
* cannot simply take the union of the ranges. To
* address that, we temporarily append the union of the
* lines to the `plain` strbuf.
*/
if (merged->end != s->plain.len) {
size_t start = s->plain.len;
strbuf_add(&s->plain, plain + merged->start,
merged->end - merged->start);
plain = s->plain.buf;
merged->start = start;
merged->end = s->plain.len;
}
strbuf_add(&s->plain,
plain + overlap_end,
hunk->end - overlap_end);
merged->end = s->plain.len;
merged->splittable_into += hunk->splittable_into;
delta = merged->delta;
merged->delta += hunk->delta;
}
header->old_count = next->old_offset + next->old_count
- header->old_offset;
header->new_count = next->new_offset + delta
+ next->new_count - header->new_offset;
}
if (i == *hunk_index)
return 0;
*hunk_index = i;
return 1;
}
static void reassemble_patch(struct add_p_state *s,
struct file_diff *file_diff, int use_all,
struct strbuf *out)
{
struct hunk *hunk;
size_t save_len = s->plain.len, i;
ssize_t delta = 0;
render_diff_header(s, file_diff, 0, out);
for (i = file_diff->mode_change; i < file_diff->hunk_nr; i++) {
struct hunk merged = { 0 };
hunk = file_diff->hunk + i;
if (!use_all && hunk->use != USE_HUNK)
delta += hunk->header.old_count
- hunk->header.new_count;
else {
/* merge overlapping hunks into a temporary hunk */
if (merge_hunks(s, file_diff, &i, use_all, &merged))
hunk = &merged;
render_hunk(s, hunk, delta, 0, out);
/*
* In case `merge_hunks()` used `plain` as a scratch
* pad (this happens when an edited hunk had to be
* coalesced with another hunk).
*/
strbuf_setlen(&s->plain, save_len);
delta += hunk->delta;
}
}
}
static int split_hunk(struct add_p_state *s, struct file_diff *file_diff,
size_t hunk_index)
{
int colored = !!s->colored.len, first = 1;
struct hunk *hunk = file_diff->hunk + hunk_index;
size_t splittable_into;
size_t end, colored_end, current, colored_current = 0, context_line_count;
struct hunk_header remaining, *header;
char marker, ch;
if (hunk_index >= file_diff->hunk_nr)
BUG("invalid hunk index: %d (must be >= 0 and < %d)",
(int)hunk_index, (int)file_diff->hunk_nr);
if (hunk->splittable_into < 2)
return 0;
splittable_into = hunk->splittable_into;
end = hunk->end;
colored_end = hunk->colored_end;
remaining = hunk->header;
file_diff->hunk_nr += splittable_into - 1;
ALLOC_GROW(file_diff->hunk, file_diff->hunk_nr, file_diff->hunk_alloc);
if (hunk_index + splittable_into < file_diff->hunk_nr)
memmove(file_diff->hunk + hunk_index + splittable_into,
file_diff->hunk + hunk_index + 1,
(file_diff->hunk_nr - hunk_index - splittable_into)
* sizeof(*hunk));
hunk = file_diff->hunk + hunk_index;
hunk->splittable_into = 1;
memset(hunk + 1, 0, (splittable_into - 1) * sizeof(*hunk));
header = &hunk->header;
header->old_count = header->new_count = 0;
current = hunk->start;
if (colored)
colored_current = hunk->colored_start;
marker = '\0';
context_line_count = 0;
while (splittable_into > 1) {
ch = s->plain.buf[current];
if (!ch)
BUG("buffer overrun while splitting hunks");
/*
* Is this the first context line after a chain of +/- lines?
* Then record the start of the next split hunk.
*/
if ((marker == '-' || marker == '+') && ch == ' ') {
first = 0;
hunk[1].start = current;
if (colored)
hunk[1].colored_start = colored_current;
context_line_count = 0;
}
/*
* Was the previous line a +/- one? Alternatively, is this the
* first line (and not a +/- one)?
*
* Then just increment the appropriate counter and continue
* with the next line.
*/
if (marker != ' ' || (ch != '-' && ch != '+')) {
next_hunk_line:
/* Comment lines are attached to the previous line */
if (ch == '\\')
ch = marker ? marker : ' ';
/* current hunk not done yet */
if (ch == ' ')
context_line_count++;
else if (ch == '-')
header->old_count++;
else if (ch == '+')
header->new_count++;
else
BUG("unhandled diff marker: '%c'", ch);
marker = ch;
current = find_next_line(&s->plain, current);
if (colored)
colored_current =
find_next_line(&s->colored,
colored_current);