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

lib: Add a to_wc_name() function for MergedTreeId. #2679

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 28 additions & 0 deletions lib/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::result::Result;
use std::vec::Vec;

use async_trait::async_trait;
use itertools::Itertools;
use thiserror::Error;

use crate::content_hash::ContentHash;
Expand Down Expand Up @@ -201,6 +202,33 @@ impl MergedTreeId {
MergedTreeId::Merge(tree_ids) => tree_ids.clone(),
}
}

/// Represent a `MergeTreeId` in a way that it may be used as a working-copy
/// name. This makes no stability guarantee, as the format may change at
/// any time.
pub fn to_wc_name(&self) -> String {
match self {
MergedTreeId::Legacy(tree_id) => tree_id.hex(),
MergedTreeId::Merge(tree_ids) => {
let ids = tree_ids
.map(|id| id.hex())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

match on self.to_merge().as_resolved() instead?

MergedTreeId::Merge(_) doesn't mean the tree has conflicts, and is equivalent to Legacy(tree_id) if tree_ids.len() == 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

match on self.to_merge().as_resolved() instead?

Why does the tree have to be resolved for a working-copy name?

MergedTreeId::Merge(_) doesn't mean the tree has conflicts, and is equivalent to Legacy(tree_id) if tree_ids.len() == 1.

Makes sense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

match on self.to_merge().as_resolved() instead?

Why does the tree have to be resolved for a working-copy name?

It doesn't have to be. I just thought you would want just {id} (without + prefix) in that case.

.iter_mut()
.enumerate()
.map(|(i, s)| {
// Incredibly "smart" way to say, append "-" if the number is odd "+"
// otherwise.
if i & 1 != 0 {
s.push('-');
} else {
s.push('+');
}
s.to_owned()
})
.collect_vec();
ids.concat()
}
}
}
}

content_hash! {
Expand Down