-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathstatus.rs
More file actions
364 lines (332 loc) · 12.5 KB
/
status.rs
File metadata and controls
364 lines (332 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright 2020 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use itertools::Itertools as _;
use jj_lib::copies::CopyRecords;
use jj_lib::merge::Diff;
use jj_lib::merged_tree::MergedTree;
use jj_lib::repo::Repo as _;
use jj_lib::repo_path::RepoPath;
use jj_lib::repo_path::RepoPathBuf;
use jj_lib::revset::RevsetExpression;
use jj_lib::revset::RevsetFilterPredicate;
use tracing::instrument;
use crate::cli_util::CommandHelper;
use crate::cli_util::print_conflicted_paths;
use crate::cli_util::print_snapshot_stats;
use crate::cli_util::print_unmatched_explicit_paths;
use crate::command_error::CommandError;
use crate::diff_util::DiffFormat;
use crate::diff_util::get_copy_records;
use crate::formatter::FormatterExt as _;
use crate::ui::Ui;
/// Show high-level repo status [default alias: st]
///
/// This includes:
///
/// * The working copy commit and its parents, and a summary of the changes in
/// the working copy (compared to the merged parents)
///
/// * Conflicts in the working copy
///
/// * [Conflicted bookmarks]
///
/// Note: You can use `jj diff --summary -r <rev>` to see the changed files for
/// a specific revision.
///
/// [Conflicted bookmarks]:
/// https://docs.jj-vcs.dev/latest/bookmarks/#conflicts
#[derive(clap::Args, Clone, Debug)]
pub(crate) struct StatusArgs {
/// Restrict the status display to these paths
#[arg(value_name = "FILESETS", value_hint = clap::ValueHint::AnyPath)]
paths: Vec<String>,
}
#[instrument(skip_all)]
pub(crate) async fn cmd_status(
ui: &mut Ui,
command: &CommandHelper,
args: &StatusArgs,
) -> Result<(), CommandError> {
let (workspace_command, snapshot_stats) = command.workspace_helper_with_stats(ui)?;
print_snapshot_stats(
ui,
&snapshot_stats,
workspace_command.env().path_converter(),
)?;
let repo = workspace_command.repo();
let maybe_wc_commit = workspace_command
.get_wc_commit_id()
.map(|id| repo.store().get_commit(id))
.transpose()?;
let fileset_expression = workspace_command.parse_file_patterns(ui, &args.paths)?;
let matcher = fileset_expression.to_matcher();
ui.request_pager();
let mut formatter = ui.stdout_formatter();
let formatter = formatter.as_mut();
if let Some(wc_commit) = &maybe_wc_commit {
let parent_tree = wc_commit.parent_tree(repo.as_ref()).await?;
let tree = wc_commit.tree();
print_unmatched_explicit_paths(ui, &workspace_command, &fileset_expression, [&tree])?;
let wc_has_changes = tree.tree_ids() != parent_tree.tree_ids();
let wc_has_untracked = !snapshot_stats.untracked_paths.is_empty();
if !wc_has_changes && !wc_has_untracked {
writeln!(formatter, "The working copy has no changes.")?;
} else {
if wc_has_changes {
writeln!(formatter, "Working copy changes:")?;
let mut copy_records = CopyRecords::default();
for parent in wc_commit.parent_ids() {
let records = get_copy_records(repo.store(), parent, wc_commit.id(), &matcher)?;
copy_records.add_records(records)?;
}
let diff_renderer = workspace_command.diff_renderer(vec![DiffFormat::Summary]);
let width = ui.term_width();
diff_renderer
.show_diff(
ui,
formatter,
Diff::new(&parent_tree, &tree),
&matcher,
©_records,
width,
)
.await?;
}
if wc_has_untracked {
writeln!(formatter, "Untracked paths:")?;
visit_collapsed_untracked_files(
snapshot_stats.untracked_paths.keys(),
tree.clone(),
|path, is_dir| {
let ui_path = workspace_command.path_converter().format_file_path(path);
writeln!(
formatter.labeled("diff").labeled("untracked"),
"? {ui_path}{}",
if is_dir {
std::path::MAIN_SEPARATOR_STR
} else {
""
}
)?;
Ok(())
},
)
.await?;
}
}
let template = workspace_command.commit_summary_template();
write!(formatter, "Working copy (@) : ")?;
template.format(wc_commit, formatter)?;
writeln!(formatter)?;
for parent in wc_commit.parents().await? {
// "Working copy (@) : "
write!(formatter, "Parent commit (@-): ")?;
template.format(&parent, formatter)?;
writeln!(formatter)?;
}
if wc_commit.has_conflict() {
let conflicts = wc_commit.tree().conflicts_matching(&matcher).collect_vec();
writeln!(
formatter.labeled("warning").with_heading("Warning: "),
"There are unresolved conflicts at these paths:"
)?;
print_conflicted_paths(conflicts, formatter, &workspace_command)?;
let wc_revset = RevsetExpression::commit(wc_commit.id().clone());
// Ancestors with conflicts, excluding the current working copy commit.
let ancestors_conflicts: Vec<_> = workspace_command
.attach_revset_evaluator(
wc_revset
.parents()
.ancestors()
.filtered(RevsetFilterPredicate::HasConflict)
.minus(&workspace_command.env().immutable_expression()),
)
.evaluate_to_commit_ids()?
.try_collect()?;
workspace_command.report_repo_conflicts(formatter, repo, ancestors_conflicts)?;
} else {
for parent in wc_commit.parents().await? {
if parent.has_conflict() {
writeln!(
formatter.labeled("hint").with_heading("Hint: "),
"Conflict in parent commit has been resolved in working copy"
)?;
break;
}
}
}
} else {
writeln!(formatter, "No working copy")?;
}
let conflicted_local_bookmarks = repo
.view()
.local_bookmarks()
.filter(|(_, target)| target.has_conflict())
.map(|(bookmark_name, _)| bookmark_name)
.collect_vec();
let conflicted_remote_bookmarks = repo
.view()
.all_remote_bookmarks()
.filter(|(_, remote_ref)| remote_ref.target.has_conflict())
.map(|(symbol, _)| symbol)
.collect_vec();
if !conflicted_local_bookmarks.is_empty() {
writeln!(
formatter.labeled("warning").with_heading("Warning: "),
"These bookmarks have conflicts:"
)?;
for name in conflicted_local_bookmarks {
write!(formatter, " ")?;
write!(formatter.labeled("bookmark"), "{}", name.as_symbol())?;
writeln!(formatter)?;
}
writeln!(
formatter.labeled("hint").with_heading("Hint: "),
"Use `jj bookmark list` to see details. Use `jj bookmark set <name> -r <rev>` to \
resolve."
)?;
}
if !conflicted_remote_bookmarks.is_empty() {
writeln!(
formatter.labeled("warning").with_heading("Warning: "),
"These remote bookmarks have conflicts:"
)?;
for symbol in conflicted_remote_bookmarks {
write!(formatter, " ")?;
write!(formatter.labeled("bookmark"), "{symbol}")?;
writeln!(formatter)?;
}
writeln!(
formatter.labeled("hint").with_heading("Hint: "),
"Use `jj bookmark list` to see details. Use `jj git fetch` to resolve."
)?;
}
Ok(())
}
async fn visit_collapsed_untracked_files(
untracked_paths: impl IntoIterator<Item = impl AsRef<RepoPath>>,
tree: MergedTree,
mut on_path: impl FnMut(&RepoPath, bool) -> Result<(), CommandError>,
) -> Result<(), CommandError> {
let trees = tree.trees().await?;
let mut stack = vec![trees];
// TODO: This loop can be improved with BTreeMap cursors once that's stable,
// would remove the need for the whole `skip_prefixed_by` thing and turn it
// into a B-tree lookup.
let mut skip_prefixed_by_dir: Option<RepoPathBuf> = None;
'untracked: for path in untracked_paths {
let path = path.as_ref();
if skip_prefixed_by_dir
.as_ref()
.is_some_and(|p| path.starts_with(p))
{
continue;
} else {
skip_prefixed_by_dir = None;
}
let mut it = path.components().dropping_back(1);
let first_mismatch = it.by_ref().enumerate().find(|(i, component)| {
stack.get(i + 1).is_none_or(|tree| {
tree.dir()
.components()
.next_back()
.expect("should always have at least one element (the root)")
!= *component
})
});
if let Some((i, component)) = first_mismatch {
stack.truncate(i + 1);
for component in std::iter::once(component).chain(it) {
let parent = stack
.last()
.expect("should always have at least one element (the root)");
if let Some(subtree) = parent.sub_tree(component).await? {
stack.push(subtree);
} else {
let dir = parent.dir().join(component);
on_path(&dir, true)?;
skip_prefixed_by_dir = Some(dir);
continue 'untracked;
}
}
}
on_path(path, false)?;
}
Ok(())
}
#[cfg(test)]
mod test {
use testutils::FutureTestExt as _;
use testutils::TestRepo;
use testutils::TestTreeBuilder;
use testutils::repo_path;
use super::*;
fn collect_collapsed_untracked_files_string(
untracked_paths: &[&RepoPath],
tree: MergedTree,
) -> String {
let mut result = String::new();
visit_collapsed_untracked_files(untracked_paths, tree, |path, is_dir| {
result.push_str("? ");
if is_dir {
result.push_str(&path.to_internal_dir_string());
} else {
result.push_str(path.as_internal_file_string());
}
result.push('\n');
Ok(())
})
.block_unwrap();
result
}
#[test]
fn test_collapsed_untracked_files() {
let repo = TestRepo::init();
let tracked = {
let mut builder = TestTreeBuilder::new(repo.repo.store().clone());
builder.file(repo_path("top_level_file"), "");
// ? "untracked_top_level_file"
// ? "dir"
// ? "dir2/c"
builder.file(repo_path("dir2/d"), "");
// ? "dir3/partially_tracked/e"
builder.file(repo_path("dir3/partially_tracked/f"), "");
// ? "dir3/fully_untracked/"
builder.file(repo_path("dir3/j"), "");
// ? "dir3/k"
builder.write_merged_tree()
};
let untracked = &[
repo_path("untracked_top_level_file"),
repo_path("dir/a"),
repo_path("dir/b"),
repo_path("dir2/c"),
repo_path("dir3/partially_tracked/e"),
repo_path("dir3/fully_untracked/g"),
repo_path("dir3/fully_untracked/h"),
repo_path("dir3/k"),
];
insta::assert_snapshot!(
collect_collapsed_untracked_files_string(untracked, tracked),
@"
? untracked_top_level_file
? dir/
? dir2/c
? dir3/partially_tracked/e
? dir3/fully_untracked/
? dir3/k
"
);
}
}