| 1 |
// Copyright 2020 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 clap_complete::ArgValueCandidates; |
| 16 |
use jj_lib::repo::Repo as _; |
| 17 |
use tracing::instrument; |
| 18 |
|
| 19 |
use crate::cli_util::CommandHelper; |
| 20 |
use crate::command_error::CommandError; |
| 21 |
use crate::commit_templater::WorkspaceRef; |
| 22 |
use crate::complete; |
| 23 |
use crate::templater::TemplateRenderer; |
| 24 |
use crate::ui::Ui; |
| 25 |
|
| 26 |
/// List workspaces |
| 27 |
#[derive(clap::Args, Clone, Debug)] |
| 28 |
pub struct WorkspaceListArgs { |
| 29 |
/// Render each workspace using the given template |
| 30 |
/// |
| 31 |
/// All 0-argument methods of the [`WorkspaceRef` type] are available as |
| 32 |
/// keywords in the template expression. See [`jj help -k templates`] for |
| 33 |
/// more information. |
| 34 |
/// |
| 35 |
/// The default template can be set by the `templates.workspace_list` |
| 36 |
/// setting. |
| 37 |
/// |
| 38 |
/// [`WorkspaceRef` type]: |
| 39 |
/// https://docs.jj-vcs.dev/latest/templates/#workspaceref-type |
| 40 |
/// |
| 41 |
/// [`jj help -k templates`]: |
| 42 |
/// https://docs.jj-vcs.dev/latest/templates/ |
| 43 |
#[arg(long, short = 'T')] |
| 44 |
#[arg(add = ArgValueCandidates::new(complete::template_aliases))] |
| 45 |
template: Option<String>, |
| 46 |
} |
| 47 |
|
| 48 |
#[instrument(skip_all)] |
| 49 |
pub async fn cmd_workspace_list( |
| 50 |
ui: &mut Ui, |
| 51 |
command: &CommandHelper, |
| 52 |
args: &WorkspaceListArgs, |
| 53 |
) -> Result<(), CommandError> { |
| 54 |
let workspace_command = command.workspace_helper(ui).await?; |
| 55 |
|
| 56 |
let template: TemplateRenderer<WorkspaceRef> = { |
| 57 |
let language = workspace_command.commit_template_language(); |
| 58 |
|
| 59 |
let text = match &args.template { |
| 60 |
Some(value) => value.to_owned(), |
| 61 |
None => workspace_command |
| 62 |
.settings() |
| 63 |
.get("templates.workspace_list")?, |
| 64 |
}; |
| 65 |
|
| 66 |
workspace_command |
| 67 |
.parse_template(ui, &language, &text)? |
| 68 |
.labeled(["workspace_list"]) |
| 69 |
}; |
| 70 |
|
| 71 |
let repo = workspace_command.repo(); |
| 72 |
let mut formatter = ui.stdout_formatter(); |
| 73 |
|
| 74 |
for (name, wc_commit_id) in repo.view().wc_commit_ids() { |
| 75 |
let commit = repo.store().get_commit_async(wc_commit_id).await?; |
| 76 |
let ws_ref = WorkspaceRef::new(name.clone(), commit); |
| 77 |
|
| 78 |
template.format(&ws_ref, formatter.as_mut())?; |
| 79 |
} |
| 80 |
|
| 81 |
Ok(()) |
| 82 |
} |