Better worktree errors (#1338)

* Info log if first create fails, properly clean up after first fail

* Show stdout on git cli error

* Fix swapped stderr/stdout
This commit is contained in:
Alex Netsch
2025-11-19 18:21:07 +00:00
committed by GitHub
parent 84454b54a1
commit 6e5cb34f7e
2 changed files with 22 additions and 4 deletions

View File

@@ -628,7 +628,14 @@ impl GitCli {
.map_err(|e| GitCliError::CommandFailed(e.to_string()))?;
if !out.status.success() {
let stderr = String::from_utf8_lossy(&out.stderr).trim().to_string();
return Err(GitCliError::CommandFailed(stderr));
let stdout = String::from_utf8_lossy(&out.stdout).trim().to_string();
let combined = match (stdout.is_empty(), stderr.is_empty()) {
(true, true) => "Command failed with no output".to_string(),
(false, false) => format!("--- stderr\n{stderr}\n--- stdout\n{stdout}"),
(false, true) => format!("--- stderr\n{stdout}"),
(true, false) => format!("--- stdout\n{stderr}"),
};
return Err(GitCliError::CommandFailed(combined));
}
Ok(String::from_utf8_lossy(&out.stdout).to_string())
}
@@ -659,7 +666,14 @@ impl GitCli {
.map_err(|e| GitCliError::CommandFailed(e.to_string()))?;
if !out.status.success() {
let stderr = String::from_utf8_lossy(&out.stderr).trim().to_string();
return Err(GitCliError::CommandFailed(stderr));
let stdout = String::from_utf8_lossy(&out.stdout).trim().to_string();
let combined = match (stdout.is_empty(), stderr.is_empty()) {
(true, true) => "Command failed with no output".to_string(),
(false, false) => format!("--- stderr\n{stderr}\n--- stdout\n{stdout}"),
(false, true) => format!("--- stderr\n{stdout}"),
(true, false) => format!("--- stdout\n{stderr}"),
};
return Err(GitCliError::CommandFailed(combined));
}
Ok(String::from_utf8_lossy(&out.stdout).to_string())
}

View File

@@ -306,20 +306,24 @@ impl WorktreeManager {
Ok(())
}
Err(e) => {
debug!(
tracing::info!(
"git worktree add failed; attempting metadata cleanup and retry: {}",
e
);
// Force cleanup metadata and try one more time
Self::force_cleanup_worktree_metadata(&git_repo_path, &worktree_name)
.map_err(WorktreeError::Io)?;
// Clean up physical directory if it exists
// Needed if previous attempt failed after directory creation
if worktree_path.exists() {
std::fs::remove_dir_all(&worktree_path).map_err(WorktreeError::Io)?;
}
if let Err(e2) = git_service.add_worktree(
&git_repo_path,
&worktree_path,
&branch_name,
false,
) {
debug!("Retry of git worktree add failed: {}", e2);
return Err(WorktreeError::GitService(e2));
}
if !worktree_path.exists() {