Skip to content

Commit 7bde6dd

Browse files
committed
revset: add working_copies() function
It includes the working copy commit of every workspace of the repo. Implements jj-vcs#3384
1 parent ebe8e6a commit 7bde6dd

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6363
* `jj split` now supports a `--siblings/-s` option that splits the target
6464
revision into siblings with the same parents and children.
6565

66+
* new function `working_copies()` for revsets to show the working copy commits of all workspaces.
67+
6668
### Fixed bugs
6769

6870
## [0.15.1] - 2024-03-06

docs/revsets.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ given [string pattern](#string-patterns).
171171
* `present(x)`: Same as `x`, but evaluated to `none()` if any of the commits
172172
in `x` doesn't exist (e.g. is an unknown branch name.)
173173

174+
* `working_copies()`: The working copy commits across all the workspaces.
175+
174176
## String patterns
175177

176178
Functions that perform string matching support the following pattern syntax:

lib/src/revset.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ pub const GENERATION_RANGE_EMPTY: Range<u64> = 0..0;
275275
#[derive(Clone, Debug, Eq, PartialEq)]
276276
pub enum RevsetCommitRef {
277277
WorkingCopy(WorkspaceId),
278+
WorkingCopies,
278279
Symbol(String),
279280
RemoteSymbol {
280281
name: String,
@@ -371,6 +372,10 @@ impl RevsetExpression {
371372
)))
372373
}
373374

375+
pub fn working_copies() -> Rc<RevsetExpression> {
376+
Rc::new(RevsetExpression::CommitRef(RevsetCommitRef::WorkingCopies))
377+
}
378+
374379
pub fn symbol(value: String) -> Rc<RevsetExpression> {
375380
Rc::new(RevsetExpression::CommitRef(RevsetCommitRef::Symbol(value)))
376381
}
@@ -1157,6 +1162,10 @@ static BUILTIN_FUNCTION_MAP: Lazy<HashMap<&'static str, RevsetFunction>> = Lazy:
11571162
expect_no_arguments(name, arguments_pair)?;
11581163
Ok(RevsetExpression::all())
11591164
});
1165+
map.insert("working_copies", |name, arguments_pair, _state| {
1166+
expect_no_arguments(name, arguments_pair)?;
1167+
Ok(RevsetExpression::working_copies())
1168+
});
11601169
map.insert("heads", |name, arguments_pair, state| {
11611170
let arg = expect_one_argument(name, arguments_pair)?;
11621171
let candidates = parse_expression_rule(arg.into_inner(), state)?;
@@ -2139,6 +2148,10 @@ fn resolve_commit_ref(
21392148
})
21402149
}
21412150
}
2151+
RevsetCommitRef::WorkingCopies => {
2152+
let wc_commits = repo.view().wc_commit_ids().values().cloned().collect_vec();
2153+
Ok(wc_commits)
2154+
}
21422155
RevsetCommitRef::VisibleHeads => Ok(repo.view().heads().iter().cloned().collect_vec()),
21432156
RevsetCommitRef::Root => Ok(vec![repo.store().root_commit_id().clone()]),
21442157
RevsetCommitRef::Branches(pattern) => {

lib/tests/test_revset.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,41 @@ fn test_resolve_working_copy() {
371371
assert_eq!(resolve(ws2), vec![commit2.id().clone()]);
372372
}
373373

374+
#[test]
375+
fn test_resolve_working_copies() {
376+
let settings = testutils::user_settings();
377+
let test_repo = TestRepo::init();
378+
let repo = &test_repo.repo;
379+
380+
let mut tx = repo.start_transaction(&settings);
381+
let mut_repo = tx.mut_repo();
382+
383+
let commit1 = write_random_commit(mut_repo, &settings);
384+
let commit2 = write_random_commit(mut_repo, &settings);
385+
386+
// Add some workspaces
387+
let ws1 = WorkspaceId::new("ws1".to_string());
388+
let ws2 = WorkspaceId::new("ws2".to_string());
389+
390+
// add one commit to each working copy
391+
mut_repo
392+
.set_wc_commit(ws1.clone(), commit1.id().clone())
393+
.unwrap();
394+
mut_repo
395+
.set_wc_commit(ws2.clone(), commit2.id().clone())
396+
.unwrap();
397+
let resolve = || -> Vec<CommitId> {
398+
RevsetExpression::working_copies()
399+
.evaluate_programmatic(mut_repo)
400+
.unwrap()
401+
.iter()
402+
.collect()
403+
};
404+
405+
// ensure our output has those two commits
406+
assert_eq!(resolve(), vec![commit2.id().clone(), commit1.id().clone()]);
407+
}
408+
374409
#[test]
375410
fn test_resolve_symbol_branches() {
376411
let settings = testutils::user_settings();

0 commit comments

Comments
 (0)