Files
repo-utils/git/src/lib.rs
Noah Knegt 5d745678f3
All checks were successful
Code style / cargo-fmt (pull_request) Successful in 1m20s
Code style / cargo-check (pull_request) Successful in 1m14s
Code style / clippy (pull_request) Successful in 1m13s
chore: Fix formatting
Signed-off-by: Noah Knegt <git@noahknegt.com>
2025-07-03 23:43:13 +02:00

149 lines
4.9 KiB
Rust

use std::{path::Path, process::Command};
use anyhow::{Context, Result};
use command_with_spinner::run_command;
use error::GitError;
mod error;
/// Git command wrapper
pub struct Git;
impl Git {
/// Clone a repository as a bare clone in a .bare directory
pub fn clone_bare_repo(repo_url: &str, target_dir: &str) -> Result<()> {
// Create the base directory first
std::fs::create_dir_all(target_dir).context("Failed to create target directory")?;
// Create the .bare subdirectory path
let bare_dir = Path::new(target_dir).join(".bare");
let bare_dir_str = bare_dir.to_string_lossy();
// Clone the repository as a bare clone into .bare directory
let mut cmd = Command::new("git");
cmd.args(["clone", "--bare", repo_url, &bare_dir_str]);
match run_command(
&mut cmd,
&format!("Cloning repository as bare clone into {bare_dir_str}"),
) {
Ok(_) => Ok(()),
Err(_) => Err(GitError::FailedNoCode.into()),
}
}
/// Set up the .git file to point to the .bare directory
pub fn setup_git_pointer(target_dir: &str) -> Result<()> {
let git_file_path = Path::new(target_dir).join(".git");
std::fs::write(git_file_path, "gitdir: ./.bare")
.context("Failed to create .git file pointing to .bare directory")
}
/// Configure remote.origin.fetch to fetch all references
pub fn configure_remote_fetch(target_dir: &str) -> Result<()> {
let bare_dir = Path::new(target_dir).join(".bare");
let bare_dir_str = bare_dir.to_string_lossy();
let mut cmd = Command::new("git");
cmd.args([
"--git-dir",
&bare_dir_str,
"config",
"remote.origin.fetch",
"+refs/heads/*:refs/remotes/origin/*",
]);
match run_command(&mut cmd, "Configuring remote.origin.fetch") {
Ok(_) => Ok(()),
Err(_) => Err(GitError::FailedNoCode.into()),
}
}
/// Fetch all remotes
pub fn fetch_remotes(target_dir: &str) -> Result<()> {
let bare_dir = Path::new(target_dir).join(".bare");
let bare_dir_str = bare_dir.to_string_lossy();
let mut cmd = Command::new("git");
cmd.args(["--git-dir", &bare_dir_str, "fetch", "--all"]);
match run_command(&mut cmd, "Fetching all remotes") {
Ok(_) => Ok(()),
Err(_) => Err(GitError::FailedNoCode.into()),
}
}
/// Check if a branch exists locally
pub fn branch_exists_locally(branch: &str) -> Result<bool> {
let output = Command::new("git")
.args(["branch", "--list", branch])
.output()
.context("Failed to check branch existence")?;
Ok(!output.stdout.is_empty())
}
/// Check if a branch exists on the remote
pub fn branch_exists_on_remote(branch: &str) -> Result<bool> {
let output = Command::new("git")
.args(["ls-remote", "--heads", "origin", branch])
.output()
.context("Failed to check remote branch existence")?;
Ok(!output.stdout.is_empty())
}
/// Create a new worktree with an existing branch
pub fn create_worktree_existing_branch(worktree_path: &str, branch: &str) -> Result<()> {
let mut cmd = Command::new("git");
cmd.args(["worktree", "add", worktree_path, branch]);
match run_command(
&mut cmd,
&format!("Generating new worktree from existing branch: {branch}"),
) {
Ok(_) => Ok(()),
Err(_) => Err(GitError::FailedNoCode.into()),
}
}
/// Create a new worktree with a new branch
pub fn create_worktree_new_branch(worktree_path: &str, branch: &str, base: &str) -> Result<()> {
let mut cmd = Command::new("git");
cmd.args(["worktree", "add", "-b", branch, worktree_path, base]);
match run_command(
&mut cmd,
&format!("Generating new worktree: {worktree_path}"),
) {
Ok(_) => Ok(()),
Err(_) => Err(GitError::FailedNoCode.into()),
}
}
/// Create and push a new remote branch
pub fn create_remote_branch(branch: &str) -> Result<()> {
let mut cmd = Command::new("git");
cmd.args(["push", "-u", "origin", branch]);
match run_command(&mut cmd, &format!("Creating remote branch {branch}...")) {
Ok(_) => Ok(()),
Err(_) => Err(GitError::FailedNoCode.into()),
}
}
/// Set the upstream branch
pub fn set_upstream_branch(branch: &str) -> Result<()> {
let mut cmd = Command::new("git");
cmd.args(["branch", "--set-upstream-to", &format!("origin/{branch}")]);
match run_command(
&mut cmd,
&format!("Setting upstream branch to 'origin/{branch}'"),
) {
Ok(_) => Ok(()),
Err(_) => Err(GitError::FailedNoCode.into()),
}
}
}