Compare commits
12 Commits
v0.1.0
...
c2df03303b
Author | SHA1 | Date | |
---|---|---|---|
c2df03303b | |||
5d745678f3
|
|||
cf5b30015a
|
|||
476f849ded
|
|||
8c22757f71
|
|||
0793c8bbd7
|
|||
ddad92dc32
|
|||
002c85329e
|
|||
b46621f772
|
|||
c0fe91f916
|
|||
446a3a2f3f
|
|||
|
69f94c64b6 |
@@ -7,18 +7,36 @@ env:
|
|||||||
CARGO_TERM_COLOR: always
|
CARGO_TERM_COLOR: always
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
cargo-fmt:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout source code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
# Cache cargo registry
|
||||||
|
- name: Hash the lock file
|
||||||
|
uses: https://gitea.com/seepine/hash-files@v1
|
||||||
|
id: get-hash
|
||||||
|
with:
|
||||||
|
patterns: |
|
||||||
|
**/Cargo.lock
|
||||||
|
|
||||||
|
- name: Setup rust
|
||||||
|
uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
components: rustfmt,clippy
|
||||||
|
cache-shared-key: cargo-${{ steps.get-hash.outputs.hash }}
|
||||||
|
|
||||||
|
- name: Rustfmt Check
|
||||||
|
uses: actions-rust-lang/rustfmt@v1
|
||||||
|
|
||||||
cargo-check:
|
cargo-check:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout source code
|
- name: Checkout source code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Setup rust
|
|
||||||
uses: actions-rs/toolchain@v1
|
|
||||||
with:
|
|
||||||
toolchain: stable
|
|
||||||
components: rustfmt,clippy
|
|
||||||
|
|
||||||
# Cache cargo registry
|
# Cache cargo registry
|
||||||
- name: Hash the lock file
|
- name: Hash the lock file
|
||||||
uses: https://gitea.com/seepine/hash-files@v1
|
uses: https://gitea.com/seepine/hash-files@v1
|
||||||
@@ -26,37 +44,23 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
patterns: |
|
patterns: |
|
||||||
**/Cargo.lock
|
**/Cargo.lock
|
||||||
- name: Cache cargo registry
|
|
||||||
uses: actions/cache@v3
|
- name: Setup rust
|
||||||
|
uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
path: |
|
toolchain: stable
|
||||||
~/.cargo/bin/
|
components: rustfmt,clippy
|
||||||
~/.cargo/registry/index/
|
cache-shared-key: cargo-${{ steps.get-hash.outputs.hash }}
|
||||||
~/.cargo/registry/cache/
|
|
||||||
~/.cargo/git/db/
|
|
||||||
target/
|
|
||||||
key: cargo-${{ steps.get-hash.outputs.hash }}
|
|
||||||
restore-keys: |
|
|
||||||
cargo-
|
|
||||||
# Cache cargo registry - end
|
|
||||||
|
|
||||||
- name: Cargo check
|
- name: Cargo check
|
||||||
uses: actions-rs/cargo@v1
|
run: cargo check --workspace
|
||||||
with:
|
|
||||||
command: check
|
|
||||||
|
|
||||||
clippy:
|
clippy:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout source code
|
- name: Checkout source code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Setup rust
|
|
||||||
uses: actions-rs/toolchain@v1
|
|
||||||
with:
|
|
||||||
toolchain: stable
|
|
||||||
components: rustfmt,clippy
|
|
||||||
|
|
||||||
# Cache cargo registry
|
# Cache cargo registry
|
||||||
- name: Hash the lock file
|
- name: Hash the lock file
|
||||||
uses: https://gitea.com/seepine/hash-files@v1
|
uses: https://gitea.com/seepine/hash-files@v1
|
||||||
@@ -64,22 +68,13 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
patterns: |
|
patterns: |
|
||||||
**/Cargo.lock
|
**/Cargo.lock
|
||||||
- name: Cache cargo registry
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
~/.cargo/bin/
|
|
||||||
~/.cargo/registry/index/
|
|
||||||
~/.cargo/registry/cache/
|
|
||||||
~/.cargo/git/db/
|
|
||||||
target/
|
|
||||||
key: cargo-${{ steps.get-hash.outputs.hash }}
|
|
||||||
restore-keys: |
|
|
||||||
cargo-
|
|
||||||
# Cache cargo registry - end
|
|
||||||
|
|
||||||
- name: Run clippy
|
- name: Setup rust
|
||||||
uses: actions-rs/clippy-check@v1
|
uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.GITHUB_TOKEN }}
|
toolchain: stable
|
||||||
args: --all-targets --all-features -- -Dwarnings
|
components: rustfmt,clippy
|
||||||
|
cache-shared-key: cargo-${{ steps.get-hash.outputs.hash }}
|
||||||
|
|
||||||
|
- name: Cargo check
|
||||||
|
run: cargo clippy --all-targets --all-features -- -Dwarnings
|
||||||
|
@@ -15,53 +15,32 @@ jobs:
|
|||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Setup rust
|
# Cache cargo registry
|
||||||
uses: actions-rs/toolchain@v1
|
|
||||||
with:
|
|
||||||
profile: minimal
|
|
||||||
toolchain: stable
|
|
||||||
|
|
||||||
# Cache cargo registry
|
|
||||||
- name: Hash the lock file
|
- name: Hash the lock file
|
||||||
uses: https://gitea.com/seepine/hash-files@v1
|
uses: https://gitea.com/seepine/hash-files@v1
|
||||||
id: get-hash
|
id: get-hash
|
||||||
with:
|
with:
|
||||||
patterns: |
|
patterns: |
|
||||||
**/Cargo.lock
|
**/Cargo.lock
|
||||||
- name: Cache cargo registry
|
|
||||||
uses: actions/cache@v4
|
- name: Setup rust
|
||||||
|
uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
path: |
|
toolchain: stable
|
||||||
~/.cargo/bin/
|
cache-shared-key: cargo-${{ steps.get-hash.outputs.hash }}
|
||||||
~/.cargo/registry/index/
|
matcher: false
|
||||||
~/.cargo/registry/cache/
|
|
||||||
~/.cargo/git/db/
|
|
||||||
target/
|
|
||||||
key: cargo-${{ steps.get-hash.outputs.hash }}
|
|
||||||
restore-keys: |
|
|
||||||
cargo-
|
|
||||||
# Cache cargo registry - end
|
|
||||||
|
|
||||||
- name: Build application
|
- name: Build application
|
||||||
uses: actions-rs/cargo@v1
|
run: cargo build --release --all-features --workspace --all-targets
|
||||||
with:
|
|
||||||
command: build
|
|
||||||
args: --release --all-features --workspace --all-targets
|
|
||||||
|
|
||||||
- name: Package application
|
- name: Package application
|
||||||
uses: actions-rs/cargo@v1
|
ru8n: cargo package --all-features --workspace --allow-dirty
|
||||||
with:
|
|
||||||
command: package
|
|
||||||
args: --all-features --workspace --allow-dirty
|
|
||||||
|
|
||||||
- name: Add registry to cargo config
|
- name: Add registry to cargo config
|
||||||
run: echo -e '[registries.gitea]\nindex = "sparse+https://git.noahknegt.com/api/packages/${{ github.repository_owner }}/cargo/"\n' > ~/.cargo/config.toml
|
run: echo -e '[registries.gitea]\nindex = "sparse+https://git.noahknegt.com/api/packages/${{ github.repository_owner }}/cargo/"\n' > ~/.cargo/config.toml
|
||||||
|
|
||||||
- name: Publish to cargo registry
|
- name: Publish to cargo registry
|
||||||
uses: actions-rs/cargo@v1
|
run: cargo publish --no-verify --registry gitea --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
|
||||||
with:
|
|
||||||
command: publish
|
|
||||||
args: --no-verify --registry gitea --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
|
|
||||||
env:
|
env:
|
||||||
CARGO_NET_GIT_FETCH_WITH_CLI: true
|
CARGO_NET_GIT_FETCH_WITH_CLI: true
|
||||||
CARGO_NET_RETRY: 2
|
CARGO_NET_RETRY: 2
|
||||||
|
28
Cargo.lock
generated
28
Cargo.lock
generated
@@ -131,6 +131,16 @@ dependencies = [
|
|||||||
"windows-sys",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "command-with-spinner"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"colored",
|
||||||
|
"indicatif",
|
||||||
|
"thiserror",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "console"
|
name = "console"
|
||||||
version = "0.15.11"
|
version = "0.15.11"
|
||||||
@@ -152,6 +162,7 @@ dependencies = [
|
|||||||
"clap",
|
"clap",
|
||||||
"colored",
|
"colored",
|
||||||
"dirs",
|
"dirs",
|
||||||
|
"git",
|
||||||
"indicatif",
|
"indicatif",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
]
|
]
|
||||||
@@ -194,6 +205,19 @@ dependencies = [
|
|||||||
"wasi",
|
"wasi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "git"
|
||||||
|
version = "0.2.0"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"command-with-spinner",
|
||||||
|
"thiserror",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "git-ssh-bitwarden"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "heck"
|
name = "heck"
|
||||||
version = "0.5.0"
|
version = "0.5.0"
|
||||||
@@ -306,13 +330,13 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "setup-repo"
|
name = "setup-repo"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
"colored",
|
"colored",
|
||||||
|
"git",
|
||||||
"indicatif",
|
"indicatif",
|
||||||
"thiserror",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@@ -1,7 +1,10 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
resolver = "3"
|
resolver = "3"
|
||||||
members = [
|
members = [
|
||||||
"create-worktree",
|
"command-with-spinner",
|
||||||
|
"create-worktree",
|
||||||
|
"git",
|
||||||
|
"git-ssh-bitwarden",
|
||||||
"setup-repo",
|
"setup-repo",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
14
command-with-spinner/Cargo.toml
Normal file
14
command-with-spinner/Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
[package]
|
||||||
|
name = "command-with-spinner"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
edition.workspace = true
|
||||||
|
authors.workspace = true
|
||||||
|
license-file.workspace = true
|
||||||
|
repository.workspace = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
anyhow = "1.0.98"
|
||||||
|
colored = "3.0.0"
|
||||||
|
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),
|
||||||
|
}
|
42
command-with-spinner/src/lib.rs
Normal file
42
command-with-spinner/src/lib.rs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
use std::process::{Command, Stdio};
|
||||||
|
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
use colored::*;
|
||||||
|
use indicatif::{ProgressBar, ProgressStyle};
|
||||||
|
|
||||||
|
mod error;
|
||||||
|
use error::CommandError;
|
||||||
|
|
||||||
|
/// Runs a git command with a progress spinner
|
||||||
|
pub 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(CommandError::Failed(code).into()),
|
||||||
|
None => Err(CommandError::FailedNoCode.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -9,9 +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.2.0", path = "../git" }
|
||||||
|
indicatif = "0.17"
|
||||||
thiserror = { version = "2.0" }
|
thiserror = { version = "2.0" }
|
||||||
|
@@ -1,11 +1,11 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process::{Command, Stdio};
|
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use clap::{ArgAction, Parser};
|
use clap::{ArgAction, Parser};
|
||||||
use colored::*;
|
use colored::*;
|
||||||
use indicatif::{ProgressBar, ProgressStyle};
|
|
||||||
|
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)]
|
||||||
@@ -39,133 +39,6 @@ struct Args {
|
|||||||
no_color: bool,
|
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
|
/// 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
|
||||||
@@ -178,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)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,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();
|
||||||
@@ -220,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
|
||||||
|
10
git-ssh-bitwarden/Cargo.toml
Normal file
10
git-ssh-bitwarden/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "git-ssh-bitwarden"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
edition.workspace = true
|
||||||
|
authors.workspace = true
|
||||||
|
license-file.workspace = true
|
||||||
|
repository.workspace = true
|
||||||
|
|
||||||
|
[dependencies]
|
3
git-ssh-bitwarden/src/main.rs
Normal file
3
git-ssh-bitwarden/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
13
git/Cargo.toml
Normal file
13
git/Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
[package]
|
||||||
|
name = "git"
|
||||||
|
version = "0.2.0"
|
||||||
|
|
||||||
|
edition.workspace = true
|
||||||
|
authors.workspace = true
|
||||||
|
license-file.workspace = true
|
||||||
|
repository.workspace = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
anyhow = "1.0.98"
|
||||||
|
command-with-spinner = { version = "0.1.0", path = "../command-with-spinner" }
|
||||||
|
thiserror = "2.0"
|
9
git/src/error.rs
Normal file
9
git/src/error.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
/// Git operations error type
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
pub enum GitError {
|
||||||
|
#[error("Command failed without exit code")]
|
||||||
|
FailedNoCode,
|
||||||
|
|
||||||
|
#[error("Failed to execute command: {0}")]
|
||||||
|
ExecutionError(#[from] std::io::Error),
|
||||||
|
}
|
148
git/src/lib.rs
Normal file
148
git/src/lib.rs
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
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()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "setup-repo"
|
name = "setup-repo"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
description = "Tool to set up Git repositories for worktree development"
|
description = "Tool to set up Git repositories for worktree development"
|
||||||
|
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
@@ -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"
|
||||||
|
git = { version = "0.2.0", path = "../git" }
|
||||||
indicatif = "0.17"
|
indicatif = "0.17"
|
||||||
thiserror = "2.0"
|
|
||||||
|
@@ -1,10 +1,8 @@
|
|||||||
use std::path::Path;
|
use anyhow::Result;
|
||||||
use std::process::{Command, Stdio};
|
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use colored::*;
|
use colored::*;
|
||||||
use indicatif::{ProgressBar, ProgressStyle};
|
|
||||||
|
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)]
|
||||||
@@ -16,7 +14,7 @@ struct Args {
|
|||||||
|
|
||||||
/// Target directory for the repository setup
|
/// Target directory for the repository setup
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
target_dir: String,
|
target_dir: Option<String>,
|
||||||
|
|
||||||
/// Enable verbose output
|
/// Enable verbose output
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
@@ -27,113 +25,6 @@ struct Args {
|
|||||||
no_color: bool,
|
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 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 {
|
|
||||||
/// 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();
|
||||||
@@ -141,28 +32,50 @@ fn main() -> Result<()> {
|
|||||||
// Enable or disable colored output
|
// Enable or disable colored output
|
||||||
colored::control::set_override(!args.no_color);
|
colored::control::set_override(!args.no_color);
|
||||||
|
|
||||||
|
let target_dir = match args.target_dir {
|
||||||
|
Some(dir) => dir,
|
||||||
|
|
||||||
|
// If a directory is not provided construct one from the last part of the URI.
|
||||||
|
// This behavior follows git itself
|
||||||
|
None => {
|
||||||
|
let start = args.repo_url.rfind("/").unwrap() + 1;
|
||||||
|
let end = match args.repo_url.rfind(".git") {
|
||||||
|
Some(index) => index,
|
||||||
|
None => args.repo_url.len(),
|
||||||
|
};
|
||||||
|
|
||||||
|
args.repo_url[start..end].to_owned()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Print verbose information if enabled
|
// Print verbose information if enabled
|
||||||
if args.verbose {
|
if args.verbose {
|
||||||
println!("{}", "Verbose mode enabled".dimmed());
|
println!("{}", "Verbose mode enabled".dimmed());
|
||||||
println!("{}", format!("Repository URL: {}", args.repo_url).dimmed());
|
println!("{}", format!("Repository URL: {}", args.repo_url).dimmed());
|
||||||
println!("{}", format!("Target directory: {}", args.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, &args.target_dir)?;
|
Git::clone_bare_repo(&args.repo_url, &target_dir)?;
|
||||||
|
|
||||||
// Set up the .git file to point to the .bare directory
|
// Set up the .git file to point to the .bare directory
|
||||||
Git::setup_git_pointer(&args.target_dir)?;
|
Git::setup_git_pointer(&target_dir)?;
|
||||||
|
|
||||||
// Configure the remote.origin.fetch setting
|
// Configure the remote.origin.fetch setting
|
||||||
Git::configure_remote_fetch(&args.target_dir)?;
|
Git::configure_remote_fetch(&target_dir)?;
|
||||||
|
|
||||||
// Fetch all remotes
|
// Fetch all remotes
|
||||||
Git::fetch_remotes(&args.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 '{}'.", args.target_dir).green());
|
println!(
|
||||||
|
"{}",
|
||||||
|
format!("You can now create worktrees in '{target_dir}'.").green()
|
||||||
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user