Skip to content

Commit

Permalink
lib: Add a to_wc_name() function for MergedTreeId.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
PhilipMetzger committed Dec 9, 2023
1 parent a110ec6 commit e689991
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 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,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! {
Expand Down

0 comments on commit e689991

Please sign in to comment.