Skip to content

Commit 2ae8eb5

Browse files
dschogitster
authored andcommitted
scalar: accept -C and -c options before the subcommand
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b23dac9 commit 2ae8eb5

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

contrib/scalar/scalar.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,25 @@ int cmd_main(int argc, const char **argv)
808808
struct strbuf scalar_usage = STRBUF_INIT;
809809
int i;
810810

811+
while (argc > 1 && *argv[1] == '-') {
812+
if (!strcmp(argv[1], "-C")) {
813+
if (argc < 3)
814+
die(_("-C requires a <directory>"));
815+
if (chdir(argv[2]) < 0)
816+
die_errno(_("could not change to '%s'"),
817+
argv[2]);
818+
argc -= 2;
819+
argv += 2;
820+
} else if (!strcmp(argv[1], "-c")) {
821+
if (argc < 3)
822+
die(_("-c requires a <key>=<value> argument"));
823+
git_config_push_parameter(argv[2]);
824+
argc -= 2;
825+
argv += 2;
826+
} else
827+
break;
828+
}
829+
811830
if (argc > 1) {
812831
argv++;
813832
argc--;
@@ -818,7 +837,8 @@ int cmd_main(int argc, const char **argv)
818837
}
819838

820839
strbuf_addstr(&scalar_usage,
821-
N_("scalar <command> [<options>]\n\nCommands:\n"));
840+
N_("scalar [-C <directory>] [-c <key>=<value>] "
841+
"<command> [<options>]\n\nCommands:\n"));
822842
for (i = 0; builtins[i].name; i++)
823843
strbuf_addf(&scalar_usage, "\t%s\n", builtins[i].name);
824844

contrib/scalar/scalar.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ The `scalar` command implements various subcommands, and different options
3636
depending on the subcommand. With the exception of `clone`, `list` and
3737
`reconfigure --all`, all subcommands expect to be run in an enlistment.
3838

39+
The following options can be specified _before_ the subcommand:
40+
41+
-C <directory>::
42+
Before running the subcommand, change the working directory. This
43+
option imitates the same option of linkgit:git[1].
44+
45+
-c <key>=<value>::
46+
For the duration of running the specified subcommand, configure this
47+
setting. This option imitates the same option of linkgit:git[1].
48+
3949
COMMANDS
4050
--------
4151

contrib/scalar/t/t9099-scalar.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,12 @@ test_expect_success 'scalar delete with enlistment' '
8585
test_path_is_missing cloned
8686
'
8787

88+
test_expect_success 'scalar supports -c/-C' '
89+
test_when_finished "scalar delete sub" &&
90+
git init sub &&
91+
scalar -C sub -c status.aheadBehind=bogus register &&
92+
test -z "$(git -C sub config --local status.aheadBehind)" &&
93+
test true = "$(git -C sub config core.preloadIndex)"
94+
'
95+
8896
test_done

0 commit comments

Comments
 (0)