d31f31d1
|
20
|
static void init_color(struct repository *r, struct add_i_state *s,
|
d31f31d1
|
24
|
char *key = xstrfmt("color.interactive.%s", slot_name);
|
d31f31d1
|
27
|
if (!s->use_color)
|
d31f31d1
|
28
|
dst[0] = '\0';
|
d31f31d1
|
29
|
else if (repo_config_get_value(r, key, &value) ||
|
d31f31d1
|
30
|
color_parse(value, dst))
|
d31f31d1
|
31
|
strlcpy(dst, default_color, COLOR_MAXLEN);
|
d31f31d1
|
33
|
free(key);
|
d31f31d1
|
34
|
}
|
d31f31d1
|
36
|
static int init_add_i_state(struct repository *r, struct add_i_state *s)
|
d31f31d1
|
40
|
s->r = r;
|
d31f31d1
|
42
|
if (repo_config_get_value(r, "color.interactive", &value))
|
d31f31d1
|
43
|
s->use_color = -1;
|
d31f31d1
|
45
|
s->use_color =
|
d31f31d1
|
46
|
git_config_colorbool("color.interactive", value);
|
d31f31d1
|
47
|
s->use_color = want_color(s->use_color);
|
d31f31d1
|
49
|
init_color(r, s, "header", s->header_color, GIT_COLOR_BOLD);
|
ec4ddbf6
|
50
|
init_color(r, s, "help", s->help_color, GIT_COLOR_BOLD_RED);
|
253cdc20
|
51
|
init_color(r, s, "prompt", s->prompt_color, GIT_COLOR_BOLD_BLUE);
|
253cdc20
|
52
|
init_color(r, s, "error", s->error_color, GIT_COLOR_BOLD_RED);
|
253cdc20
|
53
|
init_color(r, s, "reset", s->reset_color, GIT_COLOR_RESET);
|
d31f31d1
|
55
|
return 0;
|
928e9d0d
|
58
|
static ssize_t find_unique(const char *string,
|
928e9d0d
|
61
|
ssize_t found = -1, i;
|
928e9d0d
|
63
|
for (i = 0; i < nr; i++) {
|
928e9d0d
|
64
|
struct prefix_item *item = list[i];
|
928e9d0d
|
65
|
if (!starts_with(item->name, string))
|
928e9d0d
|
66
|
continue;
|
928e9d0d
|
67
|
if (found >= 0)
|
928e9d0d
|
68
|
return -1;
|
928e9d0d
|
69
|
found = i;
|
928e9d0d
|
72
|
return found;
|
928e9d0d
|
83
|
static void list(struct prefix_item **list, size_t nr,
|
81e604c5
|
86
|
int i, last_lf = 0;
|
bcbdcd36
|
88
|
if (!nr)
|
bcbdcd36
|
89
|
return;
|
bcbdcd36
|
91
|
if (opts->header)
|
d31f31d1
|
92
|
color_fprintf_ln(stdout, s->header_color,
|
bcbdcd36
|
95
|
for (i = 0; i < nr; i++) {
|
bcbdcd36
|
96
|
opts->print_item(i, list[i], opts->print_item_data);
|
81e604c5
|
98
|
if ((opts->columns) && ((i + 1) % (opts->columns))) {
|
81e604c5
|
99
|
putchar('\t');
|
81e604c5
|
100
|
last_lf = 0;
|
81e604c5
|
103
|
putchar('\n');
|
81e604c5
|
104
|
last_lf = 1;
|
81e604c5
|
108
|
if (!last_lf)
|
bcbdcd36
|
109
|
putchar('\n');
|
928e9d0d
|
127
|
static ssize_t list_and_choose(struct prefix_item **items, size_t nr,
|
81e604c5
|
131
|
struct strbuf input = STRBUF_INIT;
|
81e604c5
|
132
|
ssize_t res = LIST_AND_CHOOSE_ERROR;
|
928e9d0d
|
134
|
find_unique_prefixes(items, nr, 1, 4);
|
81e604c5
|
139
|
strbuf_reset(&input);
|
81e604c5
|
141
|
list(items, nr, s, &opts->list_opts);
|
253cdc20
|
143
|
color_fprintf(stdout, s->prompt_color, "%s", opts->prompt);
|
253cdc20
|
144
|
fputs("> ", stdout);
|
81e604c5
|
145
|
fflush(stdout);
|
81e604c5
|
147
|
if (strbuf_getline(&input, stdin) == EOF) {
|
81e604c5
|
148
|
putchar('\n');
|
81e604c5
|
149
|
res = LIST_AND_CHOOSE_QUIT;
|
81e604c5
|
150
|
break;
|
81e604c5
|
152
|
strbuf_trim(&input);
|
81e604c5
|
154
|
if (!input.len)
|
81e604c5
|
155
|
break;
|
ec4ddbf6
|
157
|
if (!strcmp(input.buf, "?")) {
|
ec4ddbf6
|
158
|
opts->print_help(s);
|
ec4ddbf6
|
159
|
continue;
|
81e604c5
|
162
|
p = input.buf;
|
81e604c5
|
164
|
size_t sep = strcspn(p, " \t\r\n,");
|
81e604c5
|
165
|
ssize_t index = -1;
|
81e604c5
|
167
|
if (!sep) {
|
81e604c5
|
168
|
if (!*p)
|
81e604c5
|
169
|
break;
|
81e604c5
|
170
|
p++;
|
81e604c5
|
171
|
continue;
|
81e604c5
|
174
|
if (isdigit(*p)) {
|
81e604c5
|
175
|
index = strtoul(p, &endp, 10) - 1;
|
81e604c5
|
176
|
if (endp != p + sep)
|
81e604c5
|
177
|
index = -1;
|
81e604c5
|
180
|
p[sep] = '\0';
|
928e9d0d
|
181
|
if (index < 0)
|
928e9d0d
|
182
|
index = find_unique(p, items, nr);
|
81e604c5
|
184
|
if (index < 0 || index >= nr)
|
253cdc20
|
185
|
color_fprintf_ln(stdout, s->error_color,
|
81e604c5
|
188
|
res = index;
|
81e604c5
|
189
|
break;
|
81e604c5
|
192
|
p += sep + 1;
|
81e604c5
|
193
|
}
|
81e604c5
|
195
|
if (res != LIST_AND_CHOOSE_ERROR)
|
81e604c5
|
196
|
break;
|
bcbdcd36
|
197
|
}
|
81e604c5
|
199
|
strbuf_release(&input);
|
81e604c5
|
200
|
return res;
|
bcbdcd36
|
216
|
static void add_file_item(struct file_list *list, const char *name)
|
bcbdcd36
|
220
|
FLEXPTR_ALLOC_STR(item, item.name, name);
|
bcbdcd36
|
222
|
ALLOC_GROW(list->file, list->nr + 1, list->alloc);
|
bcbdcd36
|
223
|
list->file[list->nr++] = item;
|
bcbdcd36
|
224
|
}
|
bcbdcd36
|
226
|
static void reset_file_list(struct file_list *list)
|
bcbdcd36
|
230
|
for (i = 0; i < list->nr; i++)
|
bcbdcd36
|
231
|
free(list->file[i]);
|
bcbdcd36
|
232
|
list->nr = 0;
|
bcbdcd36
|
233
|
}
|
bcbdcd36
|
235
|
static void release_file_list(struct file_list *list)
|
bcbdcd36
|
237
|
reset_file_list(list);
|
bcbdcd36
|
238
|
FREE_AND_NULL(list->file);
|
bcbdcd36
|
239
|
list->alloc = 0;
|
bcbdcd36
|
240
|
}
|
bcbdcd36
|
242
|
static int file_item_cmp(const void *a, const void *b)
|
bcbdcd36
|
244
|
const struct file_item * const *f1 = a;
|
bcbdcd36
|
245
|
const struct file_item * const *f2 = b;
|
bcbdcd36
|
247
|
return strcmp((*f1)->item.name, (*f2)->item.name);
|
bcbdcd36
|
256
|
static int pathname_entry_cmp(const void *unused_cmp_data,
|
bcbdcd36
|
260
|
const struct pathname_entry *e1 = entry, *e2 = entry_or_key;
|
bcbdcd36
|
262
|
return strcmp(e1->pathname,
|
bcbdcd36
|
275
|
static void collect_changes_cb(struct diff_queue_struct *q,
|
bcbdcd36
|
279
|
struct collection_status *s = data;
|
bcbdcd36
|
280
|
struct diffstat_t stat = { 0 };
|
bcbdcd36
|
283
|
if (!q->nr)
|
bcbdcd36
|
284
|
return;
|
bcbdcd36
|
286
|
compute_diffstat(options, &stat, q);
|
bcbdcd36
|
288
|
for (i = 0; i < stat.nr; i++) {
|
bcbdcd36
|
289
|
const char *name = stat.files[i]->name;
|
bcbdcd36
|
290
|
int hash = strhash(name);
|
bcbdcd36
|
296
|
entry = hashmap_get_from_hash(&s->file_map, hash, name);
|
bcbdcd36
|
297
|
if (entry)
|
bcbdcd36
|
298
|
file_index = entry->index;
|
bcbdcd36
|
300
|
FLEX_ALLOC_STR(entry, pathname, name);
|
bcbdcd36
|
301
|
hashmap_entry_init(entry, hash);
|
bcbdcd36
|
302
|
entry->index = file_index = s->list->nr;
|
bcbdcd36
|
303
|
hashmap_add(&s->file_map, entry);
|
bcbdcd36
|
305
|
add_file_item(s->list, name);
|
bcbdcd36
|
307
|
file = s->list->file[file_index];
|
bcbdcd36
|
309
|
adddel = s->phase == FROM_INDEX ? &file->index : &file->worktree;
|
bcbdcd36
|
310
|
adddel->seen = 1;
|
bcbdcd36
|
311
|
adddel->add = stat.files[i]->added;
|
bcbdcd36
|
312
|
adddel->del = stat.files[i]->deleted;
|
bcbdcd36
|
313
|
if (stat.files[i]->is_binary)
|
bcbdcd36
|
314
|
adddel->binary = 1;
|
bcbdcd36
|
318
|
static int get_modified_files(struct repository *r, struct file_list *list,
|
bcbdcd36
|
322
|
int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
|
bcbdcd36
|
324
|
struct collection_status s = { FROM_WORKTREE };
|
bcbdcd36
|
326
|
if (repo_read_index_preload(r, ps, 0) < 0)
|
bcbdcd36
|
327
|
return error(_("could not read index"));
|
bcbdcd36
|
329
|
s.list = list;
|
bcbdcd36
|
330
|
hashmap_init(&s.file_map, pathname_entry_cmp, NULL, 0);
|
bcbdcd36
|
332
|
for (s.phase = FROM_WORKTREE; s.phase <= FROM_INDEX; s.phase++) {
|
bcbdcd36
|
334
|
struct setup_revision_opt opt = { 0 };
|
bcbdcd36
|
336
|
opt.def = is_initial ?
|
bcbdcd36
|
337
|
empty_tree_oid_hex() : oid_to_hex(&head_oid);
|
bcbdcd36
|
339
|
init_revisions(&rev, NULL);
|
bcbdcd36
|
340
|
setup_revisions(0, NULL, &rev, &opt);
|
bcbdcd36
|
342
|
rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
|
bcbdcd36
|
343
|
rev.diffopt.format_callback = collect_changes_cb;
|
bcbdcd36
|
344
|
rev.diffopt.format_callback_data = &s;
|
bcbdcd36
|
346
|
if (ps)
|
bcbdcd36
|
347
|
copy_pathspec(&rev.prune_data, ps);
|
bcbdcd36
|
349
|
if (s.phase == FROM_INDEX)
|
bcbdcd36
|
350
|
run_diff_index(&rev, 1);
|
bcbdcd36
|
352
|
rev.diffopt.flags.ignore_dirty_submodules = 1;
|
bcbdcd36
|
353
|
run_diff_files(&rev, 0);
|
bcbdcd36
|
356
|
hashmap_free(&s.file_map, 1);
|
bcbdcd36
|
359
|
QSORT(list->file, list->nr, file_item_cmp);
|
bcbdcd36
|
361
|
return 0;
|
bcbdcd36
|
364
|
static void populate_wi_changes(struct strbuf *buf,
|
bcbdcd36
|
367
|
if (ad->binary)
|
bcbdcd36
|
368
|
strbuf_addstr(buf, _("binary"));
|
bcbdcd36
|
369
|
else if (ad->seen)
|
bcbdcd36
|
370
|
strbuf_addf(buf, "+%"PRIuMAX"/-%"PRIuMAX,
|
bcbdcd36
|
371
|
(uintmax_t)ad->add, (uintmax_t)ad->del);
|
bcbdcd36
|
373
|
strbuf_addstr(buf, no_changes);
|
bcbdcd36
|
374
|
}
|
928e9d0d
|
377
|
static int is_valid_prefix(const char *prefix, size_t prefix_len)
|
928e9d0d
|
379
|
return prefix_len && prefix &&
|
928e9d0d
|
385
|
strcspn(prefix, " \t\r\n,") >= prefix_len && /* separators */
|
928e9d0d
|
386
|
*prefix != '-' && /* deselection */
|
928e9d0d
|
387
|
!isdigit(*prefix) && /* selection */
|
928e9d0d
|
388
|
(prefix_len != 1 ||
|
928e9d0d
|
389
|
(*prefix != '*' && /* "all" wildcard */
|
928e9d0d
|
390
|
*prefix != '?')); /* prompt help */
|
928e9d0d
|
398
|
static void print_file_item(int i, struct prefix_item *item,
|
bcbdcd36
|
401
|
struct file_item *c = (struct file_item *)item;
|
bcbdcd36
|
402
|
struct print_file_item_data *d = print_file_item_data;
|
bcbdcd36
|
404
|
strbuf_reset(&d->index);
|
bcbdcd36
|
405
|
strbuf_reset(&d->worktree);
|
bcbdcd36
|
406
|
strbuf_reset(&d->buf);
|
bcbdcd36
|
408
|
populate_wi_changes(&d->worktree, &c->worktree, _("nothing"));
|
bcbdcd36
|
409
|
populate_wi_changes(&d->index, &c->index, _("unchanged"));
|
bcbdcd36
|
410
|
strbuf_addf(&d->buf, d->modified_fmt,
|
bcbdcd36
|
413
|
printf(" %2d: %s", i + 1, d->buf.buf);
|
bcbdcd36
|
414
|
}
|
d31f31d1
|
416
|
static int run_status(struct add_i_state *s, const struct pathspec *ps,
|
bcbdcd36
|
419
|
reset_file_list(files);
|
d31f31d1
|
421
|
if (get_modified_files(s->r, files, ps) < 0)
|
bcbdcd36
|
422
|
return -1;
|
bcbdcd36
|
424
|
if (files->nr)
|
928e9d0d
|
425
|
list((struct prefix_item **)files->file, files->nr, s, opts);
|
bcbdcd36
|
426
|
putchar('\n');
|
bcbdcd36
|
428
|
return 0;
|
f3665cfd
|
431
|
static int run_help(struct add_i_state *s, const struct pathspec *ps,
|
f3665cfd
|
434
|
const char *help_color = s->help_color;
|
f3665cfd
|
436
|
color_fprintf_ln(stdout, help_color, "status - %s",
|
f3665cfd
|
438
|
color_fprintf_ln(stdout, help_color, "update - %s",
|
f3665cfd
|
440
|
color_fprintf_ln(stdout, help_color, "revert - %s",
|
f3665cfd
|
442
|
color_fprintf_ln(stdout, help_color, "patch - %s",
|
f3665cfd
|
444
|
color_fprintf_ln(stdout, help_color, "diff - %s",
|
f3665cfd
|
446
|
color_fprintf_ln(stdout, help_color, "add untracked - %s",
|
f3665cfd
|
449
|
return 0;
|
928e9d0d
|
456
|
static void print_command_item(int i, struct prefix_item *item,
|
253cdc20
|
459
|
struct print_command_item_data *d = print_command_item_data;
|
928e9d0d
|
461
|
if (!item->prefix_length ||
|
928e9d0d
|
462
|
!is_valid_prefix(item->name, item->prefix_length))
|
928e9d0d
|
463
|
printf(" %2d: %s", i + 1, item->name);
|
253cdc20
|
465
|
printf(" %2d: %s%.*s%s%s", i + 1,
|
253cdc20
|
466
|
d->color, (int)item->prefix_length, item->name, d->reset,
|
928e9d0d
|
467
|
item->name + item->prefix_length);
|
81e604c5
|
468
|
}
|
ec4ddbf6
|
476
|
static void command_prompt_help(struct add_i_state *s)
|
ec4ddbf6
|
478
|
const char *help_color = s->help_color;
|
ec4ddbf6
|
479
|
color_fprintf_ln(stdout, help_color, "%s", _("Prompt help:"));
|
ec4ddbf6
|
480
|
color_fprintf_ln(stdout, help_color, "1 - %s",
|
ec4ddbf6
|
482
|
color_fprintf_ln(stdout, help_color, "foo - %s",
|
ec4ddbf6
|
484
|
color_fprintf_ln(stdout, help_color, " - %s",
|
ec4ddbf6
|
486
|
}
|
db07a815
|
488
|
int run_add_i(struct repository *r, const struct pathspec *ps)
|
d31f31d1
|
490
|
struct add_i_state s = { NULL };
|
81e604c5
|
492
|
struct list_and_choose_options main_loop_opts = {
|
f3665cfd
|
497
|
status = { { "status" }, run_status },
|
f3665cfd
|
498
|
help = { { "help" }, run_help };
|
81e604c5
|
499
|
struct command_item *commands[] = {
|
bcbdcd36
|
504
|
struct print_file_item_data print_file_item_data = {
|
bcbdcd36
|
507
|
struct list_options opts = {
|
bcbdcd36
|
510
|
struct strbuf header = STRBUF_INIT;
|
bcbdcd36
|
511
|
struct file_list files = { NULL };
|
bcbdcd36
|
513
|
int res = 0;
|
d31f31d1
|
515
|
if (init_add_i_state(r, &s))
|
d31f31d1
|
516
|
return error("could not parse `add -i` config");
|
253cdc20
|
522
|
if (s.use_color) {
|
253cdc20
|
523
|
data.color = s.prompt_color;
|
253cdc20
|
524
|
data.reset = s.reset_color;
|
253cdc20
|
526
|
data.color = "[";
|
253cdc20
|
527
|
data.reset = "]";
|
bcbdcd36
|
530
|
strbuf_addstr(&header, " ");
|
bcbdcd36
|
531
|
strbuf_addf(&header, print_file_item_data.modified_fmt,
|
bcbdcd36
|
533
|
opts.header = header.buf;
|
74265dd7
|
535
|
repo_refresh_and_write_index(r, REFRESH_QUIET, 1);
|
d31f31d1
|
536
|
if (run_status(&s, ps, &files, &opts) < 0)
|
74265dd7
|
537
|
res = -1;
|
928e9d0d
|
540
|
i = list_and_choose((struct prefix_item **)commands,
|
81e604c5
|
542
|
if (i == LIST_AND_CHOOSE_QUIT) {
|
81e604c5
|
543
|
printf(_("Bye.\n"));
|
81e604c5
|
544
|
res = 0;
|
81e604c5
|
545
|
break;
|
81e604c5
|
547
|
if (i != LIST_AND_CHOOSE_ERROR)
|
81e604c5
|
548
|
res = commands[i]->command(&s, ps, &files, &opts);
|
81e604c5
|
549
|
}
|
bcbdcd36
|
551
|
release_file_list(&files);
|
bcbdcd36
|
552
|
strbuf_release(&print_file_item_data.buf);
|
bcbdcd36
|
553
|
strbuf_release(&print_file_item_data.index);
|
bcbdcd36
|
554
|
strbuf_release(&print_file_item_data.worktree);
|
bcbdcd36
|
555
|
strbuf_release(&header);
|
bcbdcd36
|
557
|
return res;
|