Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
9999years committed Sep 24, 2024
1 parent 9afc843 commit b2f9a96
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 34 deletions.
29 changes: 29 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fs-err = "2.11.0"
miette = { version = "7.2.0", default-features = false, features = ["fancy-no-backtrace"] }
owo-colors = { version = "4.0.0", features = ["supports-colors"] }
regex = "1.10.6"
tempfile = "3.12.0"
tracing = { version = "0.1.40", features = ["attributes"] }
tracing-human-layer = "0.1.3"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "registry"] }
Expand Down
64 changes: 64 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use clap::CommandFactory;
use fs_err as fs;
use miette::Context;
use miette::IntoDiagnostic;

use crate::cli;
use crate::cli::Opts;
use crate::git::Git;

pub struct App {
git: Git,
config: Opts,
}

impl App {
pub fn new(config: Opts) -> Self {
Self {
config,
git: Git::new(),
}
}

pub fn run(self) -> miette::Result<()> {
match &self.config.command {
cli::Command::Completions { shell } => {
let mut clap_command = cli::Opts::command();
clap_complete::generate(
*shell,
&mut clap_command,
"git-prole",
&mut std::io::stdout(),
);
}
#[cfg(feature = "clap_mangen")]
cli::Command::Manpages { out_dir } => {
let clap_command = cli::Opts::command();
clap_mangen::generate_to(clap_command, out_dir)
.into_diagnostic()
.wrap_err("Failed to generate man pages")?;
}
cli::Command::Convert {} => self.convert()?,
}

Ok(())
}

fn convert(&self) -> miette::Result<()> {
let working_dir = self.git.working_dir()?;
let tempdir = tempfile::tempdir().into_diagnostic()?;

// Figuring out which worktrees to create is non-trivial:
// - We might have any number of remotes. Which one to use?
// - We might already have the default branch checked out.
// - We might _not_ have the default branch checked out.
// - We might have unstaged/uncommitted work.
// - We might not be on _any_ branch.

// TODO: Ask the user before we start messing around with their repo layout!
fs::rename(&working_dir, &tempdir).into_diagnostic()?;
fs::create_dir_all(&working_dir).into_diagnostic()?;

Ok(())
}
}
3 changes: 2 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ pub struct Opts {
#[allow(rustdoc::bare_urls)]
#[derive(Debug, Clone, Subcommand)]
pub enum Command {
Add {},
/// Convert a checkout into a worktree checkout.
Convert {},
/// Generate shell completions.
Completions {
/// Shell to generate completions for.
Expand Down
14 changes: 13 additions & 1 deletion src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use crate::commit_hash::CommitHash;
pub struct Git {}

impl Git {
#[expect(dead_code)]
pub fn new() -> Self {
Default::default()
}
Expand All @@ -25,6 +24,19 @@ impl Git {
Command::new("git")
}

/// `git rev-parse --show-toplevel`
pub fn working_dir(&self) -> miette::Result<Utf8PathBuf> {
Ok(self
.command()
.args(["rev-parse", "--show-toplevel"])
.output_checked_utf8()
.into_diagnostic()
.wrap_err("Failed to get working directory of repository")?
.stdout
.trim()
.into())
}

/// Get a list of all `git remote`s.
#[expect(dead_code)]
pub fn remotes(&self) -> miette::Result<Vec<String>> {
Expand Down
38 changes: 6 additions & 32 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,15 @@
mod cli;
mod commit_hash;
mod git;
mod install_tracing;

use clap::CommandFactory;
use clap::Parser;
use cli::Opts;
use install_tracing::install_tracing;

#[allow(unused_imports)]
use miette::Context;
#[allow(unused_imports)]
use miette::IntoDiagnostic;
mod app;
mod cli;
mod commit_hash;
mod git;
mod install_tracing;

fn main() -> miette::Result<()> {
let opts = Opts::parse();
install_tracing(&opts.log)?;

match opts.command {
cli::Command::Completions { shell } => {
let mut clap_command = cli::Opts::command();
clap_complete::generate(
shell,
&mut clap_command,
"git-prole",
&mut std::io::stdout(),
);
}
#[cfg(feature = "clap_mangen")]
cli::Command::Manpages { out_dir } => {
let clap_command = cli::Opts::command();
clap_mangen::generate_to(clap_command, out_dir)
.into_diagnostic()
.wrap_err("Failed to generate man pages")?;
}
cli::Command::Add {} => todo!(),
}

Ok(())
app::App::new(opts).run()
}

0 comments on commit b2f9a96

Please sign in to comment.