Create libs for common code

Update libs
This commit is contained in:
Noah Knegt
2025-05-08 14:09:29 +02:00
parent 5d372f87cb
commit 69f94c64b6
14 changed files with 314 additions and 247 deletions

View File

@@ -1,11 +1,11 @@
use std::env;
use std::path::Path;
use std::process::{Command, Stdio};
use anyhow::{Context, Result};
use clap::{ArgAction, Parser};
use colored::*;
use indicatif::{ProgressBar, ProgressStyle};
use git::Git;
/// Tool to create git worktrees with convenient branch management
#[derive(Parser, Debug)]
@@ -39,133 +39,6 @@ struct Args {
no_color: bool,
}
/// Git operations error type
#[derive(Debug, thiserror::Error)]
enum GitError {
#[error("Command failed with exit code: {0}")]
Failed(i32),
#[error("Command failed without exit code")]
FailedNoCode,
#[error("Failed to execute command: {0}")]
ExecutionError(#[from] std::io::Error),
}
/// Runs a git command with a progress spinner
fn run_command(command: &mut Command, message: &str) -> Result<()> {
let spinner = ProgressBar::new_spinner();
spinner.set_style(
ProgressStyle::default_spinner()
.tick_chars("⣾⣽⣻⢿⡿⣟⣯⣷")
.template("{spinner:.green} {msg}")
.expect("Invalid template format"),
);
spinner.set_message(message.to_string());
// Configure the command to not show output
command.stdout(Stdio::null()).stderr(Stdio::null());
// Execute the command and wait for it to complete
spinner.enable_steady_tick(std::time::Duration::from_millis(100));
let status = command.status().context("Failed to execute command")?;
spinner.finish_and_clear();
if status.success() {
println!("{message} {}", "Done.".green());
Ok(())
} else {
println!("{message} {}", "FAILED.".red());
let code = status.code();
match code {
Some(code) => Err(GitError::Failed(code).into()),
None => Err(GitError::FailedNoCode.into()),
}
}
}
/// Git command wrapper
struct Git;
impl Git {
/// Check if a branch exists locally
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
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
fn create_worktree_existing_branch(worktree_path: &str, branch: &str) -> Result<()> {
let mut cmd = Command::new("git");
cmd.args([
"worktree",
"add",
worktree_path,
branch
]);
run_command(&mut cmd, &format!("Generating new worktree from existing branch: {branch}"))
}
/// Create a new worktree with a new branch
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
]);
run_command(&mut cmd, &format!("Generating new worktree: {worktree_path}"))
}
/// Create and push a new remote branch
fn create_remote_branch(branch: &str) -> Result<()> {
let mut cmd = Command::new("git");
cmd.args([
"push",
"-u", "origin",
branch
]);
run_command(&mut cmd, &format!("Creating remote branch {branch}..."))
}
/// Set the upstream branch
fn set_upstream_branch(branch: &str) -> Result<()> {
let mut cmd = Command::new("git");
cmd.args([
"branch",
"--set-upstream-to", &format!("origin/{branch}")
]);
run_command(&mut cmd, &format!("Setting upstream branch to 'origin/{branch}'"))
}
}
/// Update or create the remote tracking branch
fn update_remote(branch: &str, create_upstream: bool) -> Result<()> {
// Do nothing if create_upstream is disabled