Skip to content

Commit 5e23c07

Browse files
theiostreamdscho
authored andcommitted
built-in add -i: implement the status command
This implements the `status` command of `git add -i`. The data structures introduced in this commit will be extended as needed later. At this point, we re-implement only part of the `list_and_choose()` function of the Perl script `git-add--interactive.perl` and call it `list()`. It does not yet color anything, or do columns, or allow user input. Over the course of the next commits, we will introduce a `list_and_choose()` function that uses `list()` to display the list of options and let the user choose one or more of the displayed items. This will be used to implement the main loop of the built-in `git add -i`, at which point the new `status` command can actually be used. Note that we pass the list of items as a `struct item **` as opposed to a `struct item *`, to allow for the actual items to contain much more information than merely the name. Signed-off-by: Daniel Ferreira <[email protected]> Signed-off-by: Slavica Djukic <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent bc99009 commit 5e23c07

File tree

1 file changed

+264
-1
lines changed

1 file changed

+264
-1
lines changed

add-interactive.c

Lines changed: 264 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,270 @@
11
#include "cache.h"
22
#include "add-interactive.h"
3+
#include "diffcore.h"
4+
#include "revision.h"
5+
#include "refs.h"
6+
7+
struct item {
8+
const char *name;
9+
};
10+
11+
struct list_options {
12+
const char *header;
13+
void (*print_item)(int i, struct item *item, void *print_item_data);
14+
void *print_item_data;
15+
};
16+
17+
static void list(struct item **list, size_t nr, struct list_options *opts)
18+
{
19+
int i;
20+
21+
if (!nr)
22+
return;
23+
24+
if (opts->header)
25+
printf("%s\n", opts->header);
26+
27+
for (i = 0; i < nr; i++) {
28+
opts->print_item(i, list[i], opts->print_item_data);
29+
putchar('\n');
30+
}
31+
}
32+
33+
struct adddel {
34+
uintmax_t add, del;
35+
unsigned seen:1, binary:1;
36+
};
37+
38+
struct file_list {
39+
struct file_item {
40+
struct item item;
41+
struct adddel index, worktree;
42+
} **file;
43+
size_t nr, alloc;
44+
};
45+
46+
static void add_file_item(struct file_list *list, const char *name)
47+
{
48+
struct file_item *item;
49+
50+
FLEXPTR_ALLOC_STR(item, item.name, name);
51+
52+
ALLOC_GROW(list->file, list->nr + 1, list->alloc);
53+
list->file[list->nr++] = item;
54+
}
55+
56+
static void reset_file_list(struct file_list *list)
57+
{
58+
size_t i;
59+
60+
for (i = 0; i < list->nr; i++)
61+
free(list->file[i]);
62+
list->nr = 0;
63+
}
64+
65+
static void release_file_list(struct file_list *list)
66+
{
67+
reset_file_list(list);
68+
FREE_AND_NULL(list->file);
69+
list->alloc = 0;
70+
}
71+
72+
static int file_item_cmp(const void *a, const void *b)
73+
{
74+
const struct file_item * const *f1 = a;
75+
const struct file_item * const *f2 = b;
76+
77+
return strcmp((*f1)->item.name, (*f2)->item.name);
78+
}
79+
80+
struct pathname_entry {
81+
struct hashmap_entry ent;
82+
size_t index;
83+
char pathname[FLEX_ARRAY];
84+
};
85+
86+
static int pathname_entry_cmp(const void *unused_cmp_data,
87+
const void *entry, const void *entry_or_key,
88+
const void *pathname)
89+
{
90+
const struct pathname_entry *e1 = entry, *e2 = entry_or_key;
91+
92+
return strcmp(e1->pathname,
93+
pathname ? (const char *)pathname : e2->pathname);
94+
}
95+
96+
struct collection_status {
97+
enum { FROM_WORKTREE = 0, FROM_INDEX = 1 } phase;
98+
99+
const char *reference;
100+
101+
struct file_list *list;
102+
struct hashmap file_map;
103+
};
104+
105+
static void collect_changes_cb(struct diff_queue_struct *q,
106+
struct diff_options *options,
107+
void *data)
108+
{
109+
struct collection_status *s = data;
110+
struct diffstat_t stat = { 0 };
111+
int i;
112+
113+
if (!q->nr)
114+
return;
115+
116+
compute_diffstat(options, &stat, q);
117+
118+
for (i = 0; i < stat.nr; i++) {
119+
const char *name = stat.files[i]->name;
120+
int hash = strhash(name);
121+
struct pathname_entry *entry;
122+
size_t file_index;
123+
struct file_item *file;
124+
struct adddel *adddel;
125+
126+
entry = hashmap_get_from_hash(&s->file_map, hash, name);
127+
if (entry)
128+
file_index = entry->index;
129+
else {
130+
FLEX_ALLOC_STR(entry, pathname, name);
131+
hashmap_entry_init(entry, hash);
132+
entry->index = file_index = s->list->nr;
133+
hashmap_add(&s->file_map, entry);
134+
135+
add_file_item(s->list, name);
136+
}
137+
file = s->list->file[file_index];
138+
139+
adddel = s->phase == FROM_INDEX ? &file->index : &file->worktree;
140+
adddel->seen = 1;
141+
adddel->add = stat.files[i]->added;
142+
adddel->del = stat.files[i]->deleted;
143+
if (stat.files[i]->is_binary)
144+
adddel->binary = 1;
145+
}
146+
}
147+
148+
static int get_modified_files(struct repository *r, struct file_list *list,
149+
const struct pathspec *ps)
150+
{
151+
struct object_id head_oid;
152+
int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
153+
&head_oid, NULL);
154+
struct collection_status s = { FROM_WORKTREE };
155+
156+
if (repo_read_index_preload(r, ps, 0) < 0)
157+
return error(_("could not read index"));
158+
159+
s.list = list;
160+
hashmap_init(&s.file_map, pathname_entry_cmp, NULL, 0);
161+
162+
for (s.phase = FROM_WORKTREE; s.phase <= FROM_INDEX; s.phase++) {
163+
struct rev_info rev;
164+
struct setup_revision_opt opt = { 0 };
165+
166+
opt.def = is_initial ?
167+
empty_tree_oid_hex() : oid_to_hex(&head_oid);
168+
169+
init_revisions(&rev, NULL);
170+
setup_revisions(0, NULL, &rev, &opt);
171+
172+
rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
173+
rev.diffopt.format_callback = collect_changes_cb;
174+
rev.diffopt.format_callback_data = &s;
175+
176+
if (ps)
177+
copy_pathspec(&rev.prune_data, ps);
178+
179+
if (s.phase == FROM_INDEX)
180+
run_diff_index(&rev, 1);
181+
else {
182+
rev.diffopt.flags.ignore_dirty_submodules = 1;
183+
run_diff_files(&rev, 0);
184+
}
185+
}
186+
hashmap_free(&s.file_map, 1);
187+
188+
/* While the diffs are ordered already, we ran *two* diffs... */
189+
QSORT(list->file, list->nr, file_item_cmp);
190+
191+
return 0;
192+
}
193+
194+
static void populate_wi_changes(struct strbuf *buf,
195+
struct adddel *ad, const char *no_changes)
196+
{
197+
if (ad->binary)
198+
strbuf_addstr(buf, _("binary"));
199+
else if (ad->seen)
200+
strbuf_addf(buf, "+%"PRIuMAX"/-%"PRIuMAX,
201+
(uintmax_t)ad->add, (uintmax_t)ad->del);
202+
else
203+
strbuf_addstr(buf, no_changes);
204+
}
205+
206+
struct print_file_item_data {
207+
const char *modified_fmt;
208+
struct strbuf buf, index, worktree;
209+
};
210+
211+
static void print_file_item(int i, struct item *item,
212+
void *print_file_item_data)
213+
{
214+
struct file_item *c = (struct file_item *)item;
215+
struct print_file_item_data *d = print_file_item_data;
216+
217+
strbuf_reset(&d->index);
218+
strbuf_reset(&d->worktree);
219+
strbuf_reset(&d->buf);
220+
221+
populate_wi_changes(&d->worktree, &c->worktree, _("nothing"));
222+
populate_wi_changes(&d->index, &c->index, _("unchanged"));
223+
strbuf_addf(&d->buf, d->modified_fmt,
224+
d->index.buf, d->worktree.buf, item->name);
225+
226+
printf(" %2d: %s", i + 1, d->buf.buf);
227+
}
228+
229+
static int run_status(struct repository *r, const struct pathspec *ps,
230+
struct file_list *files, struct list_options *opts)
231+
{
232+
reset_file_list(files);
233+
234+
if (get_modified_files(r, files, ps) < 0)
235+
return -1;
236+
237+
if (files->nr)
238+
list((struct item **)files->file, files->nr, opts);
239+
putchar('\n');
240+
241+
return 0;
242+
}
3243

4244
int run_add_i(struct repository *r, const struct pathspec *ps)
5245
{
6-
die(_("No commands are available in the built-in `git add -i` yet!"));
246+
struct print_file_item_data print_file_item_data = {
247+
"%12s %12s %s", STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
248+
};
249+
struct list_options opts = {
250+
NULL, print_file_item, &print_file_item_data
251+
};
252+
struct strbuf header = STRBUF_INIT;
253+
struct file_list files = { NULL };
254+
int res = 0;
255+
256+
strbuf_addstr(&header, " ");
257+
strbuf_addf(&header, print_file_item_data.modified_fmt,
258+
_("staged"), _("unstaged"), _("path"));
259+
opts.header = header.buf;
260+
261+
res = run_status(r, ps, &files, &opts);
262+
263+
release_file_list(&files);
264+
strbuf_release(&print_file_item_data.buf);
265+
strbuf_release(&print_file_item_data.index);
266+
strbuf_release(&print_file_item_data.worktree);
267+
strbuf_release(&header);
268+
269+
return res;
7270
}

0 commit comments

Comments
 (0)