Skip to content

Commit

Permalink
feat: support for listing worktrees with gix worktree list
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jul 25, 2024
1 parent 6c8850b commit c7213bc
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions gitoxide-core/src/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ pub mod status;
pub mod submodule;
pub mod tree;
pub mod verify;
pub mod worktree;
28 changes: 28 additions & 0 deletions gitoxide-core/src/repository/worktree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::OutputFormat;
use anyhow::bail;

pub fn list(repo: gix::Repository, out: &mut dyn std::io::Write, format: OutputFormat) -> anyhow::Result<()> {
if format != OutputFormat::Human {
bail!("JSON output isn't implemented yet");
}

if let Some(worktree) = repo.worktree() {
writeln!(
out,
"{base} [{branch}]",
base = gix::path::realpath(worktree.base())?.display(),
branch = repo
.head_name()?
.map_or("<detached>".into(), |name| name.shorten().to_owned()),
)?;
}
for proxy in repo.worktrees()? {
writeln!(
out,
"{base} [{name}]",
base = proxy.base()?.display(),
name = proxy.id()
)?;
}
Ok(())
}
11 changes: 11 additions & 0 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ pub fn main() -> Result<()> {
}

match cmd {
Subcommands::Worktree(crate::plumbing::options::worktree::Platform { cmd }) => match cmd {
crate::plumbing::options::worktree::SubCommands::List => prepare_and_run(
"worktree-list",
trace,
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, _err| core::repository::worktree::list(repository(Mode::Lenient)?, out, format),
),
},
Subcommands::IsClean | Subcommands::IsChanged => {
let mode = if matches!(cmd, Subcommands::IsClean) {
core::repository::dirty::Mode::IsClean
Expand Down
16 changes: 16 additions & 0 deletions src/plumbing/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ pub enum Subcommands {
Config(config::Platform),
#[cfg(feature = "gitoxide-core-tools-corpus")]
Corpus(corpus::Platform),
Worktree(worktree::Platform),
/// Subcommands that need no git repository to run.
#[clap(subcommand)]
Free(free::Subcommands),
Expand Down Expand Up @@ -267,6 +268,21 @@ pub mod status {
}
}

pub mod worktree {
#[derive(Debug, clap::Parser)]
#[command(about = "Commands for handling worktrees")]
pub struct Platform {
#[clap(subcommand)]
pub cmd: SubCommands,
}

#[derive(Debug, clap::Subcommand)]
pub enum SubCommands {
/// List all worktrees, along with some accompanying information
List,
}
}

#[cfg(feature = "gitoxide-core-tools-corpus")]
pub mod corpus {
use std::path::PathBuf;
Expand Down

0 comments on commit c7213bc

Please sign in to comment.