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

Code before:
        it was supposed to be in virtualbranches.toml"
        )
    })
}

/// Get a stable `StackId` for the given `name`. It's fetched from `meta`, assuming it's backed by a toml file
/// and assuming that `name` is stored there as applied or unapplied branch.
/// It's `None` if `name` isn't known to the workspace.
fn id_from_name_v2_to_v3_opt(
    name: &gix::refs::FullNameRef,
    meta: &VirtualBranchesTomlMetadata,
) -> anyhow::Result<Option<StackId>> {
    let ref_meta = meta.branch(name)?;
    Ok(ref_meta.stack_id().map(|id| {
        id.to_string()
            .parse()
            .expect("new stack ids are just UUIDs, like the old ones")
    }))
}

/// Get the associated forge review information out of the metadata, if any.
fn review_id_from_meta(
    name: &gix::refs::FullNameRef,
    meta: &VirtualBranchesTomlMetadata,
) -> anyhow::Result<Option<usize>> {
    let ref_meta = meta.branch(name)?;
    Ok(ref_meta.review.pull_request)
}

/// Returns the list of branch information for the branches in a stack.
pub fn stack_heads_info(stack: &Stack, repo: &gix::Repository) -> anyhow::Result<Vec<StackHeadInfo>> {
    let branches = stack
        .branches()
        .into_iter()
        .rev()
        .filter_map(|branch| {
            let tip = branch.head_oid(repo).ok()?;
            Some(StackHeadInfo {
                name: branch.name().to_owned().into(),
                review_id: branch.pr_number,
                tip,
                is_checked_out: false,
            })
        })
        .collect::<Vec<_>>();

    Ok(branches)
}

fn try_from_stack_v3(
    repo: &gix::Repository,

Review comment (suggestion):
Should this use `meta.branch_opt`. just `.branch` will error if there isn't a corresponding `virtual_branches.toml` entry.

Since it seems like the only time we call this function is in the case where we haven't already found the `virtual_branches.toml` entry, it seems quite likely that this will cause unexpected errors.

Code after:
        it was supposed to be in virtualbranches.toml"
        )
    })
}

/// Get a stable `StackId` for the given `name`. It's fetched from `meta`, assuming it's backed by a toml file
/// and assuming that `name` is stored there as applied or unapplied branch.
/// It's `None` if `name` isn't known to the workspace.
fn id_from_name_v2_to_v3_opt(
    name: &gix::refs::FullNameRef,
    meta: &VirtualBranchesTomlMetadata,
) -> anyhow::Result<Option<StackId>> {
    let ref_meta = meta.branch(name)?;
    Ok(ref_meta.stack_id().map(|id| {
        id.to_string()
            .parse()
            .expect("new stack ids are just UUIDs, like the old ones")
    }))
}

/// Get the associated forge review information out of the metadata, if any.
fn review_id_from_meta(
    name: &gix::refs::FullNameRef,
    meta: &VirtualBranchesTomlMetadata,
) -> anyhow::Result<Option<usize>> {
    let pull_request = meta.branch_opt(name)?.and_then(|ref_meta| ref_meta.review.pull_request);
    Ok(pull_request)
}

/// Returns the list of branch information for the branches in a stack.
pub fn stack_heads_info(stack: &Stack, repo: &gix::Repository) -> anyhow::Result<Vec<StackHeadInfo>> {
    let branches = stack
        .branches()
        .into_iter()
        .rev()
        .filter_map(|branch| {
            let tip = branch.head_oid(repo).ok()?;
            Some(StackHeadInfo {
                name: branch.name().to_owned().into(),
                review_id: branch.pr_number,
                tip,
                is_checked_out: false,
            })
        })
        .collect::<Vec<_>>();

    Ok(branches)
}

fn try_from_stack_v3(
    repo: &gix::Repository,