Skip to content

Commit 41e36a5

Browse files
committed
built-in add -i: implement the update command
After `status` and `help`, it is now turn to port the `update` command to C, the second command that is shown in the main loop menu of `git add -i`. This `git add -i` command is the first one which lets the user choose a subset of a list of files, and as such, this patch lays the groundwork for the other commands of that category: - It teaches the `print_file_item()` function to show a unique prefix if we found any (the code to find it had been added already in the previous patch where we colored the unique prefixes of the main loop commands, but that patch uses the `print_command_item()` function to display the menu items). - This patch also adds the help text that is shown when the user input to select items from the shown list could not be parsed. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 7a479f4 commit 41e36a5

File tree

1 file changed

+112
-12
lines changed

1 file changed

+112
-12
lines changed

add-interactive.c

Lines changed: 112 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "revision.h"
77
#include "refs.h"
88
#include "prefix-map.h"
9+
#include "lockfile.h"
910

1011
struct add_i_state {
1112
struct repository *r;
@@ -452,30 +453,43 @@ static int is_valid_prefix(const char *prefix, size_t prefix_len)
452453
}
453454

454455
struct print_file_item_data {
455-
const char *modified_fmt;
456-
struct strbuf buf, index, worktree;
456+
const char *modified_fmt, *color, *reset;
457+
struct strbuf buf, name, index, worktree;
457458
};
458459

459460
static void print_file_item(int i, int selected, struct prefix_item *item,
460461
void *print_file_item_data)
461462
{
462463
struct file_item *c = (struct file_item *)item;
463464
struct print_file_item_data *d = print_file_item_data;
465+
const char *highlighted = NULL;
464466

465467
strbuf_reset(&d->index);
466468
strbuf_reset(&d->worktree);
467469
strbuf_reset(&d->buf);
468470

471+
/* Format the item with the prefix highlighted. */
472+
if (item->prefix_length > 0 &&
473+
is_valid_prefix(item->name, item->prefix_length)) {
474+
strbuf_reset(&d->name);
475+
strbuf_addf(&d->name, "%s%.*s%s%s", d->color,
476+
(int)item->prefix_length, item->name, d->reset,
477+
item->name + item->prefix_length);
478+
highlighted = d->name.buf;
479+
}
480+
469481
populate_wi_changes(&d->worktree, &c->worktree, _("nothing"));
470482
populate_wi_changes(&d->index, &c->index, _("unchanged"));
471483
strbuf_addf(&d->buf, d->modified_fmt,
472-
d->index.buf, d->worktree.buf, item->name);
484+
d->index.buf, d->worktree.buf,
485+
highlighted ? highlighted : item->name);
473486

474487
printf("%c%2d: %s", selected ? '*' : ' ', i + 1, d->buf.buf);
475488
}
476489

477490
static int run_status(struct add_i_state *s, const struct pathspec *ps,
478-
struct file_list *files, struct list_options *opts)
491+
struct file_list *files,
492+
struct list_and_choose_options *opts)
479493
{
480494
reset_file_list(files);
481495

@@ -484,14 +498,72 @@ static int run_status(struct add_i_state *s, const struct pathspec *ps,
484498

485499
if (files->nr)
486500
list((struct prefix_item **)files->file, NULL, files->nr,
487-
s, opts);
501+
s, &opts->list_opts);
488502
putchar('\n');
489503

490504
return 0;
491505
}
492506

507+
static int run_update(struct add_i_state *s, const struct pathspec *ps,
508+
struct file_list *files,
509+
struct list_and_choose_options *opts)
510+
{
511+
int res = 0, fd, *selected = NULL;
512+
size_t count, i;
513+
struct lock_file index_lock;
514+
515+
reset_file_list(files);
516+
517+
if (get_modified_files(s->r, WORKTREE_ONLY, files, ps) < 0)
518+
return -1;
519+
520+
if (!files->nr) {
521+
putchar('\n');
522+
return 0;
523+
}
524+
525+
opts->prompt = N_("Update");
526+
CALLOC_ARRAY(selected, files->nr);
527+
528+
count = list_and_choose((struct prefix_item **)files->file,
529+
selected, files->nr, s, opts);
530+
if (count <= 0) {
531+
putchar('\n');
532+
free(selected);
533+
return 0;
534+
}
535+
536+
fd = repo_hold_locked_index(s->r, &index_lock, LOCK_REPORT_ON_ERROR);
537+
if (fd < 0) {
538+
putchar('\n');
539+
free(selected);
540+
return -1;
541+
}
542+
543+
for (i = 0; i < files->nr; i++) {
544+
const char *name = files->file[i]->item.name;
545+
if (selected[i] &&
546+
add_file_to_index(s->r->index, name, 0) < 0) {
547+
res = error(_("could not stage '%s'"), name);
548+
break;
549+
}
550+
}
551+
552+
if (!res && write_locked_index(s->r->index, &index_lock, COMMIT_LOCK) < 0)
553+
res = error(_("could not write index"));
554+
555+
if (!res)
556+
printf(Q_("updated %d path\n",
557+
"updated %d paths\n", count), (int)count);
558+
559+
putchar('\n');
560+
free(selected);
561+
return res;
562+
}
563+
493564
static int run_help(struct add_i_state *s, const struct pathspec *ps,
494-
struct file_list *files, struct list_options *opts)
565+
struct file_list *files,
566+
struct list_and_choose_options *opts)
495567
{
496568
const char *help_color = s->help_color;
497569

@@ -511,6 +583,27 @@ static int run_help(struct add_i_state *s, const struct pathspec *ps,
511583
return 0;
512584
}
513585

586+
static void choose_prompt_help(struct add_i_state *s)
587+
{
588+
const char *help_color = s->help_color;
589+
color_fprintf_ln(stdout, help_color, "%s",
590+
_("Prompt help:"));
591+
color_fprintf_ln(stdout, help_color, "1 - %s",
592+
_("select a single item"));
593+
color_fprintf_ln(stdout, help_color, "3-5 - %s",
594+
_("select a range of items"));
595+
color_fprintf_ln(stdout, help_color, "2-3,6-9 - %s",
596+
_("select multiple ranges"));
597+
color_fprintf_ln(stdout, help_color, "foo - %s",
598+
_("select item based on unique prefix"));
599+
color_fprintf_ln(stdout, help_color, "-... - %s",
600+
_("unselect specified items"));
601+
color_fprintf_ln(stdout, help_color, "* - %s",
602+
_("choose all items"));
603+
color_fprintf_ln(stdout, help_color, " - %s",
604+
_("(empty) finish selecting"));
605+
}
606+
514607
struct print_command_item_data {
515608
const char *color, *reset;
516609
};
@@ -532,7 +625,8 @@ static void print_command_item(int i, int selected, struct prefix_item *item,
532625
struct command_item {
533626
struct prefix_item item;
534627
int (*command)(struct add_i_state *s, const struct pathspec *ps,
535-
struct file_list *files, struct list_options *opts);
628+
struct file_list *files,
629+
struct list_and_choose_options *opts);
536630
};
537631

538632
static void command_prompt_help(struct add_i_state *s)
@@ -557,17 +651,20 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
557651
};
558652
struct command_item
559653
status = { { "status" }, run_status },
654+
update = { { "update" }, run_update },
560655
help = { { "help" }, run_help };
561656
struct command_item *commands[] = {
562-
&status,
657+
&status, &update,
563658
&help
564659
};
565660

566661
struct print_file_item_data print_file_item_data = {
567-
"%12s %12s %s", STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
662+
"%12s %12s %s", NULL, NULL,
663+
STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
568664
};
569-
struct list_options opts = {
570-
0, NULL, print_file_item, &print_file_item_data
665+
struct list_and_choose_options opts = {
666+
{ 0, NULL, print_file_item, &print_file_item_data },
667+
NULL, 0, choose_prompt_help
571668
};
572669
struct strbuf header = STRBUF_INIT;
573670
struct file_list files = { NULL };
@@ -588,11 +685,13 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
588685
data.color = "[";
589686
data.reset = "]";
590687
}
688+
print_file_item_data.color = data.color;
689+
print_file_item_data.reset = data.reset;
591690

592691
strbuf_addstr(&header, " ");
593692
strbuf_addf(&header, print_file_item_data.modified_fmt,
594693
_("staged"), _("unstaged"), _("path"));
595-
opts.header = header.buf;
694+
opts.list_opts.header = header.buf;
596695

597696
repo_refresh_and_write_index(r, REFRESH_QUIET, 1);
598697
if (run_status(&s, ps, &files, &opts) < 0)
@@ -612,6 +711,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
612711

613712
release_file_list(&files);
614713
strbuf_release(&print_file_item_data.buf);
714+
strbuf_release(&print_file_item_data.name);
615715
strbuf_release(&print_file_item_data.index);
616716
strbuf_release(&print_file_item_data.worktree);
617717
strbuf_release(&header);

0 commit comments

Comments
 (0)