navid/veins / e362a182625e66b06bd48dcda2992ca49b89e9b8638e55b72b043f1e7ea705b1 / veins-rust/src/store/ops.rs

ops.rs

← veins-rust/src/store · 4577 bytes · raw · download

use std::fs;
use std::path::Path;

use crate::error::{Error, Result};
use crate::object::id::Id;
use crate::object::view::{Bookmark, View, WorkspaceRef};
use crate::store::layout;
use crate::store::reader::Store;

pub fn read_heads(root: &Path) -> Result<Vec<Id>> {
    let directory = layout::op_heads_dir(root);
    let mut heads = Vec::new();
    if let Ok(entries) = fs::read_dir(&directory) {
        for entry in entries.flatten() {
            let name = entry.file_name();
            let text = name.to_string_lossy();
            if text.len() == 64
                && let Ok(id) = Id::from_hex(&text)
            {
                heads.push(id);
            }
        }
    }
    heads.sort_by(|left, right| left.0.cmp(&right.0));
    Ok(heads)
}

pub fn write_head(root: &Path, id: Id) -> Result<()> {
    let path = layout::op_heads_dir(root).join(id.to_hex());
    layout::write_atomic(&path, &[])
}

pub fn delete_head(root: &Path, id: Id) -> Result<()> {
    let path = layout::op_heads_dir(root).join(id.to_hex());
    match fs::remove_file(path) {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(error.into()),
    }
}

pub fn empty_view() -> View {
    View::default()
}

pub fn current_view(root: &Path, store: &mut Store) -> Result<Option<View>> {
    let heads = read_heads(root)?;
    let Some(head) = heads.first() else {
        return Ok(None);
    };
    let operation = store
        .operation(*head)?
        .ok_or_else(|| Error::new("missing operation state"))?;
    store.view(operation.view)
}

pub fn working_copy_id(view: &View) -> Option<Id> {
    view.working_copies
        .iter()
        .find(|workspace| workspace.workspace == b"default")
        .map(|workspace| workspace.target)
}

pub fn sort_ids(ids: &mut [Id]) {
    ids.sort_by(|left, right| left.0.cmp(&right.0));
}

pub fn sort_view(view: &mut View) {
    view.heads.sort_by(|left, right| left.0.cmp(&right.0));
    view.bookmarks
        .sort_by(|left, right| left.name.cmp(&right.name));
    view.tags.sort_by(|left, right| left.name.cmp(&right.name));
    view.remote_refs.sort_by(|left, right| {
        left.remote
            .cmp(&right.remote)
            .then_with(|| left.name.cmp(&right.name))
    });
    view.working_copies
        .sort_by(|left, right| left.workspace.cmp(&right.workspace));
}

pub fn add_unique_head(view: &mut View, id: Id) {
    if !view.heads.contains(&id) {
        view.heads.push(id);
    }
}

pub fn rebuild_heads(view: &mut View) {
    view.heads.clear();
    let mut ids = Vec::new();
    for bookmark in &view.bookmarks {
        for id in bookmark.targets.adds.iter().flatten() {
            ids.push(*id);
        }
    }
    for tag in &view.tags {
        ids.push(tag.target);
    }
    for workspace in &view.working_copies {
        ids.push(workspace.target);
    }
    for id in ids {
        add_unique_head(view, id);
    }
    sort_ids(&mut view.heads);
}

pub fn set_workspace(view: &mut View, id: Id) {
    for workspace in &mut view.working_copies {
        if workspace.workspace == b"default" {
            workspace.target = id;
            return;
        }
    }
    view.working_copies.push(WorkspaceRef {
        workspace: b"default".to_vec(),
        target: id,
    });
    view.working_copies
        .sort_by(|left, right| left.workspace.cmp(&right.workspace));
}

pub fn remove_head(view: &mut View, id: Id) {
    view.heads.retain(|head| *head != id);
}

pub fn move_bookmarks(view: &mut View, old_id: Id, new_id: Id) {
    if old_id == new_id {
        return;
    }
    for bookmark in &mut view.bookmarks {
        if bookmark.targets.adds.contains(&Some(old_id)) {
            bookmark.targets.adds = vec![Some(new_id)];
            bookmark.targets.removes.clear();
        }
    }
}

pub fn find_bookmark(view: &View, name: &[u8]) -> Option<usize> {
    view.bookmarks
        .iter()
        .position(|bookmark| bookmark.name == name)
}

pub fn bookmark_points_at(bookmark: &Bookmark, target: Id) -> bool {
    bookmark.targets.adds.len() == 1 && bookmark.targets.adds[0] == Some(target)
}

pub fn tag_names(view: &View, id: Id) -> Vec<Vec<u8>> {
    view.tags
        .iter()
        .filter(|tag| tag.target == id)
        .map(|tag| tag.name.clone())
        .collect()
}

pub fn bookmark_names(view: &View, id: Id) -> Vec<Vec<u8>> {
    view.bookmarks
        .iter()
        .filter(|bookmark| bookmark.targets.adds.contains(&Some(id)))
        .map(|bookmark| bookmark.name.clone())
        .collect()
}