use std::io::Write;
use std::slice;
use std::time::{Duration, SystemTime};
use clap::Subcommand;
use clap_complete::Shell;
use jj_lib::repo::Repo;
use tracing::instrument;
use crate::cli_util::{user_error, CommandError, CommandHelper};
use crate::ui::Ui;
#[derive(Subcommand, Clone, Debug)]
pub(crate) enum UtilCommand {
Completion(UtilCompletionArgs),
Gc(UtilGcArgs),
Mangen(UtilMangenArgs),
MarkdownHelp(UtilMarkdownHelp),
ConfigSchema(UtilConfigSchemaArgs),
}
#[doc = r#"Print a command-line-completion script
Apply it by running one of these:
- **bash**: `source <(jj util completion)`
- **fish**: `jj util completion --fish | source`
- **zsh**:
```shell
autoload -U compinit
compinit
source <(jj util completion --zsh)
```
"#]
#[derive(clap::Args, Clone, Debug)]
#[command(verbatim_doc_comment)]
pub(crate) struct UtilCompletionArgs {
shell: Option<Shell>,
#[arg(long, hide = true)]
bash: bool,
#[arg(long, hide = true)]
fish: bool,
#[arg(long, hide = true)]
zsh: bool,
}
#[derive(clap::Args, Clone, Debug)]
pub(crate) struct UtilGcArgs {
#[arg(long)]
expire: Option<String>,
}
#[derive(clap::Args, Clone, Debug)]
pub(crate) struct UtilMangenArgs {}
#[derive(clap::Args, Clone, Debug)]
pub(crate) struct UtilMarkdownHelp {}
#[derive(clap::Args, Clone, Debug)]
pub(crate) struct UtilConfigSchemaArgs {}
#[instrument(skip_all)]
pub(crate) fn cmd_util(
ui: &mut Ui,
command: &CommandHelper,
subcommand: &UtilCommand,
) -> Result<(), CommandError> {
match subcommand {
UtilCommand::Completion(args) => cmd_util_completion(ui, command, args),
UtilCommand::Gc(args) => cmd_util_gc(ui, command, args),
UtilCommand::Mangen(args) => cmd_util_mangen(ui, command, args),
UtilCommand::MarkdownHelp(args) => cmd_util_markdownhelp(ui, command, args),
UtilCommand::ConfigSchema(args) => cmd_util_config_schema(ui, command, args),
}
}
fn cmd_util_completion(
ui: &mut Ui,
command: &CommandHelper,
args: &UtilCompletionArgs,
) -> Result<(), CommandError> {
let mut app = command.app().clone();
let warn = |shell| {
writeln!(
ui.warning(),
"Warning: `jj util completion --{shell}` will be removed in a future version, and \
this will be a hard error"
)?;
writeln!(ui.hint(), "Hint: Use `jj util completion {shell}` instead")
};
let mut buf = vec![];
let shell = match (args.shell, args.fish, args.zsh, args.bash) {
(Some(s), false, false, false) => s,
(None, true, false, false) => {
warn("fish")?;
Shell::Fish
}
(None, false, true, false) => {
warn("zsh")?;
Shell::Zsh
}
(None, false, false, _) => {
warn("bash")?;
Shell::Bash
}
_ => {
return Err(user_error(
"cannot generate completion for multiple shells at once",
))
}
};
clap_complete::generate(shell, &mut app, "jj", &mut buf);
ui.stdout_formatter().write_all(&buf)?;
Ok(())
}
fn cmd_util_gc(
ui: &mut Ui,
command: &CommandHelper,
args: &UtilGcArgs,
) -> Result<(), CommandError> {
if command.global_args().at_operation != "@" {
return Err(user_error(
"Cannot garbage collect from a non-head operation",
));
}
let keep_newer = match args.expire.as_deref() {
None => SystemTime::now() - Duration::from_secs(14 * 86400),
Some("now") => SystemTime::now() - Duration::ZERO,
_ => return Err(user_error("--expire only accepts 'now'")),
};
let workspace_command = command.workspace_helper(ui)?;
let repo = workspace_command.repo();
repo.op_store()
.gc(slice::from_ref(repo.op_id()), keep_newer)?;
repo.store().gc(repo.index(), keep_newer)?;
Ok(())
}
fn cmd_util_mangen(
ui: &mut Ui,
command: &CommandHelper,
_args: &UtilMangenArgs,
) -> Result<(), CommandError> {
let mut buf = vec![];
let man = clap_mangen::Man::new(command.app().clone());
man.render(&mut buf)?;
ui.stdout_formatter().write_all(&buf)?;
Ok(())
}
fn cmd_util_markdownhelp(
ui: &mut Ui,
command: &CommandHelper,
_args: &UtilMarkdownHelp,
) -> Result<(), CommandError> {
let markdown = clap_markdown::help_markdown_command(command.app()).into_bytes();
ui.stdout_formatter().write_all(&markdown)?;
Ok(())
}
fn cmd_util_config_schema(
ui: &mut Ui,
_command: &CommandHelper,
_args: &UtilConfigSchemaArgs,
) -> Result<(), CommandError> {
let buf = include_bytes!("../config-schema.json");
ui.stdout_formatter().write_all(buf)?;
Ok(())
}