]> git.ipfire.org Git - thirdparty/git.git/commitdiff
scalar: accept -C and -c options before the subcommand
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Fri, 28 Jan 2022 14:31:57 +0000 (14:31 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 28 Jan 2022 23:14:38 +0000 (15:14 -0800)
The `git` executable has these two very useful options:

-C <directory>:
switch to the specified directory before performing any actions

-c <key>=<value>:
temporarily configure this setting for the duration of the
specified scalar subcommand

With this commit, we teach the `scalar` executable the same trick.

Note: It might look like a good idea to try to reuse the
`handle_options()` function in `git.c` instead of replicating only the
`-c`/`-C` part. However, that function is not only not in `libgit.a`, it
is also intricately entangled with the rest of the code in `git.c` that
is necessary e.g. to handle `--paginate`. Besides, no other option
handled by that `handle_options()` function is relevant to Scalar,
therefore the cost of refactoring vastly would outweigh the benefit.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
contrib/scalar/scalar.c
contrib/scalar/scalar.txt
contrib/scalar/t/t9099-scalar.sh

index 1ce9c2b00e8058bba4b5bb33a875cddf40b1047b..7db2a97416e03c2a869cd8216d43684ebdb9264d 100644 (file)
@@ -808,6 +808,25 @@ int cmd_main(int argc, const char **argv)
        struct strbuf scalar_usage = STRBUF_INIT;
        int i;
 
+       while (argc > 1 && *argv[1] == '-') {
+               if (!strcmp(argv[1], "-C")) {
+                       if (argc < 3)
+                               die(_("-C requires a <directory>"));
+                       if (chdir(argv[2]) < 0)
+                               die_errno(_("could not change to '%s'"),
+                                         argv[2]);
+                       argc -= 2;
+                       argv += 2;
+               } else if (!strcmp(argv[1], "-c")) {
+                       if (argc < 3)
+                               die(_("-c requires a <key>=<value> argument"));
+                       git_config_push_parameter(argv[2]);
+                       argc -= 2;
+                       argv += 2;
+               } else
+                       break;
+       }
+
        if (argc > 1) {
                argv++;
                argc--;
@@ -818,7 +837,8 @@ int cmd_main(int argc, const char **argv)
        }
 
        strbuf_addstr(&scalar_usage,
-                     N_("scalar <command> [<options>]\n\nCommands:\n"));
+                     N_("scalar [-C <directory>] [-c <key>=<value>] "
+                        "<command> [<options>]\n\nCommands:\n"));
        for (i = 0; builtins[i].name; i++)
                strbuf_addf(&scalar_usage, "\t%s\n", builtins[i].name);
 
index f416d637289c2cfae93c3c32184a1b1a5177b4e0..cf4e5b889cc364e992b1f28405442ed5950103fa 100644 (file)
@@ -36,6 +36,16 @@ The `scalar` command implements various subcommands, and different options
 depending on the subcommand. With the exception of `clone`, `list` and
 `reconfigure --all`, all subcommands expect to be run in an enlistment.
 
+The following options can be specified _before_ the subcommand:
+
+-C <directory>::
+       Before running the subcommand, change the working directory. This
+       option imitates the same option of linkgit:git[1].
+
+-c <key>=<value>::
+       For the duration of running the specified subcommand, configure this
+       setting. This option imitates the same option of linkgit:git[1].
+
 COMMANDS
 --------
 
index 2e1502ad45e1d3995b5100a188e540241e100a78..89781568f43abf301b6df3a932b064c44ecd3e80 100755 (executable)
@@ -85,4 +85,12 @@ test_expect_success 'scalar delete with enlistment' '
        test_path_is_missing cloned
 '
 
+test_expect_success 'scalar supports -c/-C' '
+       test_when_finished "scalar delete sub" &&
+       git init sub &&
+       scalar -C sub -c status.aheadBehind=bogus register &&
+       test -z "$(git -C sub config --local status.aheadBehind)" &&
+       test true = "$(git -C sub config core.preloadIndex)"
+'
+
 test_done