diff --git a/CHANGELOG.md b/CHANGELOG.md index 8304f8ced..cb64678d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 external program. For configuration, see [the documentation](docs/config.md). [#1886](https://github.com/martinvonz/jj/issues/1886) +* `jj log`/`obslog`/`op log` now supports `--limit N` option to show the first + `N` entries. + ### Fixed bugs * SSH authentication could hang when ssh-agent couldn't be reached diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index 0d22c65c4..88854d2c4 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -368,6 +368,11 @@ struct LogArgs { /// Show revisions in the opposite order (older revisions first) #[arg(long)] reversed: bool, + /// Limit number of revisions to show + /// + /// Applied after revisions are filtered and reordered. + #[arg(long, short)] + limit: Option, /// Don't show the graph, show a flat list of revisions #[arg(long)] no_graph: bool, @@ -390,6 +395,9 @@ struct LogArgs { struct ObslogArgs { #[arg(long, short, default_value = "@")] revision: RevisionArg, + /// Limit number of revisions to show + #[arg(long, short)] + limit: Option, /// Don't show the graph, show a flat list of revisions #[arg(long)] no_graph: bool, @@ -1664,7 +1672,7 @@ fn cmd_log(ui: &mut Ui, command: &CommandHelper, args: &LogArgs) -> Result<(), C } else { Box::new(forward_iter) }; - for (commit_id, edges) in iter { + for (commit_id, edges) in iter.take(args.limit.unwrap_or(usize::MAX)) { let mut graphlog_edges = vec![]; // TODO: Should we update RevsetGraphIterator to yield this flag instead of all // the missing edges since we don't care about where they point here @@ -1727,7 +1735,7 @@ fn cmd_log(ui: &mut Ui, command: &CommandHelper, args: &LogArgs) -> Result<(), C } else { Box::new(revset.iter()) }; - for commit_or_error in iter.commits(store) { + for commit_or_error in iter.commits(store).take(args.limit.unwrap_or(usize::MAX)) { let commit = commit_or_error?; with_content_format .write(formatter, |formatter| template.format(&commit, formatter))?; @@ -1791,11 +1799,14 @@ fn cmd_obslog(ui: &mut Ui, command: &CommandHelper, args: &ObslogArgs) -> Result let formatter = formatter.as_mut(); formatter.push_label("log")?; - let commits = topo_order_reverse( + let mut commits = topo_order_reverse( vec![start_commit], |commit: &Commit| commit.id().clone(), |commit: &Commit| commit.predecessors(), ); + if let Some(n) = args.limit { + commits.truncate(n); + } if !args.no_graph { let mut graph = get_graphlog(command.settings(), formatter.raw()); let default_node_symbol = graph.default_node_symbol().to_owned(); diff --git a/cli/src/commands/operation.rs b/cli/src/commands/operation.rs index fbf96eba9..daf3f78dd 100644 --- a/cli/src/commands/operation.rs +++ b/cli/src/commands/operation.rs @@ -26,6 +26,9 @@ pub enum OperationCommands { /// Show the operation log #[derive(clap::Args, Clone, Debug)] pub struct OperationLogArgs { + /// Limit number of operations to show + #[arg(long, short)] + limit: Option, /// Render each operation using the given template /// /// For the syntax, see https://github.com/martinvonz/jj/blob/main/docs/templates.md @@ -119,7 +122,7 @@ fn cmd_op_log( let formatter = formatter.as_mut(); let mut graph = get_graphlog(command.settings(), formatter.raw()); let default_node_symbol = graph.default_node_symbol().to_owned(); - for op in operation::walk_ancestors(&head_op) { + for op in operation::walk_ancestors(&head_op).take(args.limit.unwrap_or(usize::MAX)) { let mut edges = vec![]; for parent in op.parents() { edges.push(Edge::direct(parent.id().clone())); diff --git a/cli/tests/test_log_command.rs b/cli/tests/test_log_command.rs index 5e5d30991..52cf81186 100644 --- a/cli/tests/test_log_command.rs +++ b/cli/tests/test_log_command.rs @@ -796,6 +796,88 @@ fn test_log_filtered_by_path() { "###); } +#[test] +fn test_log_limit() { + let test_env = TestEnvironment::default(); + test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); + let repo_path = test_env.env_root().join("repo"); + + test_env.jj_cmd_success(&repo_path, &["describe", "-m", "a"]); + std::fs::write(repo_path.join("a"), "").unwrap(); + test_env.jj_cmd_success(&repo_path, &["new", "-m", "b"]); + std::fs::write(repo_path.join("b"), "").unwrap(); + test_env.jj_cmd_success(&repo_path, &["new", "-m", "c", "description(a)"]); + std::fs::write(repo_path.join("c"), "").unwrap(); + test_env.jj_cmd_success( + &repo_path, + &["new", "-m", "d", "description(c)", "description(b)"], + ); + + let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "description", "--limit=3"]); + insta::assert_snapshot!(stdout, @r###" + @ d + ├─╮ + │ ◉ b + ◉ │ c + ├─╯ + "###); + + // Applied on sorted DAG + let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "description", "--limit=2"]); + insta::assert_snapshot!(stdout, @r###" + @ d + ├─╮ + │ ◉ b + "###); + + let stdout = test_env.jj_cmd_success( + &repo_path, + &["log", "-T", "description", "--limit=2", "--no-graph"], + ); + insta::assert_snapshot!(stdout, @r###" + d + c + "###); + + // Applied on reversed DAG + let stdout = test_env.jj_cmd_success( + &repo_path, + &["log", "-T", "description", "--limit=3", "--reversed"], + ); + insta::assert_snapshot!(stdout, @r###" + ◉ + ◉ a + ├─╮ + │ ◉ c + "###); + let stdout = test_env.jj_cmd_success( + &repo_path, + &[ + "log", + "-T", + "description", + "--limit=3", + "--reversed", + "--no-graph", + ], + ); + insta::assert_snapshot!(stdout, @r###" + a + b + "###); + + // Applied on filtered commits + let stdout = test_env.jj_cmd_success( + &repo_path, + &["log", "-T", "description", "--limit=1", "b", "c"], + ); + insta::assert_snapshot!(stdout, @r###" + ◉ c + │ + ~ + "###); +} + #[test] fn test_log_warn_path_might_be_revset() { let test_env = TestEnvironment::default(); diff --git a/cli/tests/test_obslog_command.rs b/cli/tests/test_obslog_command.rs index 5e85c1b1e..e2e5f6d08 100644 --- a/cli/tests/test_obslog_command.rs +++ b/cli/tests/test_obslog_command.rs @@ -80,6 +80,15 @@ fn test_obslog_with_or_without_diff() { (empty) my description "###); + // Test `--limit` + let stdout = test_env.jj_cmd_success(&repo_path, &["obslog", "--limit=2"]); + insta::assert_snapshot!(stdout, @r###" + @ rlvkpnrz test.user@example.com 2001-02-03 04:05:10.000 +07:00 66b42ad3 + │ my description + ◉ rlvkpnrz hidden test.user@example.com 2001-02-03 04:05:09.000 +07:00 af536e5a conflict + │ my description + "###); + // Test `--no-graph` let stdout = test_env.jj_cmd_success(&repo_path, &["obslog", "--no-graph"]); insta::assert_snapshot!(stdout, @r###" diff --git a/cli/tests/test_operations.rs b/cli/tests/test_operations.rs index 91629e9e6..3dd18570b 100644 --- a/cli/tests/test_operations.rs +++ b/cli/tests/test_operations.rs @@ -118,6 +118,18 @@ fn test_op_log() { "###); } +#[test] +fn test_op_log_limit() { + let test_env = TestEnvironment::default(); + test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); + let repo_path = test_env.env_root().join("repo"); + + let stdout = test_env.jj_cmd_success(&repo_path, &["op", "log", "-Tdescription", "--limit=1"]); + insta::assert_snapshot!(stdout, @r###" + @ add workspace 'default' + "###); +} + #[test] fn test_op_log_template() { let test_env = TestEnvironment::default();