Skip to content

Commit d63a98c

Browse files
committed
util: add exec command for arbitrary aliases
1 parent 7d1041c commit d63a98c

File tree

6 files changed

+206
-0
lines changed

6 files changed

+206
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
8888
for some keywords (e.g. `jj help -k revsets`). To see a list of the available
8989
keywords you can do `jj help --help`.
9090

91+
* New command `jj util exec` that can be used for arbitrary aliases.
92+
9193
### Fixed bugs
9294

9395
* Error on `trunk()` revset resolution is now handled gracefully.

cli/src/commands/util/exec.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2020-2024 The Jujutsu Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use crate::cli_util::CommandHelper;
16+
use crate::command_error::user_error;
17+
use crate::command_error::CommandError;
18+
use crate::ui::Ui;
19+
20+
/// Execute an external command via jj
21+
///
22+
/// This is useful for arbitrary aliases.
23+
///
24+
/// !! WARNING !!
25+
///
26+
/// The following technique just provides a convenient syntax for running
27+
/// arbitrary code on your system. Using it irresponsibly may cause damage
28+
/// ranging from breaking the behavior of `jj op undo` to wiping your file
29+
/// system. Exercise the same amount of caution while writing these aliases as
30+
/// you would when typing commands into the terminal!
31+
///
32+
/// Let's assume you have a script called "my-jj-script" in you $PATH and you
33+
/// would like to execute it as "jj my-script". You would add the following line
34+
/// to your configuration file to achieve that:
35+
///
36+
/// ```toml
37+
/// [aliases]
38+
/// my-script = ["util", "exec", "--", "my-jj-script"]
39+
/// # ^^^^
40+
/// # This makes sure that flags are passed to your script instead of parsed by jj.
41+
/// ```
42+
///
43+
/// If you don't want to manage your script as a separate file, you can even
44+
/// inline it into your config file:
45+
///
46+
/// ```toml
47+
/// [aliases]
48+
/// my-inline-script = ["util", "exec", "--", "bash", "-c", """
49+
/// #!/usr/bin/env bash
50+
/// set -euo pipefail
51+
/// echo "Look Ma, everything in one file!"
52+
/// echo "args: $@"
53+
/// """, ""]
54+
/// # ^^
55+
/// # This last empty string will become "$0" in bash, so your actual arguments
56+
/// # are all included in "$@" and start at "$1" as expected.
57+
/// ```
58+
#[derive(clap::Args, Clone, Debug)]
59+
#[command(verbatim_doc_comment)]
60+
pub(crate) struct UtilExecArgs {
61+
/// External command to execute
62+
command: String,
63+
/// Arguments to pass to the external command
64+
args: Vec<String>,
65+
}
66+
67+
pub fn cmd_util_exec(
68+
_ui: &mut Ui,
69+
_command: &CommandHelper,
70+
args: &UtilExecArgs,
71+
) -> Result<(), CommandError> {
72+
let status = std::process::Command::new(&args.command)
73+
.args(&args.args)
74+
.status()
75+
.map_err(user_error)?;
76+
if !status.success() {
77+
let error_msg = if let Some(exit_code) = status.code() {
78+
format!("External command exited with {exit_code}")
79+
} else {
80+
"External command was terminated by signal".into()
81+
};
82+
return Err(user_error(error_msg));
83+
}
84+
Ok(())
85+
}

cli/src/commands/util/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
mod completion;
1616
mod config_schema;
17+
mod exec;
1718
mod gc;
1819
mod mangen;
1920
mod markdown_help;
@@ -25,6 +26,8 @@ use self::completion::cmd_util_completion;
2526
use self::completion::UtilCompletionArgs;
2627
use self::config_schema::cmd_util_config_schema;
2728
use self::config_schema::UtilConfigSchemaArgs;
29+
use self::exec::cmd_util_exec;
30+
use self::exec::UtilExecArgs;
2831
use self::gc::cmd_util_gc;
2932
use self::gc::UtilGcArgs;
3033
use self::mangen::cmd_util_mangen;
@@ -40,6 +43,7 @@ use crate::ui::Ui;
4043
pub(crate) enum UtilCommand {
4144
Completion(UtilCompletionArgs),
4245
ConfigSchema(UtilConfigSchemaArgs),
46+
Exec(UtilExecArgs),
4347
Gc(UtilGcArgs),
4448
Mangen(UtilMangenArgs),
4549
MarkdownHelp(UtilMarkdownHelp),
@@ -54,6 +58,7 @@ pub(crate) fn cmd_util(
5458
match subcommand {
5559
UtilCommand::Completion(args) => cmd_util_completion(ui, command, args),
5660
UtilCommand::ConfigSchema(args) => cmd_util_config_schema(ui, command, args),
61+
UtilCommand::Exec(args) => cmd_util_exec(ui, command, args),
5762
UtilCommand::Gc(args) => cmd_util_gc(ui, command, args),
5863
UtilCommand::Mangen(args) => cmd_util_mangen(ui, command, args),
5964
UtilCommand::MarkdownHelp(args) => cmd_util_markdown_help(ui, command, args),

cli/tests/[email protected]

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ This document contains the help content for the `jj` command-line program.
9393
* [`jj util`↴](#jj-util)
9494
* [`jj util completion`↴](#jj-util-completion)
9595
* [`jj util config-schema`↴](#jj-util-config-schema)
96+
* [`jj util exec`↴](#jj-util-exec)
9697
* [`jj util gc`↴](#jj-util-gc)
9798
* [`jj util mangen`↴](#jj-util-mangen)
9899
* [`jj util markdown-help`↴](#jj-util-markdown-help)
@@ -2116,6 +2117,7 @@ Infrequently used commands such as for generating shell completions
21162117
21172118
* `completion` — Print a command-line-completion script
21182119
* `config-schema` — Print the JSON schema for the jj TOML config format
2120+
* `exec` — Execute an external command via jj
21192121
* `gc` — Run backend-dependent garbage collection
21202122
* `mangen` — Print a ROFF (manpage)
21212123
* `markdown-help` — Print the CLI help for all subcommands in Markdown
@@ -2161,6 +2163,56 @@ Print the JSON schema for the jj TOML config format
21612163
21622164
21632165
2166+
## `jj util exec`
2167+
2168+
Execute an external command via jj
2169+
2170+
This is useful for arbitrary aliases.
2171+
2172+
!! WARNING !!
2173+
2174+
The following technique just provides a convenient syntax for running
2175+
arbitrary code on your system. Using it irresponsibly may cause damage
2176+
ranging from breaking the behavior of `jj op undo` to wiping your file
2177+
system. Exercise the same amount of caution while writing these aliases as
2178+
you would when typing commands into the terminal!
2179+
2180+
Let's assume you have a script called "my-jj-script" in you $PATH and you
2181+
would like to execute it as "jj my-script". You would add the following line
2182+
to your configuration file to achieve that:
2183+
2184+
```toml
2185+
[aliases]
2186+
my-script = ["util", "exec", "--", "my-jj-script"]
2187+
# ^^^^
2188+
# This makes sure that flags are passed to your script instead of parsed by jj.
2189+
```
2190+
2191+
If you don't want to manage your script as a separate file, you can even
2192+
inline it into your config file:
2193+
2194+
```toml
2195+
[aliases]
2196+
my-inline-script = ["util", "exec", "--", "bash", "-c", """
2197+
#!/usr/bin/env bash
2198+
set -euo pipefail
2199+
echo "Look Ma, everything in one file!"
2200+
echo "args: $@"
2201+
""", ""]
2202+
# ^^
2203+
# This last empty string will become "$0" in bash, so your actual arguments
2204+
# are all included in "$@" and start at "$1" as expected.
2205+
```
2206+
2207+
**Usage:** `jj util exec <COMMAND> [ARGS]...`
2208+
2209+
###### **Arguments:**
2210+
2211+
* `<COMMAND>` — External command to execute
2212+
* `<ARGS>` — Arguments to pass to the external command
2213+
2214+
2215+
21642216
## `jj util gc`
21652217
21662218
Run backend-dependent garbage collection

cli/tests/test_util_command.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,33 @@ fn test_shell_completions() {
112112
test("nushell");
113113
test("zsh");
114114
}
115+
116+
#[test]
117+
fn test_util_exec() {
118+
let test_env = TestEnvironment::default();
119+
let formatter_path = assert_cmd::cargo::cargo_bin("fake-formatter");
120+
let (out, err) = test_env.jj_cmd_ok(
121+
test_env.env_root(),
122+
&[
123+
"util",
124+
"exec",
125+
"--",
126+
formatter_path.to_str().unwrap(),
127+
"--append",
128+
"hello",
129+
],
130+
);
131+
insta::assert_snapshot!(out, @"hello");
132+
// Ensures only stdout contains text
133+
assert!(err.is_empty());
134+
}
135+
136+
#[test]
137+
fn test_util_exec_fail() {
138+
let test_env = TestEnvironment::default();
139+
let err = test_env.jj_cmd_failure(
140+
test_env.env_root(),
141+
&["util", "exec", "--", "missing-program"],
142+
);
143+
assert!(!err.is_empty());
144+
}

docs/config.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,38 @@ You can define aliases for commands, including their arguments. For example:
528528
aliases.l = ["log", "-r", "(main..@):: | (main..@)-"]
529529
```
530530

531+
This alias syntax can only run a single jj command. However, you may want to
532+
execute multiple jj commands with a single alias, or run arbitrary scripts that
533+
complement your version control workflow. This can be done, but be aware of the
534+
danger:
535+
536+
!!! warning
537+
538+
The following technique just provides a convenient syntax for running
539+
arbitrary code on your system. Using it irresponsibly may cause damage
540+
ranging from breaking the behavior of `jj op undo` to wiping your file
541+
system. Exercise the same amount of caution while writing these aliases as
542+
you would when typing commands into the terminal!
543+
544+
The command `jj util exec` will simply run any command you pass to it as an
545+
argument. Additional arguments are passed through. Here are some examples:
546+
547+
```toml
548+
[aliases]
549+
my-script = ["util", "exec", "--", "my-jj-script"]
550+
# ^^^^
551+
# This makes sure that flags are passed to your script instead of parsed by jj.
552+
my-inline-script = ["util", "exec", "--", "bash", "-c", """
553+
#!/usr/bin/env bash
554+
set -euo pipefail
555+
echo "Look Ma, everything in one file!"
556+
echo "args: $@"
557+
""", ""]
558+
# ^^
559+
# This last empty string will become "$0" in bash, so your actual arguments
560+
# are all included in "$@" and start at "$1" as expected.
561+
```
562+
531563
## Editor
532564

533565
The default editor is set via `ui.editor`, though there are several places to

0 commit comments

Comments
 (0)