mirror of
https://github.com/martinvonz/jj.git
synced 2024-10-23 06:57:32 +00:00
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:
parent
a110ec6d95
commit
f7c87fdcf8
1 changed files with 28 additions and 0 deletions
|
@ -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! {
|
||||
|
|
Loading…
Reference in a new issue