Skip to content

Commit

Permalink
Merge branch 'master' into pr/2899
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelLaptev committed Feb 26, 2024
2 parents 2949c89 + 8040417 commit 992a988
Show file tree
Hide file tree
Showing 48 changed files with 1,448 additions and 647 deletions.
1 change: 0 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"rust-lang.rust-analyzer",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"ZixuanChen.vitest-explorer",
"EditorConfig.EditorConfig"
]
}
16 changes: 9 additions & 7 deletions gitbutler-app/src/git/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl Default for Options {
pub fn workdir(
repository: &Repository,
commit_oid: &git::Oid,
context_lines: u32,
) -> Result<HashMap<path::PathBuf, Vec<Hunk>>> {
let commit = repository
.find_commit(*commit_oid)
Expand All @@ -72,7 +73,7 @@ pub fn workdir(
.show_binary(true)
.show_untracked_content(true)
.ignore_submodules(true)
.context_lines(0);
.context_lines(context_lines);

let diff = repository.diff_tree_to_workdir(Some(&tree), Some(&mut diff_opts))?;

Expand All @@ -83,14 +84,15 @@ pub fn trees(
repository: &Repository,
old_tree: &git::Tree,
new_tree: &git::Tree,
context_lines: u32,
) -> Result<HashMap<path::PathBuf, Vec<Hunk>>> {
let mut diff_opts = git2::DiffOptions::new();
diff_opts
.recurse_untracked_dirs(true)
.include_untracked(true)
.show_binary(true)
.ignore_submodules(true)
.context_lines(0)
.context_lines(context_lines)
.show_untracked_content(true);

let diff =
Expand Down Expand Up @@ -340,7 +342,7 @@ mod tests {

let head_commit_id = repository.head().unwrap().peel_to_commit().unwrap().id();

let diff = workdir(&repository, &head_commit_id).unwrap();
let diff = workdir(&repository, &head_commit_id, 0).unwrap();
assert_eq!(diff.len(), 1);
assert_eq!(
diff[&path::PathBuf::from("file")],
Expand All @@ -363,7 +365,7 @@ mod tests {

let head_commit_id = repository.head().unwrap().peel_to_commit().unwrap().id();

let diff = workdir(&repository, &head_commit_id).unwrap();
let diff = workdir(&repository, &head_commit_id, 0).unwrap();
assert_eq!(diff.len(), 1);
assert_eq!(
diff[&path::PathBuf::from("first")],
Expand All @@ -387,7 +389,7 @@ mod tests {

let head_commit_id = repository.head().unwrap().peel_to_commit().unwrap().id();

let diff = workdir(&repository, &head_commit_id).unwrap();
let diff = workdir(&repository, &head_commit_id, 0).unwrap();
assert_eq!(diff.len(), 2);
assert_eq!(
diff[&path::PathBuf::from("first")],
Expand Down Expand Up @@ -431,7 +433,7 @@ mod tests {

let head_commit_id = repository.head().unwrap().peel_to_commit().unwrap().id();

let diff = workdir(&repository, &head_commit_id).unwrap();
let diff = workdir(&repository, &head_commit_id, 0).unwrap();
assert_eq!(
diff[&path::PathBuf::from("image")],
vec![Hunk {
Expand Down Expand Up @@ -474,7 +476,7 @@ mod tests {

let head_commit_id = repository.head().unwrap().peel_to_commit().unwrap().id();

let diff = workdir(&repository, &head_commit_id).unwrap();
let diff = workdir(&repository, &head_commit_id, 0).unwrap();
assert_eq!(
diff[&path::PathBuf::from("file")],
vec![Hunk {
Expand Down
4 changes: 4 additions & 0 deletions gitbutler-app/src/projects/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ impl From<controller::AddError> for Error {
code: Code::Projects,
message: "Path not found".to_string(),
},
controller::AddError::SubmodulesNotSupported => Error::UserError {
code: Code::Projects,
message: "Repositories with git submodules are not supported".to_string(),
},
controller::AddError::User(error) => error.into(),
controller::AddError::Other(error) => {
tracing::error!(?error, "failed to add project");
Expand Down
7 changes: 7 additions & 0 deletions gitbutler-app/src/projects/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl Controller {
return Err(AddError::NotAGitRepository);
};

if path.join(".gitmodules").exists() {
return Err(AddError::SubmodulesNotSupported);
}

let id = uuid::Uuid::new_v4().to_string();

// title is the base name of the file
Expand All @@ -79,6 +83,7 @@ impl Controller {
title,
path: path.to_path_buf(),
api: None,
use_diff_context: Some(true),
..Default::default()
};

Expand Down Expand Up @@ -253,6 +258,8 @@ pub enum AddError {
PathNotFound,
#[error("project already exists")]
AlreadyExists,
#[error("submodules not supported")]
SubmodulesNotSupported,
#[error(transparent)]
User(#[from] users::GetError),
#[error(transparent)]
Expand Down
2 changes: 2 additions & 0 deletions gitbutler-app/src/projects/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ pub struct Project {
pub project_data_last_fetch: Option<FetchResult>,
#[serde(default)]
pub omit_certificate_check: Option<bool>,
#[serde(default)]
pub use_diff_context: Option<bool>,
}

impl AsRef<Project> for Project {
Expand Down
5 changes: 5 additions & 0 deletions gitbutler-app/src/projects/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub struct UpdateRequest {
pub gitbutler_code_push_state: Option<project::CodePushState>,
pub project_data_last_fetched: Option<project::FetchResult>,
pub omit_certificate_check: Option<bool>,
pub use_diff_context: Option<bool>,
}

#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -139,6 +140,10 @@ impl Storage {
project.omit_certificate_check = Some(omit_certificate_check);
}

if let Some(use_diff_context) = update_request.use_diff_context {
project.use_diff_context = Some(use_diff_context);
}

self.storage
.write(PROJECTS_FILE, &serde_json::to_string_pretty(&projects)?)?;

Expand Down
14 changes: 13 additions & 1 deletion gitbutler-app/src/virtual_branches/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ pub fn set_base_branch(
// if there are any commits on the head branch or uncommitted changes in the working directory, we need to
// put them into a virtual branch

let wd_diff = diff::workdir(repo, &current_head_commit.id())?;
let use_context = project_repository
.project()
.use_diff_context
.unwrap_or(false);
let context_lines = if use_context { 3_u32 } else { 0_u32 };
let wd_diff = diff::workdir(repo, &current_head_commit.id(), context_lines)?;
if !wd_diff.is_empty() || current_head_commit.id() != target.sha {
let hunks_by_filepath =
super::virtual_hunks_by_filepath(&project_repository.project().path, &wd_diff);
Expand Down Expand Up @@ -308,6 +313,12 @@ pub fn update_base_branch(
let branch_writer =
branch::Writer::new(gb_repository).context("failed to create branch writer")?;

let use_context = project_repository
.project()
.use_diff_context
.unwrap_or(false);
let context_lines = if use_context { 3_u32 } else { 0_u32 };

// try to update every branch
let updated_vbranches = super::get_status_by_branch(gb_repository, project_repository)?
.into_iter()
Expand Down Expand Up @@ -341,6 +352,7 @@ pub fn update_base_branch(
&project_repository.git_repository,
&branch_head_tree,
&branch_tree,
context_lines,
)?;
if non_commited_files.is_empty() {
// if there are no commited files, then the branch is fully merged
Expand Down
16 changes: 14 additions & 2 deletions gitbutler-app/src/virtual_branches/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
};

use super::{
branch::BranchId,
branch::{self, BranchId},
controller::{Controller, ControllerError},
BaseBranch, RemoteBranchFile,
};
Expand Down Expand Up @@ -80,11 +80,23 @@ pub async fn list_virtual_branches(
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let branches = handle
let (branches, uses_diff_context) = handle
.state::<Controller>()
.list_virtual_branches(&project_id)
.await?;

// Migration: If use_diff_context is not already set and if there are no vbranches, set use_diff_context to true
if !uses_diff_context && branches.is_empty() {
let _ = handle
.state::<projects::Controller>()
.update(&projects::UpdateRequest {
id: project_id,
use_diff_context: Some(true),
..Default::default()
})
.await;
}

let proxy = handle.state::<assets::Proxy>();
let branches = proxy.proxy_virtual_branches(branches).await;
Ok(branches)
Expand Down
20 changes: 15 additions & 5 deletions gitbutler-app/src/virtual_branches/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ impl Controller {
pub async fn list_virtual_branches(
&self,
project_id: &ProjectId,
) -> Result<Vec<super::VirtualBranch>, ControllerError<errors::ListVirtualBranchesError>> {
) -> Result<(Vec<super::VirtualBranch>, bool), ControllerError<errors::ListVirtualBranchesError>>
{
self.inner(project_id)
.await
.list_virtual_branches(project_id)
Expand Down Expand Up @@ -493,7 +494,8 @@ impl ControllerInner {
pub async fn list_virtual_branches(
&self,
project_id: &ProjectId,
) -> Result<Vec<super::VirtualBranch>, ControllerError<errors::ListVirtualBranchesError>> {
) -> Result<(Vec<super::VirtualBranch>, bool), ControllerError<errors::ListVirtualBranchesError>>
{
let _permit = self.semaphore.acquire().await;

self.with_verify_branch(project_id, |gb_repository, project_repository, _| {
Expand Down Expand Up @@ -570,9 +572,17 @@ impl ControllerInner {
) -> Result<Vec<RemoteBranchFile>, Error> {
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::open(&project)?;

super::list_remote_commit_files(&project_repository.git_repository, commit_oid)
.map_err(Into::into)
let use_context = project_repository
.project()
.use_diff_context
.unwrap_or(false);
let context_lines = if use_context { 3_u32 } else { 0_u32 };
super::list_remote_commit_files(
&project_repository.git_repository,
commit_oid,
context_lines,
)
.map_err(Into::into)
}

pub fn set_base_branch(
Expand Down
3 changes: 2 additions & 1 deletion gitbutler-app/src/virtual_branches/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct RemoteBranchFile {
pub fn list_remote_commit_files(
repository: &git::Repository,
commit_oid: git::Oid,
context_lines: u32,
) -> Result<Vec<RemoteBranchFile>, errors::ListRemoteCommitFilesError> {
let commit = match repository.find_commit(commit_oid) {
Ok(commit) => Ok(commit),
Expand All @@ -35,7 +36,7 @@ pub fn list_remote_commit_files(
let parent = commit.parent(0).context("failed to get parent commit")?;
let commit_tree = commit.tree().context("failed to get commit tree")?;
let parent_tree = parent.tree().context("failed to get parent tree")?;
let diff = diff::trees(repository, &parent_tree, &commit_tree)?;
let diff = diff::trees(repository, &parent_tree, &commit_tree, context_lines)?;

let files = diff
.into_iter()
Expand Down
Loading

0 comments on commit 992a988

Please sign in to comment.