Skip to content

Commit 5198426

Browse files
committed
Merge branch 'zh/ls-files-deduplicate'
"git ls-files" can and does show multiple entries when the index is unmerged, which is a source for confusion unless -s/-u option is in use. A new option --deduplicate has been introduced. * zh/ls-files-deduplicate: ls-files.c: add --deduplicate option ls_files.c: consolidate two for loops into one ls_files.c: bugfix for --deleted and --modified
2 parents a0a2d75 + 93a7d98 commit 5198426

File tree

3 files changed

+124
-31
lines changed

3 files changed

+124
-31
lines changed

Documentation/git-ls-files.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ SYNOPSIS
1313
(--[cached|deleted|others|ignored|stage|unmerged|killed|modified])*
1414
(-[c|d|o|i|s|u|k|m])*
1515
[--eol]
16+
[--deduplicate]
1617
[-x <pattern>|--exclude=<pattern>]
1718
[-X <file>|--exclude-from=<file>]
1819
[--exclude-per-directory=<file>]
@@ -80,6 +81,13 @@ OPTIONS
8081
\0 line termination on output and do not quote filenames.
8182
See OUTPUT below for more information.
8283

84+
--deduplicate::
85+
When only filenames are shown, suppress duplicates that may
86+
come from having multiple stages during a merge, or giving
87+
`--deleted` and `--modified` option at the same time.
88+
When any of the `-t`, `--unmerged`, or `--stage` option is
89+
in use, this option has no effect.
90+
8391
-x <pattern>::
8492
--exclude=<pattern>::
8593
Skip untracked files matching pattern.

builtin/ls-files.c

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static int line_terminator = '\n';
3535
static int debug_mode;
3636
static int show_eol;
3737
static int recurse_submodules;
38+
static int skipping_duplicates;
3839

3940
static const char *prefix;
4041
static int max_prefix_len;
@@ -312,45 +313,59 @@ static void show_files(struct repository *repo, struct dir_struct *dir)
312313
if (show_killed)
313314
show_killed_files(repo->index, dir);
314315
}
315-
if (show_cached || show_stage) {
316-
for (i = 0; i < repo->index->cache_nr; i++) {
317-
const struct cache_entry *ce = repo->index->cache[i];
318316

319-
construct_fullname(&fullname, repo, ce);
317+
if (!(show_cached || show_stage || show_deleted || show_modified))
318+
return;
319+
for (i = 0; i < repo->index->cache_nr; i++) {
320+
const struct cache_entry *ce = repo->index->cache[i];
321+
struct stat st;
322+
int stat_err;
320323

321-
if ((dir->flags & DIR_SHOW_IGNORED) &&
322-
!ce_excluded(dir, repo->index, fullname.buf, ce))
323-
continue;
324-
if (show_unmerged && !ce_stage(ce))
325-
continue;
326-
if (ce->ce_flags & CE_UPDATE)
327-
continue;
324+
construct_fullname(&fullname, repo, ce);
325+
326+
if ((dir->flags & DIR_SHOW_IGNORED) &&
327+
!ce_excluded(dir, repo->index, fullname.buf, ce))
328+
continue;
329+
if (ce->ce_flags & CE_UPDATE)
330+
continue;
331+
if ((show_cached || show_stage) &&
332+
(!show_unmerged || ce_stage(ce))) {
328333
show_ce(repo, dir, ce, fullname.buf,
329334
ce_stage(ce) ? tag_unmerged :
330335
(ce_skip_worktree(ce) ? tag_skip_worktree :
331336
tag_cached));
337+
if (skipping_duplicates)
338+
goto skip_to_next_name;
332339
}
333-
}
334-
if (show_deleted || show_modified) {
335-
for (i = 0; i < repo->index->cache_nr; i++) {
336-
const struct cache_entry *ce = repo->index->cache[i];
337-
struct stat st;
338-
int err;
339340

340-
construct_fullname(&fullname, repo, ce);
341-
342-
if ((dir->flags & DIR_SHOW_IGNORED) &&
343-
!ce_excluded(dir, repo->index, fullname.buf, ce))
344-
continue;
345-
if (ce->ce_flags & CE_UPDATE)
346-
continue;
347-
if (ce_skip_worktree(ce))
348-
continue;
349-
err = lstat(fullname.buf, &st);
350-
if (show_deleted && err)
351-
show_ce(repo, dir, ce, fullname.buf, tag_removed);
352-
if (show_modified && ie_modified(repo->index, ce, &st, 0))
353-
show_ce(repo, dir, ce, fullname.buf, tag_modified);
341+
if (!(show_deleted || show_modified))
342+
continue;
343+
if (ce_skip_worktree(ce))
344+
continue;
345+
stat_err = lstat(fullname.buf, &st);
346+
if (stat_err && (errno != ENOENT && errno != ENOTDIR))
347+
error_errno("cannot lstat '%s'", fullname.buf);
348+
if (stat_err && show_deleted) {
349+
show_ce(repo, dir, ce, fullname.buf, tag_removed);
350+
if (skipping_duplicates)
351+
goto skip_to_next_name;
352+
}
353+
if (show_modified &&
354+
(stat_err || ie_modified(repo->index, ce, &st, 0))) {
355+
show_ce(repo, dir, ce, fullname.buf, tag_modified);
356+
if (skipping_duplicates)
357+
goto skip_to_next_name;
358+
}
359+
continue;
360+
361+
skip_to_next_name:
362+
{
363+
int j;
364+
struct cache_entry **cache = repo->index->cache;
365+
for (j = i + 1; j < repo->index->cache_nr; j++)
366+
if (strcmp(ce->name, cache[j]->name))
367+
break;
368+
i = j - 1; /* compensate for the for loop */
354369
}
355370
}
356371

@@ -578,6 +593,8 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
578593
N_("pretend that paths removed since <tree-ish> are still present")),
579594
OPT__ABBREV(&abbrev),
580595
OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
596+
OPT_BOOL(0, "deduplicate", &skipping_duplicates,
597+
N_("suppress duplicate entries")),
581598
OPT_END()
582599
};
583600

@@ -617,6 +634,8 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
617634
* you also show the stage information.
618635
*/
619636
show_stage = 1;
637+
if (show_tag || show_stage)
638+
skipping_duplicates = 0;
620639
if (dir.exclude_per_dir)
621640
exc_given = 1;
622641

t/t3012-ls-files-dedup.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/sh
2+
3+
test_description='git ls-files --deduplicate test'
4+
5+
. ./test-lib.sh
6+
7+
test_expect_success 'setup' '
8+
>a.txt &&
9+
>b.txt &&
10+
>delete.txt &&
11+
git add a.txt b.txt delete.txt &&
12+
git commit -m base &&
13+
echo a >a.txt &&
14+
echo b >b.txt &&
15+
echo delete >delete.txt &&
16+
git add a.txt b.txt delete.txt &&
17+
git commit -m tip &&
18+
git tag tip &&
19+
git reset --hard HEAD^ &&
20+
echo change >a.txt &&
21+
git commit -a -m side &&
22+
git tag side
23+
'
24+
25+
test_expect_success 'git ls-files --deduplicate to show unique unmerged path' '
26+
test_must_fail git merge tip &&
27+
git ls-files --deduplicate >actual &&
28+
cat >expect <<-\EOF &&
29+
a.txt
30+
b.txt
31+
delete.txt
32+
EOF
33+
test_cmp expect actual &&
34+
git merge --abort
35+
'
36+
37+
test_expect_success 'git ls-files -d -m --deduplicate with different display options' '
38+
git reset --hard side &&
39+
test_must_fail git merge tip &&
40+
rm delete.txt &&
41+
git ls-files -d -m --deduplicate >actual &&
42+
cat >expect <<-\EOF &&
43+
a.txt
44+
delete.txt
45+
EOF
46+
test_cmp expect actual &&
47+
git ls-files -d -m -t --deduplicate >actual &&
48+
cat >expect <<-\EOF &&
49+
C a.txt
50+
C a.txt
51+
C a.txt
52+
R delete.txt
53+
C delete.txt
54+
EOF
55+
test_cmp expect actual &&
56+
git ls-files -d -m -c --deduplicate >actual &&
57+
cat >expect <<-\EOF &&
58+
a.txt
59+
b.txt
60+
delete.txt
61+
EOF
62+
test_cmp expect actual &&
63+
git merge --abort
64+
'
65+
66+
test_done

0 commit comments

Comments
 (0)