Skip to content

Commit

Permalink
cli: allow resolving shorter ids within a configured revset
Browse files Browse the repository at this point in the history
This adds a config called `revsets.short-prefixes`, which lets the
user specify a revset in which to disambiguate otherwise ambiguous
change/commit ids. It defaults to the value of `revsets.log`.


I made it so you can disable the feature by setting
`revsets.short-prefixes = ""`. I don't like that the default value
(using `revsets.log`) cannot be configured explicitly by the
user. That will be addressed if we decide to merge the `[revsets]` and
`[revset-aliases]` sections some day.
  • Loading branch information
martinvonz committed May 11, 2023
1 parent 0b99b37 commit 1d5984d
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Description tempfiles created via `jj describe` now have the file extension
`.jjdescription` to help external tooling detect a unique filetype.

* The shortest unique change ID prefixes and commit ID prefixes in `jj log` are
now shorter within the default log revset. You can override the default by
setting the `revsets.short-prefixes` config to a different revset.

### Fixed bugs

* Modify/delete conflicts now include context lines
Expand Down
7 changes: 7 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ To customize these separately, use the `format_short_commit_id()` and
'format_short_change_id(id)' = 'format_short_id(id).upper()'
```

To get shorter prefixes for certain revisions, set `revsets.short-prefixes`:

```toml
# Prioritize the current branch
revsets.short-prefixes = "(main..@):"
```

### Relative timestamps

Can be customized by the `format_timestamp()` template alias.
Expand Down
13 changes: 12 additions & 1 deletion src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,17 @@ impl WorkspaceCommandHelper {

pub fn id_prefix_context(&self) -> &IdPrefixContext<'_> {
self.user_repo.id_prefix_context.get_or_init(|| {
let context: IdPrefixContext<'_> = IdPrefixContext::new(self.user_repo.repo.as_ref());
let mut context: IdPrefixContext<'_> = IdPrefixContext::new(self.repo().as_ref());
let revset_string: String = self
.settings
.config()
.get_string("revsets.short-prefixes")
.unwrap_or_else(|_| self.settings.default_revset());
if !revset_string.is_empty() {
let disambiguation_revset = self.parse_revset(&revset_string).unwrap();
context =
context.disambiguate_within(disambiguation_revset, Some(self.workspace_id()));
}
let context: IdPrefixContext<'static> = unsafe { std::mem::transmute(context) };
context
})
Expand Down Expand Up @@ -1288,6 +1298,7 @@ impl WorkspaceCommandTransaction<'_> {
formatter: &mut dyn Formatter,
commit: &Commit,
) -> std::io::Result<()> {
// TODO: Use the disambiguation revset
let id_prefix_context = IdPrefixContext::new(self.tx.repo());
let template = parse_commit_summary_template(
self.tx.repo(),
Expand Down
5 changes: 5 additions & 0 deletions src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@
"type": "string",
"description": "Default set of revisions to show when no explicit revset is given for jj log and similar commands",
"default": "@ | (remote_branches() | tags()).. | ((remote_branches() | tags())..)-"
},
"short-prefixes": {
"type": "string",
"description": "Revisions to give shorter change and commit IDs to",
"default": "<revsets.log>"
}
},
"additionalProperties": {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_commit_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ fn test_log_git_head() {
insta::assert_snapshot!(stdout, @r###"
@ rlvkpnrzqnoo [38;5;[email protected] 2001-02-03 04:05:09.000 +07:00 50aaf4754c1e
│ initial
◉ [1m[38;5;5mq[0m[38;5;8mpvuntsmwlqt[39m [38;5;[email protected][39m [38;5;6m2001-02-03 04:05:07.000 +07:00[39m [38;5;5mmaster[39m [38;5;5mHEAD@git[39m [1m[38;5;4m23[0m[38;5;8m0dd059e1b0[39m
◉ [1m[38;5;5mq[0m[38;5;8mpvuntsmwlqt[39m [38;5;[email protected][39m [38;5;6m2001-02-03 04:05:07.000 +07:00[39m [38;5;5mmaster[39m [38;5;5mHEAD@git[39m [1m[38;5;4m2[0m[38;5;8m30dd059e1b0[39m
│ (empty) (no description set)
◉ zzzzzzzzzzzz 1970-01-01 00:00:00.000 +00:00 000000000000
(empty) (no description set)
Expand Down
50 changes: 43 additions & 7 deletions tests/test_log_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,49 @@ fn test_log_shortest_accessors() {
zn 38
yo 0cf
vr 9e
yq 06f
yq 06
ro 1f
mz 7b
qpv ba1
zzz 00
"###);

insta::assert_snapshot!(
render(":@", r#"format_id(change_id) ++ " " ++ format_id(commit_id) ++ "\n""#),
@r###"
wq[nwkozpkust] 03[f51310b83e]
km[kuslswpqwq] f7[7fb1909080]
kp[qxywonksrl] e7[15ad5db646]
zn[kkpsqqskkl] 38[622e54e2e5]
yo[stqsxwqrlt] 0cf[42f60199c]
vr[uxwmqvtpmx] 9e[6015e4e622]
yq[osqzytrlsw] 06[f34d9b1475]
ro[yxmykxtrkr] 1f[99a5e19891]
mz[vwutvlkqwt] 7b[1f7dee65b4]
qpv[untsmwlqt] ba1[a30916d29]
zzz[zzzzzzzzz] 00[0000000000]
"###);

// Can get shorter prefixes in configured revset
test_env.add_config(r#"revsets.short-prefixes = "(@----):""#);
insta::assert_snapshot!(
render(":@", r#"format_id(change_id) ++ " " ++ format_id(commit_id) ++ "\n""#),
@r###"
w[qnwkozpkust] 03[f51310b83e]
km[kuslswpqwq] f[77fb1909080]
kp[qxywonksrl] e[715ad5db646]
z[nkkpsqqskkl] 3[8622e54e2e5]
y[ostqsxwqrlt] 0c[f42f60199c]
vr[uxwmqvtpmx] 9e[6015e4e622]
yq[osqzytrlsw] 06f[34d9b1475]
ro[yxmykxtrkr] 1f[99a5e19891]
mz[vwutvlkqwt] 7b[1f7dee65b4]
qpv[untsmwlqt] ba1[a30916d29]
zzz[zzzzzzzzz] 00[0000000000]
"###);

// Can disable short prefixes by setting to empty string
test_env.add_config(r#"revsets.short-prefixes = """#);
insta::assert_snapshot!(
render(":@", r#"format_id(change_id) ++ " " ++ format_id(commit_id) ++ "\n""#),
@r###"
Expand Down Expand Up @@ -409,7 +445,7 @@ fn test_log_prefix_highlight_styled() {
◉ Change znkkpsqqskkl commit6 38622e54e2e5
◉ Change yostqsxwqrlt commit5 0cf42f60199c
◉ Change vruxwmqvtpmx commit4 9e6015e4e622
◉ Change [1m[38;5;5myq[0m[38;5;8mosqzytrlsw[39m commit3 [1m[38;5;4m06f[0m[38;5;8m34d9b1475[39m
◉ Change [1m[38;5;5myq[0m[38;5;8mosqzytrlsw[39m commit3 [1m[38;5;4m06[0m[38;5;8mf34d9b1475[39m
◉ Change royxmykxtrkr commit2 1f99a5e19891
◉ Change mzvwutvlkqwt commit1 7b1f7dee65b4
◉ Change qpvuntsmwlqt initial ba1a30916d29 original
Expand All @@ -435,7 +471,7 @@ fn test_log_prefix_highlight_styled() {
◉ Change znk commit6 386
◉ Change yos commit5 0cf
◉ Change vru commit4 9e6
◉ Change [1m[38;5;5myq[0m[38;5;8mo[39m commit3 [1m[38;5;4m06f[0m
◉ Change [1m[38;5;5myq[0m[38;5;8mo[39m commit3 [1m[38;5;4m06[0m[38;5;8mf[39m
◉ Change roy commit2 1f9
◉ Change mzv commit1 7b1
◉ Change qpv initial ba1 original
Expand All @@ -461,7 +497,7 @@ fn test_log_prefix_highlight_styled() {
◉ Change zn commit6 38
◉ Change yo commit5 0cf
◉ Change vr commit4 9e
◉ Change [1m[38;5;5myq[0m commit3 [1m[38;5;4m06f[0m
◉ Change [1m[38;5;5myq[0m commit3 [1m[38;5;4m06[0m
◉ Change ro commit2 1f
◉ Change mz commit1 7b
◉ Change qpv initial ba1 original
Expand Down Expand Up @@ -514,10 +550,10 @@ fn test_log_prefix_highlight_counts_hidden_commits() {
insta::assert_snapshot!(
test_env.jj_cmd_success(&repo_path, &["log", "-T", prefix_format]),
@r###"
@ Change w[qnwkozpkust] 44[4c3c5066d3]
│ ◉ Change q[pvuntsmwlqt] initial ba[1a30916d29] original
@ Change w[qnwkozpkust] 4[44c3c5066d3]
│ ◉ Change q[pvuntsmwlqt] initial b[a1a30916d29] original
├─╯
◉ Change z[zzzzzzzzzzz] 00[0000000000]
◉ Change z[zzzzzzzzzzz] 0[00000000000]
"###
);
insta::assert_snapshot!(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_obslog_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn test_obslog_with_or_without_diff() {
// Color
let stdout = test_env.jj_cmd_success(&repo_path, &["--color=always", "obslog"]);
insta::assert_snapshot!(stdout, @r###"
@ [1m[38;5;13mr[38;5;8mlvkpnrzqnoo[39m [38;5;[email protected][39m [38;5;14m2001-02-03 04:05:10.000 +07:00[39m [38;5;12m66[38;5;8mb42ad36073[39m[0m
@ [1m[38;5;13mr[38;5;8mlvkpnrzqnoo[39m [38;5;[email protected][39m [38;5;14m2001-02-03 04:05:10.000 +07:00[39m [38;5;12m6[38;5;8m6b42ad36073[39m[0m
│ my description
◉ rlvkpnrzqnoo hidden [38;5;[email protected] 2001-02-03 04:05:09.000 +07:00 af536e5af67e conflict
│ my description
Expand Down

0 comments on commit 1d5984d

Please sign in to comment.