Skip to content

Commit

Permalink
squash! built-in add -i: allow filtering the modified files list
Browse files Browse the repository at this point in the history
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 <johannes.schindelin@gmx.de>
  • Loading branch information
dscho committed Dec 6, 2019
1 parent 8715ee2 commit 8e27c90
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions add-interactive.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ?
Expand All @@ -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;
Expand Down

0 comments on commit 8e27c90

Please sign in to comment.