Skip to main content

workon/
convert_to_bare.rs

1use std::fs::{rename, write};
2
3use git2::Repository;
4use log::debug;
5
6use crate::error::Result;
7use crate::workon_root;
8
9/// Convert a standard git repository into the worktrees layout.
10///
11/// Performs the following steps:
12/// 1. Sets `core.bare = true` in git config
13/// 2. Renames `.git` to `.bare`
14/// 3. Writes a `.git` link file containing `gitdir: ./.bare`
15/// 4. Configures `origin`'s fetch refspec to mirror all branches
16///
17/// Returns the re-opened bare repository.
18pub fn convert_to_bare(mut repo: Repository) -> Result<Repository> {
19    debug!("Converting to bare repository");
20    // git config core.bare true
21    let mut config = repo.config()?;
22    config.set_bool("core.bare", true)?;
23    let root = workon_root(&repo)?;
24    // mv .git .bare
25    rename(repo.path(), root.join(".bare"))?;
26    // create a git-link file: `echo "gitdir: ./.bare" > .git`
27    write(root.join(".git"), "gitdir: ./.bare")?;
28
29    repo = Repository::open(root.join(".bare"))?;
30    repo.remote_add_fetch("origin", "+refs/heads/*:refs/remotes/origin/*")?;
31
32    Ok(repo)
33}