Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

templater: Add operation.snapshot() boolean and default template hook #3460

Merged
merged 3 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Breaking changes

* The default template alias `builtin_op_log_root(op_id: OperationId)` was replaced by `format_root_operation(root: Operation)`.

* The default template alias `builtin_log_root(change_id: ChangeId, commit_id: CommitId)` was replaced by `format_root_commit(root: Commit)`.

### New features

* The list of conflicted paths is printed whenever the working copy changes.
Expand All @@ -30,6 +34,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* File path arguments now support [file pattern
syntax](docs/filesets.md#file-patterns).

* Operation objects in templates now have a `snapshot() -> Boolean` method that
evaluates to true if the operation was a snapshot created by a non-mutating
command (e.g. `jj log`).

### Fixed bugs

* Revsets now support `\`-escapes in string literal.
Expand Down Expand Up @@ -231,7 +239,7 @@ No code changes (fixing Rust `Cargo.toml` stuff).
When symlink support is unavailable, they will be materialized as regular files in the
working copy (instead of resulting in a crash).
[#2](https://github.com/martinvonz/jj/issues/2)

* On Windows, the `:builtin` pager is now used by default, rather than being
disabled entirely.

Expand Down Expand Up @@ -278,7 +286,7 @@ Thanks to the people who made this release happen!
copy commit on top of a single specified revision, i.e. with one parent.
`merge` creates a new working copy commit on top of *at least* two specified
revisions, i.e. with two or more parents.

The only difference between these commands and `jj new`, which *also* creates
a new working copy commit, is that `new` can create a working copy commit on
top of any arbitrary number of revisions, so it can handle both the previous
Expand Down Expand Up @@ -435,7 +443,7 @@ Thanks to the people who made this release happen!

* `jj branch set` no longer creates a new branch. Use `jj branch create`
instead.

* `jj init --git` in an existing Git repository now errors and exits rather than
creating a second Git store.

Expand Down Expand Up @@ -599,8 +607,8 @@ Thanks to the people who made this release happen!

### New features

* The `ancestors()` revset function now takes an optional `depth` argument
to limit the depth of the ancestor set. For example, use `jj log -r
* The `ancestors()` revset function now takes an optional `depth` argument
to limit the depth of the ancestor set. For example, use `jj log -r
'ancestors(@, 5)` to view the last 5 commits.

* Support for the Watchman filesystem monitor is now bundled by default. Set
Expand Down Expand Up @@ -765,13 +773,13 @@ Thanks to the people who made this release happen!
respectively.

* `jj log` timestamp format now accepts `.utc()` to convert a timestamp to UTC.

* templates now support additional string methods `.starts_with(x)`, `.ends_with(x)`
`.remove_prefix(x)`, `.remove_suffix(x)`, and `.substr(start, end)`.

* `jj next` and `jj prev` are added, these allow you to traverse the history
in a linear style. For people coming from Sapling and `git-branchles`
see [#2126](https://github.com/martinvonz/jj/issues/2126) for
see [#2126](https://github.com/martinvonz/jj/issues/2126) for
further pending improvements.

* `jj diff --stat` has been implemented. It shows a histogram of the changes,
Expand Down
60 changes: 28 additions & 32 deletions cli/src/config/templates.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ show = 'builtin_log_detailed'
[template-aliases]
builtin_log_oneline = '''
if(root,
builtin_log_root(change_id, commit_id),
format_root_commit(self),
label(if(current_working_copy, "working_copy"),
concat(
separate(" ",
Expand All @@ -64,7 +64,7 @@ if(root,
'''
builtin_log_compact = '''
if(root,
builtin_log_root(change_id, commit_id),
format_root_commit(self),
label(if(current_working_copy, "working_copy"),
concat(
separate(" ",
Expand All @@ -87,7 +87,7 @@ if(root,
)
'''
builtin_log_comfortable = 'builtin_log_compact ++ "\n"'
'builtin_log_detailed' = '''
builtin_log_detailed = '''
concat(
"Commit ID: " ++ commit_id ++ "\n",
"Change ID: " ++ change_id ++ "\n",
Expand All @@ -102,39 +102,16 @@ concat(
'''

builtin_op_log_compact = '''
if(root,
builtin_op_log_root(id),
label(if(current_operation, "current_operation"),
necauqua marked this conversation as resolved.
Show resolved Hide resolved
concat(
separate(" ",
id.short(),
user,
format_time_range(time),
) ++ "\n",
description.first_line() ++ "\n",
if(tags, tags ++ "\n"),
),
label(if(current_operation, "current_operation"),
coalesce(
if(snapshot, format_snapshot_operation(self)),
if(root, format_root_operation(self)),
format_operation(self),
)
)
'''
builtin_op_log_comfortable = 'builtin_op_log_compact ++ "\n"'

'builtin_log_root(change_id, commit_id)' = '''
separate(" ",
format_short_change_id(change_id),
label("root", "root()"),
format_short_commit_id(commit_id),
branches
) ++ "\n"
'''

'builtin_op_log_root(op_id)' = '''
separate(" ",
op_id.short(),
label("root", "root()"),
) ++ "\n"
'''


description_placeholder = '''
label(if(empty, "empty ") ++ "description placeholder", "(no description set)")'''
Expand All @@ -156,12 +133,31 @@ commit_summary_separator = 'label("separator", " | ")'
time_range.start().ago() ++ label("time", ", lasted ") ++ time_range.duration()'''
'format_timestamp(timestamp)' = 'timestamp.local().format("%Y-%m-%d %H:%M:%S")'

'format_root_commit(root)' = '''
necauqua marked this conversation as resolved.
Show resolved Hide resolved
separate(" ",
format_short_change_id(root.change_id()),
label("root", "root()"),
format_short_commit_id(root.commit_id()),
root.branches()
) ++ "\n"
'''

'format_operation(op)' = '''
concat(
separate(" ", op.id().short(), op.user(), format_time_range(op.time())), "\n",
op.description().first_line(), "\n",
if(op.tags(), op.tags() ++ "\n"),
)
'''
'format_snapshot_operation(op)' = 'format_operation(op)'
'format_root_operation(root)' = 'separate(" ", root.id().short(), label("root", "root()")) ++ "\n"'

# We have "hidden" override "divergent", since a hidden revision does not cause
# change id conflicts and is not affected by such conflicts; you have to use the
# commit id to refer to a hidden revision regardless.
builtin_change_id_with_hidden_and_divergent_info = '''
if(hidden,
label("hidden",
label("hidden",
format_short_change_id(change_id) ++ " hidden"
),
label(if(divergent, "divergent"),
Expand Down
8 changes: 8 additions & 0 deletions cli/src/operation_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ fn builtin_operation_methods() -> OperationTemplateBuildMethodFnMap<Operation> {
});
Ok(L::wrap_string(out_property))
});
map.insert(
"snapshot",
necauqua marked this conversation as resolved.
Show resolved Hide resolved
|_language, _build_ctx, self_property, function| {
template_parser::expect_no_arguments(function)?;
let out_property = self_property.map(|op| op.metadata().is_snapshot);
Ok(L::wrap_boolean(out_property))
},
);
map.insert("time", |_language, _build_ctx, self_property, function| {
template_parser::expect_no_arguments(function)?;
let out_property = self_property.map(|op| TimestampRange {
Expand Down
3 changes: 2 additions & 1 deletion docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ This type cannot be printed. The following methods are defined.
* `tags() -> String`
* `time() -> TimestampRange`
* `user() -> String`
* `root() -> Boolean`: True if the commit is the root commit.
* `snapshot() -> Boolean`: True if the operation is a snapshot operation.
* `root() -> Boolean`: True if the operation is the root operation.

### OperationId type

Expand Down