Skip to content

Commit c81b140

Browse files
committed
Assure the most-recent workspace head is used when emitting changed files.
The reason for this is that now the outcome of this can be re-used in `list_virtual_branches`, which is sensitive to seeing the latest vbranch state which is not a given. Calculating it makes sure there is no mismatch, and probably "can't be wrong". Also note that `head_commit()` is still kept around as it's the idea to eventually use it more.
1 parent 9234939 commit c81b140

File tree

6 files changed

+13
-10
lines changed

6 files changed

+13
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ pub(crate) fn set_base_branch(
181181
// if there are any commits on the head branch or uncommitted changes in the working directory, we need to
182182
// put them into a virtual branch
183183

184-
let wd_diff = gitbutler_diff::workdir(repo, &current_head_commit.id())?;
184+
let wd_diff = gitbutler_diff::workdir(repo, current_head_commit.id())?;
185185
if !wd_diff.is_empty() || current_head_commit.id() != target.sha {
186186
// assign ownership to the branch
187187
let ownership = wd_diff.iter().fold(

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::integration::get_workspace_head;
12
use crate::{RemoteBranchFile, VirtualBranchesExt};
23
use anyhow::{bail, Context, Result};
34
use bstr::{BStr, ByteSlice};
@@ -9,7 +10,7 @@ use gitbutler_command_context::CommandContext;
910
use gitbutler_diff::DiffByPathMap;
1011
use gitbutler_project::access::WorktreeReadPermission;
1112
use gitbutler_reference::normalize_branch_name;
12-
use gitbutler_repo::{GixRepositoryExt, RepositoryExt};
13+
use gitbutler_repo::GixRepositoryExt;
1314
use gitbutler_serde::BStringForFrontend;
1415
use gix::object::tree::diff::Action;
1516
use gix::prelude::ObjectIdExt;
@@ -25,12 +26,10 @@ use std::{
2526
};
2627

2728
pub(crate) fn get_uncommited_files_raw(
28-
context: &CommandContext,
29+
ctx: &CommandContext,
2930
_permission: &WorktreeReadPermission,
3031
) -> Result<DiffByPathMap> {
31-
let repository = context.repository();
32-
let head_commit = repository.head_commit()?;
33-
gitbutler_diff::workdir(repository, &head_commit.id())
32+
gitbutler_diff::workdir(ctx.repository(), get_workspace_head(ctx)?)
3433
.context("Failed to list uncommited files")
3534
}
3635

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub fn get_applied_status_cached(
5656
// any of its inputs will update the intragration commit right away.
5757
// It's for another day though - right now the integration commit may be slightly stale.
5858
let integration_commit_id = get_workspace_head(ctx)?;
59-
gitbutler_diff::workdir(ctx.repository(), &integration_commit_id.to_owned())
59+
gitbutler_diff::workdir(ctx.repository(), integration_commit_id.to_owned())
6060
.context("failed to diff workdir")
6161
})?;
6262

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ pub(crate) fn move_commit_file(
12151215
let mut upstream_commits = ctx.l(target_branch.head, LogUntil::Commit(amend_commit.id()))?;
12161216

12171217
// get a list of all the diffs across all the virtual branches
1218-
let base_file_diffs = gitbutler_diff::workdir(ctx.repository(), &default_target.sha)
1218+
let base_file_diffs = gitbutler_diff::workdir(ctx.repository(), default_target.sha)
12191219
.context("failed to diff workdir")?;
12201220

12211221
// filter base_file_diffs to HashMap<filepath, Vec<GitHunk>> only for hunks in target_ownership

crates/gitbutler-diff/src/diff.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ pub struct FileDiff {
123123
}
124124

125125
#[instrument(level = tracing::Level::DEBUG, skip(repo))]
126-
pub fn workdir(repo: &git2::Repository, commit_oid: &git2::Oid) -> Result<DiffByPathMap> {
126+
pub fn workdir(repo: &git2::Repository, commit_oid: git2::Oid) -> Result<DiffByPathMap> {
127127
let commit = repo
128-
.find_commit(*commit_oid)
128+
.find_commit(commit_oid)
129129
.context("failed to find commit")?;
130130
let old_tree = repo.find_real_tree(&commit, Default::default())?;
131131

crates/gitbutler-repo/src/repository_ext.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ use tracing::instrument;
1717
///
1818
/// For now, it collects useful methods from `gitbutler-core::git::Repository`
1919
pub trait RepositoryExt {
20+
/// Return `HEAD^{commit}` - ideal for obtaining the integration branch commit in open-workspace mode
21+
/// when it's clear that it's representing the current state.
22+
///
23+
/// Ideally, this is used in places of `get_workspace_head()`.
2024
fn head_commit(&self) -> Result<git2::Commit<'_>>;
2125
fn remote_branches(&self) -> Result<Vec<RemoteRefname>>;
2226
fn remotes_as_string(&self) -> Result<Vec<String>>;

0 commit comments

Comments
 (0)