Skip to content

Commit

Permalink
Make it easier to pass diffs to list vbranch function
Browse files Browse the repository at this point in the history
- cached function should not accept option
- pass reference to diffs rather than consuming them
  • Loading branch information
mtsgrd committed Jan 6, 2025
1 parent 96382f2 commit 1ecc9b0
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 33 deletions.
4 changes: 2 additions & 2 deletions crates/gitbutler-branch-actions/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn list_virtual_branches(project: &Project) -> Result<StackListResult> {

pub fn list_virtual_branches_cached(
project: &Project,
worktree_changes: Option<DiffByPathMap>,
worktree_changes: DiffByPathMap,
) -> Result<StackListResult> {
let ctx = open_with_verify(project)?;

Expand All @@ -89,7 +89,7 @@ pub fn list_virtual_branches_cached(
vbranch::list_virtual_branches_cached(
&ctx,
project.exclusive_worktree_access().write_permission(),
worktree_changes,
&worktree_changes,
)
.map_err(Into::into)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/gitbutler-branch-actions/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pub(crate) fn set_base_branch(
gitbutler_diff::write::hunks_onto_commit(
ctx,
current_head_commit.id(),
gitbutler_diff::diff_files_into_hunks(wd_diff),
gitbutler_diff::diff_files_into_hunks(&wd_diff),
)?,
current_head_commit.id(),
0,
Expand Down
2 changes: 1 addition & 1 deletion crates/gitbutler-branch-actions/src/move_commits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn get_source_branch_diffs(
&uncommitted_changes_tree,
true,
)
.map(|diff| gitbutler_diff::diff_files_into_hunks(diff).collect::<HashMap<_, _>>())?;
.map(|diff| gitbutler_diff::diff_files_into_hunks(&diff).collect::<HashMap<_, _>>())?;

Ok(uncommitted_changes_diff)
}
Expand Down
14 changes: 5 additions & 9 deletions crates/gitbutler-branch-actions/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ pub fn get_applied_status(
ctx: &CommandContext,
perm: Option<&mut WorktreeWritePermission>,
) -> Result<VirtualBranchesStatus> {
get_applied_status_cached(ctx, perm, None)
let diffs = gitbutler_diff::workdir(ctx.repo(), get_workspace_head(ctx)?)?;
get_applied_status_cached(ctx, perm, &diffs)
}

/// Returns branches and their associated file changes, in addition to a list
Expand All @@ -47,26 +48,21 @@ pub fn get_applied_status(
pub fn get_applied_status_cached(
ctx: &CommandContext,
perm: Option<&mut WorktreeWritePermission>,
worktree_changes: Option<gitbutler_diff::DiffByPathMap>,
worktree_changes: &gitbutler_diff::DiffByPathMap,
) -> Result<VirtualBranchesStatus> {
assure_open_workspace_mode(ctx).context("ng applied status requires open workspace mode")?;
let workspace_head = get_workspace_head(ctx)?;
let mut virtual_branches = ctx
.project()
.virtual_branches()
.list_stacks_in_workspace()?;
let base_file_diffs = worktree_changes.map(Ok).unwrap_or_else(|| {
gitbutler_diff::workdir(ctx.repo(), workspace_head.to_owned())
.context("failed to diff workdir")
})?;

let mut skipped_files: Vec<gitbutler_diff::FileDiff> = Vec::new();
for file_diff in base_file_diffs.values() {
for file_diff in worktree_changes.values() {
if file_diff.skipped {
skipped_files.push(file_diff.clone());
}
}
let mut base_diffs: HashMap<_, _> = diff_files_into_hunks(base_file_diffs).collect();
let mut base_diffs: HashMap<_, _> = diff_files_into_hunks(worktree_changes).collect();

// sort by order, so that the default branch is first (left in the ui)
virtual_branches.sort_by(|a, b| a.order.cmp(&b.order));
Expand Down
8 changes: 3 additions & 5 deletions crates/gitbutler-branch-actions/src/undo_commit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashMap;

use anyhow::{bail, Context as _, Result};
use gitbutler_command_context::CommandContext;
use gitbutler_commit::commit_ext::CommitExt as _;
Expand Down Expand Up @@ -76,12 +74,12 @@ fn inner_undo_commit(
.context("failed to get parent tree")?;

let diff = gitbutler_diff::trees(repository, &commit_parent_tree, &commit_tree, true)?;
let diff: HashMap<_, _> = gitbutler_diff::diff_files_into_hunks(diff).collect();
let ownership_update = diff
.iter()
.filter_map(|(file_path, hunks)| {
.filter_map(|(file_path, file_diff)| {
let file_path = file_path.clone();
let hunks = hunks
let hunks = file_diff
.hunks
.iter()
.map(Into::into)
.filter(|hunk: &Hunk| hunk.start != 0 && hunk.end != 0)
Expand Down
26 changes: 15 additions & 11 deletions crates/gitbutler-branch-actions/src/virtual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ pub fn list_virtual_branches(
ctx: &CommandContext,
perm: &mut WorktreeWritePermission,
) -> Result<StackListResult> {
list_virtual_branches_cached(ctx, perm, None)
let diffs = gitbutler_diff::workdir(ctx.repo(), get_workspace_head(ctx)?)?;
list_virtual_branches_cached(ctx, perm, &diffs)
}

/// `worktree_changes` are all changed files against the current `HEAD^{tree}` and index
Expand All @@ -321,7 +322,7 @@ pub fn list_virtual_branches_cached(
// TODO(ST): this should really only shared access, but there is some internals
// that conditionally write things.
perm: &mut WorktreeWritePermission,
worktree_changes: Option<gitbutler_diff::DiffByPathMap>,
worktree_changes: &gitbutler_diff::DiffByPathMap,
) -> Result<StackListResult> {
assure_open_workspace_mode(ctx)
.context("Listing virtual branches requires open workspace mode")?;
Expand Down Expand Up @@ -349,7 +350,7 @@ pub fn list_virtual_branches_cached(
let cache = gix_repo.commit_graph_if_enabled()?;
let mut graph = gix_repo.revision_graph(cache.as_ref());
for (mut branch, mut files) in status.branches {
update_conflict_markers(ctx, files.clone())?;
update_conflict_markers(ctx, worktree_changes)?;

let upstream_branch = match &branch.upstream {
Some(upstream) => repo.maybe_find_branch_by_refname(&Refname::from(upstream))?,
Expand Down Expand Up @@ -770,7 +771,8 @@ pub fn commit(
let message = &message_buffer;

// get the files to commit
let statuses = get_applied_status(ctx, None)
let diffs = gitbutler_diff::workdir(ctx.repo(), get_workspace_head(ctx)?)?;
let statuses = get_applied_status_cached(ctx, None, &diffs)
.context("failed to get status by branch")?
.branches;

Expand All @@ -779,7 +781,7 @@ pub fn commit(
.find(|(stack, _)| stack.id == stack_id)
.with_context(|| format!("stack {stack_id} not found"))?;

update_conflict_markers(ctx, files.clone()).context(Code::CommitMergeConflictFailure)?;
update_conflict_markers(ctx, &diffs).context(Code::CommitMergeConflictFailure)?;

ctx.assure_unconflicted()
.context(Code::CommitMergeConflictFailure)?;
Expand Down Expand Up @@ -1713,14 +1715,16 @@ pub(crate) fn update_commit_message(
// Goes through a set of changes and checks if conflicts are present. If no conflicts
// are present in a file it will be resolved, meaning it will be removed from the
// conflicts file.
fn update_conflict_markers(ctx: &CommandContext, files: Vec<VirtualBranchFile>) -> Result<()> {
fn update_conflict_markers(
ctx: &CommandContext,
files: &gitbutler_diff::DiffByPathMap,
) -> Result<()> {
let conflicting_files = conflicts::conflicting_files(ctx)?;
for file in files {
for (path, file_diff) in files {
let mut conflicted = false;
if conflicting_files.contains(&file.path) {
if conflicting_files.contains(path) {
// check file for conflict markers, resolve the file if there are none in any hunk
for hunk in file.hunks {
let hunk: GitHunk = hunk.clone().into();
for hunk in &file_diff.hunks {
if hunk.diff_lines.contains_str(b"<<<<<<< ours") {
conflicted = true;
}
Expand All @@ -1729,7 +1733,7 @@ fn update_conflict_markers(ctx: &CommandContext, files: Vec<VirtualBranchFile>)
}
}
if !conflicted {
conflicts::resolve(ctx, file.path).unwrap();
conflicts::resolve(ctx, path).unwrap();
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions crates/gitbutler-diff/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,9 +656,11 @@ pub fn reverse_hunk_lines(
}

pub fn diff_files_into_hunks(
files: DiffByPathMap,
) -> impl Iterator<Item = (PathBuf, Vec<GitHunk>)> {
files.into_iter().map(|(path, file)| (path, file.hunks))
files: &DiffByPathMap,
) -> impl Iterator<Item = (PathBuf, Vec<GitHunk>)> + '_ {
files
.iter()
.map(|(path, file)| (path.clone(), file.hunks.clone()))
}

#[cfg(test)]
Expand Down
7 changes: 6 additions & 1 deletion crates/gitbutler-watcher/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ impl Handler {
.projects
.get(project_id)
.context("failed to get project")?;
match gitbutler_branch_actions::list_virtual_branches_cached(&project, worktree_changes) {
let virtual_branches = if let Some(changes) = worktree_changes {
gitbutler_branch_actions::list_virtual_branches_cached(&project, changes)
} else {
gitbutler_branch_actions::list_virtual_branches(&project)
};
match virtual_branches {
Ok(StackListResult {
branches,
skipped_files,
Expand Down

0 comments on commit 1ecc9b0

Please sign in to comment.