Repository: gitbutlerapp/gitbutler
File: crates/but-meta/src/legacy.rs (Rust)

Code before:
    }
}

fn full_branch_name(name: &str) -> Option<gix::refs::FullName> {
    gix::refs::FullName::try_from(format!("refs/heads/{name}")).ok()
}

/// Make it appear managed, which it is as we created it. Can only make the date up though,
/// which shouldn't matter yet. Let's hope we never use the time while this store is in play.
fn managed_ref_info() -> RefInfo {
    RefInfo {
        created_at: Some(standard_time()),
        updated_at: None,
    }
}

fn branch_to_stack_branch(
    ref_name: &gix::refs::FullNameRef,
    Branch {
        ref_info: _, // TODO: should change parent stack if it's the top.
        review,
    }: &Branch,
    archived: bool,
) -> StackBranch {
    StackBranch::new_with_zero_head(
        ref_name.shorten().to_string(),
        review.pull_request,
        review.review_id.clone(),
        archived,
    )
}

/// Deterministically compare two stacks by their `order` field, using `name` and `id` as a tiebreaker.
fn order_then_name(a: &&Stack, b: &&Stack) -> Ordering {
    a.order
        .cmp(&b.order)
        .then_with(|| a.name.cmp(&b.name).then_with(|| a.id.cmp(&b.id)))
}

/// Copied from `gitbutler-fs` - shouldn't be needed anymore in future.
mod fs {
    use std::{
        fs::File,
        io::{Read, Write},
        path::Path,
    };

    use anyhow::Context as _;
    use gix::tempfile::{AutoRemove, ContainingDirectory};
    use serde::de::DeserializeOwned;


Review comment (suggestion):
@Byron I am not sure why the meta code does this ordering... but the name is a value that is no longer persisted, so we should look into removing that check here. This is the **only** place where the value is being read.

Code after:
    RefInfo {
        created_at: Some(standard_time()),
        updated_at: None,
    }
}

fn branch_to_stack_branch(
    ref_name: &gix::refs::FullNameRef,
    Branch {
        ref_info: _, // TODO: should change parent stack if it's the top.
        review,
    }: &Branch,
    archived: bool,
) -> StackBranch {
    StackBranch::new_with_zero_head(
        ref_name.shorten().to_string(),
        review.pull_request,
        review.review_id.clone(),
        archived,
    )
}

/// Deterministically compare two stacks by their `order` field, using `name` and `id` as a tiebreaker.
fn order_then_name(a: &&Stack, b: &&Stack) -> Ordering {
    #[allow(deprecated)]
    // TODO: Need to remove the name ordering - no such field will be persisted.
    a.order
        .cmp(&b.order)
        .then_with(|| a.name.cmp(&b.name).then_with(|| a.id.cmp(&b.id)))
}

/// Copied from `gitbutler-fs` - shouldn't be needed anymore in future.
mod fs {
    use std::{
        fs::File,
        io::{Read, Write},
        path::Path,
    };

    use anyhow::Context as _;
    use gix::tempfile::{AutoRemove, ContainingDirectory};
    use serde::de::DeserializeOwned;

    /// Write a single file so that the write either fully succeeds, or fully fails,
    /// assuming the containing directory already exists.
    pub fn write<P: AsRef<Path>>(file_path: P, contents: impl AsRef<[u8]>) -> anyhow::Result<()> {
        let mut temp_file = gix::tempfile::new(
            file_path.as_ref().parent().unwrap(),
            ContainingDirectory::Exists,
            AutoRemove::Tempfile,
        )?;