Save work if one_top_level_dir is absolute

* src/misc.c (chdir_arg): Optimize --one-top-level=X where X is an
absolute file name.
This commit is contained in:
Paul Eggert
2026-07-29 23:12:46 -07:00
parent 25d8b4b1ac
commit 05f21fe936
+5
View File
@@ -1047,6 +1047,7 @@ ensure_wd (void)
/* DIR is the operand of a -C option; add it to vector of chdir targets,
and return the index of its location. If --one-top-level-dir, add
two targets to the vector. However, if DIR is "." or an equivalent,
or if --one-top-level-dir is an absolute file name,
just reuse the last item in the vector. */
idx_t
chdir_arg (char *dir)
@@ -1062,6 +1063,10 @@ chdir_arg (char *dir)
return wd_count - 1;
}
/* Optimize --one-top-level=X where X is an absolute file name. */
if (one_top_level_dir && IS_ABSOLUTE_FILE_NAME (one_top_level_dir))
return wd_count - 1;
ptrdiff_t shortage = 1 + !!one_top_level_dir - (wd_alloc - wd_count);
if (0 < shortage)
wd = xpalloc (wd, &wd_alloc, shortage, -1, sizeof *wd);