Skip to content

Commit dfabd37

Browse files
committed
Make every but rub command create an oplog snapshot
Ensure each `but rub` command records an oplog entry by creating a snapshot before performing state-changing operations. The change imports oplog types and adds a helper create_snapshot that uses the project's exclusive worktree access and CommandContext::create_snapshot. Snapshot creation calls (with appropriate OperationKind values) are added across all relevant match arms (moves, assigns, amends, undo, squash, etc.).
1 parent 978a8cc commit dfabd37

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

crates/but/src/rub/mod.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use but_settings::AppSettings;
55
use colored::Colorize;
66
use gitbutler_command_context::CommandContext;
77
use gitbutler_project::Project;
8+
use gitbutler_oplog::{OplogExt, entry::{OperationKind, SnapshotDetails}};
89
mod amend;
910
mod assign;
1011
mod move_commit;
@@ -29,12 +30,15 @@ pub(crate) fn handle(
2930
bail!(makes_no_sense_error(&source, &target))
3031
}
3132
(CliId::UncommittedFile { path, .. }, CliId::Unassigned) => {
33+
create_snapshot(ctx, &project, OperationKind::MoveHunk);
3234
assign::unassign_file(ctx, path)
3335
}
3436
(CliId::UncommittedFile { path, assignment }, CliId::Commit { oid }) => {
37+
create_snapshot(ctx, &project, OperationKind::AmendCommit);
3538
amend::file_to_commit(ctx, path, *assignment, oid)
3639
}
3740
(CliId::UncommittedFile { path, .. }, CliId::Branch { name }) => {
41+
create_snapshot(ctx, &project, OperationKind::MoveHunk);
3842
assign::assign_file_to_branch(ctx, path, name)
3943
}
4044
(CliId::UncommittedFile { .. }, CliId::CommittedFile { .. }) => {
@@ -48,6 +52,7 @@ pub(crate) fn handle(
4852
bail!(makes_no_sense_error(&source, &target))
4953
}
5054
(CliId::CommittedFile { path, commit_oid }, CliId::Unassigned) => {
55+
create_snapshot(ctx, &project, OperationKind::FileChanges);
5156
uncommit::file_from_commit(ctx, path, commit_oid)
5257
}
5358
(CliId::CommittedFile { .. }, CliId::Branch { .. }) => {
@@ -64,8 +69,14 @@ pub(crate) fn handle(
6469
(CliId::Unassigned, CliId::Unassigned) => {
6570
bail!(makes_no_sense_error(&source, &target))
6671
}
67-
(CliId::Unassigned, CliId::Commit { oid }) => amend::assignments_to_commit(ctx, None, oid),
68-
(CliId::Unassigned, CliId::Branch { name: to }) => assign::assign_all(ctx, None, Some(to)),
72+
(CliId::Unassigned, CliId::Commit { oid }) => {
73+
create_snapshot(ctx, &project, OperationKind::AmendCommit);
74+
amend::assignments_to_commit(ctx, None, oid)
75+
},
76+
(CliId::Unassigned, CliId::Branch { name: to }) => {
77+
create_snapshot(ctx, &project, OperationKind::MoveHunk);
78+
assign::assign_all(ctx, None, Some(to))
79+
},
6980
(CliId::Unassigned, CliId::CommittedFile { .. }) => {
7081
bail!(makes_no_sense_error(&source, &target))
7182
}
@@ -75,24 +86,34 @@ pub(crate) fn handle(
7586
(CliId::Commit { .. }, CliId::CommittedFile { .. }) => {
7687
bail!(makes_no_sense_error(&source, &target))
7788
}
78-
(CliId::Commit { oid }, CliId::Unassigned) => undo::commit(ctx, oid),
89+
(CliId::Commit { oid }, CliId::Unassigned) => {
90+
create_snapshot(ctx, &project, OperationKind::UndoCommit);
91+
undo::commit(ctx, oid)
92+
},
7993
(CliId::Commit { oid: source }, CliId::Commit { oid: destination }) => {
94+
create_snapshot(ctx, &project, OperationKind::SquashCommit);
8095
squash::commits(ctx, source, destination)
8196
}
82-
(CliId::Commit { oid }, CliId::Branch { name }) => move_commit::to_branch(ctx, oid, name),
97+
(CliId::Commit { oid }, CliId::Branch { name }) => {
98+
create_snapshot(ctx, &project, OperationKind::MoveCommit);
99+
move_commit::to_branch(ctx, oid, name)
100+
},
83101
(CliId::Branch { .. }, CliId::UncommittedFile { .. }) => {
84102
bail!(makes_no_sense_error(&source, &target))
85103
}
86104
(CliId::Branch { .. }, CliId::CommittedFile { .. }) => {
87105
bail!(makes_no_sense_error(&source, &target))
88106
}
89107
(CliId::Branch { name: from }, CliId::Unassigned) => {
108+
create_snapshot(ctx, &project, OperationKind::MoveHunk);
90109
assign::assign_all(ctx, Some(from), None)
91110
}
92111
(CliId::Branch { name }, CliId::Commit { oid }) => {
112+
create_snapshot(ctx, &project, OperationKind::AmendCommit);
93113
amend::assignments_to_commit(ctx, Some(name), oid)
94114
}
95115
(CliId::Branch { name: from }, CliId::Branch { name: to }) => {
116+
create_snapshot(ctx, &project, OperationKind::MoveHunk);
96117
assign::assign_all(ctx, Some(from), Some(to))
97118
}
98119
}
@@ -155,3 +176,13 @@ fn ids(ctx: &mut CommandContext, source: &str, target: &str) -> anyhow::Result<(
155176
}
156177
Ok((source_result[0].clone(), target_result[0].clone()))
157178
}
179+
180+
fn create_snapshot(ctx: &mut CommandContext, project: &Project, operation: OperationKind) {
181+
let mut guard = project.exclusive_worktree_access();
182+
let _snapshot = ctx
183+
.create_snapshot(
184+
SnapshotDetails::new(operation),
185+
guard.write_permission(),
186+
)
187+
.ok(); // Ignore errors for snapshot creation
188+
}

0 commit comments

Comments
 (0)