Update libs
This commit is contained in:
8
Cargo.lock
generated
8
Cargo.lock
generated
@@ -137,8 +137,8 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"colored",
|
"colored",
|
||||||
"git",
|
|
||||||
"indicatif",
|
"indicatif",
|
||||||
|
"thiserror",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -161,8 +161,8 @@ dependencies = [
|
|||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
"colored",
|
"colored",
|
||||||
"command-with-spinner",
|
|
||||||
"dirs",
|
"dirs",
|
||||||
|
"git",
|
||||||
"indicatif",
|
"indicatif",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
]
|
]
|
||||||
@@ -209,6 +209,8 @@ dependencies = [
|
|||||||
name = "git"
|
name = "git"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"command-with-spinner",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -333,7 +335,7 @@ dependencies = [
|
|||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
"colored",
|
"colored",
|
||||||
"command-with-spinner",
|
"git",
|
||||||
"indicatif",
|
"indicatif",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "command-with-spinner"
|
name = "command-with-spinner"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
license-file.workspace = true
|
license-file.workspace = true
|
||||||
@@ -9,5 +10,5 @@ repository.workspace = true
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.98"
|
anyhow = "1.0.98"
|
||||||
colored = "3.0.0"
|
colored = "3.0.0"
|
||||||
git = { version = "0.1.0", path = "../git" }
|
|
||||||
indicatif = "0.17.11"
|
indicatif = "0.17.11"
|
||||||
|
thiserror = "2.0"
|
||||||
|
12
command-with-spinner/src/error.rs
Normal file
12
command-with-spinner/src/error.rs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
/// Git operations error type
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
pub enum CommandError {
|
||||||
|
#[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),
|
||||||
|
}
|
@@ -4,7 +4,8 @@ use anyhow::{Context, Result};
|
|||||||
use colored::*;
|
use colored::*;
|
||||||
use indicatif::{ProgressBar, ProgressStyle};
|
use indicatif::{ProgressBar, ProgressStyle};
|
||||||
|
|
||||||
use git::error::GitError;
|
mod error;
|
||||||
|
use error::CommandError;
|
||||||
|
|
||||||
/// Runs a git command with a progress spinner
|
/// Runs a git command with a progress spinner
|
||||||
pub fn run_command(command: &mut Command, message: &str) -> Result<()> {
|
pub fn run_command(command: &mut Command, message: &str) -> Result<()> {
|
||||||
@@ -34,8 +35,8 @@ pub fn run_command(command: &mut Command, message: &str) -> Result<()> {
|
|||||||
|
|
||||||
let code = status.code();
|
let code = status.code();
|
||||||
match code {
|
match code {
|
||||||
Some(code) => Err(GitError::Failed(code).into()),
|
Some(code) => Err(CommandError::Failed(code).into()),
|
||||||
None => Err(GitError::FailedNoCode.into()),
|
None => Err(CommandError::FailedNoCode.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -9,10 +9,10 @@ license-file.workspace = true
|
|||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.5", features = ["derive"] }
|
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
|
clap = { version = "4.5", features = ["derive"] }
|
||||||
colored = "3.0"
|
colored = "3.0"
|
||||||
indicatif = "0.17"
|
|
||||||
dirs = "6.0"
|
dirs = "6.0"
|
||||||
|
git = { version = "0.1.0", path = "../git" }
|
||||||
|
indicatif = "0.17"
|
||||||
thiserror = { version = "2.0" }
|
thiserror = { version = "2.0" }
|
||||||
command-with-spinner = { version = "0.1.0", path = "../command-with-spinner" }
|
|
||||||
|
@@ -1,12 +1,11 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process::Command;
|
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use clap::{ArgAction, Parser};
|
use clap::{ArgAction, Parser};
|
||||||
use colored::*;
|
use colored::*;
|
||||||
|
|
||||||
use command_with_spinner::run_command;
|
use git::Git;
|
||||||
|
|
||||||
/// Tool to create git worktrees with convenient branch management
|
/// Tool to create git worktrees with convenient branch management
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
@@ -40,86 +39,6 @@ struct Args {
|
|||||||
no_color: bool,
|
no_color: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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
|
/// Update or create the remote tracking branch
|
||||||
fn update_remote(branch: &str, create_upstream: bool) -> Result<()> {
|
fn update_remote(branch: &str, create_upstream: bool) -> Result<()> {
|
||||||
// Do nothing if create_upstream is disabled
|
// Do nothing if create_upstream is disabled
|
||||||
|
@@ -8,4 +8,6 @@ license-file.workspace = true
|
|||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
anyhow = "1.0.98"
|
||||||
|
command-with-spinner = { version = "0.1.0", path = "../command-with-spinner" }
|
||||||
thiserror = "2.0"
|
thiserror = "2.0"
|
||||||
|
171
git/src/lib.rs
171
git/src/lib.rs
@@ -1 +1,170 @@
|
|||||||
pub mod error;
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -12,5 +12,5 @@ repository.workspace = true
|
|||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
clap = { version = "4.5", features = ["derive"] }
|
clap = { version = "4.5", features = ["derive"] }
|
||||||
colored = "3.0"
|
colored = "3.0"
|
||||||
command-with-spinner = { version = "0.1.0", path = "../command-with-spinner" }
|
git = { version = "0.1.0", path = "../git" }
|
||||||
indicatif = "0.17"
|
indicatif = "0.17"
|
||||||
|
@@ -1,11 +1,8 @@
|
|||||||
use std::path::Path;
|
use anyhow::Result;
|
||||||
use std::process::Command;
|
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use colored::*;
|
use colored::*;
|
||||||
|
|
||||||
use command_with_spinner::run_command;
|
use git::Git;
|
||||||
|
|
||||||
/// Tool to set up Git repositories for worktree development
|
/// Tool to set up Git repositories for worktree development
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
@@ -28,66 +25,6 @@ struct Args {
|
|||||||
no_color: bool,
|
no_color: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Git command wrapper
|
|
||||||
struct Git;
|
|
||||||
|
|
||||||
impl Git {
|
|
||||||
/// Clone a repository as a bare clone in a .bare directory
|
|
||||||
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
|
|
||||||
]);
|
|
||||||
run_command(&mut cmd, &format!("Cloning repository as bare clone into {bare_dir_str}"))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set up the .git file to point to the .bare directory
|
|
||||||
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
|
|
||||||
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/*"
|
|
||||||
]);
|
|
||||||
run_command(&mut cmd, "Configuring remote.origin.fetch")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Fetch all remotes
|
|
||||||
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"
|
|
||||||
]);
|
|
||||||
run_command(&mut cmd, "Fetching all remotes")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
// Parse arguments
|
// Parse arguments
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
Reference in New Issue
Block a user