2023-03-01 09:18:00 +00:00
|
|
|
# Templates
|
|
|
|
|
|
|
|
Jujutsu supports a functional language to customize output of commands.
|
|
|
|
The language consists of literals, keywords, operators, functions, and
|
|
|
|
methods.
|
|
|
|
|
|
|
|
A couple of `jj` commands accept a template via `-T`/`--template` option.
|
|
|
|
|
|
|
|
## Keywords
|
|
|
|
|
|
|
|
Keywords represent objects of different types; the types are described in
|
2024-02-22 09:57:29 +00:00
|
|
|
a follow-up section. In addition to context-specific keywords, the top-level
|
|
|
|
object can be referenced as `self`.
|
2023-03-01 09:18:00 +00:00
|
|
|
|
|
|
|
### Commit keywords
|
|
|
|
|
2024-02-22 11:35:07 +00:00
|
|
|
In `jj log`/`jj obslog` templates, all 0-argument methods of [the `Commit`
|
2024-02-22 09:57:29 +00:00
|
|
|
type](#commit-type) are available as keywords. For example, `commit_id` is
|
|
|
|
equivalent to `self.commit_id()`.
|
2023-03-01 09:18:00 +00:00
|
|
|
|
|
|
|
### Operation keywords
|
|
|
|
|
2024-02-22 11:35:07 +00:00
|
|
|
In `jj op log` templates, all 0-argument methods of [the `Operation`
|
2024-02-22 09:57:29 +00:00
|
|
|
type](#operation-type) are available as keywords. For example,
|
|
|
|
`current_operation` is equivalent to `self.current_operation()`.
|
2023-03-01 09:18:00 +00:00
|
|
|
|
|
|
|
## Operators
|
|
|
|
|
|
|
|
The following operators are supported.
|
|
|
|
|
|
|
|
* `x.f()`: Method call.
|
2024-02-06 12:57:59 +00:00
|
|
|
* `-x`: Negate integer value.
|
2024-02-05 11:49:06 +00:00
|
|
|
* `!x`: Logical not.
|
2024-03-21 04:14:53 +00:00
|
|
|
* `x && y`: Logical and, short-circuiting.
|
|
|
|
* `x || y`: Logical or, short-circuiting.
|
2023-03-01 09:18:00 +00:00
|
|
|
* `x ++ y`: Concatenate `x` and `y` templates.
|
|
|
|
|
2024-06-06 07:19:26 +00:00
|
|
|
(listed in order of binding strengths)
|
|
|
|
|
2023-03-01 09:18:00 +00:00
|
|
|
## Global functions
|
|
|
|
|
|
|
|
The following functions are defined.
|
|
|
|
|
templater: add fill(width, content) function
The parameter order follows indent()/label() functions, but this might be
a bad idea because fill() is more likely to have optional parameters. We can
instead add template.fill(width) method as well as .indent(prefix). If we take
this approach, we'll probably need to add string.fill()/indent() methods,
and/or implicit cast at method resolution. The good thing about the method
syntax is that we can add string.refill(), etc. for free, without inventing
generic labeled template functions.
For #1043, I think it's better to add a config like ui.log-word-wrap = true.
We could add term_width/graph_width keywords to the templater, but the
implementation would be more complicated, and is difficult to use for the
basic use case. Unlike Mercurial, our templater doesn't have a context map
to override the graph_width stub.
2023-03-04 14:07:14 +00:00
|
|
|
* `fill(width: Integer, content: Template) -> Template`: Fill lines at
|
|
|
|
the given `width`.
|
2023-03-02 08:16:22 +00:00
|
|
|
* `indent(prefix: Template, content: Template) -> Template`: Indent
|
|
|
|
non-empty lines by the given `prefix`.
|
2023-03-01 09:18:00 +00:00
|
|
|
* `label(label: Template, content: Template) -> Template`: Apply label to
|
|
|
|
the content. The `label` is evaluated as a space-separated string.
|
|
|
|
* `if(condition: Boolean, then: Template[, else: Template]) -> Template`:
|
|
|
|
Conditionally evaluate `then`/`else` template content.
|
2024-03-25 09:19:36 +00:00
|
|
|
* `coalesce(content: Template...) -> Template`: Returns the first **non-empty**
|
|
|
|
content.
|
2023-03-01 09:18:00 +00:00
|
|
|
* `concat(content: Template...) -> Template`:
|
|
|
|
Same as `content_1 ++ ... ++ content_n`.
|
|
|
|
* `separate(separator: Template, content: Template...) -> Template`:
|
|
|
|
Insert separator between **non-empty** contents.
|
templater: add surround(prefix, suffix, content) function
I'll probably add infix logical operators later, but the surround() function
is still useful because we don't have to repeat the condition:
if(x || y, "<" ++ separate(" ", x, y) ++ ">")
surround("<", ">", separate(" ", x, y))
It can't be used if we want to add placeholder text, though:
if(x || y, "<" ++ separate(" ", x, y) ++ ">", "(none)")
Closes #2924
2024-02-06 10:28:51 +00:00
|
|
|
* `surround(prefix: Template, suffix: Template, content: Template) -> Template`:
|
|
|
|
Surround **non-empty** content with texts such as parentheses.
|
2023-03-01 09:18:00 +00:00
|
|
|
|
|
|
|
## Types
|
|
|
|
|
|
|
|
### Boolean type
|
|
|
|
|
2023-09-02 08:49:21 +00:00
|
|
|
No methods are defined. Can be constructed with `false` or `true` literal.
|
2023-03-01 09:18:00 +00:00
|
|
|
|
2023-03-19 12:13:47 +00:00
|
|
|
### Commit type
|
|
|
|
|
2024-02-22 11:35:07 +00:00
|
|
|
This type cannot be printed. The following methods are defined.
|
|
|
|
|
|
|
|
* `description() -> String`
|
|
|
|
* `change_id() -> ChangeId`
|
|
|
|
* `commit_id() -> CommitId`
|
|
|
|
* `parents() -> List<Commit>`
|
|
|
|
* `author() -> Signature`
|
|
|
|
* `committer() -> Signature`
|
2024-04-04 15:43:59 +00:00
|
|
|
* `mine() -> Boolean`: Commits where the author's email matches the email of the current
|
|
|
|
user.
|
2024-02-22 11:35:07 +00:00
|
|
|
* `working_copies() -> String`: For multi-workspace repository, indicate
|
|
|
|
working-copy commit as `<workspace name>@`.
|
|
|
|
* `current_working_copy() -> Boolean`: True for the working-copy commit of the
|
|
|
|
current workspace.
|
|
|
|
* `branches() -> List<RefName>`: Local and remote branches pointing to the commit.
|
|
|
|
A tracking remote branch will be included only if its target is different
|
|
|
|
from the local one.
|
|
|
|
* `local_branches() -> List<RefName>`: All local branches pointing to the commit.
|
|
|
|
* `remote_branches() -> List<RefName>`: All remote branches pointing to the commit.
|
|
|
|
* `tags() -> List<RefName>`
|
|
|
|
* `git_refs() -> List<RefName>`
|
2024-03-24 12:57:07 +00:00
|
|
|
* `git_head() -> Option<RefName>`
|
2024-02-22 11:35:07 +00:00
|
|
|
* `divergent() -> Boolean`: True if the commit's change id corresponds to multiple
|
|
|
|
visible commits.
|
|
|
|
* `hidden() -> Boolean`: True if the commit is not visible (a.k.a. abandoned).
|
2024-03-13 07:45:41 +00:00
|
|
|
* `immutable() -> Boolean`: True if the commit is included in [the set of
|
|
|
|
immutable commits](config.md#set-of-immutable-commits).
|
2024-04-26 14:56:40 +00:00
|
|
|
* `contained_in(revset: String) -> Boolean`: True if the commit is included in [the provided revset](revsets.md).
|
2024-02-22 11:35:07 +00:00
|
|
|
* `conflict() -> Boolean`: True if the commit contains merge conflicts.
|
|
|
|
* `empty() -> Boolean`: True if the commit modifies no files.
|
|
|
|
* `root() -> Boolean`: True if the commit is the root commit.
|
2023-03-19 12:13:47 +00:00
|
|
|
|
2023-03-01 09:18:00 +00:00
|
|
|
### CommitId / ChangeId type
|
|
|
|
|
|
|
|
The following methods are defined.
|
|
|
|
|
|
|
|
* `.short([len: Integer]) -> String`
|
|
|
|
* `.shortest([min_len: Integer]) -> ShortestIdPrefix`: Shortest unique prefix.
|
|
|
|
|
|
|
|
### Integer type
|
|
|
|
|
|
|
|
No methods are defined.
|
|
|
|
|
2023-03-06 12:02:57 +00:00
|
|
|
### List type
|
|
|
|
|
2023-10-27 00:50:32 +00:00
|
|
|
A list can be implicitly converted to `Boolean`. The following methods are
|
|
|
|
defined.
|
2023-03-06 13:02:39 +00:00
|
|
|
|
2024-02-29 04:17:04 +00:00
|
|
|
* `.len() -> Integer`: Number of elements in the list.
|
2023-03-06 13:02:39 +00:00
|
|
|
* `.join(separator: Template) -> Template`: Concatenate elements with
|
|
|
|
the given `separator`.
|
2023-03-14 10:57:03 +00:00
|
|
|
* `.map(|item| expression) -> ListTemplate`: Apply template `expression`
|
2023-03-19 12:13:47 +00:00
|
|
|
to each element. Example: `parents.map(|c| c.commit_id().short())`
|
2023-03-06 12:02:57 +00:00
|
|
|
|
2023-03-14 10:57:03 +00:00
|
|
|
### ListTemplate type
|
|
|
|
|
|
|
|
The following methods are defined. See also the `List` type.
|
|
|
|
|
|
|
|
* `.join(separator: Template) -> Template`
|
|
|
|
|
2024-02-22 08:39:43 +00:00
|
|
|
### Operation type
|
|
|
|
|
2024-02-22 11:35:07 +00:00
|
|
|
This type cannot be printed. The following methods are defined.
|
|
|
|
|
|
|
|
* `current_operation() -> Boolean`
|
|
|
|
* `description() -> String`
|
|
|
|
* `id() -> OperationId`
|
|
|
|
* `tags() -> String`
|
|
|
|
* `time() -> TimestampRange`
|
|
|
|
* `user() -> String`
|
2024-04-05 11:21:36 +00:00
|
|
|
* `snapshot() -> Boolean`: True if the operation is a snapshot operation.
|
|
|
|
* `root() -> Boolean`: True if the operation is the root operation.
|
2024-02-22 08:39:43 +00:00
|
|
|
|
2023-03-01 09:18:00 +00:00
|
|
|
### OperationId type
|
|
|
|
|
|
|
|
The following methods are defined.
|
|
|
|
|
|
|
|
* `.short([len: Integer]) -> String`
|
|
|
|
|
2024-03-18 11:52:35 +00:00
|
|
|
### Option type
|
|
|
|
|
|
|
|
An option can be implicitly converted to `Boolean` denoting whether the
|
|
|
|
contained value is set. If set, all methods of the contained value can be
|
|
|
|
invoked. If not set, an error will be reported inline on method call.
|
|
|
|
|
2023-10-25 06:36:22 +00:00
|
|
|
### RefName type
|
|
|
|
|
|
|
|
The following methods are defined.
|
|
|
|
|
|
|
|
* `.name() -> String`: Local branch or tag name.
|
|
|
|
* `.remote() -> String`: Remote name or empty if this is a local ref.
|
2024-04-25 09:05:23 +00:00
|
|
|
* `.present() -> Boolean`: True if the ref points to any commit.
|
|
|
|
* `.conflict() -> Boolean`: True if [the branch or tag is
|
|
|
|
conflicted](branches.md#conflicts).
|
|
|
|
* `.normal_target() -> Option<Commit>`: Target commit if the ref is not
|
|
|
|
conflicted and points to a commit.
|
|
|
|
* `.removed_targets() -> List<Commit>`: Old target commits if conflicted.
|
|
|
|
* `.added_targets() -> List<Commit>`: New target commits. The list usually
|
|
|
|
contains one "normal" target.
|
2024-04-30 04:51:32 +00:00
|
|
|
* `.tracked() -> Boolean`: True if the ref is tracked by a local ref. The local
|
|
|
|
ref might have been deleted (but not pushed yet.)
|
|
|
|
* `.tracking_present() -> Boolean`: True if the ref is tracked by a local ref,
|
|
|
|
and if the local ref points to any commit.
|
|
|
|
* `.tracking_ahead_count() -> SizeHint`: Number of commits ahead of the tracking
|
|
|
|
local ref.
|
|
|
|
* `.tracking_behind_count() -> SizeHint`: Number of commits behind of the
|
|
|
|
tracking local ref.
|
2023-10-25 06:36:22 +00:00
|
|
|
|
2023-03-01 09:18:00 +00:00
|
|
|
### ShortestIdPrefix type
|
|
|
|
|
|
|
|
The following methods are defined.
|
|
|
|
|
|
|
|
* `.prefix() -> String`
|
|
|
|
* `.rest() -> String`
|
|
|
|
* `.upper() -> ShortestIdPrefix`
|
|
|
|
* `.lower() -> ShortestIdPrefix`
|
|
|
|
|
|
|
|
### Signature type
|
|
|
|
|
|
|
|
The following methods are defined.
|
|
|
|
|
|
|
|
* `.name() -> String`
|
|
|
|
* `.email() -> String`
|
|
|
|
* `.username() -> String`
|
|
|
|
* `.timestamp() -> Timestamp`
|
|
|
|
|
templater: add SizeHint type to represent revset.count_estimate() value
We'll probably add binary comparison operators at some point, but this patch
also adds size_hint.zero() method. Otherwise, we'll have to write
"if(x.upper() && x.upper() == 0, ..)" to deal with None.
The resulting "branch list" template will look like:
```
separate(", ",
if(!ref.tracking_ahead_count().zero(),
if(ref.tracking_ahead_count().exact(),
"ahead by " ++ ref.tracking_ahead_count().exact() ++ " commits",
"ahead by at least " ++ ref.tracking_ahead_count().lower() ++ " commits")),
if(!ref.tracking_behind_count().zero(),
if(ref.tracking_behind_count().exact(),
"behind by " ++ ref.tracking_behind_count().exact() ++ " commits",
"behind by at least " ++ ref.tracking_behind_count().lower() ++ " commits")),
)
```
2024-04-30 05:24:08 +00:00
|
|
|
### SizeHint type
|
|
|
|
|
|
|
|
This type cannot be printed. The following methods are defined.
|
|
|
|
|
|
|
|
* `.lower() -> Integer`: Lower bound.
|
|
|
|
* `.upper() -> Option<Integer>`: Upper bound if known.
|
|
|
|
* `.exact() -> Option<Integer>`: Exact value if upper bound is known and it
|
|
|
|
equals to the lower bound.
|
|
|
|
* `.zero() -> Boolean`: True if upper bound is known and is `0`.
|
|
|
|
|
2023-03-01 09:18:00 +00:00
|
|
|
### String type
|
|
|
|
|
|
|
|
A string can be implicitly converted to `Boolean`. The following methods are
|
|
|
|
defined.
|
|
|
|
|
2024-02-29 04:17:04 +00:00
|
|
|
* `.len() -> Integer`: Length in UTF-8 bytes.
|
2023-03-01 09:18:00 +00:00
|
|
|
* `.contains(needle: Template) -> Boolean`
|
|
|
|
* `.first_line() -> String`
|
2023-03-07 09:19:50 +00:00
|
|
|
* `.lines() -> List<String>`: Split into lines excluding newline characters.
|
2023-03-01 09:18:00 +00:00
|
|
|
* `.upper() -> String`
|
|
|
|
* `.lower() -> String`
|
2023-08-22 18:40:51 +00:00
|
|
|
* `.starts_with(needle: Template) -> Boolean`
|
|
|
|
* `.ends_with(needle: Template) -> Boolean`
|
|
|
|
* `.remove_prefix(needle: Template) -> String`: Removes the passed prefix, if present
|
|
|
|
* `.remove_suffix(needle: Template) -> String`: Removes the passed suffix, if present
|
2024-02-29 04:00:59 +00:00
|
|
|
* `.substr(start: Integer, end: Integer) -> String`: Extract substring. The
|
|
|
|
`start`/`end` indices should be specified in UTF-8 bytes. Negative values
|
|
|
|
count from the end of the string.
|
2023-03-01 09:18:00 +00:00
|
|
|
|
2023-09-14 23:43:47 +00:00
|
|
|
#### String literals
|
|
|
|
|
2024-04-17 12:17:40 +00:00
|
|
|
String literals must be surrounded by single or double quotes (`'` or `"`).
|
|
|
|
A double-quoted string literal supports the following escape sequences:
|
|
|
|
|
|
|
|
* `\"`: double quote
|
|
|
|
* `\\`: backslash
|
|
|
|
* `\t`: horizontal tab
|
|
|
|
* `\r`: carriage return
|
|
|
|
* `\n`: new line
|
|
|
|
* `\0`: null
|
|
|
|
|
|
|
|
Other escape sequences are not supported. Any UTF-8 characters are allowed
|
|
|
|
inside a string literal, with two exceptions: unescaped `"`-s and uses of `\`
|
|
|
|
that don't form a valid escape sequence.
|
|
|
|
|
|
|
|
A single-quoted string literal has no escape syntax. `'` can't be expressed
|
|
|
|
inside a single-quoted string literal.
|
2023-09-14 23:43:47 +00:00
|
|
|
|
2023-03-01 09:18:00 +00:00
|
|
|
### Template type
|
|
|
|
|
2023-03-19 11:43:51 +00:00
|
|
|
Most types can be implicitly converted to `Template`. No methods are defined.
|
2023-03-01 09:18:00 +00:00
|
|
|
|
|
|
|
### Timestamp type
|
|
|
|
|
|
|
|
The following methods are defined.
|
|
|
|
|
|
|
|
* `.ago() -> String`: Format as relative timestamp.
|
2023-03-14 12:40:36 +00:00
|
|
|
* `.format(format: String) -> String`: Format with [the specified strftime-like
|
|
|
|
format string](https://docs.rs/chrono/latest/chrono/format/strftime/).
|
2023-08-11 20:31:47 +00:00
|
|
|
* `.utc() -> Timestamp`: Convert timestamp into UTC timezone.
|
2024-02-12 07:09:33 +00:00
|
|
|
* `.local() -> Timestamp`: Convert timestamp into local timezone.
|
2023-03-01 09:18:00 +00:00
|
|
|
|
|
|
|
### TimestampRange type
|
|
|
|
|
|
|
|
The following methods are defined.
|
|
|
|
|
|
|
|
* `.start() -> Timestamp`
|
|
|
|
* `.end() -> Timestamp`
|
|
|
|
* `.duration() -> String`
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
2023-08-13 01:38:57 +00:00
|
|
|
The default templates and aliases() are defined in the `[templates]` and
|
|
|
|
`[template-aliases]` sections of the config respectively. The exact definitions
|
|
|
|
can be seen in the `cli/src/config/templates.toml` file in jj's source tree.
|
|
|
|
|
|
|
|
<!--- TODO: Find a way to embed the default config files in the docs -->
|
2023-03-01 09:18:00 +00:00
|
|
|
|
|
|
|
New keywords and functions can be defined as aliases, by using any
|
|
|
|
combination of the predefined keywords/functions and other aliases.
|
|
|
|
|
2024-06-05 13:29:10 +00:00
|
|
|
Alias functions can be overloaded by the number of parameters. However, builtin
|
|
|
|
function will be shadowed by name, and can't co-exist with aliases.
|
|
|
|
|
2023-03-01 09:18:00 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
```toml
|
|
|
|
[template-aliases]
|
|
|
|
'commit_change_ids' = '''
|
|
|
|
concat(
|
|
|
|
format_field("Commit ID", commit_id),
|
|
|
|
format_field("Change ID", commit_id),
|
|
|
|
)
|
|
|
|
'''
|
|
|
|
'format_field(key, value)' = 'key ++ ": " ++ value ++ "\n"'
|
|
|
|
```
|