From e6899914520c19bcb4cbeb8d3043df8110e656bf Mon Sep 17 00:00:00 2001 From: Philip Metzger Date: Sat, 4 Nov 2023 16:32:14 +0100 Subject: [PATCH] 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. --- lib/src/backend.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/src/backend.rs b/lib/src/backend.rs index f0b2bf8cd4..9224936da8 100644 --- a/lib/src/backend.rs +++ b/lib/src/backend.rs @@ -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,32 @@ 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! {