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

revset: add working_copies() function #3418

Merged
merged 1 commit into from
Apr 2, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `jj split` now supports a `--siblings/-s` option that splits the target
revision into siblings with the same parents and children.

* new function `working_copies()` for revsets to show the working copy commits of all workspaces.

### Fixed bugs

## [0.15.1] - 2024-03-06
Expand Down
2 changes: 2 additions & 0 deletions docs/revsets.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ given [string pattern](#string-patterns).
* `present(x)`: Same as `x`, but evaluated to `none()` if any of the commits
in `x` doesn't exist (e.g. is an unknown branch name.)

* `working_copies()`: The working copy commits across all the workspaces.

## String patterns

Functions that perform string matching support the following pattern syntax:
Expand Down
13 changes: 13 additions & 0 deletions lib/src/revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ pub const GENERATION_RANGE_EMPTY: Range<u64> = 0..0;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RevsetCommitRef {
WorkingCopy(WorkspaceId),
WorkingCopies,
Symbol(String),
RemoteSymbol {
name: String,
Expand Down Expand Up @@ -371,6 +372,10 @@ impl RevsetExpression {
)))
}

pub fn working_copies() -> Rc<RevsetExpression> {
Rc::new(RevsetExpression::CommitRef(RevsetCommitRef::WorkingCopies))
}

pub fn symbol(value: String) -> Rc<RevsetExpression> {
Rc::new(RevsetExpression::CommitRef(RevsetCommitRef::Symbol(value)))
}
Expand Down Expand Up @@ -1157,6 +1162,10 @@ static BUILTIN_FUNCTION_MAP: Lazy<HashMap<&'static str, RevsetFunction>> = Lazy:
expect_no_arguments(name, arguments_pair)?;
Ok(RevsetExpression::all())
});
map.insert("working_copies", |name, arguments_pair, _state| {
expect_no_arguments(name, arguments_pair)?;
Ok(RevsetExpression::working_copies())
});
map.insert("heads", |name, arguments_pair, state| {
let arg = expect_one_argument(name, arguments_pair)?;
let candidates = parse_expression_rule(arg.into_inner(), state)?;
Expand Down Expand Up @@ -2139,6 +2148,10 @@ fn resolve_commit_ref(
})
}
}
RevsetCommitRef::WorkingCopies => {
let wc_commits = repo.view().wc_commit_ids().values().cloned().collect_vec();
Ok(wc_commits)
}
RevsetCommitRef::VisibleHeads => Ok(repo.view().heads().iter().cloned().collect_vec()),
RevsetCommitRef::Root => Ok(vec![repo.store().root_commit_id().clone()]),
RevsetCommitRef::Branches(pattern) => {
Expand Down
35 changes: 35 additions & 0 deletions lib/tests/test_revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,41 @@ fn test_resolve_working_copy() {
assert_eq!(resolve(ws2), vec![commit2.id().clone()]);
}

#[test]
fn test_resolve_working_copies() {
let settings = testutils::user_settings();
let test_repo = TestRepo::init();
let repo = &test_repo.repo;

let mut tx = repo.start_transaction(&settings);
let mut_repo = tx.mut_repo();

let commit1 = write_random_commit(mut_repo, &settings);
let commit2 = write_random_commit(mut_repo, &settings);

// Add some workspaces
let ws1 = WorkspaceId::new("ws1".to_string());
let ws2 = WorkspaceId::new("ws2".to_string());

// add one commit to each working copy
mut_repo
.set_wc_commit(ws1.clone(), commit1.id().clone())
.unwrap();
mut_repo
.set_wc_commit(ws2.clone(), commit2.id().clone())
.unwrap();
let resolve = || -> Vec<CommitId> {
RevsetExpression::working_copies()
.evaluate_programmatic(mut_repo)
.unwrap()
.iter()
.collect()
};

// ensure our output has those two commits
assert_eq!(resolve(), vec![commit2.id().clone(), commit1.id().clone()]);
}

#[test]
fn test_resolve_symbol_branches() {
let settings = testutils::user_settings();
Expand Down