| 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 jj_lib::ref_name::WorkspaceNameBuf; |
| 16 |
use jj_lib::workspace_store::SimpleWorkspaceStore; |
| 17 |
use jj_lib::workspace_store::WorkspaceStore as _; |
| 18 |
use tracing::instrument; |
| 19 |
|
| 20 |
use crate::cli_util::CommandHelper; |
| 21 |
use crate::command_error::CommandError; |
| 22 |
use crate::command_error::user_error; |
| 23 |
use crate::ui::Ui; |
| 24 |
|
| 25 |
/// Renames the current workspace |
| 26 |
#[derive(clap::Args, Clone, Debug)] |
| 27 |
pub struct WorkspaceRenameArgs { |
| 28 |
/// The name of the workspace to update to. |
| 29 |
new_workspace_name: WorkspaceNameBuf, |
| 30 |
} |
| 31 |
|
| 32 |
#[instrument(skip_all)] |
| 33 |
pub async fn cmd_workspace_rename( |
| 34 |
ui: &mut Ui, |
| 35 |
command: &CommandHelper, |
| 36 |
args: &WorkspaceRenameArgs, |
| 37 |
) -> Result<(), CommandError> { |
| 38 |
if args.new_workspace_name.as_str().is_empty() { |
| 39 |
return Err(user_error("New workspace name cannot be empty")); |
| 40 |
} |
| 41 |
|
| 42 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 43 |
|
| 44 |
let old_name = workspace_command.working_copy().workspace_name().to_owned(); |
| 45 |
let new_name = &*args.new_workspace_name; |
| 46 |
if new_name == old_name { |
| 47 |
writeln!(ui.status(), "Nothing changed.")?; |
| 48 |
return Ok(()); |
| 49 |
} |
| 50 |
|
| 51 |
if workspace_command |
| 52 |
.repo() |
| 53 |
.view() |
| 54 |
.get_wc_commit_id(&old_name) |
| 55 |
.is_none() |
| 56 |
{ |
| 57 |
return Err(user_error(format!( |
| 58 |
"The current workspace '{}' is not tracked in the repo.", |
| 59 |
old_name.as_symbol() |
| 60 |
))); |
| 61 |
} |
| 62 |
|
| 63 |
let workspace_store = SimpleWorkspaceStore::load(workspace_command.repo_path())?; |
| 64 |
|
| 65 |
let mut tx = workspace_command.start_transaction().into_inner(); |
| 66 |
let (mut locked_ws, _wc_commit) = workspace_command.start_working_copy_mutation().await?; |
| 67 |
|
| 68 |
locked_ws.locked_wc().rename_workspace(new_name.to_owned()); |
| 69 |
|
| 70 |
tx.repo_mut() |
| 71 |
.rename_workspace(&old_name, new_name.to_owned())?; |
| 72 |
|
| 73 |
workspace_store.rename(&old_name, new_name)?; |
| 74 |
|
| 75 |
let repo = tx |
| 76 |
.commit(format!( |
| 77 |
"Renamed workspace '{old}' to '{new}'", |
| 78 |
old = old_name.as_symbol(), |
| 79 |
new = new_name.as_symbol() |
| 80 |
)) |
| 81 |
.await?; |
| 82 |
locked_ws.finish(repo.op_id().clone()).await?; |
| 83 |
|
| 84 |
Ok(()) |
| 85 |
} |