Skip to content

Commit 5556c79

Browse files
committed
More defensively build the format string in case the workspace commit isn't managed (#10466)
Note that it's still unclear how the graph workspace can report that it's not pointed to by the managed workspace ref, yet the first commit it finds seems to be the managed workspace commit. This really shouldn't be possible.
1 parent 3dbc754 commit 5556c79

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

crates/but-workspace/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ pub struct RefInfo {
221221
#[derive(Debug, Clone)]
222222
pub struct AncestorWorkspaceCommit {
223223
/// The commits along the first parent that are between the managed workspace reference and the managed workspace commit.
224-
/// The vec is never empty.
225-
pub commits_outside: Vec<crate::ref_info::Commit>,
224+
/// The vec *should* not be empty, but it can be empty in practice for reasons yet to be discovered.
225+
pub commits_outside: Vec<ref_info::Commit>,
226226
/// The index of the segment that actually holds the managed workspace commit.
227227
pub segment_with_managed_commit: SegmentIndex,
228228
/// The index of the workspace commit within the `commits` array in its parent segment.

crates/but-workspace/src/ref_info.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ pub(crate) mod function {
337337
use crate::ref_info::{LocalCommit, LocalCommitRelation};
338338
use crate::ui::PushStatus;
339339
use crate::{AncestorWorkspaceCommit, RefInfo, WorkspaceCommit, branch};
340-
use anyhow::{Context, bail};
340+
use anyhow::bail;
341341
use but_core::ref_metadata::ValueInfo;
342342
use but_graph::petgraph::Direction;
343343
use but_graph::{
@@ -488,10 +488,17 @@ pub(crate) mod function {
488488
msg.push_str(
489489
"Run the following command in your working directory to fix this and restore the committed changes.\n\n",
490490
);
491-
msg.push_str(&format!(" git stash && git reset --hard {ws_commit_id} && git checkout {user_commit_id} -- .",
492-
user_commit_id = info.commits_outside
493-
.first()
494-
.context("BUG: at least one user commit on top")?.id));
491+
msg.push_str(&format!(
492+
" git stash && git reset --hard {ws_commit_id}{checkout_clause}",
493+
checkout_clause = info
494+
.commits_outside
495+
.first()
496+
.map(|c| format!(
497+
" && git checkout {user_commit_id} -- .",
498+
user_commit_id = c.id
499+
))
500+
.unwrap_or_default()
501+
));
495502
bail!("{msg}");
496503
}
497504
info.compute_similarity(graph, repo, opts.expensive_commit_info)?;

0 commit comments

Comments
 (0)