diff --git a/cli/src/commands/status.rs b/cli/src/commands/status.rs index a0e857384f..7c95ea0922 100644 --- a/cli/src/commands/status.rs +++ b/cli/src/commands/status.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::io; + use itertools::Itertools; use jj_lib::copies::CopyRecords; use jj_lib::repo::Repo; @@ -47,7 +49,7 @@ pub(crate) fn cmd_status( command: &CommandHelper, args: &StatusArgs, ) -> Result<(), CommandError> { - let (workspace_command, _snapshot_stats) = command.workspace_helper_with_stats(ui)?; + let (workspace_command, snapshot_stats) = command.workspace_helper_with_stats(ui)?; let repo = workspace_command.repo(); let maybe_wc_commit = workspace_command .get_wc_commit_id() @@ -63,26 +65,44 @@ pub(crate) fn cmd_status( if let Some(wc_commit) = &maybe_wc_commit { let parent_tree = wc_commit.parent_tree(repo.as_ref())?; let tree = wc_commit.tree()?; - if tree.id() == parent_tree.id() { + + let wc_has_changes = tree.id() != parent_tree.id(); + let wc_has_untracked = !snapshot_stats.untracked_paths.is_empty(); + if !wc_has_changes && !wc_has_untracked { writeln!(formatter, "The working copy is clean")?; } else { - 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)?; + 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, + &parent_tree, + &tree, + &matcher, + ©_records, + width, + )?; + } + + if wc_has_untracked { + // TODO: make sure this always display all untracked non-ignored files, even + // when using watchman. See https://github.com/jj-vcs/jj/commit/168c7979feab40d58f49fe19683975697a7bc089 for details. + writeln!(formatter, "Untracked paths:")?; + formatter.with_label("diff", |formatter| { + for path in snapshot_stats.untracked_paths.keys() { + let ui_path = workspace_command.path_converter().format_file_path(path); + writeln!(formatter.labeled("untracked"), "? {ui_path}")?; + } + io::Result::Ok(()) + })?; } - let diff_renderer = workspace_command.diff_renderer(vec![DiffFormat::Summary]); - let width = ui.term_width(); - diff_renderer.show_diff( - ui, - formatter, - &parent_tree, - &tree, - &matcher, - ©_records, - width, - )?; } // TODO: Conflicts should also be filtered by the `matcher`. See the related diff --git a/cli/src/config/colors.toml b/cli/src/config/colors.toml index 6586de33a0..557c1d7e64 100644 --- a/cli/src/config/colors.toml +++ b/cli/src/config/colors.toml @@ -93,6 +93,7 @@ "diff added" = { fg = "green" } "diff token" = { underline = true } "diff modified" = "cyan" +"diff untracked" = "magenta" "diff renamed" = "cyan" "diff copied" = "green" "diff access-denied" = { bg = "red" } diff --git a/cli/tests/test_status_command.rs b/cli/tests/test_status_command.rs index 12ade3a04d..60a463cf5b 100644 --- a/cli/tests/test_status_command.rs +++ b/cli/tests/test_status_command.rs @@ -368,3 +368,69 @@ fn test_status_simplify_conflict_sides() { Then run `jj squash` to move the resolution into the conflicted commit. "###); } + +#[test] +fn test_status_untracked_files() { + let test_env = TestEnvironment::default(); + test_env.add_config(r#"snapshot.auto-track = "none()""#); + + test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]); + let repo_path = test_env.env_root().join("repo"); + + std::fs::write(repo_path.join("initially-untracked-file"), "...").unwrap(); + std::fs::write(repo_path.join("always-untracked-file"), "...").unwrap(); + + let stdout = test_env.jj_cmd_success(&repo_path, &["status"]); + insta::assert_snapshot!(stdout, @r" + Untracked paths: + ? always-untracked-file + ? initially-untracked-file + Working copy : qpvuntsm 230dd059 (empty) (no description set) + Parent commit: zzzzzzzz 00000000 (empty) (no description set) + "); + + test_env.jj_cmd_success(&repo_path, &["file", "track", "initially-untracked-file"]); + + let stdout = test_env.jj_cmd_success(&repo_path, &["status"]); + insta::assert_snapshot!(stdout, @r" + Working copy changes: + A initially-untracked-file + Untracked paths: + ? always-untracked-file + Working copy : qpvuntsm 203bfea9 (no description set) + Parent commit: zzzzzzzz 00000000 (empty) (no description set) + "); + + test_env.jj_cmd_ok(&repo_path, &["new"]); + + let stdout = test_env.jj_cmd_success(&repo_path, &["status"]); + insta::assert_snapshot!(stdout, @r" + Untracked paths: + ? always-untracked-file + Working copy : mzvwutvl 69b48d55 (empty) (no description set) + Parent commit: qpvuntsm 203bfea9 (no description set) + "); + + test_env.jj_cmd_success(&repo_path, &["file", "untrack", "initially-untracked-file"]); + let stdout = test_env.jj_cmd_success(&repo_path, &["status"]); + insta::assert_snapshot!(stdout, @r" + Working copy changes: + D initially-untracked-file + Untracked paths: + ? always-untracked-file + ? initially-untracked-file + Working copy : mzvwutvl 16169825 (no description set) + Parent commit: qpvuntsm 203bfea9 (no description set) + "); + + test_env.jj_cmd_ok(&repo_path, &["new"]); + + let stdout = test_env.jj_cmd_success(&repo_path, &["status"]); + insta::assert_snapshot!(stdout, @r" + Untracked paths: + ? always-untracked-file + ? initially-untracked-file + Working copy : yostqsxw 9b87b665 (empty) (no description set) + Parent commit: mzvwutvl 16169825 (no description set) + "); +}