Skip to main content

workon/
init.rs

1use std::path::PathBuf;
2
3use git2::Repository;
4use log::debug;
5
6use crate::error::Result;
7use crate::{convert_to_bare, empty_commit};
8
9/// Initialise a new bare repository at `path` in the worktrees layout.
10///
11/// Creates a bare repository, adds an initial empty commit so that HEAD is
12/// valid, then converts to the worktrees layout (see [`convert_to_bare`]).
13pub fn init(path: PathBuf) -> Result<Repository> {
14    debug!("initializing bare repository at {}", path.display());
15
16    let repo = Repository::init(&path)?;
17
18    // 2. Add an initial (empty) commit. We need this to create a valid HEAD.
19    empty_commit(&repo)?;
20
21    // 3. git config core.bare true
22    convert_to_bare(repo)
23}