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 <adlternative@gmail.com> [jc: corrected the log message] Signed-off-by: Junio C Hamano <gitster@pobox.com>

ZheNing Hu committed Jan 23, 2021 at 10:20 UTC ed644d1666415cbed14cc6cd17a658bb56e7f28b
1 file changed +27 -36
builtin/ls-files.c
+27 -36
@@ -312,49 +312,40 @@ static void show_files(struct repository *repo, struct dir_struct *dir)
312 if (show_killed)
313 show_killed_files(repo->index, dir);
314 }
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];
315
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;
322
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)))
332 show_ce(repo, dir, ce, fullname.buf,
333 ce_stage(ce) ? tag_unmerged :
334 (ce_skip_worktree(ce) ? tag_skip_worktree :
335 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);
336
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);
349 }
350
351 strbuf_release(&fullname);