10
10
#include "prompt.h"
11
11
12
12
enum prompt_mode_type {
13
- PROMPT_MODE_CHANGE = 0 , PROMPT_DELETION , PROMPT_HUNK ,
13
+ PROMPT_MODE_CHANGE = 0 , PROMPT_DELETION , PROMPT_ADDITION , PROMPT_HUNK ,
14
14
PROMPT_MODE_MAX , /* must be last */
15
15
};
16
16
@@ -33,6 +33,7 @@ static struct patch_mode patch_mode_add = {
33
33
.prompt_mode = {
34
34
N_ ("Stage mode change [y,n,q,a,d%s,?]? " ),
35
35
N_ ("Stage deletion [y,n,q,a,d%s,?]? " ),
36
+ N_ ("Stage addition [y,n,q,a,d%s,?]? " ),
36
37
N_ ("Stage this hunk [y,n,q,a,d%s,?]? " )
37
38
},
38
39
.edit_hunk_hint = N_ ("If the patch applies cleanly, the edited hunk "
@@ -54,6 +55,7 @@ static struct patch_mode patch_mode_stash = {
54
55
.prompt_mode = {
55
56
N_ ("Stash mode change [y,n,q,a,d%s,?]? " ),
56
57
N_ ("Stash deletion [y,n,q,a,d%s,?]? " ),
58
+ N_ ("Stash addition [y,n,q,a,d%s,?]? " ),
57
59
N_ ("Stash this hunk [y,n,q,a,d%s,?]? " ),
58
60
},
59
61
.edit_hunk_hint = N_ ("If the patch applies cleanly, the edited hunk "
@@ -77,6 +79,7 @@ static struct patch_mode patch_mode_reset_head = {
77
79
.prompt_mode = {
78
80
N_ ("Unstage mode change [y,n,q,a,d%s,?]? " ),
79
81
N_ ("Unstage deletion [y,n,q,a,d%s,?]? " ),
82
+ N_ ("Unstage addition [y,n,q,a,d%s,?]? " ),
80
83
N_ ("Unstage this hunk [y,n,q,a,d%s,?]? " ),
81
84
},
82
85
.edit_hunk_hint = N_ ("If the patch applies cleanly, the edited hunk "
@@ -99,6 +102,7 @@ static struct patch_mode patch_mode_reset_nothead = {
99
102
.prompt_mode = {
100
103
N_ ("Apply mode change to index [y,n,q,a,d%s,?]? " ),
101
104
N_ ("Apply deletion to index [y,n,q,a,d%s,?]? " ),
105
+ N_ ("Apply addition to index [y,n,q,a,d%s,?]? " ),
102
106
N_ ("Apply this hunk to index [y,n,q,a,d%s,?]? " ),
103
107
},
104
108
.edit_hunk_hint = N_ ("If the patch applies cleanly, the edited hunk "
@@ -121,6 +125,7 @@ static struct patch_mode patch_mode_checkout_index = {
121
125
.prompt_mode = {
122
126
N_ ("Discard mode change from worktree [y,n,q,a,d%s,?]? " ),
123
127
N_ ("Discard deletion from worktree [y,n,q,a,d%s,?]? " ),
128
+ N_ ("Discard addition from worktree [y,n,q,a,d%s,?]? " ),
124
129
N_ ("Discard this hunk from worktree [y,n,q,a,d%s,?]? " ),
125
130
},
126
131
.edit_hunk_hint = N_ ("If the patch applies cleanly, the edited hunk "
@@ -143,6 +148,7 @@ static struct patch_mode patch_mode_checkout_head = {
143
148
.prompt_mode = {
144
149
N_ ("Discard mode change from index and worktree [y,n,q,a,d%s,?]? " ),
145
150
N_ ("Discard deletion from index and worktree [y,n,q,a,d%s,?]? " ),
151
+ N_ ("Discard addition from index and worktree [y,n,q,a,d%s,?]? " ),
146
152
N_ ("Discard this hunk from index and worktree [y,n,q,a,d%s,?]? " ),
147
153
},
148
154
.edit_hunk_hint = N_ ("If the patch applies cleanly, the edited hunk "
@@ -164,6 +170,7 @@ static struct patch_mode patch_mode_checkout_nothead = {
164
170
.prompt_mode = {
165
171
N_ ("Apply mode change to index and worktree [y,n,q,a,d%s,?]? " ),
166
172
N_ ("Apply deletion to index and worktree [y,n,q,a,d%s,?]? " ),
173
+ N_ ("Apply addition to index and worktree [y,n,q,a,d%s,?]? " ),
167
174
N_ ("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? " ),
168
175
},
169
176
.edit_hunk_hint = N_ ("If the patch applies cleanly, the edited hunk "
@@ -186,6 +193,7 @@ static struct patch_mode patch_mode_worktree_head = {
186
193
.prompt_mode = {
187
194
N_ ("Discard mode change from index and worktree [y,n,q,a,d%s,?]? " ),
188
195
N_ ("Discard deletion from index and worktree [y,n,q,a,d%s,?]? " ),
196
+ N_ ("Discard addition from index and worktree [y,n,q,a,d%s,?]? " ),
189
197
N_ ("Discard this hunk from index and worktree [y,n,q,a,d%s,?]? " ),
190
198
},
191
199
.edit_hunk_hint = N_ ("If the patch applies cleanly, the edited hunk "
@@ -207,6 +215,7 @@ static struct patch_mode patch_mode_worktree_nothead = {
207
215
.prompt_mode = {
208
216
N_ ("Apply mode change to index and worktree [y,n,q,a,d%s,?]? " ),
209
217
N_ ("Apply deletion to index and worktree [y,n,q,a,d%s,?]? " ),
218
+ N_ ("Apply addition to index and worktree [y,n,q,a,d%s,?]? " ),
210
219
N_ ("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? " ),
211
220
},
212
221
.edit_hunk_hint = N_ ("If the patch applies cleanly, the edited hunk "
@@ -248,7 +257,7 @@ struct add_p_state {
248
257
struct hunk head ;
249
258
struct hunk * hunk ;
250
259
size_t hunk_nr , hunk_alloc ;
251
- unsigned deleted :1 , mode_change :1 ,binary :1 ;
260
+ unsigned deleted :1 , added : 1 , mode_change :1 ,binary :1 ;
252
261
} * file_diff ;
253
262
size_t file_diff_nr ;
254
263
@@ -442,7 +451,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
442
451
pend = p + plain -> len ;
443
452
while (p != pend ) {
444
453
char * eol = memchr (p , '\n' , pend - p );
445
- const char * deleted = NULL , * mode_change = NULL ;
454
+ const char * deleted = NULL , * added = NULL , * mode_change = NULL ;
446
455
447
456
if (!eol )
448
457
eol = pend ;
@@ -461,11 +470,12 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
461
470
} else if (p == plain -> buf )
462
471
BUG ("diff starts with unexpected line:\n"
463
472
"%.*s\n" , (int )(eol - p ), p );
464
- else if (file_diff -> deleted )
473
+ else if (file_diff -> deleted || file_diff -> added )
465
474
; /* keep the rest of the file in a single "hunk" */
466
475
else if (starts_with (p , "@@ " ) ||
467
476
(hunk == & file_diff -> head &&
468
- skip_prefix (p , "deleted file" , & deleted ))) {
477
+ (skip_prefix (p , "deleted file" , & deleted ) ||
478
+ skip_prefix (p , "new file" , & added )))) {
469
479
if (marker == '-' || marker == '+' )
470
480
/*
471
481
* Should not happen; previous hunk did not end
@@ -485,6 +495,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
485
495
486
496
if (deleted )
487
497
file_diff -> deleted = 1 ;
498
+ else if (added )
499
+ file_diff -> added = 1 ;
488
500
else if (parse_hunk_header (s , hunk ) < 0 )
489
501
return -1 ;
490
502
@@ -537,8 +549,10 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
537
549
starts_with (p , "Binary files " ))
538
550
file_diff -> binary = 1 ;
539
551
540
- if (file_diff -> deleted && file_diff -> mode_change )
541
- BUG ("diff contains delete *and* a mode change?!?\n%.*s" ,
552
+ if (!!file_diff -> deleted + !!file_diff -> added +
553
+ !!file_diff -> mode_change > 1 )
554
+ BUG ("diff can only contain delete *or* add *or* a "
555
+ "mode change?!?\n%.*s" ,
542
556
(int )(eol - (plain -> buf + file_diff -> head .start )),
543
557
plain -> buf + file_diff -> head .start );
544
558
@@ -1397,6 +1411,8 @@ static int patch_update_file(struct add_p_state *s,
1397
1411
1398
1412
if (file_diff -> deleted )
1399
1413
prompt_mode_type = PROMPT_DELETION ;
1414
+ else if (file_diff -> added )
1415
+ prompt_mode_type = PROMPT_ADDITION ;
1400
1416
else if (file_diff -> mode_change && !hunk_index )
1401
1417
prompt_mode_type = PROMPT_MODE_CHANGE ;
1402
1418
else
0 commit comments