lib: Add a to_wc_name() function for MergedTreeId.

To be used in a follow-up for `jj run`, as we use them for directory names.
Documented as permanently unstable, as the representation will change in the future.
This commit is contained in:
Philip Metzger 2023-11-04 16:32:14 +01:00
parent a110ec6d95
commit f7c87fdcf8

View file

@ -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;
@ -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())
.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! {