Skip to content

Commit

Permalink
view: migrate in-memory structure to per-remote branches map
Browse files Browse the repository at this point in the history
There's a subtle behavior change. Unlike the original remove_remote_branch(),
remote_views entry is not discarded when the branches map becomes empty. The
reasoning here is that the remote view can be added/removed when the remote
is added/removed respectively, though that's not implemented yet. Since the
serialized data cannot represent an empty remote, such view may generate
non-unique content hash.
  • Loading branch information
yuja committed Oct 14, 2023
1 parent 3ec3cac commit b7c7b19
Show file tree
Hide file tree
Showing 15 changed files with 192 additions and 276 deletions.
8 changes: 3 additions & 5 deletions cli/src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,15 @@ fn find_globs(
let glob = glob::Pattern::new(glob_str)?;
let names = view
.branches()
.iter()
.filter_map(|(branch_name, branch_target)| {
if glob.matches(branch_name)
&& (allow_deleted || branch_target.local_target.is_present())
{
Some(branch_name)
Some(branch_name.to_owned())
} else {
None
}
})
.cloned()
.collect_vec();
if names.is_empty() {
failed_globs.push(glob);
Expand Down Expand Up @@ -385,10 +383,10 @@ fn cmd_branch_list(
let mut formatter = ui.stdout_formatter();
let formatter = formatter.as_mut();

let branches_to_list = view.branches().iter().filter(|&(name, _)| {
let branches_to_list = view.branches().filter(|&(name, _)| {
branch_names_to_list
.as_ref()
.map_or(true, |branch_names| branch_names.contains(name.as_str()))
.map_or(true, |branch_names| branch_names.contains(name))
});
for (name, branch_target) in branches_to_list {
let found_non_git_remote = {
Expand Down
3 changes: 2 additions & 1 deletion cli/src/commands/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,8 @@ fn cmd_git_push(
// Check if there are conflicts in any commits we're about to push that haven't
// already been pushed.
let mut old_heads = vec![];
for branch_target in repo.view().branches().values() {
// TODO: get list of branches by remote name instead of using .branches()
for (_, branch_target) in repo.view().branches() {
if let Some(old_head) = branch_target.remote_targets.get(&remote) {
old_heads.extend(old_head.added_ids().cloned());
}
Expand Down
37 changes: 3 additions & 34 deletions cli/src/commands/operation.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::collections::{BTreeMap, BTreeSet};

use clap::Subcommand;
use jj_lib::backend::ObjectId;
use jj_lib::op_store::{BranchTarget, RefTarget};
use jj_lib::operation;
use jj_lib::repo::Repo;

Expand Down Expand Up @@ -179,39 +176,11 @@ fn view_with_desired_portions_restored(
return new_view;
}

let all_branch_names: BTreeSet<_> = itertools::chain(
view_being_restored.branches.keys(),
current_view.branches.keys(),
)
.collect();
let branch_source_view = if what.contains(&UndoWhatToRestore::RemoteTracking) {
view_being_restored
if what.contains(&UndoWhatToRestore::RemoteTracking) {
new_view.remote_views = view_being_restored.remote_views.clone();
} else {
current_view
};
let mut new_branches = BTreeMap::default();
for branch_name in all_branch_names {
let local_target = new_view
.branches
.get(branch_name)
.map(|br| br.local_target.clone())
.unwrap_or_else(RefTarget::absent);
let remote_targets = branch_source_view
.branches
.get(branch_name)
.map(|br| br.remote_targets.clone())
.unwrap_or_default();
if local_target.is_present() || !remote_targets.is_empty() {
new_branches.insert(
branch_name.to_string(),
BranchTarget {
local_target,
remote_targets,
},
);
}
new_view.remote_views = current_view.remote_views.clone();
}
new_view.branches = new_branches;
new_view
}

Expand Down
2 changes: 1 addition & 1 deletion cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ fn build_branches_index(repo: &dyn Repo) -> RefNamesIndex {
} else if unsynced_remote_targets.peek().is_some() {
format!("{branch_name}*")
} else {
branch_name.clone()
branch_name.to_owned()
};
index.insert(local_target.added_ids(), decorated_name);
}
Expand Down
20 changes: 10 additions & 10 deletions cli/tests/test_concurrent_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ fn test_concurrent_operations_auto_rebase() {
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "initial"]);
let stdout = test_env.jj_cmd_success(&repo_path, &["op", "log"]);
insta::assert_snapshot!(stdout, @r###"
@ cfc96ff553b9 [email protected] 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
@ 8626e1f87784 [email protected] 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
│ describe commit 123ed18e4c4c0d77428df41112bc02ffc83fb935
│ args: jj describe -m initial
65a6c90b9544 [email protected] 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
80b17428029f [email protected] 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
│ snapshot working copy
│ args: jj describe -m initial
19b8089fc78b [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
d50e0e495b10 [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
│ add workspace 'default'
f1c462c494be [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
23b83cc0392f [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
initialize repo
"###);
let op_id_hex = stdout[3..15].to_string();
Expand Down Expand Up @@ -168,21 +168,21 @@ fn test_concurrent_snapshot_wc_reloadable() {
let template = r#"id ++ "\n" ++ description ++ "\n" ++ tags"#;
let op_log_stdout = test_env.jj_cmd_success(&repo_path, &["op", "log", "-T", template]);
insta::assert_snapshot!(op_log_stdout, @r###"
@ 9be517934aaabc351597e88ed4119aa9454ae3588ab7f28646a810272c82f3dafb1deb20b3c978dbb58ba9abc8f08fe870fe3c7ce5f682411991e83eee40a77f
@ 98aae8ac6dabe0f4e97b0708afd5848f22f0831d8f5be3169063120efe0769b27d123b038f505f2a78d1cd92a0b8345eee2bd201ba6d687fb8c9326dac1fcfaa
│ commit 323b414dd255b51375d7f4392b7b2641ffe4289f
│ args: jj commit -m 'new child1'
d967c09eb12b38dad2065a0bc9e251824247f9f84ba406a7356f5405e4c93c21562178a3f00cafedfa1df1435ba496265f39da9d1ccebaccb78bdcb4bd7031e1
13ce1be149ff5fe4386896a753f9321e1826cb6603a6310e50eb778aacbaf7080ca50e7eb3c3ddcacb3e433c619e222fe0f7637dec14551350a03b8a6959f739
│ snapshot working copy
│ args: jj commit -m 'new child1'
b6d168ba4fb4534257b6e58d53eb407582567342358eab07cf5a01a7e4d797313b692f27664c2fb7935b2380d398d0298233c9732f821b8c687e35607ea08a55
21126efd140dc442d7663ac963912f664dcde62b397e71bcc1628d914f454c3767a55ae16756a47793303b68099150d4a86344129e32e561f4853f283e745814
│ commit 3d918700494a9895696e955b85fa05eb0d314cc6
│ args: jj commit -m initial
5e9e3f82fc14750ff985c5a39f1935ed8876b973b8800b56bc03d1c9754795e724956d862d1fcb2c533d06ca36abc9fa9f7cb7d3b2b64e993e9a87f80d5af670
f29c2903a0a18deefc8323c6e1ac8c1165c238f74ed508180a7ae77b46ba1c1be0ff13d7112d59368d56a3552564476b8595f7c2b3a6dc26aec1e926d0f280e6
│ snapshot working copy
│ args: jj commit -m initial
19b8089fc78b7c49171f3c8934248be6f89f52311005e961cab5780f9f138b142456d77b27d223d7ee84d21d8c30c4a80100eaf6735b548b1acd0da688f94c80
d50e0e495b10b2575e2a9b914fed7045e427254baec051bc769d22f607f2f55f09788e6f6c9e12a4e56ec994ac7a63decb1fa7e353b2c8fd20ce77760777283d
│ add workspace 'default'
f1c462c494be39f6690928603c5393f908866bc8d81d8cd1ae0bb2ea02cb4f78cafa47165fa5b7cda258e2178f846881de199066991960a80954ba6066ba0821
23b83cc0392f51ef6b548af289e54efbe8661fe88ab5d8c036444d8c4ec798fb9d4d6063f02c4db768b17bde4608bcae2d75ba79da91ddd59dc57a23d45b5c57
initialize repo
"###);
let op_log_lines = op_log_stdout.lines().collect_vec();
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/test_debug_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn test_debug_operation_id() {
let stdout =
test_env.jj_cmd_success(&workspace_path, &["debug", "operation", "--display", "id"]);
assert_snapshot!(filter_index_stats(&stdout), @r###"
19b8089fc78b7c49171f3c8934248be6f89f52311005e961cab5780f9f138b142456d77b27d223d7ee84d21d8c30c4a80100eaf6735b548b1acd0da688f94c80
d50e0e495b10b2575e2a9b914fed7045e427254baec051bc769d22f607f2f55f09788e6f6c9e12a4e56ec994ac7a63decb1fa7e353b2c8fd20ce77760777283d
"###
);
}
Expand Down
44 changes: 22 additions & 22 deletions cli/tests/test_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ fn test_op_log() {
],
);
insta::assert_snapshot!(&stdout, @r###"
@ 98f7262e4a06 [email protected] 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
@ 745ab9998f2f [email protected] 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
│ describe commit 230dd059e1b059aefc0da06a2e5a7dbf22362f22
│ args: jj describe -m 'description 0'
19b8089fc78b [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
d50e0e495b10 [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
│ add workspace 'default'
f1c462c494be [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
23b83cc0392f [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
initialize repo
"###);
let op_log_lines = stdout.lines().collect_vec();
Expand Down Expand Up @@ -139,9 +139,9 @@ fn test_op_log_no_graph() {
let stdout =
test_env.jj_cmd_success(&repo_path, &["op", "log", "--no-graph", "--color=always"]);
insta::assert_snapshot!(stdout, @r###"
[1m[38;5;12m19b8089fc78b[39m [38;5;[email protected][39m [38;5;14m2001-02-03 04:05:07.000 +07:00[39m - [38;5;14m2001-02-03 04:05:07.000 +07:00[39m[0m
[1m[38;5;12md50e0e495b10[39m [38;5;[email protected][39m [38;5;14m2001-02-03 04:05:07.000 +07:00[39m - [38;5;14m2001-02-03 04:05:07.000 +07:00[39m[0m
add workspace 'default'
[38;5;4mf1c462c494be[39m [38;5;[email protected][39m [38;5;6m2001-02-03 04:05:07.000 +07:00[39m - [38;5;6m2001-02-03 04:05:07.000 +07:00[39m
[38;5;4m23b83cc0392f[39m [38;5;[email protected][39m [38;5;6m2001-02-03 04:05:07.000 +07:00[39m - [38;5;6m2001-02-03 04:05:07.000 +07:00[39m
initialize repo
"###);
}
Expand All @@ -164,7 +164,7 @@ fn test_op_log_no_graph_null_terminated() {
r#"id.short(4) ++ "\0""#,
],
);
insta::assert_debug_snapshot!(stdout, @r###""c8b0\07277\019b8\0f1c4\0""###);
insta::assert_debug_snapshot!(stdout, @r###""16e6\0fb05\0d50e\023b8\0""###);
}

#[test]
Expand All @@ -175,14 +175,14 @@ fn test_op_log_template() {
let render = |template| test_env.jj_cmd_success(&repo_path, &["op", "log", "-T", template]);

insta::assert_snapshot!(render(r#"id ++ "\n""#), @r###"
@ 19b8089fc78b7c49171f3c8934248be6f89f52311005e961cab5780f9f138b142456d77b27d223d7ee84d21d8c30c4a80100eaf6735b548b1acd0da688f94c80
f1c462c494be39f6690928603c5393f908866bc8d81d8cd1ae0bb2ea02cb4f78cafa47165fa5b7cda258e2178f846881de199066991960a80954ba6066ba0821
@ d50e0e495b10b2575e2a9b914fed7045e427254baec051bc769d22f607f2f55f09788e6f6c9e12a4e56ec994ac7a63decb1fa7e353b2c8fd20ce77760777283d
23b83cc0392f51ef6b548af289e54efbe8661fe88ab5d8c036444d8c4ec798fb9d4d6063f02c4db768b17bde4608bcae2d75ba79da91ddd59dc57a23d45b5c57
"###);
insta::assert_snapshot!(
render(r#"separate(" ", id.short(5), current_operation, user,
time.start(), time.end(), time.duration()) ++ "\n""#), @r###"
@ 19b80 true [email protected] 2001-02-03 04:05:07.000 +07:00 2001-02-03 04:05:07.000 +07:00 less than a microsecond
f1c46 false [email protected] 2001-02-03 04:05:07.000 +07:00 2001-02-03 04:05:07.000 +07:00 less than a microsecond
@ d50e0 true [email protected] 2001-02-03 04:05:07.000 +07:00 2001-02-03 04:05:07.000 +07:00 less than a microsecond
23b83 false [email protected] 2001-02-03 04:05:07.000 +07:00 2001-02-03 04:05:07.000 +07:00 less than a microsecond
"###);

// Negative length shouldn't cause panic (and is clamped.)
Expand All @@ -204,9 +204,9 @@ fn test_op_log_template() {
let regex = Regex::new(r"\d\d years").unwrap();
let stdout = test_env.jj_cmd_success(&repo_path, &["op", "log"]);
insta::assert_snapshot!(regex.replace_all(&stdout, "NN years"), @r###"
@ 19b8089fc78b [email protected] NN years ago, lasted less than a microsecond
@ d50e0e495b10 [email protected] NN years ago, lasted less than a microsecond
│ add workspace 'default'
f1c462c494be [email protected] NN years ago, lasted less than a microsecond
23b83cc0392f [email protected] NN years ago, lasted less than a microsecond
initialize repo
"###);
}
Expand All @@ -220,24 +220,24 @@ fn test_op_log_builtin_templates() {
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "description 0"]);

insta::assert_snapshot!(render(r#"builtin_op_log_compact"#), @r###"
@ 98f7262e4a06 [email protected] 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
@ 745ab9998f2f [email protected] 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
│ describe commit 230dd059e1b059aefc0da06a2e5a7dbf22362f22
│ args: jj describe -m 'description 0'
19b8089fc78b [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
d50e0e495b10 [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
│ add workspace 'default'
f1c462c494be [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
23b83cc0392f [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
initialize repo
"###);

insta::assert_snapshot!(render(r#"builtin_op_log_comfortable"#), @r###"
@ 98f7262e4a06 [email protected] 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
@ 745ab9998f2f [email protected] 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
│ describe commit 230dd059e1b059aefc0da06a2e5a7dbf22362f22
│ args: jj describe -m 'description 0'
19b8089fc78b [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
d50e0e495b10 [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
│ add workspace 'default'
f1c462c494be [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
23b83cc0392f [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
initialize repo
"###);
Expand All @@ -264,18 +264,18 @@ fn test_op_log_word_wrap() {

// ui.log-word-wrap option works
insta::assert_snapshot!(render(&["op", "log"], 40, false), @r###"
@ 19b8089fc78b [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
@ d50e0e495b10 [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
│ add workspace 'default'
f1c462c494be [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
23b83cc0392f [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
initialize repo
"###);
insta::assert_snapshot!(render(&["op", "log"], 40, true), @r###"
@ 19b8089fc78b
@ d50e0e495b10
[email protected]
│ 2001-02-03 04:05:07.000 +07:00 -
│ 2001-02-03 04:05:07.000 +07:00
│ add workspace 'default'
f1c462c494be
23b83cc0392f
[email protected]
2001-02-03 04:05:07.000 +07:00 -
2001-02-03 04:05:07.000 +07:00
Expand Down
6 changes: 3 additions & 3 deletions cli/tests/test_workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ fn test_workspaces_conflicting_edits() {
"###);
let stderr = test_env.jj_cmd_failure(&secondary_path, &["st"]);
insta::assert_snapshot!(stderr, @r###"
Error: The working copy is stale (not updated since operation 5c95db542ebd).
Error: The working copy is stale (not updated since operation d47ba8bebf5d).
Hint: Run `jj workspace update-stale` to update it.
See https://github.com/martinvonz/jj/blob/main/docs/working-copy.md#stale-working-copy for more information.
"###);
// Same error on second run, and from another command
let stderr = test_env.jj_cmd_failure(&secondary_path, &["log"]);
insta::assert_snapshot!(stderr, @r###"
Error: The working copy is stale (not updated since operation 5c95db542ebd).
Error: The working copy is stale (not updated since operation d47ba8bebf5d).
Hint: Run `jj workspace update-stale` to update it.
See https://github.com/martinvonz/jj/blob/main/docs/working-copy.md#stale-working-copy for more information.
"###);
Expand Down Expand Up @@ -261,7 +261,7 @@ fn test_workspaces_updated_by_other() {
"###);
let stderr = test_env.jj_cmd_failure(&secondary_path, &["st"]);
insta::assert_snapshot!(stderr, @r###"
Error: The working copy is stale (not updated since operation 5c95db542ebd).
Error: The working copy is stale (not updated since operation d47ba8bebf5d).
Hint: Run `jj workspace update-stale` to update it.
See https://github.com/martinvonz/jj/blob/main/docs/working-copy.md#stale-working-copy for more information.
"###);
Expand Down
5 changes: 2 additions & 3 deletions lib/src/op_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ content_hash! {
pub head_ids: HashSet<CommitId>,
/// Heads of the set of public commits.
pub public_head_ids: HashSet<CommitId>,
pub branches: BTreeMap<String, BranchTarget>,
pub local_branches: BTreeMap<String, RefTarget>,
pub tags: BTreeMap<String, RefTarget>,
pub remote_views: BTreeMap<String, RemoteView>,
pub git_refs: BTreeMap<String, RefTarget>,
/// The commit the Git HEAD points to.
// TODO: Support multiple Git worktrees?
Expand All @@ -213,7 +214,6 @@ content_hash! {
}

/// Iterates pair of local and remote branches by branch name.
#[allow(dead_code)] // TODO
pub(crate) fn merge_join_branch_views<'a>(
local_branches: &'a BTreeMap<String, RefTarget>,
remote_views: &'a BTreeMap<String, RemoteView>,
Expand Down Expand Up @@ -251,7 +251,6 @@ pub(crate) fn merge_join_branch_views<'a>(
}

/// Iterates branch `((name, remote_name), target)`s in lexicographical order.
#[allow(dead_code)] // TODO
pub(crate) fn flatten_remote_branches(
remote_views: &BTreeMap<String, RemoteView>,
) -> impl Iterator<Item = ((&str, &str), &RefTarget)> {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn diff_named_refs<'a, 'b, K: Ord>(
/// Iterates `refs1` and `refs2` target pairs by name.
///
/// `refs1` and `refs2` must be sorted by `K`.
fn iter_named_ref_pairs<'a, 'b, K: Ord>(
pub fn iter_named_ref_pairs<'a, 'b, K: Ord>(
refs1: impl IntoIterator<Item = (K, &'a RefTarget)>,
refs2: impl IntoIterator<Item = (K, &'b RefTarget)>,
) -> impl Iterator<Item = (K, (&'a RefTarget, &'b RefTarget))> {
Expand Down
Loading

0 comments on commit b7c7b19

Please sign in to comment.