From 504a2b3fd04451e5eefbe639ea2aca52904d121c Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 12 Mar 2023 09:34:21 -0700 Subject: [PATCH] cli: add test of `jj debug reindex`, do full reindexing I broke the commands in a27da7d8d56d and thought I just fixed it in c7cf914694a8. However, as I added a test, I realized that I made it only reindex the commits since the previous operation. I meant for the command to do a full reindexing of th repo. This fixes that. --- lib/src/default_index_store.rs | 7 ++-- src/commands/mod.rs | 5 +-- tests/test_debug_command.rs | 75 ++++++++++++++++++++++++++-------- 3 files changed, 65 insertions(+), 22 deletions(-) diff --git a/lib/src/default_index_store.rs b/lib/src/default_index_store.rs index 2b1232262..e40057aa7 100644 --- a/lib/src/default_index_store.rs +++ b/lib/src/default_index_store.rs @@ -66,9 +66,10 @@ impl DefaultIndexStore { } } - pub fn reinit(&self, op_id: &OperationId) { - let op_id_file = self.dir.join("operations").join(op_id.hex()); - std::fs::remove_file(op_id_file).unwrap(); + pub fn reinit(&self) { + let op_dir = self.dir.join("operations"); + std::fs::remove_dir_all(&op_dir).unwrap(); + std::fs::create_dir(op_dir).unwrap(); } fn load_index_at_operation( diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 97f8fe428..30fb2635a 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -3110,9 +3110,8 @@ fn cmd_debug( let default_index_store: Option<&DefaultIndexStore> = repo.index_store().as_any().downcast_ref(); if let Some(default_index_store) = default_index_store { - let op = repo.operation(); - default_index_store.reinit(op.id()); - let repo = repo.reload_at(op); + default_index_store.reinit(); + let repo = repo.reload_at(repo.operation()); writeln!( ui, "Finished indexing {:?} commits.", diff --git a/tests/test_debug_command.rs b/tests/test_debug_command.rs index 2aa785727..4d846686c 100644 --- a/tests/test_debug_command.rs +++ b/tests/test_debug_command.rs @@ -13,6 +13,7 @@ // limitations under the License. use insta::assert_snapshot; +use regex::Regex; use crate::common::TestEnvironment; @@ -44,21 +45,63 @@ fn test_debug_index() { test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); let workspace_path = test_env.env_root().join("repo"); let stdout = test_env.jj_cmd_success(&workspace_path, &["debug", "index"]); - insta::with_settings!( - {filters => vec![ - (r" Name: [0-9a-z]+", " Name: [hash]"), - ]}, - { - assert_snapshot!(stdout, @r###" + assert_snapshot!(filter_index_stats(&stdout), @r###" + Number of commits: 2 + Number of merges: 0 + Max generation number: 1 + Number of heads: 1 + Number of changes: 2 + Stats per level: + Level 0: Number of commits: 2 - Number of merges: 0 - Max generation number: 1 - Number of heads: 1 - Number of changes: 2 - Stats per level: - Level 0: - Number of commits: 2 - Name: [hash] - "###) - }); + Name: [hash] + "### + ); +} + +#[test] +fn test_debug_reindex() { + let test_env = TestEnvironment::default(); + test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); + let workspace_path = test_env.env_root().join("repo"); + test_env.jj_cmd_success(&workspace_path, &["new"]); + test_env.jj_cmd_success(&workspace_path, &["new"]); + let stdout = test_env.jj_cmd_success(&workspace_path, &["debug", "index"]); + assert_snapshot!(filter_index_stats(&stdout), @r###" + Number of commits: 4 + Number of merges: 0 + Max generation number: 3 + Number of heads: 1 + Number of changes: 4 + Stats per level: + Level 0: + Number of commits: 3 + Name: [hash] + Level 1: + Number of commits: 1 + Name: [hash] + "### + ); + let stdout = test_env.jj_cmd_success(&workspace_path, &["debug", "reindex"]); + assert_snapshot!(stdout, @r###" + Finished indexing 4 commits. + "###); + let stdout = test_env.jj_cmd_success(&workspace_path, &["debug", "index"]); + assert_snapshot!(filter_index_stats(&stdout), @r###" + Number of commits: 4 + Number of merges: 0 + Max generation number: 3 + Number of heads: 1 + Number of changes: 4 + Stats per level: + Level 0: + Number of commits: 4 + Name: [hash] + "### + ); +} + +fn filter_index_stats(text: &str) -> String { + let regex = Regex::new(r" Name: [0-9a-z]+").unwrap(); + regex.replace_all(text, " Name: [hash]").to_string() }