Skip to content

Commit c7213bc

Browse files
committed
feat: support for listing worktrees with gix worktree list
1 parent 6c8850b commit c7213bc

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

Diff for: gitoxide-core/src/repository/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@ pub mod status;
5151
pub mod submodule;
5252
pub mod tree;
5353
pub mod verify;
54+
pub mod worktree;

Diff for: gitoxide-core/src/repository/worktree.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use crate::OutputFormat;
2+
use anyhow::bail;
3+
4+
pub fn list(repo: gix::Repository, out: &mut dyn std::io::Write, format: OutputFormat) -> anyhow::Result<()> {
5+
if format != OutputFormat::Human {
6+
bail!("JSON output isn't implemented yet");
7+
}
8+
9+
if let Some(worktree) = repo.worktree() {
10+
writeln!(
11+
out,
12+
"{base} [{branch}]",
13+
base = gix::path::realpath(worktree.base())?.display(),
14+
branch = repo
15+
.head_name()?
16+
.map_or("<detached>".into(), |name| name.shorten().to_owned()),
17+
)?;
18+
}
19+
for proxy in repo.worktrees()? {
20+
writeln!(
21+
out,
22+
"{base} [{name}]",
23+
base = proxy.base()?.display(),
24+
name = proxy.id()
25+
)?;
26+
}
27+
Ok(())
28+
}

Diff for: src/plumbing/main.rs

+11
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ pub fn main() -> Result<()> {
146146
}
147147

148148
match cmd {
149+
Subcommands::Worktree(crate::plumbing::options::worktree::Platform { cmd }) => match cmd {
150+
crate::plumbing::options::worktree::SubCommands::List => prepare_and_run(
151+
"worktree-list",
152+
trace,
153+
verbose,
154+
progress,
155+
progress_keep_open,
156+
None,
157+
move |_progress, out, _err| core::repository::worktree::list(repository(Mode::Lenient)?, out, format),
158+
),
159+
},
149160
Subcommands::IsClean | Subcommands::IsChanged => {
150161
let mode = if matches!(cmd, Subcommands::IsClean) {
151162
core::repository::dirty::Mode::IsClean

Diff for: src/plumbing/options/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ pub enum Subcommands {
138138
Config(config::Platform),
139139
#[cfg(feature = "gitoxide-core-tools-corpus")]
140140
Corpus(corpus::Platform),
141+
Worktree(worktree::Platform),
141142
/// Subcommands that need no git repository to run.
142143
#[clap(subcommand)]
143144
Free(free::Subcommands),
@@ -267,6 +268,21 @@ pub mod status {
267268
}
268269
}
269270

271+
pub mod worktree {
272+
#[derive(Debug, clap::Parser)]
273+
#[command(about = "Commands for handling worktrees")]
274+
pub struct Platform {
275+
#[clap(subcommand)]
276+
pub cmd: SubCommands,
277+
}
278+
279+
#[derive(Debug, clap::Subcommand)]
280+
pub enum SubCommands {
281+
/// List all worktrees, along with some accompanying information
282+
List,
283+
}
284+
}
285+
270286
#[cfg(feature = "gitoxide-core-tools-corpus")]
271287
pub mod corpus {
272288
use std::path::PathBuf;

0 commit comments

Comments
 (0)