Skip to content

Commit 005d399

Browse files
committed
Improve error messages for missing stack commits
Clarify why a commit might not be found and suggest running 'but status' to refresh state. Provide more context in errors for source/target lookups, and include the short commit id in squash/undo errors so users know which commit is affected.
1 parent 46c6b83 commit 005d399

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

crates/but/src/rub/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ fn ids(ctx: &mut CommandContext, source: &str, target: &str) -> anyhow::Result<(
112112
let source_result = crate::id::CliId::from_str(ctx, source)?;
113113
if source_result.len() != 1 {
114114
if source_result.is_empty() {
115-
return Err(anyhow::anyhow!("Source '{}' not found", source));
115+
return Err(anyhow::anyhow!(
116+
"Source '{}' not found. If you just performed a Git operation (squash, rebase, etc.), try running 'but status' to refresh the current state.",
117+
source
118+
));
116119
} else {
117120
let matches: Vec<String> = source_result.iter().map(|id| {
118121
match id {
@@ -131,7 +134,10 @@ fn ids(ctx: &mut CommandContext, source: &str, target: &str) -> anyhow::Result<(
131134
let target_result = crate::id::CliId::from_str(ctx, target)?;
132135
if target_result.len() != 1 {
133136
if target_result.is_empty() {
134-
return Err(anyhow::anyhow!("Target '{}' not found", target));
137+
return Err(anyhow::anyhow!(
138+
"Target '{}' not found. If you just performed a Git operation (squash, rebase, etc.), try running 'but status' to refresh the current state.",
139+
target
140+
));
135141
} else {
136142
let matches: Vec<String> = target_result.iter().map(|id| {
137143
match id {

crates/but/src/rub/squash.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ pub(crate) fn commits(
1010
source: &ObjectId,
1111
destination: &ObjectId,
1212
) -> anyhow::Result<()> {
13-
let source_stack = stack_id_by_commit_id(ctx, source)?;
14-
let destination_stack = stack_id_by_commit_id(ctx, destination)?;
13+
// Validate both commits exist in stacks before proceeding
14+
let source_stack = stack_id_by_commit_id(ctx, source).map_err(|e| {
15+
anyhow::anyhow!("Source commit {}: {}", &source.to_string()[..7], e)
16+
})?;
17+
let destination_stack = stack_id_by_commit_id(ctx, destination).map_err(|e| {
18+
anyhow::anyhow!("Destination commit {}: {}", &destination.to_string()[..7], e)
19+
})?;
1520
if source_stack != destination_stack {
1621
anyhow::bail!("Cannot squash commits from different stacks");
1722
}

crates/but/src/rub/undo.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@ pub(crate) fn stack_id_by_commit_id(
2929
}) {
3030
return Ok(*id);
3131
}
32-
anyhow::bail!("No stack found for commit {}", oid.to_string())
32+
anyhow::bail!(
33+
"No stack found for commit {}. The commit may have been removed by a previous operation (e.g., squash, rebase). Try refreshing with 'but status' to see the current state.",
34+
&oid.to_string()[..7]
35+
)
3336
}

0 commit comments

Comments
 (0)