al3x
al3x

Reputation: 732

How to build/represent's worktree of git bare repo on memory with libgit2

I'm building a git server written in rust, I'm using git2 library that is a bindings of libgit2 for rust.

Actually i can create bare repository though the API, now i want build the worktree on memory from the bare repo for later serve and render the worktree in the web ui, but i do not understand how to make it.

Git2 provides Repository and Worktree structures for manipulate it, both haves methods for work with worktrees whether to create, operate, or open them, but both requires or exist in the repo (which would no longer be a bare repo), or for create worktrees make it in the repo(which would no longer be a bare repo).

I think that i'm speaking is about "the process for build verify copy (worktree) from git objects", but i dot not understand how this works, in the git pro book in the chapter 10 section 2 the book speaks about how git represents a worktree (the objects tree and blobs), but i do not find how "the process works".

can anyone share a resource on this, or explain me what I'm missing?

Upvotes: 0

Views: 286

Answers (1)

al3x
al3x

Reputation: 732

Thanks for comment of @torek, I just found a way to traverse the file tree, getting the head of the repo as a Reference struct, this struct provides the method peel_to_tree() that returns the underlying tree to the reference as a Tree structure and this provides the walk method, and from here i inspect the tree.

    let repo = Repository::open_bare( Path::new("/some/path.git") ).expect("error open repo");
    
    let head = repo.head().expect("error");

    let tree = head.peel_to_tree();

    match tree {
        Ok(tree) => {
            let mut ct = 0;
            tree.walk( TreeWalkMode::PreOrder, |root, entry| {
                println!("root_path: {}\nName: {}\nKind: {:?}\n\n", root, entry.name().expect("error name"), entry.kind().expect("error kind")  );
                ct +=1;
                TreeWalkResult::Ok
            } ).unwrap();

            println!("All entries: {}",ct);
        },
        Err(e) => println!("{:?}", e),
    }

I'm not entirely sure how good or bad this process is, but I think it's a good starting point for what I want to do.

Upvotes: 0

Related Questions