Debian Codegraph @

cli/src/commands/unsign.rs

raw
1 // Copyright 2023 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::ArgValueCompleter;
16 use futures::TryStreamExt as _;
17 use indexmap::IndexSet;
18 use itertools::Itertools as _;
19 use jj_lib::commit::Commit;
20 use jj_lib::commit::CommitIteratorExt as _;
21 use jj_lib::repo::Repo as _;
22 use jj_lib::revset::RevsetStreamExt as _;
23 use jj_lib::signing::SignBehavior;
24
25 use crate::cli_util::CommandHelper;
26 use crate::cli_util::RevisionArg;
27 use crate::cli_util::print_updated_commits;
28 use crate::command_error::CommandError;
29 use crate::complete;
30 use crate::ui::Ui;
31
32 /// Drop a cryptographic signature
33 ///
34 /// See also [commit signing] docs.
35 ///
36 /// [commit signing]:
37 /// https://docs.jj-vcs.dev/latest/config/#commit-signing
38 #[derive(clap::Args, Clone, Debug)]
39 pub struct UnsignArgs {
40 /// What revision(s) to unsign
41 #[arg(long = "revision", short, value_name = "REVSETS", alias = "revisions")]
42 #[arg(add = ArgValueCompleter::new(complete::revset_expression_mutable))]
43 revisions: Vec<RevisionArg>,
44 }
45
46 pub async fn cmd_unsign(
47 ui: &mut Ui,
48 command: &CommandHelper,
49 args: &UnsignArgs,
50 ) -> Result<(), CommandError> {
51 let mut workspace_command = command.workspace_helper(ui).await?;
52
53 let target_expr = workspace_command
54 .parse_union_revsets(ui, &args.revisions)?
55 .resolve()?;
56 workspace_command
57 .check_rewritable_expr(&target_expr)
58 .await?;
59 let commits: IndexSet<Commit> = target_expr
60 .evaluate(workspace_command.repo().as_ref())?
61 .stream()
62 .commits(workspace_command.repo().store())
63 .try_collect()
64 .await?;
65
66 let to_unsign: IndexSet<Commit> = commits
67 .into_iter()
68 .filter(|commit| commit.is_signed())
69 .collect();
70
71 let mut tx = workspace_command.start_transaction();
72
73 let mut unsigned_commits = vec![];
74 let mut num_reparented = 0;
75
76 tx.repo_mut()
77 .transform_descendants(
78 to_unsign.iter().ids().cloned().collect_vec(),
79 async |rewriter| {
80 let old_commit = rewriter.old_commit().clone();
81 let commit_builder = rewriter.reparent();
82
83 if to_unsign.contains(&old_commit) {
84 let new_commit = commit_builder
85 .set_sign_behavior(SignBehavior::Drop)
86 .write()
87 .await?;
88
89 unsigned_commits.push(new_commit);
90 } else {
91 commit_builder.write().await?;
92 num_reparented += 1;
93 }
94 Ok(())
95 },
96 )
97 .await?;
98
99 if let Some(mut formatter) = ui.status_formatter()
100 && !unsigned_commits.is_empty()
101 {
102 writeln!(formatter, "Unsigned {} commits:", unsigned_commits.len())?;
103 print_updated_commits(
104 formatter.as_mut(),
105 &tx.commit_summary_template(),
106 &unsigned_commits,
107 )?;
108 }
109
110 let num_not_authored_by_me = unsigned_commits
111 .iter()
112 .filter(|commit| commit.author().email != tx.settings().user_email())
113 .count();
114 if num_not_authored_by_me > 0 {
115 writeln!(
116 ui.warning_default(),
117 "{num_not_authored_by_me} of these commits are not authored by you.",
118 )?;
119 }
120
121 if num_reparented > 0 {
122 writeln!(ui.status(), "Rebased {num_reparented} descendant commits.")?;
123 }
124
125 let transaction_description = match &*unsigned_commits {
126 [] => "".to_string(),
127 [commit] => format!("unsign commit {}", commit.id()),
128 commits => format!(
129 "unsign commit {} and {} more",
130 commits[0].id(),
131 commits.len() - 1
132 ),
133 };
134 tx.finish(ui, transaction_description).await?;
135
136 Ok(())
137 }