Skip to content

Commit 000bddb

Browse files
authored
Merge pull request #4820 from gitbutlerapp/unapply-only-hunks-from-lane
Only discard the files changes inside the lane
2 parents 36e9845 + 3b0a276 commit 000bddb

File tree

6 files changed

+52
-38
lines changed

6 files changed

+52
-38
lines changed

apps/desktop/src/lib/file/FileContextMenu.svelte

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import { join } from '@tauri-apps/api/path';
1515
import { open as openFile } from '@tauri-apps/api/shell';
1616
17+
export let branchId: string | undefined;
1718
export let target: HTMLElement | undefined;
1819
export let isUnapplied;
1920
@@ -115,7 +116,12 @@
115116
style="error"
116117
kind="solid"
117118
onclick={() => {
118-
branchController.unapplyFiles(item.files);
119+
if (!branchId) {
120+
console.error('Branch ID is not set');
121+
toasts.error('Failed to discard changes');
122+
return;
123+
}
124+
branchController.unapplyFiles(branchId, item.files);
119125
close();
120126
}}
121127
>

apps/desktop/src/lib/file/FileListItem.svelte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@
8585
});
8686
</script>
8787

88-
<FileContextMenu bind:this={contextMenu} target={draggableEl} {isUnapplied} />
88+
<FileContextMenu
89+
bind:this={contextMenu}
90+
target={draggableEl}
91+
{isUnapplied}
92+
branchId={$branch?.id}
93+
/>
8994

9095
<FileListItem
9196
id={`file-${file.id}`}

apps/desktop/src/lib/vbranches/branchController.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,12 @@ export class BranchController {
226226
}
227227
}
228228

229-
async unapplyFiles(files: LocalFile[]) {
229+
async unapplyFiles(branchId: string, files: LocalFile[]) {
230230
try {
231231
await invoke<void>('reset_files', {
232232
projectId: this.projectId,
233-
files: files.flatMap((f) => f.path).join('\n')
233+
branchId,
234+
files: files.flatMap((f) => f.path)
234235
});
235236
} catch (err) {
236237
showError('Failed to unapply file changes', err);

crates/gitbutler-branch-actions/src/actions.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use gitbutler_oplog::{
2525
use gitbutler_project::{FetchResult, Project};
2626
use gitbutler_reference::{ReferenceName, Refname, RemoteRefname};
2727
use gitbutler_repo::{credentials::Helper, RepoActionsExt, RepositoryExt};
28+
use std::path::PathBuf;
2829
use tracing::instrument;
2930

3031
#[derive(Clone, Copy, Default)]
@@ -285,7 +286,12 @@ impl VirtualBranchActions {
285286
branch::unapply_ownership(&ctx, ownership, guard.write_permission()).map_err(Into::into)
286287
}
287288

288-
pub fn reset_files(&self, project: &Project, files: &Vec<String>) -> Result<()> {
289+
pub fn reset_files(
290+
&self,
291+
project: &Project,
292+
branch_id: BranchId,
293+
files: &[PathBuf],
294+
) -> Result<()> {
289295
let ctx = open_with_verify(project)?;
290296
assure_open_workspace_mode(&ctx)
291297
.context("Resetting a file requires open workspace mode")?;
@@ -294,7 +300,7 @@ impl VirtualBranchActions {
294300
SnapshotDetails::new(OperationKind::DiscardFile),
295301
guard.write_permission(),
296302
);
297-
branch::reset_files(&ctx, files).map_err(Into::into)
303+
branch::reset_files(&ctx, branch_id, files, guard.write_permission()).map_err(Into::into)
298304
}
299305

300306
pub fn amend(

crates/gitbutler-branch-actions/src/virtual.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@ use gitbutler_repo::{
3232
use gitbutler_time::time::now_since_unix_epoch_ms;
3333
use serde::Serialize;
3434
use std::collections::HashSet;
35-
use std::{
36-
borrow::Cow,
37-
collections::HashMap,
38-
path::{Path, PathBuf},
39-
vec,
40-
};
35+
use std::{borrow::Cow, collections::HashMap, path::PathBuf, vec};
4136
use tracing::instrument;
4237

4338
// this struct is a mapping to the view `Branch` type in Typescript
@@ -183,28 +178,32 @@ pub fn unapply_ownership(
183178
}
184179

185180
// reset a file in the project to the index state
186-
pub(crate) fn reset_files(ctx: &CommandContext, files: &Vec<String>) -> Result<()> {
181+
pub(crate) fn reset_files(
182+
ctx: &CommandContext,
183+
branch_id: BranchId,
184+
files: &[PathBuf],
185+
perm: &mut WorktreeWritePermission,
186+
) -> Result<()> {
187187
ctx.assure_resolved()?;
188188

189-
// for each tree, we need to checkout the entry from the index at that path
190-
// or if it doesn't exist, remove the file from the working directory
191-
let repo = ctx.repository();
192-
let index = repo.index().context("failed to get index")?;
193-
for file in files {
194-
let entry = index.get_path(Path::new(file), 0);
195-
if entry.is_some() {
196-
repo.checkout_index_path_builder(Path::new(file))
197-
.context("failed to checkout index")?;
198-
} else {
199-
// find the project root
200-
let project_root = &ctx.project().path;
201-
let path = Path::new(file);
202-
//combine the project root with the file path
203-
let path = &project_root.join(path);
204-
std::fs::remove_file(path).context("failed to remove file")?;
205-
}
206-
}
189+
let branch = ctx
190+
.project()
191+
.virtual_branches()
192+
.list_branches_in_workspace()
193+
.context("failed to read virtual branches")?
194+
.into_iter()
195+
.find(|b| b.id == branch_id)
196+
.with_context(|| {
197+
format!("could not find applied branch with id {branch_id} to reset files from")
198+
})?;
199+
let claims: Vec<_> = branch
200+
.ownership
201+
.claims
202+
.into_iter()
203+
.filter(|claim| files.contains(&claim.file_path))
204+
.collect();
207205

206+
unapply_ownership(ctx, &BranchOwnershipClaims { claims }, perm)?;
208207
Ok(())
209208
}
210209
fn find_base_tree<'a>(

crates/gitbutler-tauri/src/virtual_branches.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub mod commands {
1414
use gitbutler_reference::{
1515
normalize_branch_name as normalize_name, ReferenceName, Refname, RemoteRefname,
1616
};
17+
use std::path::PathBuf;
1718
use tauri::State;
1819
use tracing::instrument;
1920

@@ -250,15 +251,11 @@ pub mod commands {
250251
windows: State<'_, WindowState>,
251252
projects: State<'_, projects::Controller>,
252253
project_id: ProjectId,
253-
files: &str,
254+
branch_id: BranchId,
255+
files: Vec<PathBuf>,
254256
) -> Result<(), Error> {
255257
let project = projects.get(project_id)?;
256-
// convert files to Vec<String>
257-
let files = files
258-
.split('\n')
259-
.map(std::string::ToString::to_string)
260-
.collect::<Vec<String>>();
261-
VirtualBranchActions.reset_files(&project, &files)?;
258+
VirtualBranchActions.reset_files(&project, branch_id, &files)?;
262259
emit_vbranches(&windows, project_id);
263260
Ok(())
264261
}

0 commit comments

Comments
 (0)