mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2025-01-04 15:53:30 +03:00
hunk context lines are computed against the correct head
This commit is contained in:
parent
3e37ffc095
commit
29b5499869
@ -4,23 +4,6 @@ use std::{path, str};
|
|||||||
|
|
||||||
use super::Result;
|
use super::Result;
|
||||||
|
|
||||||
pub fn show_file_at_head<P: AsRef<path::Path>>(
|
|
||||||
repository: &Repository,
|
|
||||||
file_path: P,
|
|
||||||
) -> Result<String> {
|
|
||||||
let file_path = file_path.as_ref();
|
|
||||||
let head_tree = repository.head()?.peel_to_commit()?.tree()?;
|
|
||||||
match head_tree.get_path(file_path) {
|
|
||||||
Ok(tree_entry) => {
|
|
||||||
let blob = repository.find_blob(tree_entry.id())?;
|
|
||||||
let content = str::from_utf8(blob.content())?;
|
|
||||||
Ok(content.to_string())
|
|
||||||
}
|
|
||||||
// If a file is new, it's content at HEAD is the empty string
|
|
||||||
Err(_) => Ok(String::new()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn show_file_at_tree<P: AsRef<path::Path>>(
|
pub fn show_file_at_tree<P: AsRef<path::Path>>(
|
||||||
repository: &Repository,
|
repository: &Repository,
|
||||||
file_path: P,
|
file_path: P,
|
||||||
|
@ -20,7 +20,9 @@ use crate::{
|
|||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
branch::{self, Branch, BranchCreateRequest, BranchId, FileOwnership, Hunk, Ownership},
|
branch::{self, Branch, BranchCreateRequest, BranchId, FileOwnership, Hunk, Ownership},
|
||||||
branch_to_remote_branch, context, errors, target, Iterator, RemoteBranch,
|
branch_to_remote_branch, context, errors,
|
||||||
|
target::{self, Target},
|
||||||
|
Iterator, RemoteBranch,
|
||||||
};
|
};
|
||||||
|
|
||||||
type AppliedStatuses = Vec<(branch::Branch, HashMap<path::PathBuf, Vec<diff::Hunk>>)>;
|
type AppliedStatuses = Vec<(branch::Branch, HashMap<path::PathBuf, Vec<diff::Hunk>>)>;
|
||||||
@ -57,6 +59,7 @@ pub struct VirtualBranch {
|
|||||||
pub ownership: Ownership,
|
pub ownership: Ownership,
|
||||||
pub updated_at: u128,
|
pub updated_at: u128,
|
||||||
pub selected_for_changes: bool,
|
pub selected_for_changes: bool,
|
||||||
|
pub head: git::Oid,
|
||||||
}
|
}
|
||||||
|
|
||||||
// this is the struct that maps to the view `Commit` type in Typescript
|
// this is the struct that maps to the view `Commit` type in Typescript
|
||||||
@ -874,15 +877,20 @@ pub fn list_virtual_branches(
|
|||||||
ownership: branch.ownership.clone(),
|
ownership: branch.ownership.clone(),
|
||||||
updated_at: branch.updated_timestamp_ms,
|
updated_at: branch.updated_timestamp_ms,
|
||||||
selected_for_changes: branch.selected_for_changes == Some(max_selected_for_changes),
|
selected_for_changes: branch.selected_for_changes == Some(max_selected_for_changes),
|
||||||
|
head: branch.head,
|
||||||
};
|
};
|
||||||
branches.push(branch);
|
branches.push(branch);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut branches = branches_with_hunk_locks(branches, project_repository)?;
|
let mut branches = branches_with_hunk_locks(branches, project_repository)?;
|
||||||
for branch in &mut branches {
|
for branch in &mut branches {
|
||||||
branch.files =
|
branch.files = files_with_hunk_context(
|
||||||
files_with_hunk_context(&project_repository.git_repository, branch.files.clone(), 3)
|
&project_repository.git_repository,
|
||||||
.context("failed to add hunk context")?;
|
branch.files.clone(),
|
||||||
|
3,
|
||||||
|
branch.head,
|
||||||
|
)
|
||||||
|
.context("failed to add hunk context")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
branches.sort_by(|a, b| a.order.cmp(&b.order));
|
branches.sort_by(|a, b| a.order.cmp(&b.order));
|
||||||
@ -947,14 +955,18 @@ fn files_with_hunk_context(
|
|||||||
repository: &git::Repository,
|
repository: &git::Repository,
|
||||||
mut files: Vec<VirtualBranchFile>,
|
mut files: Vec<VirtualBranchFile>,
|
||||||
context_lines: usize,
|
context_lines: usize,
|
||||||
|
branch_head: git::Oid,
|
||||||
) -> Result<Vec<VirtualBranchFile>> {
|
) -> Result<Vec<VirtualBranchFile>> {
|
||||||
for file in &mut files {
|
for file in &mut files {
|
||||||
if file.binary {
|
if file.binary {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Get file content as it looked before the diffs
|
// Get file content as it looked before the diffs
|
||||||
let file_content_before = show::show_file_at_head(repository, file.path.clone())
|
let branch_head_commit = repository.find_commit(branch_head)?;
|
||||||
.context("failed to get file contents at HEAD")?;
|
let head_tree = branch_head_commit.tree()?;
|
||||||
|
let file_content_before =
|
||||||
|
show::show_file_at_tree(repository, file.path.clone(), &head_tree)
|
||||||
|
.context("failed to get file contents at base")?;
|
||||||
let file_lines_before = file_content_before.split('\n').collect::<Vec<_>>();
|
let file_lines_before = file_content_before.split('\n').collect::<Vec<_>>();
|
||||||
|
|
||||||
// Update each hunk with contex lines before & after
|
// Update each hunk with contex lines before & after
|
||||||
|
Loading…
Reference in New Issue
Block a user