Skip to content

Commit

Permalink
revset: add author_date and committer_date revset functions
Browse files Browse the repository at this point in the history
Author dates and committer dates can be filtered like so:

    committer_date(before:"1 hour ago") # more than 1 hour ago
    committer_date(after:"1 hour ago")  # 1 hour ago or less

A date range can be created by combining revsets. For example, to see any
revisions committed yesterday:

    committer_date(after:"yesterday") & committer_date(before:"today")
  • Loading branch information
jennings committed Jul 12, 2024
1 parent 5fa1c2f commit 248a9db
Show file tree
Hide file tree
Showing 7 changed files with 351 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
address unconditionally. Only ASCII case folding is currently implemented,
but this will likely change in the future.

* Added revset functions `author_date` and `committer_date`.

### Fixed bugs

## [0.19.0] - 2024-07-03
Expand Down
1 change: 1 addition & 0 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,7 @@ impl WorkspaceCommandHelper {
RevsetParseContext::new(
&self.revset_aliases_map,
self.settings.user_email(),
chrono::Local::now().into(),
&self.revset_extensions,
Some(workspace_context),
)
Expand Down
110 changes: 109 additions & 1 deletion cli/tests/test_revset_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ fn test_function_name_hint() {
| ^-----^
|
= Function "author_" doesn't exist
Hint: Did you mean "author", "my_author"?
Hint: Did you mean "author", "author_date", "my_author"?
"###);

insta::assert_snapshot!(evaluate_err("my_branches"), @r###"
Expand Down Expand Up @@ -594,3 +594,111 @@ fn test_all_modifier() {
For help, see https://github.com/martinvonz/jj/blob/main/docs/config.md.
"###);
}

/// Verifies that the committer_date revset honors the local time zone.
/// This test cannot run on Windows because The TZ env var does not control
/// chrono::Local on that platform.
#[test]
#[cfg(not(target_os = "windows"))]
fn test_revset_committer_date_with_timezone() {
let mut test_env = TestEnvironment::default();
test_env.add_env_var("TZ", "EST+4");
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let repo_path = test_env.env_root().join("repo");

test_env.jj_cmd_ok(
&repo_path,
&[
"--config-toml",
"debug.commit-timestamp='2023-03-25T11:30:00-04:00'",
"describe",
"-m",
"first",
],
);
test_env.jj_cmd_ok(
&repo_path,
&[
"--config-toml",
"debug.commit-timestamp='2023-03-25T12:30:00-04:00'",
"new",
"-m",
"second",
],
);
test_env.jj_cmd_ok(
&repo_path,
&[
"--config-toml",
"debug.commit-timestamp='2023-03-25T13:30:00-04:00'",
"new",
"-m",
"third",
],
);

let after_log = test_env.jj_cmd_success(
&repo_path,
&[
"log",
"--no-graph",
"-T",
"description",
"-r",
"committer_date(after:'2023-03-25 12:00')",
],
);
insta::assert_snapshot!(after_log, @r###"
third
second
"###);

let before_log = test_env.jj_cmd_success(
&repo_path,
&[
"log",
"--no-graph",
"-T",
"description",
"-r",
"committer_date(before:'2023-03-25 12:00')",
],
);
insta::assert_snapshot!(before_log, @r###"
first
"###);

// Change the local time zone and ensure the result changes
test_env.add_env_var("TZ", "CDT+5");

let after_log = test_env.jj_cmd_success(
&repo_path,
&[
"log",
"--no-graph",
"-T",
"description",
"-r",
"committer_date(after:'2023-03-25 12:00')",
],
);
insta::assert_snapshot!(after_log, @r###"
third
"###);

let before_log = test_env.jj_cmd_success(
&repo_path,
&[
"log",
"--no-graph",
"-T",
"description",
"-r",
"committer_date(before:'2023-03-25 12:00')",
],
);
insta::assert_snapshot!(before_log, @r###"
second
first
"###);
}
26 changes: 26 additions & 0 deletions docs/revsets.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ revsets (expressions) as arguments.
* `committer(pattern)`: Commits with the committer's name or email matching the
given [string pattern](#string-patterns).

* `author_date(pattern)`: Commits with author dates matching the specified [date
pattern](#date-patterns).

* `committer_date(pattern)`: Commits with committer dates matching the specified
[date pattern](#date-patterns).

* `empty()`: Commits modifying no files. This also includes `merges()` without
user modifications and `root()`.

Expand Down Expand Up @@ -336,6 +342,26 @@ Functions that perform string matching support the following pattern syntax:
You can append `-i` after the kind to match case‐insensitively (e.g.
`glob-i:"fix*jpeg*"`).

## Date patterns

Functions that perform date matching support the following pattern syntax:

* `after:"string"`: Matches dates exactly at or after the given date.
* `before:"string"`: Matches dates before, but not including, the given date.

Date strings can be specified in several forms, including:

* 2024-02-01
* 2024-02-01T12:00:00
* 2024-02-01T12:00:00-08:00
* 2024-02-01 12:00:00
* 2 days ago
* 5 minutes ago
* yesterday
* yesterday 5pm
* yesterday 10:30
* yesterday 15:30

## Aliases

New symbols and functions can be defined in the config file, by using any
Expand Down
18 changes: 18 additions & 0 deletions lib/src/default_index/revset_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,24 @@ fn build_predicate_fn(
|| pattern.matches(&commit.committer().email)
})
}
RevsetFilterPredicate::AuthorDate(expression) => {
let expression = expression.clone();
box_pure_predicate_fn(move |index, pos| {
let entry = index.entry_by_pos(pos);
let commit = store.get_commit(&entry.commit_id()).unwrap();
let author_date = &commit.author().timestamp;
expression.matches(author_date)
})
}
RevsetFilterPredicate::CommitterDate(expression) => {
let expression = expression.clone();
box_pure_predicate_fn(move |index, pos| {
let entry = index.entry_by_pos(pos);
let commit = store.get_commit(&entry.commit_id()).unwrap();
let committer_date = &commit.committer().timestamp;
expression.matches(committer_date)
})
}
RevsetFilterPredicate::File(expr) => {
let matcher: Rc<dyn Matcher> = expr.to_matcher().into();
box_pure_predicate_fn(move |index, pos| {
Expand Down
44 changes: 44 additions & 0 deletions lib/src/revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub use crate::revset_parser::{
};
use crate::store::Store;
use crate::str_util::StringPattern;
use crate::time_util::{DatePattern, DatePatternContext};
use crate::{dsl_util, revset_parser};

/// Error occurred during symbol resolution.
Expand Down Expand Up @@ -131,6 +132,10 @@ pub enum RevsetFilterPredicate {
Author(StringPattern),
/// Commits with committer name or email matching the pattern.
Committer(StringPattern),
/// Commits with author dates matching the given date pattern.
AuthorDate(DatePattern),
/// Commits with committer dates matching the given date pattern.
CommitterDate(DatePattern),
/// Commits modifying the paths specified by the fileset.
File(FilesetExpression),
/// Commits with conflicts
Expand Down Expand Up @@ -685,6 +690,13 @@ static BUILTIN_FUNCTION_MAP: Lazy<HashMap<&'static str, RevsetFunction>> = Lazy:
pattern,
)))
});
map.insert("author_date", |function, context| {
let [arg] = function.expect_exact_arguments()?;
let pattern = expect_date_pattern(arg, context.date_pattern_context())?;
Ok(RevsetExpression::filter(RevsetFilterPredicate::AuthorDate(
pattern,
)))
});
map.insert("mine", |function, context| {
function.expect_no_arguments()?;
// Email address domains are inherently case‐insensitive, and the local‐parts
Expand All @@ -701,6 +713,13 @@ static BUILTIN_FUNCTION_MAP: Lazy<HashMap<&'static str, RevsetFunction>> = Lazy:
pattern,
)))
});
map.insert("committer_date", |function, context| {
let [arg] = function.expect_exact_arguments()?;
let pattern = expect_date_pattern(arg, context.date_pattern_context())?;
Ok(RevsetExpression::filter(
RevsetFilterPredicate::CommitterDate(pattern),
))
});
map.insert("empty", |function, _context| {
function.expect_no_arguments()?;
Ok(RevsetExpression::is_empty())
Expand Down Expand Up @@ -752,6 +771,20 @@ pub fn expect_string_pattern(node: &ExpressionNode) -> Result<StringPattern, Rev
revset_parser::expect_pattern_with("string pattern", node, parse_pattern)
}

fn expect_date_pattern(
node: &ExpressionNode,
context: &DatePatternContext,
) -> Result<DatePattern, RevsetParseError> {
let parse_pattern =
|value: &str, kind: Option<&str>| -> Result<_, Box<dyn std::error::Error + Send + Sync>> {
match kind {
None => Err("Date pattern must specify 'after' or 'before'".into()),
Some(kind) => Ok(context.parse_relative(value, kind)?),
}
};
revset_parser::expect_pattern_with("date pattern", node, parse_pattern)
}

/// Resolves function call by using the given function map.
fn lower_function_call(
function: &FunctionCallNode,
Expand Down Expand Up @@ -1980,6 +2013,8 @@ impl RevsetExtensions {
pub struct RevsetParseContext<'a> {
aliases_map: &'a RevsetAliasesMap,
user_email: String,
/// The current local time when the revset expression was written.
date_pattern_context: DatePatternContext,
extensions: &'a RevsetExtensions,
workspace: Option<RevsetWorkspaceContext<'a>>,
}
Expand All @@ -1988,12 +2023,14 @@ impl<'a> RevsetParseContext<'a> {
pub fn new(
aliases_map: &'a RevsetAliasesMap,
user_email: String,
date_pattern_context: DatePatternContext,
extensions: &'a RevsetExtensions,
workspace: Option<RevsetWorkspaceContext<'a>>,
) -> Self {
Self {
aliases_map,
user_email,
date_pattern_context,
extensions,
workspace,
}
Expand All @@ -2007,6 +2044,10 @@ impl<'a> RevsetParseContext<'a> {
&self.user_email
}

pub fn date_pattern_context(&self) -> &DatePatternContext {
&self.date_pattern_context
}

pub fn symbol_resolvers(&self) -> &[impl AsRef<dyn SymbolResolverExtension>] {
self.extensions.symbol_resolvers()
}
Expand Down Expand Up @@ -2050,6 +2091,7 @@ mod tests {
let context = RevsetParseContext::new(
&aliases_map,
"[email protected]".to_string(),
chrono::Utc::now().into(),
&extensions,
None,
);
Expand Down Expand Up @@ -2078,6 +2120,7 @@ mod tests {
let context = RevsetParseContext::new(
&aliases_map,
"[email protected]".to_string(),
chrono::Utc::now().into(),
&extensions,
Some(workspace_ctx),
);
Expand All @@ -2102,6 +2145,7 @@ mod tests {
let context = RevsetParseContext::new(
&aliases_map,
"[email protected]".to_string(),
chrono::Utc::now().into(),
&extensions,
None,
);
Expand Down
Loading

0 comments on commit 248a9db

Please sign in to comment.