Have jj op log obey ui.relative-timestamps option

This commit is contained in:
Ilya Grigoriev 2023-01-06 19:43:46 -08:00
parent c5563115a4
commit a48b4855be
3 changed files with 40 additions and 6 deletions

View file

@ -36,6 +36,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Per-repository configuration is now read from `.jj/repo/config.toml`.
* The `ui.relative-timestamps` option now also affects `jj op log`.
### Fixed bugs
* When sharing the working copy with a Git repo, we used to forget to export

View file

@ -26,7 +26,7 @@ use clap::builder::NonEmptyStringValueParser;
use clap::{ArgGroup, ArgMatches, CommandFactory, FromArgMatches, Subcommand};
use config::Source;
use itertools::Itertools;
use jujutsu_lib::backend::{CommitId, ObjectId, TreeValue};
use jujutsu_lib::backend::{CommitId, ObjectId, Timestamp, TreeValue};
use jujutsu_lib::commit::Commit;
use jujutsu_lib::dag_walk::topo_order_reverse;
use jujutsu_lib::git::{GitFetchError, GitRefUpdate};
@ -60,7 +60,9 @@ use crate::diff_util::{self, DiffFormat, DiffFormatArgs};
use crate::formatter::{Formatter, PlainTextFormatter};
use crate::graphlog::{AsciiGraphDrawer, Edge};
use crate::progress::Progress;
use crate::template_parser::{format_absolute_timestamp, TemplateParser};
use crate::template_parser::{
format_absolute_timestamp, format_timestamp_relative_to_now, TemplateParser,
};
use crate::templater::Template;
use crate::ui::Ui;
@ -3354,7 +3356,18 @@ fn cmd_op_log(
ui.request_pager();
let mut formatter = ui.stdout_formatter();
let mut formatter = formatter.as_mut();
struct OpTemplate;
struct OpTemplate {
relative_timestamps: bool,
}
impl OpTemplate {
fn format_timestamp(&self, timestamp: &Timestamp) -> String {
if self.relative_timestamps {
format_timestamp_relative_to_now(timestamp)
} else {
format_absolute_timestamp(timestamp)
}
}
}
impl Template<Operation> for OpTemplate {
fn format(&self, op: &Operation, formatter: &mut dyn Formatter) -> io::Result<()> {
// TODO: Make this templated
@ -3368,8 +3381,8 @@ fn cmd_op_log(
formatter.with_label("time", |formatter| {
formatter.write_str(&format!(
"{} - {}",
format_absolute_timestamp(&metadata.start_time),
format_absolute_timestamp(&metadata.end_time)
self.format_timestamp(&metadata.start_time),
self.format_timestamp(&metadata.end_time)
))
})?;
formatter.write_str("\n")?;
@ -3384,7 +3397,9 @@ fn cmd_op_log(
Ok(())
}
}
let template = OpTemplate;
let template = OpTemplate {
relative_timestamps: command.settings().relative_timestamps(),
};
let mut graph = AsciiGraphDrawer::new(&mut formatter);
for op in topo_order_reverse(

View file

@ -14,6 +14,8 @@
use std::path::Path;
use regex::Regex;
use crate::common::TestEnvironment;
pub mod common;
@ -35,6 +37,21 @@ fn test_op_log() {
o 56b94dfc38e7 test-username@host.example.com 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
initialize repo
"###);
// Test op log with relative dates
let stdout = test_env.jj_cmd_success(
&repo_path,
&["op", "log", "--config-toml", "ui.relative-timestamps=true"],
);
let regex = Regex::new(r"\d\d years").unwrap();
insta::assert_snapshot!(regex.replace_all(&stdout, "NN years"), @r###"
@ 45108169c0f8 test-username@host.example.com NN years ago - NN years ago
| describe commit 230dd059e1b059aefc0da06a2e5a7dbf22362f22
| args: jj describe -m 'description 0'
o a99a3fd5c51e test-username@host.example.com NN years ago - NN years ago
| add workspace 'default'
o 56b94dfc38e7 test-username@host.example.com NN years ago - NN years ago
initialize repo
"###);
let add_workspace_id = "a99a3fd5c51e";
let initialize_repo_id = "56b94dfc38e7";