Skip to content

Commit 0220abb

Browse files
authored
Merge pull request #9751 from Byron/fix
Turn silent removal of stacks into error that says how to fix it when committed onto ws-commit. (#9740)
2 parents 3fe2c11 + a2082f1 commit 0220abb

File tree

2 files changed

+41
-90
lines changed
  • crates/but-workspace

2 files changed

+41
-90
lines changed

crates/but-workspace/src/ref_info.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ pub(crate) mod function {
323323
use super::ui::{LocalCommit, LocalCommitRelation};
324324
use crate::ui::PushStatus;
325325
use crate::{AncestorWorkspaceCommit, RefInfo, WorkspaceCommit, branch, ref_info::ui};
326+
use anyhow::{Context, bail};
326327
use but_core::ref_metadata::ValueInfo;
327328
use but_graph::petgraph::Direction;
328329
use but_graph::{
@@ -449,10 +450,28 @@ pub(crate) mod function {
449450
ancestor_workspace_commit,
450451
is_entrypoint: graph.lookup_entrypoint()?.segment_index == id,
451452
};
452-
if info.ancestor_workspace_commit.is_some() {
453-
// Don't trust the stacks anymore, they now contain the workspace comit and shouldn't be acted on.
454-
// That way it's clear this must be fixed first.
455-
info.stacks.clear();
453+
454+
if let Some(info) = &info.ancestor_workspace_commit {
455+
// This is the MVP version of what should be guided by the UI - just communicate through
456+
// an error message, which can only be recovered once the command is executed.
457+
let mut msg = format!(
458+
"Found {} commit(s) on top of the workspace commit.\n\n",
459+
info.commits_outside.len()
460+
);
461+
let ws_commit_id = graph[info.segment_with_managed_commit].commits
462+
[info.commit_index_of_managed_commit]
463+
.id;
464+
msg.push_str(
465+
"The current changes will be stashed and must be re-applied manually. Commit them otherwise.\n",
466+
);
467+
msg.push_str(
468+
"Run the following command in your working directory to fix this and restore the committed changes.\n\n",
469+
);
470+
msg.push_str(&format!(" git stash && git reset --hard {ws_commit_id} && git checkout {user_commit_id} -- .",
471+
user_commit_id = info.commits_outside
472+
.first()
473+
.context("BUG: at least one user commit on top")?.id));
474+
bail!("{msg}");
456475
}
457476
info.compute_similarity(graph, repo, opts.expensive_commit_info)?;
458477
Ok(info)

crates/but-workspace/tests/workspace/ref_info/with_workspace_commit/mod.rs

Lines changed: 18 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3207,49 +3207,15 @@ fn advanced_workspace_multi_stack() -> anyhow::Result<()> {
32073207
add_stack_with_segments(&mut meta, 1, "B", StackState::InWorkspace, &[]);
32083208

32093209
let opts = standard_options();
3210-
let info = head_info(&repo, &meta, opts)?;
3211-
// It can find the exact location of the workspace commit in the ancestry.
3212-
insta::assert_debug_snapshot!(info, @r#"
3213-
RefInfo {
3214-
workspace_ref_name: Some(
3215-
FullName(
3216-
"refs/heads/gitbutler/workspace",
3217-
),
3218-
),
3219-
stacks: [],
3220-
target: Some(
3221-
Target {
3222-
ref_name: FullName(
3223-
"refs/remotes/origin/main",
3224-
),
3225-
segment_index: NodeIndex(1),
3226-
commits_ahead: 0,
3227-
},
3228-
),
3229-
extra_target: Some(
3230-
NodeIndex(1),
3231-
),
3232-
lower_bound: Some(
3233-
NodeIndex(2),
3234-
),
3235-
is_managed_ref: true,
3236-
is_managed_commit: false,
3237-
ancestor_workspace_commit: Some(
3238-
AncestorWorkspaceCommit {
3239-
commits_outside: [
3240-
Commit(a7131b1, "on-top4\n"🏘️),
3241-
Commit(4d3831e, "on-top3\n"🏘️),
3242-
Commit(468357f, "on-top2-merge\n"🏘️),
3243-
Commit(d3166f7, "on-top-sibling\n"🏘️),
3244-
Commit(118ddbb, "on-top1\n"🏘️),
3245-
],
3246-
segment_with_managed_commit: NodeIndex(10),
3247-
commit_index_of_managed_commit: 0,
3248-
},
3249-
),
3250-
is_entrypoint: true,
3251-
}
3252-
"#);
3210+
let err = head_info(&repo, &meta, opts).unwrap_err();
3211+
insta::assert_snapshot!(err.to_string(), @r"
3212+
Found 5 commit(s) on top of the workspace commit.
3213+
3214+
The current changes will be stashed and must be re-applied manually. Commit them otherwise.
3215+
Run the following command in your working directory to fix this and restore the committed changes.
3216+
3217+
git stash && git reset --hard 619d5486e6f5ae7f5e4ecc4a6ea506d6c8433410 && git checkout a7131b181b20c699ecbe4f6145bbb8746716c18b -- .
3218+
");
32533219
Ok(())
32543220
}
32553221

@@ -3273,49 +3239,15 @@ fn advanced_workspace_single_stack() -> anyhow::Result<()> {
32733239
add_stack_with_segments(&mut meta, 0, "A", StackState::InWorkspace, &[]);
32743240

32753241
let opts = standard_options();
3276-
let info = head_info(&repo, &meta, opts)?;
3277-
// It can find the exact location of the workspace commit in the ancestry.
3278-
insta::assert_debug_snapshot!(info, @r#"
3279-
RefInfo {
3280-
workspace_ref_name: Some(
3281-
FullName(
3282-
"refs/heads/gitbutler/workspace",
3283-
),
3284-
),
3285-
stacks: [],
3286-
target: Some(
3287-
Target {
3288-
ref_name: FullName(
3289-
"refs/remotes/origin/main",
3290-
),
3291-
segment_index: NodeIndex(1),
3292-
commits_ahead: 0,
3293-
},
3294-
),
3295-
extra_target: Some(
3296-
NodeIndex(1),
3297-
),
3298-
lower_bound: Some(
3299-
NodeIndex(2),
3300-
),
3301-
is_managed_ref: true,
3302-
is_managed_commit: false,
3303-
ancestor_workspace_commit: Some(
3304-
AncestorWorkspaceCommit {
3305-
commits_outside: [
3306-
Commit(da912a8, "on-top4\n"🏘️),
3307-
Commit(198eaf8, "on-top3\n"🏘️),
3308-
Commit(3147997, "on-top2-merge\n"🏘️),
3309-
Commit(dd7bb9a, "on-top-sibling\n"🏘️),
3310-
Commit(9785229, "on-top1\n"🏘️),
3311-
],
3312-
segment_with_managed_commit: NodeIndex(7),
3313-
commit_index_of_managed_commit: 1,
3314-
},
3315-
),
3316-
is_entrypoint: true,
3317-
}
3318-
"#);
3242+
let err = head_info(&repo, &meta, opts).unwrap_err();
3243+
insta::assert_snapshot!(err.to_string(), @r"
3244+
Found 5 commit(s) on top of the workspace commit.
3245+
3246+
The current changes will be stashed and must be re-applied manually. Commit them otherwise.
3247+
Run the following command in your working directory to fix this and restore the committed changes.
3248+
3249+
git stash && git reset --hard c58f15768a8ba280fe773ffc4820b13ad882cd84 && git checkout da912a8a455bca9e94ed65cb42328177fa891081 -- .
3250+
");
33193251
Ok(())
33203252
}
33213253

0 commit comments

Comments
 (0)