@@ -51,11 +51,17 @@ fn update_remote(branch: &str, create_upstream: bool) -> Result<()> {
|
|||||||
|
|
||||||
if !has_remote {
|
if !has_remote {
|
||||||
// Create remote branch
|
// Create remote branch
|
||||||
println!("{}", format!("Branch '{branch}' does not exist on remote. Creating.").dimmed());
|
println!(
|
||||||
|
"{}",
|
||||||
|
format!("Branch '{branch}' does not exist on remote. Creating.").dimmed()
|
||||||
|
);
|
||||||
Git::set_upstream_branch(branch)?;
|
Git::set_upstream_branch(branch)?;
|
||||||
// Git::create_remote_branch(branch)?;
|
// Git::create_remote_branch(branch)?;
|
||||||
} else {
|
} else {
|
||||||
println!("{}", format!("Branch '{branch}' exists. Setting upstream.").dimmed());
|
println!(
|
||||||
|
"{}",
|
||||||
|
format!("Branch '{branch}' exists. Setting upstream.").dimmed()
|
||||||
|
);
|
||||||
Git::set_upstream_branch(branch)?;
|
Git::set_upstream_branch(branch)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,11 +79,16 @@ fn main() -> Result<()> {
|
|||||||
if args.verbose {
|
if args.verbose {
|
||||||
println!("{}", "Verbose mode enabled".dimmed());
|
println!("{}", "Verbose mode enabled".dimmed());
|
||||||
println!("{}", format!("Base branch: {}", args.base).dimmed());
|
println!("{}", format!("Base branch: {}", args.base).dimmed());
|
||||||
println!("{}", format!("Create upstream: {}", args.no_create_upstream).dimmed());
|
println!(
|
||||||
|
"{}",
|
||||||
|
format!("Create upstream: {}", args.no_create_upstream).dimmed()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine branch name if not specified
|
// Determine branch name if not specified
|
||||||
let branch = args.branch.unwrap_or_else(|| format!("{}{}", args.prefix, args.worktree));
|
let branch = args
|
||||||
|
.branch
|
||||||
|
.unwrap_or_else(|| format!("{}{}", args.prefix, args.worktree));
|
||||||
|
|
||||||
// Normalize paths
|
// Normalize paths
|
||||||
let worktree_path = Path::new(&args.worktree).to_string_lossy();
|
let worktree_path = Path::new(&args.worktree).to_string_lossy();
|
||||||
@@ -93,7 +104,10 @@ fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Change to worktree directory
|
// Change to worktree directory
|
||||||
println!("{}", format!("Moving into worktree: {worktree_path}").dimmed());
|
println!(
|
||||||
|
"{}",
|
||||||
|
format!("Moving into worktree: {worktree_path}").dimmed()
|
||||||
|
);
|
||||||
env::set_current_dir(&args.worktree).context("Failed to change directory")?;
|
env::set_current_dir(&args.worktree).context("Failed to change directory")?;
|
||||||
|
|
||||||
// Update remote
|
// Update remote
|
||||||
|
@@ -21,16 +21,14 @@ impl Git {
|
|||||||
|
|
||||||
// Clone the repository as a bare clone into .bare directory
|
// Clone the repository as a bare clone into .bare directory
|
||||||
let mut cmd = Command::new("git");
|
let mut cmd = Command::new("git");
|
||||||
cmd.args([
|
cmd.args(["clone", "--bare", repo_url, &bare_dir_str]);
|
||||||
"clone",
|
|
||||||
"--bare",
|
|
||||||
repo_url,
|
|
||||||
&bare_dir_str
|
|
||||||
]);
|
|
||||||
|
|
||||||
match run_command(&mut cmd, &format!("Cloning repository as bare clone into {bare_dir_str}")) {
|
match run_command(
|
||||||
|
&mut cmd,
|
||||||
|
&format!("Cloning repository as bare clone into {bare_dir_str}"),
|
||||||
|
) {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(_) => Err(GitError::FailedNoCode.into())
|
Err(_) => Err(GitError::FailedNoCode.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,14 +46,16 @@ impl Git {
|
|||||||
|
|
||||||
let mut cmd = Command::new("git");
|
let mut cmd = Command::new("git");
|
||||||
cmd.args([
|
cmd.args([
|
||||||
"--git-dir", &bare_dir_str,
|
"--git-dir",
|
||||||
|
&bare_dir_str,
|
||||||
"config",
|
"config",
|
||||||
"remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*"
|
"remote.origin.fetch",
|
||||||
|
"+refs/heads/*:refs/remotes/origin/*",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
match run_command(&mut cmd, "Configuring remote.origin.fetch") {
|
match run_command(&mut cmd, "Configuring remote.origin.fetch") {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(_) => Err(GitError::FailedNoCode.into())
|
Err(_) => Err(GitError::FailedNoCode.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,26 +65,18 @@ impl Git {
|
|||||||
let bare_dir_str = bare_dir.to_string_lossy();
|
let bare_dir_str = bare_dir.to_string_lossy();
|
||||||
|
|
||||||
let mut cmd = Command::new("git");
|
let mut cmd = Command::new("git");
|
||||||
cmd.args([
|
cmd.args(["--git-dir", &bare_dir_str, "fetch", "--all"]);
|
||||||
"--git-dir", &bare_dir_str,
|
|
||||||
"fetch",
|
|
||||||
"--all"
|
|
||||||
]);
|
|
||||||
|
|
||||||
match run_command(&mut cmd, "Fetching all remotes") {
|
match run_command(&mut cmd, "Fetching all remotes") {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(_) => Err(GitError::FailedNoCode.into())
|
Err(_) => Err(GitError::FailedNoCode.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if a branch exists locally
|
/// Check if a branch exists locally
|
||||||
pub fn branch_exists_locally(branch: &str) -> Result<bool> {
|
pub fn branch_exists_locally(branch: &str) -> Result<bool> {
|
||||||
let output = Command::new("git")
|
let output = Command::new("git")
|
||||||
.args([
|
.args(["branch", "--list", branch])
|
||||||
"branch",
|
|
||||||
"--list",
|
|
||||||
branch
|
|
||||||
])
|
|
||||||
.output()
|
.output()
|
||||||
.context("Failed to check branch existence")?;
|
.context("Failed to check branch existence")?;
|
||||||
|
|
||||||
@@ -94,12 +86,7 @@ impl Git {
|
|||||||
/// Check if a branch exists on the remote
|
/// Check if a branch exists on the remote
|
||||||
pub fn branch_exists_on_remote(branch: &str) -> Result<bool> {
|
pub fn branch_exists_on_remote(branch: &str) -> Result<bool> {
|
||||||
let output = Command::new("git")
|
let output = Command::new("git")
|
||||||
.args([
|
.args(["ls-remote", "--heads", "origin", branch])
|
||||||
"ls-remote",
|
|
||||||
"--heads",
|
|
||||||
"origin",
|
|
||||||
branch
|
|
||||||
])
|
|
||||||
.output()
|
.output()
|
||||||
.context("Failed to check remote branch existence")?;
|
.context("Failed to check remote branch existence")?;
|
||||||
|
|
||||||
@@ -109,62 +96,53 @@ impl Git {
|
|||||||
/// Create a new worktree with an existing branch
|
/// Create a new worktree with an existing branch
|
||||||
pub fn create_worktree_existing_branch(worktree_path: &str, branch: &str) -> Result<()> {
|
pub fn create_worktree_existing_branch(worktree_path: &str, branch: &str) -> Result<()> {
|
||||||
let mut cmd = Command::new("git");
|
let mut cmd = Command::new("git");
|
||||||
cmd.args([
|
cmd.args(["worktree", "add", worktree_path, branch]);
|
||||||
"worktree",
|
|
||||||
"add",
|
|
||||||
worktree_path,
|
|
||||||
branch
|
|
||||||
]);
|
|
||||||
|
|
||||||
match run_command(&mut cmd, &format!("Generating new worktree from existing branch: {branch}")) {
|
match run_command(
|
||||||
|
&mut cmd,
|
||||||
|
&format!("Generating new worktree from existing branch: {branch}"),
|
||||||
|
) {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(_) => Err(GitError::FailedNoCode.into())
|
Err(_) => Err(GitError::FailedNoCode.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new worktree with a new branch
|
/// Create a new worktree with a new branch
|
||||||
pub fn create_worktree_new_branch(worktree_path: &str, branch: &str, base: &str) -> Result<()> {
|
pub fn create_worktree_new_branch(worktree_path: &str, branch: &str, base: &str) -> Result<()> {
|
||||||
let mut cmd = Command::new("git");
|
let mut cmd = Command::new("git");
|
||||||
cmd.args([
|
cmd.args(["worktree", "add", "-b", branch, worktree_path, base]);
|
||||||
"worktree",
|
|
||||||
"add",
|
|
||||||
"-b", branch,
|
|
||||||
worktree_path,
|
|
||||||
base
|
|
||||||
]);
|
|
||||||
|
|
||||||
match run_command(&mut cmd, &format!("Generating new worktree: {worktree_path}")) {
|
match run_command(
|
||||||
|
&mut cmd,
|
||||||
|
&format!("Generating new worktree: {worktree_path}"),
|
||||||
|
) {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(_) => Err(GitError::FailedNoCode.into())
|
Err(_) => Err(GitError::FailedNoCode.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create and push a new remote branch
|
/// Create and push a new remote branch
|
||||||
pub fn create_remote_branch(branch: &str) -> Result<()> {
|
pub fn create_remote_branch(branch: &str) -> Result<()> {
|
||||||
let mut cmd = Command::new("git");
|
let mut cmd = Command::new("git");
|
||||||
cmd.args([
|
cmd.args(["push", "-u", "origin", branch]);
|
||||||
"push",
|
|
||||||
"-u", "origin",
|
|
||||||
branch
|
|
||||||
]);
|
|
||||||
|
|
||||||
match run_command(&mut cmd, &format!("Creating remote branch {branch}...")) {
|
match run_command(&mut cmd, &format!("Creating remote branch {branch}...")) {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(_) => Err(GitError::FailedNoCode.into())
|
Err(_) => Err(GitError::FailedNoCode.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the upstream branch
|
/// Set the upstream branch
|
||||||
pub fn set_upstream_branch(branch: &str) -> Result<()> {
|
pub fn set_upstream_branch(branch: &str) -> Result<()> {
|
||||||
let mut cmd = Command::new("git");
|
let mut cmd = Command::new("git");
|
||||||
cmd.args([
|
cmd.args(["branch", "--set-upstream-to", &format!("origin/{branch}")]);
|
||||||
"branch",
|
|
||||||
"--set-upstream-to", &format!("origin/{branch}")
|
|
||||||
]);
|
|
||||||
|
|
||||||
match run_command(&mut cmd, &format!("Setting upstream branch to 'origin/{branch}'")) {
|
match run_command(
|
||||||
|
&mut cmd,
|
||||||
|
&format!("Setting upstream branch to 'origin/{branch}'"),
|
||||||
|
) {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(_) => Err(GitError::FailedNoCode.into())
|
Err(_) => Err(GitError::FailedNoCode.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -39,10 +39,9 @@ fn main() -> Result<()> {
|
|||||||
// This behavior follows git itself
|
// This behavior follows git itself
|
||||||
None => {
|
None => {
|
||||||
let start = args.repo_url.rfind("/").unwrap() + 1;
|
let start = args.repo_url.rfind("/").unwrap() + 1;
|
||||||
let end = match args.repo_url.rfind(".git")
|
let end = match args.repo_url.rfind(".git") {
|
||||||
{
|
|
||||||
Some(index) => index,
|
Some(index) => index,
|
||||||
None => args.repo_url.len()
|
None => args.repo_url.len(),
|
||||||
};
|
};
|
||||||
|
|
||||||
args.repo_url[start..end].to_owned()
|
args.repo_url[start..end].to_owned()
|
||||||
@@ -56,7 +55,10 @@ fn main() -> Result<()> {
|
|||||||
println!("{}", format!("Target directory: {target_dir}").dimmed());
|
println!("{}", format!("Target directory: {target_dir}").dimmed());
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("{}", "Setting up repository for worktree development".blue());
|
println!(
|
||||||
|
"{}",
|
||||||
|
"Setting up repository for worktree development".blue()
|
||||||
|
);
|
||||||
|
|
||||||
// Clone the repository as a bare clone
|
// Clone the repository as a bare clone
|
||||||
Git::clone_bare_repo(&args.repo_url, &target_dir)?;
|
Git::clone_bare_repo(&args.repo_url, &target_dir)?;
|
||||||
@@ -71,6 +73,9 @@ fn main() -> Result<()> {
|
|||||||
Git::fetch_remotes(&target_dir)?;
|
Git::fetch_remotes(&target_dir)?;
|
||||||
|
|
||||||
println!("{}", "Repository setup complete.".green());
|
println!("{}", "Repository setup complete.".green());
|
||||||
println!("{}", format!("You can now create worktrees in '{target_dir}'.").green());
|
println!(
|
||||||
|
"{}",
|
||||||
|
format!("You can now create worktrees in '{target_dir}'.").green()
|
||||||
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user