Skip to content

Commit 08daf32

Browse files
committed
util: add exec command for arbitrary aliases
1 parent 2aadae1 commit 08daf32

File tree

6 files changed

+215
-0
lines changed

6 files changed

+215
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1717
* Templates now support the `==` logical operator for `Boolean`, `Integer`, and
1818
`String` types.
1919

20+
* New command `jj util exec` that can be used for arbitrary aliases.
21+
2022
### Fixed bugs
2123

2224
## [0.23.0] - 2024-11-06

cli/src/commands/util/exec.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 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 undo` to wiping your file system.
29+
/// Exercise the same amount of caution while writing these aliases as you would
30+
/// when typing commands into the terminal!
31+
///
32+
/// This feature may be removed entirely in the future. It may be replaced by
33+
/// an embedded scripting language with limited capabilities for safety.
34+
///
35+
/// Let's assume you have a script called "my-jj-script" in you $PATH and you
36+
/// would like to execute it as "jj my-script". You would add the following line
37+
/// to your configuration file to achieve that:
38+
///
39+
/// ```toml
40+
/// [aliases]
41+
/// my-script = ["util", "exec", "--", "my-jj-script"]
42+
/// # ^^^^
43+
/// # This makes sure that flags are passed to your script instead of parsed by jj.
44+
/// ```
45+
///
46+
/// If you don't want to manage your script as a separate file, you can even
47+
/// inline it into your config file:
48+
///
49+
/// ```toml
50+
/// [aliases]
51+
/// my-inline-script = ["util", "exec", "--", "bash", "-c", """
52+
/// #!/usr/bin/env bash
53+
/// set -euo pipefail
54+
/// echo "Look Ma, everything in one file!"
55+
/// echo "args: $@"
56+
/// """, ""]
57+
/// # ^^
58+
/// # This last empty string will become "$0" in bash, so your actual arguments
59+
/// # are all included in "$@" and start at "$1" as expected.
60+
/// ```
61+
#[derive(clap::Args, Clone, Debug)]
62+
#[command(verbatim_doc_comment)]
63+
pub(crate) struct UtilExecArgs {
64+
/// External command to execute
65+
command: String,
66+
/// Arguments to pass to the external command
67+
args: Vec<String>,
68+
}
69+
70+
pub fn cmd_util_exec(
71+
_ui: &mut Ui,
72+
_command: &CommandHelper,
73+
args: &UtilExecArgs,
74+
) -> Result<(), CommandError> {
75+
let status = std::process::Command::new(&args.command)
76+
.args(&args.args)
77+
.status()
78+
.map_err(user_error)?;
79+
if !status.success() {
80+
let error_msg = if let Some(exit_code) = status.code() {
81+
format!("External command exited with {exit_code}")
82+
} else {
83+
"External command was terminated by signal".into()
84+
};
85+
return Err(user_error(error_msg));
86+
}
87+
Ok(())
88+
}

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: 55 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,59 @@ 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 undo` to wiping your file system.
2177+
Exercise the same amount of caution while writing these aliases as you would
2178+
when typing commands into the terminal!
2179+
2180+
This feature may be removed entirely in the future. It may be replaced by
2181+
an embedded scripting language with limited capabilities for safety.
2182+
2183+
Let's assume you have a script called "my-jj-script" in you $PATH and you
2184+
would like to execute it as "jj my-script". You would add the following line
2185+
to your configuration file to achieve that:
2186+
2187+
```toml
2188+
[aliases]
2189+
my-script = ["util", "exec", "--", "my-jj-script"]
2190+
# ^^^^
2191+
# This makes sure that flags are passed to your script instead of parsed by jj.
2192+
```
2193+
2194+
If you don't want to manage your script as a separate file, you can even
2195+
inline it into your config file:
2196+
2197+
```toml
2198+
[aliases]
2199+
my-inline-script = ["util", "exec", "--", "bash", "-c", """
2200+
#!/usr/bin/env bash
2201+
set -euo pipefail
2202+
echo "Look Ma, everything in one file!"
2203+
echo "args: $@"
2204+
""", ""]
2205+
# ^^
2206+
# This last empty string will become "$0" in bash, so your actual arguments
2207+
# are all included in "$@" and start at "$1" as expected.
2208+
```
2209+
2210+
**Usage:** `jj util exec <COMMAND> [ARGS]...`
2211+
2212+
###### **Arguments:**
2213+
2214+
* `<COMMAND>` — External command to execute
2215+
* `<ARGS>` — Arguments to pass to the external command
2216+
2217+
2218+
21642219
## `jj util gc`
21652220
21662221
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,41 @@ 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 undo` to wiping your file system.
541+
Exercise the same amount of caution while writing these aliases as you would
542+
when typing commands into the terminal!
543+
544+
This feature may be removed entirely in the future. It may be replaced by
545+
an embedded scripting language with limited capabilities for safety.
546+
547+
The command `jj util exec` will simply run any command you pass to it as an
548+
argument. Additional arguments are passed through. Here are some examples:
549+
550+
```toml
551+
[aliases]
552+
my-script = ["util", "exec", "--", "my-jj-script"]
553+
# ^^^^
554+
# This makes sure that flags are passed to your script instead of parsed by jj.
555+
my-inline-script = ["util", "exec", "--", "bash", "-c", """
556+
#!/usr/bin/env bash
557+
set -euo pipefail
558+
echo "Look Ma, everything in one file!"
559+
echo "args: $@"
560+
""", ""]
561+
# ^^
562+
# This last empty string will become "$0" in bash, so your actual arguments
563+
# are all included in "$@" and start at "$1" as expected.
564+
```
565+
531566
## Editor
532567

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

0 commit comments

Comments
 (0)