Skip to content

Commit ed644d1

Browse files
adlternativegitster
authored andcommitted
ls_files.c: consolidate two for loops into one
This will make it easier to show only one entry per filename in the next step. Signed-off-by: ZheNing Hu <[email protected]> [jc: corrected the log message] Signed-off-by: Junio C Hamano <[email protected]>
1 parent f1c462e commit ed644d1

File tree

1 file changed

+27
-36
lines changed

1 file changed

+27
-36
lines changed

builtin/ls-files.c

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -312,49 +312,40 @@ static void show_files(struct repository *repo, struct dir_struct *dir)
312312
if (show_killed)
313313
show_killed_files(repo->index, dir);
314314
}
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];
318315

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

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;
323+
construct_fullname(&fullname, repo, ce);
324+
325+
if ((dir->flags & DIR_SHOW_IGNORED) &&
326+
!ce_excluded(dir, repo->index, fullname.buf, ce))
327+
continue;
328+
if (ce->ce_flags & CE_UPDATE)
329+
continue;
330+
if ((show_cached || show_stage) &&
331+
(!show_unmerged || ce_stage(ce)))
328332
show_ce(repo, dir, ce, fullname.buf,
329333
ce_stage(ce) ? tag_unmerged :
330334
(ce_skip_worktree(ce) ? tag_skip_worktree :
331335
tag_cached));
332-
}
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 stat_err;
339-
340-
construct_fullname(&fullname, repo, ce);
341336

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-
stat_err = lstat(fullname.buf, &st);
350-
if (stat_err && (errno != ENOENT && errno != ENOTDIR))
351-
error_errno("cannot lstat '%s'", fullname.buf);
352-
if (stat_err && show_deleted)
353-
show_ce(repo, dir, ce, fullname.buf, tag_removed);
354-
if (show_modified &&
355-
(stat_err || ie_modified(repo->index, ce, &st, 0)))
356-
show_ce(repo, dir, ce, fullname.buf, tag_modified);
357-
}
337+
if (!(show_deleted || show_modified))
338+
continue;
339+
if (ce_skip_worktree(ce))
340+
continue;
341+
stat_err = lstat(fullname.buf, &st);
342+
if (stat_err && (errno != ENOENT && errno != ENOTDIR))
343+
error_errno("cannot lstat '%s'", fullname.buf);
344+
if (stat_err && show_deleted)
345+
show_ce(repo, dir, ce, fullname.buf, tag_removed);
346+
if (show_modified &&
347+
(stat_err || ie_modified(repo->index, ce, &st, 0)))
348+
show_ce(repo, dir, ce, fullname.buf, tag_modified);
358349
}
359350

360351
strbuf_release(&fullname);

0 commit comments

Comments
 (0)