scalar: validate the optional enlistment argument

The `scalar` command needs a Scalar enlistment for many subcommands, and looks in the current directory for such an enlistment (traversing the parent directories until it finds one). These is subcommands can also be called with an optional argument specifying the enlistment. Here, too, we traverse parent directories as needed, until we find an enlistment. However, if the specified directory does not even exist, or is not a directory, we should stop right there, with an error message. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed May 28, 2022 at 16:11 UTC b44855743b1674caf3bd4f42814473771b129c48
2 files changed +9 -2
contrib/scalar/scalar.c
+4 -2
@@ -43,9 +43,11 @@ static void setup_enlistment_directory(int argc, const char **argv,
43 usage_with_options(usagestr, options);
44
45 /* find the worktree, determine its corresponding root */
46 - if (argc == 1)
46 + if (argc == 1) {
47 strbuf_add_absolute_path(&path, argv[0]);
48 - else if (strbuf_getcwd(&path) < 0)
48 + if (!is_directory(path.buf))
49 + die(_("'%s' does not exist"), path.buf);
50 + } else if (strbuf_getcwd(&path) < 0)
51 die(_("need a working directory"));
52
53 strbuf_trim_trailing_dir_sep(&path);
contrib/scalar/t/t9099-scalar.sh
+5
@@ -93,4 +93,9 @@ test_expect_success 'scalar supports -c/-C' '
93 test true = "$(git -C sub config core.preloadIndex)"
94 '
95
96 +test_expect_success '`scalar [...] <dir>` errors out when dir is missing' '
97 + ! scalar run config cloned 2>err &&
98 + grep "cloned. does not exist" err
99 +'
100 +
101 test_done