Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 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 gitoxide-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ anyhow = "1.0.102"
thiserror = "2.0.18"
bytesize = "2.3.1"
tempfile = "3.26.0"
unicode-width = "0.2.2"

# for async-client
async-trait = { version = "0.1.51", optional = true }
Expand Down
91 changes: 76 additions & 15 deletions gitoxide-core/src/repository/worktree.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,90 @@
use anyhow::bail;
use gix::prelude::ObjectIdExt;
use unicode_width::UnicodeWidthStr;

use crate::OutputFormat;

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");
}
let main_repo = repo.main_repo()?;
let mut worktrees = Vec::new();

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()),
)?;
if let Some(worktree) = main_repo.worktree() {
worktrees.push(create_worktree_info(&main_repo, gix::path::realpath(worktree.base())?)?);
}

for proxy in main_repo.worktrees()? {
let base = gix::path::realpath(proxy.base()?)?;

match proxy.into_repo() {
Ok(worktree_repo) => {
worktrees.push(create_worktree_info(&worktree_repo, base)?);
}
Err(_) => {
worktrees.push(create_inaccessible_worktree_info(&repo, base));
}
}
}
Comment thread
shvvkz marked this conversation as resolved.
for proxy in repo.worktrees()? {

let path_width = worktrees
.iter()
.map(|worktree| UnicodeWidthStr::width(worktree.base.as_str()))
.max()
.unwrap_or(0);

for worktree in worktrees {
worktree.write(out, path_width)?;
}

Ok(())
}

struct WorktreeInfo {
base: String,
head: String,
branch: String,
}

impl WorktreeInfo {
fn write(&self, out: &mut dyn std::io::Write, path_width: usize) -> std::io::Result<()> {
writeln!(
out,
"{base} [{name}]",
base = proxy.base()?.display(),
name = proxy.id()
)?;
"{}{} {} [{}]",
self.base,
" ".repeat(path_width.saturating_sub(UnicodeWidthStr::width(self.base.as_str()))),
self.head,
self.branch,
)
}
}

fn create_worktree_info(repo: &gix::Repository, base: std::path::PathBuf) -> anyhow::Result<WorktreeInfo> {
let head = repo
.head_id()
.map_or_else(
|_| repo.object_hash().null().attach(repo).shorten_or_id(),
|id| id.shorten_or_id(),
)
.to_string();

let branch = repo.head_name()?.map_or_else(
|| "<detached>".to_string(),
|name| name.shorten().to_owned().to_string(),
Comment thread
shvvkz marked this conversation as resolved.
);

Ok(WorktreeInfo {
base: base.display().to_string(),
head,
branch,
})
}

fn create_inaccessible_worktree_info(repo: &gix::Repository, base: std::path::PathBuf) -> WorktreeInfo {
WorktreeInfo {
base: base.display().to_string(),
head: repo.object_hash().null().attach(repo).shorten_or_id().to_string(),
branch: "<unknown>".to_string(),
}
Ok(())
}
Loading