From 8e27c907bef8d043df9e8087b7ada87f41ef1c09 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 29 Nov 2019 21:11:42 +0000 Subject: [PATCH] squash! built-in add -i: allow filtering the modified files list built-in add -i: allow filtering the modified files list In the `update` command of `git add -i`, we are primarily interested in the list of modified files that have worktree (i.e. unstaged) changes. At the same time, we need to determine _also_ the staged changes, to be able to produce the full added/deleted information. The Perl script version of `git add -i` has a parameter of the `list_modified()` function for that matter. In C, we can be a lot more precise, using an `enum`. The C implementation of the filter also has an easier time to avoid unnecessary work, simply by using an adaptive order of the `diff-index` and `diff-files` phases, and then skipping files in the second phase when they have not been seen in the first phase. Seeing as we change the meaning of the `phase` field, we rename it to `mode` to reflect that the order depends on the exact invocation of the `git add -i` command. Signed-off-by: Johannes Schindelin --- add-interactive.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/add-interactive.c b/add-interactive.c index e2a19dbe34..ce8e64abc6 100644 --- a/add-interactive.c +++ b/add-interactive.c @@ -427,7 +427,7 @@ static int pathname_entry_cmp(const void *unused_cmp_data, } struct collection_status { - enum { FROM_WORKTREE = 0, FROM_INDEX = 1 } phase; + enum { FROM_WORKTREE = 0, FROM_INDEX = 1 } mode; const char *reference; @@ -473,7 +473,7 @@ static void collect_changes_cb(struct diff_queue_struct *q, } file_item = entry->item; - adddel = s->phase == FROM_INDEX ? + adddel = s->mode == FROM_INDEX ? &file_item->index : &file_item->worktree; other_adddel = s->phase == FROM_INDEX ? &file_item->worktree : &file_item->index; @@ -526,9 +526,9 @@ static int get_modified_files(struct repository *r, struct setup_revision_opt opt = { 0 }; if (filter == INDEX_ONLY) - s.phase = i ? FROM_WORKTREE : FROM_INDEX; + s.mode = (i == 0) ? FROM_INDEX : FROM_WORKTREE; else - s.phase = i ? FROM_INDEX : FROM_WORKTREE; + s.mode = (i == 0) ? FROM_WORKTREE : FROM_INDEX; s.skip_unseen = filter && i; opt.def = is_initial ? @@ -544,7 +544,7 @@ static int get_modified_files(struct repository *r, if (ps) copy_pathspec(&rev.prune_data, ps); - if (s.phase == FROM_INDEX) + if (s.mode == FROM_INDEX) run_diff_index(&rev, 1); else { rev.diffopt.flags.ignore_dirty_submodules = 1;