Skip to content

Commit 5c49879

Browse files
committed
built-in add -i: re-implement add-untracked in C
This is yet another command, ported to C. It builds nicely on the support functions introduced for other commands, with the notable difference that only names are displayed for untracked files, no file type or diff summary. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 5c31bbd commit 5c49879

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

add-interactive.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "refs.h"
88
#include "string-list.h"
99
#include "lockfile.h"
10+
#include "dir.h"
1011

1112
struct add_i_state {
1213
struct repository *r;
@@ -559,6 +560,7 @@ static int is_valid_prefix(const char *prefix, size_t prefix_len)
559560
struct print_file_item_data {
560561
const char *modified_fmt, *color, *reset;
561562
struct strbuf buf, name, index, worktree;
563+
unsigned only_names:1;
562564
};
563565

564566
static void print_file_item(int i, int selected, struct string_list_item *item,
@@ -582,6 +584,12 @@ static void print_file_item(int i, int selected, struct string_list_item *item,
582584
highlighted = d->name.buf;
583585
}
584586

587+
if (d->only_names) {
588+
printf("%c%2d: %s", selected ? '*' : ' ', i + 1,
589+
highlighted ? highlighted : item->string);
590+
return;
591+
}
592+
585593
render_adddel(&d->worktree, &c->worktree, _("nothing"));
586594
render_adddel(&d->index, &c->index, _("unchanged"));
587595

@@ -761,6 +769,88 @@ static int run_revert(struct add_i_state *s, const struct pathspec *ps,
761769
return res;
762770
}
763771

772+
static int get_untracked_files(struct repository *r,
773+
struct prefix_item_list *files,
774+
const struct pathspec *ps)
775+
{
776+
struct dir_struct dir = { 0 };
777+
size_t i;
778+
struct strbuf buf = STRBUF_INIT;
779+
780+
if (repo_read_index(r) < 0)
781+
return error(_("could not read index"));
782+
783+
prefix_item_list_clear(files);
784+
setup_standard_excludes(&dir);
785+
add_pattern_list(&dir, EXC_CMDL, "--exclude option");
786+
fill_directory(&dir, r->index, ps);
787+
788+
for (i = 0; i < dir.nr; i++) {
789+
struct dir_entry *ent = dir.entries[i];
790+
791+
if (index_name_is_other(r->index, ent->name, ent->len)) {
792+
strbuf_reset(&buf);
793+
strbuf_add(&buf, ent->name, ent->len);
794+
add_file_item(&files->items, buf.buf);
795+
}
796+
}
797+
798+
strbuf_release(&buf);
799+
return 0;
800+
}
801+
802+
static int run_add_untracked(struct add_i_state *s, const struct pathspec *ps,
803+
struct prefix_item_list *files,
804+
struct list_and_choose_options *opts)
805+
{
806+
struct print_file_item_data *d = opts->list_opts.print_item_data;
807+
int res = 0, fd;
808+
size_t count, i;
809+
struct lock_file index_lock;
810+
811+
if (get_untracked_files(s->r, files, ps) < 0)
812+
return -1;
813+
814+
if (!files->items.nr) {
815+
printf(_("No untracked files.\n"));
816+
goto finish_add_untracked;
817+
}
818+
819+
opts->prompt = N_("Add untracked");
820+
d->only_names = 1;
821+
count = list_and_choose(s, files, opts);
822+
d->only_names = 0;
823+
if (count <= 0)
824+
goto finish_add_untracked;
825+
826+
fd = repo_hold_locked_index(s->r, &index_lock, LOCK_REPORT_ON_ERROR);
827+
if (fd < 0) {
828+
res = -1;
829+
goto finish_add_untracked;
830+
}
831+
832+
for (i = 0; i < files->items.nr; i++) {
833+
const char *name = files->items.items[i].string;
834+
if (files->selected[i] &&
835+
add_file_to_index(s->r->index, name, 0) < 0) {
836+
res = error(_("could not stage '%s'"), name);
837+
break;
838+
}
839+
}
840+
841+
if (!res &&
842+
write_locked_index(s->r->index, &index_lock, COMMIT_LOCK) < 0)
843+
res = error(_("could not write index"));
844+
845+
if (!res)
846+
printf(Q_("added %d path\n",
847+
"added %d paths\n", count), (int)count);
848+
849+
finish_add_untracked:
850+
putchar('\n');
851+
return res;
852+
}
853+
764854
static int run_help(struct add_i_state *s, const struct pathspec *unused_ps,
765855
struct prefix_item_list *unused_files,
766856
struct list_and_choose_options *unused_opts)
@@ -857,6 +947,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
857947
{ "status", run_status },
858948
{ "update", run_update },
859949
{ "revert", run_revert },
950+
{ "add untracked", run_add_untracked },
860951
{ "help", run_help },
861952
};
862953
struct prefix_item_list commands = PREFIX_ITEM_LIST_INIT;

0 commit comments

Comments
 (0)