From 207e90413f3087cd77ce1d1014eb18b90b008795 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 1 Jan 2024 16:01:39 +0000 Subject: [PATCH 01/12] chore(deps): bump serde_json from 1.0.108 to 1.0.109 Bumps [serde_json](https://github.com/serde-rs/json) from 1.0.108 to 1.0.109. - [Release notes](https://github.com/serde-rs/json/releases) - [Commits](https://github.com/serde-rs/json/compare/v1.0.108...v1.0.109) --- updated-dependencies: - dependency-name: serde_json dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- Cargo.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e60e64690..dbde17d29 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4472,9 +4472,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.108" +version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" +checksum = "cb0652c533506ad7a2e353cce269330d6afd8bdfb6d75e0ace5b35aacbd7b9e9" dependencies = [ "itoa 1.0.9", "ryu", From fa60e297043e4bbbff622d5741a47369100e445a Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 1 Jan 2024 16:02:22 +0000 Subject: [PATCH 02/12] chore(deps): bump tauri-build from 1.5.0 to 1.5.1 Bumps [tauri-build](https://github.com/tauri-apps/tauri) from 1.5.0 to 1.5.1. - [Release notes](https://github.com/tauri-apps/tauri/releases) - [Commits](https://github.com/tauri-apps/tauri/compare/tauri-build-v1.5...tauri-build-v1.5.1) --- updated-dependencies: - dependency-name: tauri-build dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- Cargo.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e60e64690..877b8622e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5084,9 +5084,9 @@ dependencies = [ [[package]] name = "tauri-build" -version = "1.5.0" +version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "defbfc551bd38ab997e5f8e458f87396d2559d05ce32095076ad6c30f7fc5f9c" +checksum = "e9914a4715e0b75d9f387a285c7e26b5bbfeb1249ad9f842675a82481565c532" dependencies = [ "anyhow", "cargo_toml", From fcd311f70fa946562f3cbc30b4ba35254479ec63 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 1 Jan 2024 16:02:58 +0000 Subject: [PATCH 03/12] chore(deps): bump bstr from 1.8.0 to 1.9.0 Bumps [bstr](https://github.com/BurntSushi/bstr) from 1.8.0 to 1.9.0. - [Commits](https://github.com/BurntSushi/bstr/compare/1.8.0...1.9.0) --- updated-dependencies: - dependency-name: bstr dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- Cargo.lock | 12 ++++++------ gitbutler-app/Cargo.toml | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e60e64690..8f898a851 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -460,9 +460,9 @@ dependencies = [ [[package]] name = "bstr" -version = "1.8.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "542f33a8835a0884b006a0c3df3dadd99c0c3f296ed26c2fdc8028e01ad6230c" +checksum = "c48f0051a4b4c5e0b6d365cd04af53aeaa209e3cc15ec2cdb69e73cc87fbd0dc" dependencies = [ "memchr", "regex-automata 0.4.3", @@ -1782,7 +1782,7 @@ dependencies = [ "async-trait", "backoff", "backtrace", - "bstr 1.8.0", + "bstr 1.9.0", "byteorder", "chrono", "console-subscriber", @@ -1906,7 +1906,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "759c97c1e17c55525b57192c06a267cda0ac5210b222d6b82189a2338fa1c13d" dependencies = [ "aho-corasick", - "bstr 1.8.0", + "bstr 1.9.0", "fnv", "log", "regex", @@ -2782,9 +2782,9 @@ checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771" [[package]] name = "memchr" -version = "2.6.3" +version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" +checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" [[package]] name = "memoffset" diff --git a/gitbutler-app/Cargo.toml b/gitbutler-app/Cargo.toml index d53130cd3..ffbe03b28 100644 --- a/gitbutler-app/Cargo.toml +++ b/gitbutler-app/Cargo.toml @@ -25,7 +25,7 @@ anyhow = "1.0.72" async-trait = "0.1.74" backoff = "0.4.0" backtrace = { version = "0.3.69", optional = true } -bstr = "1.8.0" +bstr = "1.9.0" byteorder = "1.5.0" chrono = { version = "0.4.31", features = ["serde"] } console-subscriber = "0.2.0" From 6503a15309bbe413de3addd6328567b881a82424 Mon Sep 17 00:00:00 2001 From: Nikita Galaiko Date: Mon, 8 Jan 2024 10:00:38 +0100 Subject: [PATCH 04/12] refactor: with_reader for readers, open for writers --- gitbutler-app/src/deltas/reader.rs | 2 +- gitbutler-app/src/deltas/writer.rs | 6 +- gitbutler-app/src/gb_repository/repository.rs | 16 ++--- .../src/gb_repository/repository_tests.rs | 12 ++-- gitbutler-app/src/sessions/tests.rs | 6 +- gitbutler-app/src/sessions/writer.rs | 2 +- gitbutler-app/src/virtual_branches/base.rs | 8 +-- .../src/virtual_branches/branch/reader.rs | 8 +-- .../src/virtual_branches/branch/writer.rs | 8 +-- .../src/virtual_branches/controller.rs | 2 +- .../src/virtual_branches/integration.rs | 2 +- .../src/virtual_branches/iterator.rs | 6 +- .../src/virtual_branches/target/reader.rs | 12 ++-- .../src/virtual_branches/target/writer.rs | 10 +-- gitbutler-app/src/virtual_branches/tests.rs | 24 +++---- gitbutler-app/src/virtual_branches/virtual.rs | 62 +++++++++---------- .../handlers/calculate_deltas_handler.rs | 36 +++++------ .../src/watcher/handlers/index_handler.rs | 2 +- 18 files changed, 112 insertions(+), 112 deletions(-) diff --git a/gitbutler-app/src/deltas/reader.rs b/gitbutler-app/src/deltas/reader.rs index e473dfc53..208eebb70 100644 --- a/gitbutler-app/src/deltas/reader.rs +++ b/gitbutler-app/src/deltas/reader.rs @@ -11,7 +11,7 @@ pub struct DeltasReader<'reader> { } impl<'reader> DeltasReader<'reader> { - pub fn new(reader: &'reader dyn reader::Reader) -> Self { + pub fn with_reader(reader: &'reader dyn reader::Reader) -> Self { DeltasReader { reader } } diff --git a/gitbutler-app/src/deltas/writer.rs b/gitbutler-app/src/deltas/writer.rs index 52ebe76bf..f0560a5d2 100644 --- a/gitbutler-app/src/deltas/writer.rs +++ b/gitbutler-app/src/deltas/writer.rs @@ -13,7 +13,7 @@ pub struct DeltasWriter<'writer> { } impl<'writer> DeltasWriter<'writer> { - pub fn new(repository: &'writer gb_repository::Repository) -> Self { + pub fn open(repository: &'writer gb_repository::Repository) -> Self { let writer = writer::DirWriter::open(repository.root()); Self { writer, repository } } @@ -90,11 +90,11 @@ mod tests { fn write_no_vbranches() -> Result<()> { let Case { gb_repository, .. } = Suite::default().new_case(); - let deltas_writer = DeltasWriter::new(&gb_repository); + let deltas_writer = DeltasWriter::open(&gb_repository); let session = gb_repository.get_or_create_current_session()?; let session_reader = sessions::Reader::open(&gb_repository, &session)?; - let deltas_reader = deltas::Reader::new(&session_reader); + let deltas_reader = deltas::Reader::with_reader(&session_reader); let path = "test.txt"; let deltas = vec![ diff --git a/gitbutler-app/src/gb_repository/repository.rs b/gitbutler-app/src/gb_repository/repository.rs index 1f1408517..4a662b879 100644 --- a/gitbutler-app/src/gb_repository/repository.rs +++ b/gitbutler-app/src/gb_repository/repository.rs @@ -261,8 +261,8 @@ impl Repository { .into_iter() .collect::>(); - let src_target_reader = virtual_branches::target::Reader::new(&last_session_reader); - let dst_target_writer = virtual_branches::target::Writer::new(self); + let src_target_reader = virtual_branches::target::Reader::with_reader(&last_session_reader); + let dst_target_writer = virtual_branches::target::Writer::open(self); // copy default target let default_target = match src_target_reader.read_default() { @@ -291,7 +291,7 @@ impl Repository { .with_context(|| format!("{}: failed to write target", branch.id))?; } - let dst_branch_writer = virtual_branches::branch::Writer::new(self); + let dst_branch_writer = virtual_branches::branch::Writer::open(self); // copy branches that we don't already have for branch in &branches { @@ -334,7 +334,7 @@ impl Repository { }; // write session to disk - sessions::Writer::new(self) + sessions::Writer::open(self) .write(&session) .context("failed to write session")?; @@ -371,7 +371,7 @@ impl Repository { ..current_session }; - sessions::Writer::new(self) + sessions::Writer::open(self) .write(&updated_session) .context("failed to write session")?; @@ -434,7 +434,7 @@ impl Repository { let _lock = self.lock(); // update last timestamp - sessions::Writer::new(self).write(session)?; + sessions::Writer::open(self).write(session)?; let mut tree_builder = self.git_repository.treebuilder(None); @@ -510,7 +510,7 @@ impl Repository { .context("failed to get current session")?; let current_session_reader = sessions::Reader::open(self, ¤t_session) .context("failed to open current session")?; - let target_reader = target::Reader::new(¤t_session_reader); + let target_reader = target::Reader::with_reader(¤t_session_reader); match target_reader.read_default() { Result::Ok(target) => Ok(Some(target)), Err(reader::Error::NotFound) => Ok(None), @@ -589,7 +589,7 @@ fn build_wd_tree_from_reference( } let session_reader = reader::DirReader::open(gb_repository.root()); - let deltas = deltas::Reader::new(&session_reader) + let deltas = deltas::Reader::with_reader(&session_reader) .read(None) .context("failed to read deltas")?; let wd_files = session_reader.list_files(path::Path::new("session/wd"))?; diff --git a/gitbutler-app/src/gb_repository/repository_tests.rs b/gitbutler-app/src/gb_repository/repository_tests.rs index c48258af4..741ea03b6 100644 --- a/gitbutler-app/src/gb_repository/repository_tests.rs +++ b/gitbutler-app/src/gb_repository/repository_tests.rs @@ -96,7 +96,7 @@ fn test_list_deltas_from_current_session() -> Result<()> { let Case { gb_repository, .. } = Suite::default().new_case(); let current_session = gb_repository.get_or_create_current_session()?; - let writer = deltas::Writer::new(&gb_repository); + let writer = deltas::Writer::open(&gb_repository); writer.write( "test.txt", &vec![deltas::Delta { @@ -106,7 +106,7 @@ fn test_list_deltas_from_current_session() -> Result<()> { )?; let session_reader = sessions::Reader::open(&gb_repository, ¤t_session)?; - let deltas_reader = deltas::Reader::new(&session_reader); + let deltas_reader = deltas::Reader::with_reader(&session_reader); let deltas = deltas_reader.read(None)?; assert_eq!(deltas.len(), 1); @@ -130,7 +130,7 @@ fn test_list_deltas_from_flushed_session() -> Result<()> { .. } = Suite::default().new_case(); - let writer = deltas::Writer::new(&gb_repository); + let writer = deltas::Writer::open(&gb_repository); writer.write( "test.txt", &vec![deltas::Delta { @@ -141,7 +141,7 @@ fn test_list_deltas_from_flushed_session() -> Result<()> { let session = gb_repository.flush(&project_repository, None)?; let session_reader = sessions::Reader::open(&gb_repository, &session.unwrap())?; - let deltas_reader = deltas::Reader::new(&session_reader); + let deltas_reader = deltas::Reader::with_reader(&session_reader); let deltas = deltas_reader.read(None)?; assert_eq!(deltas.len(), 1); @@ -235,7 +235,7 @@ async fn test_remote_syncronization() -> Result<()> { .await?; let case_one = case_one.refresh(); - let writer = deltas::Writer::new(&case_one.gb_repository); + let writer = deltas::Writer::open(&case_one.gb_repository); writer.write( "test.txt", &vec![deltas::Delta { @@ -273,7 +273,7 @@ async fn test_remote_syncronization() -> Result<()> { assert_eq!(sessions_two[0].id, session_one.id); let session_reader = sessions::Reader::open(&case_two.gb_repository, &sessions_two[0])?; - let deltas_reader = deltas::Reader::new(&session_reader); + let deltas_reader = deltas::Reader::with_reader(&session_reader); let deltas = deltas_reader.read(None)?; let files = session_reader.files(None)?; assert_eq!(deltas.len(), 1); diff --git a/gitbutler-app/src/sessions/tests.rs b/gitbutler-app/src/sessions/tests.rs index 72788e3b1..e3c2720bf 100644 --- a/gitbutler-app/src/sessions/tests.rs +++ b/gitbutler-app/src/sessions/tests.rs @@ -22,7 +22,7 @@ fn test_should_not_write_session_with_hash() { }, }; - assert!(Writer::new(&gb_repository).write(&session).is_err()); + assert!(Writer::open(&gb_repository).write(&session).is_err()); } #[test] @@ -40,7 +40,7 @@ fn test_should_write_full_session() -> Result<()> { }, }; - Writer::new(&gb_repository).write(&session)?; + Writer::open(&gb_repository).write(&session)?; assert_eq!( std::fs::read_to_string(gb_repository.session_path().join("meta/id"))?, @@ -81,7 +81,7 @@ fn test_should_write_partial_session() -> Result<()> { }, }; - Writer::new(&gb_repository).write(&session)?; + Writer::open(&gb_repository).write(&session)?; assert_eq!( std::fs::read_to_string(gb_repository.session_path().join("meta/id"))?, diff --git a/gitbutler-app/src/sessions/writer.rs b/gitbutler-app/src/sessions/writer.rs index 788c543db..990af6cf4 100644 --- a/gitbutler-app/src/sessions/writer.rs +++ b/gitbutler-app/src/sessions/writer.rs @@ -16,7 +16,7 @@ pub struct SessionWriter<'writer> { } impl<'writer> SessionWriter<'writer> { - pub fn new(repository: &'writer gb_repository::Repository) -> Self { + pub fn open(repository: &'writer gb_repository::Repository) -> Self { let writer = writer::DirWriter::open(repository.root()); SessionWriter { repository, writer } } diff --git a/gitbutler-app/src/virtual_branches/base.rs b/gitbutler-app/src/virtual_branches/base.rs index b26d0afe4..b9b982ad7 100644 --- a/gitbutler-app/src/virtual_branches/base.rs +++ b/gitbutler-app/src/virtual_branches/base.rs @@ -114,7 +114,7 @@ pub fn set_base_branch( last_fetched_ms: None, }; - let target_writer = target::Writer::new(gb_repository); + let target_writer = target::Writer::open(gb_repository); target_writer.write_default(&target)?; if !head_name @@ -192,7 +192,7 @@ pub fn set_base_branch( order: 0, }; - let branch_writer = branch::Writer::new(gb_repository); + let branch_writer = branch::Writer::open(gb_repository); branch_writer.write(&mut branch)?; } } @@ -293,7 +293,7 @@ pub fn update_base_branch( target.sha ))?; - let branch_writer = branch::Writer::new(gb_repository); + let branch_writer = branch::Writer::open(gb_repository); // try to update every branch let updated_vbranches = super::get_status_by_branch(gb_repository, project_repository)? @@ -519,7 +519,7 @@ pub fn update_base_branch( )?; // write new target oid - let target_writer = target::Writer::new(gb_repository); + let target_writer = target::Writer::open(gb_repository); target_writer.write_default(&target::Target { sha: new_target_commit.id(), ..target diff --git a/gitbutler-app/src/virtual_branches/branch/reader.rs b/gitbutler-app/src/virtual_branches/branch/reader.rs index 39c78d195..a090e9cf0 100644 --- a/gitbutler-app/src/virtual_branches/branch/reader.rs +++ b/gitbutler-app/src/virtual_branches/branch/reader.rs @@ -9,7 +9,7 @@ pub struct BranchReader<'reader> { } impl<'reader> BranchReader<'reader> { - pub fn new(reader: &'reader dyn Reader) -> Self { + pub fn with_reader(reader: &'reader dyn Reader) -> Self { Self { reader } } @@ -102,7 +102,7 @@ mod tests { let session = gb_repository.get_or_create_current_session()?; let session_reader = sessions::Reader::open(&gb_repository, &session)?; - let reader = BranchReader::new(&session_reader); + let reader = BranchReader::with_reader(&session_reader); let result = reader.read(&BranchId::generate()); assert!(result.is_err()); assert_eq!(result.unwrap_err().to_string(), "file not found"); @@ -116,13 +116,13 @@ mod tests { let mut branch = test_branch(); - let writer = Writer::new(&gb_repository); + let writer = Writer::open(&gb_repository); writer.write(&mut branch)?; let session = gb_repository.get_current_session()?.unwrap(); let session_reader = sessions::Reader::open(&gb_repository, &session)?; - let reader = BranchReader::new(&session_reader); + let reader = BranchReader::with_reader(&session_reader); assert_eq!(branch, reader.read(&branch.id).unwrap()); diff --git a/gitbutler-app/src/virtual_branches/branch/writer.rs b/gitbutler-app/src/virtual_branches/branch/writer.rs index bc475a9f0..e5ad5b013 100644 --- a/gitbutler-app/src/virtual_branches/branch/writer.rs +++ b/gitbutler-app/src/virtual_branches/branch/writer.rs @@ -13,7 +13,7 @@ pub struct BranchWriter<'writer> { } impl<'writer> BranchWriter<'writer> { - pub fn new(repository: &'writer gb_repository::Repository) -> Self { + pub fn open(repository: &'writer gb_repository::Repository) -> Self { Self { repository, writer: writer::DirWriter::open(repository.root()), @@ -190,7 +190,7 @@ mod tests { let mut branch = test_branch(); - let writer = BranchWriter::new(&gb_repository); + let writer = BranchWriter::open(&gb_repository); writer.write(&mut branch)?; let root = gb_repository @@ -251,7 +251,7 @@ mod tests { let mut branch = test_branch(); - let writer = BranchWriter::new(&gb_repository); + let writer = BranchWriter::open(&gb_repository); writer.write(&mut branch)?; assert!(gb_repository.get_current_session()?.is_some()); @@ -265,7 +265,7 @@ mod tests { let mut branch = test_branch(); - let writer = BranchWriter::new(&gb_repository); + let writer = BranchWriter::open(&gb_repository); writer.write(&mut branch)?; let mut updated_branch = Branch { diff --git a/gitbutler-app/src/virtual_branches/controller.rs b/gitbutler-app/src/virtual_branches/controller.rs index 618bc0b7c..7cf47221e 100644 --- a/gitbutler-app/src/virtual_branches/controller.rs +++ b/gitbutler-app/src/virtual_branches/controller.rs @@ -857,7 +857,7 @@ impl ControllerInner { .fetch(default_target.branch.remote(), &self.helper) .map_err(errors::FetchFromTargetError::Remote)?; - let target_writer = target::Writer::new(gb_repository); + let target_writer = target::Writer::open(gb_repository); target_writer .write_default(&target::Target { last_fetched_ms: Some( diff --git a/gitbutler-app/src/virtual_branches/integration.rs b/gitbutler-app/src/virtual_branches/integration.rs index 4022cfab6..35cae36b0 100644 --- a/gitbutler-app/src/virtual_branches/integration.rs +++ b/gitbutler-app/src/virtual_branches/integration.rs @@ -262,7 +262,7 @@ fn verify_head_is_clean( .context("failed to create virtual branch")?; // rebasing the extra commits onto the new branch - let writer = super::branch::Writer::new(gb_repository); + let writer = super::branch::Writer::open(gb_repository); extra_commits.reverse(); let mut head = new_branch.head; for commit in extra_commits { diff --git a/gitbutler-app/src/virtual_branches/iterator.rs b/gitbutler-app/src/virtual_branches/iterator.rs index b2adabcb5..ff4d780a5 100644 --- a/gitbutler-app/src/virtual_branches/iterator.rs +++ b/gitbutler-app/src/virtual_branches/iterator.rs @@ -35,7 +35,7 @@ impl<'iterator> BranchIterator<'iterator> { .collect(); ids.sort(); Ok(Self { - branch_reader: branch::Reader::new(reader), + branch_reader: branch::Reader::with_reader(reader), ids, }) } @@ -148,10 +148,10 @@ mod tests { fn test_iterate_all() -> Result<()> { let Case { gb_repository, .. } = Suite::default().new_case(); - let target_writer = target::Writer::new(&gb_repository); + let target_writer = target::Writer::open(&gb_repository); target_writer.write_default(&test_target())?; - let branch_writer = branch::Writer::new(&gb_repository); + let branch_writer = branch::Writer::open(&gb_repository); let mut branch_1 = test_branch(); branch_writer.write(&mut branch_1)?; let mut branch_2 = test_branch(); diff --git a/gitbutler-app/src/virtual_branches/target/reader.rs b/gitbutler-app/src/virtual_branches/target/reader.rs index eee63d6fe..156e35cf0 100644 --- a/gitbutler-app/src/virtual_branches/target/reader.rs +++ b/gitbutler-app/src/virtual_branches/target/reader.rs @@ -12,7 +12,7 @@ pub struct TargetReader<'reader> { } impl<'reader> TargetReader<'reader> { - pub fn new(reader: &'reader dyn reader::Reader) -> Self { + pub fn with_reader(reader: &'reader dyn reader::Reader) -> Self { Self { reader } } @@ -105,7 +105,7 @@ mod tests { let session = gb_repository.get_or_create_current_session()?; let session_reader = sessions::Reader::open(&gb_repository, &session)?; - let reader = TargetReader::new(&session_reader); + let reader = TargetReader::with_reader(&session_reader); let result = reader.read(&BranchId::generate()); assert!(result.is_err()); assert_eq!(result.unwrap_err().to_string(), "file not found"); @@ -136,7 +136,7 @@ mod tests { let session = gb_repository.get_or_create_current_session()?; let session_reader = sessions::Reader::open(&gb_repository, &session)?; - let reader = TargetReader::new(&session_reader); + let reader = TargetReader::with_reader(&session_reader); let read = reader.read_default().unwrap(); assert_eq!(read.branch.branch(), "master"); @@ -175,14 +175,14 @@ mod tests { last_fetched_ms: Some(1), }; - let branch_writer = branch::Writer::new(&gb_repository); + let branch_writer = branch::Writer::open(&gb_repository); branch_writer.write(&mut branch)?; let session = gb_repository.get_current_session()?.unwrap(); let session_reader = sessions::Reader::open(&gb_repository, &session)?; - let target_writer = TargetWriter::new(&gb_repository); - let reader = TargetReader::new(&session_reader); + let target_writer = TargetWriter::open(&gb_repository); + let reader = TargetReader::with_reader(&session_reader); target_writer.write_default(&default_target)?; assert_eq!(default_target, reader.read(&branch.id)?); diff --git a/gitbutler-app/src/virtual_branches/target/writer.rs b/gitbutler-app/src/virtual_branches/target/writer.rs index 9cf607d81..a0a50bcea 100644 --- a/gitbutler-app/src/virtual_branches/target/writer.rs +++ b/gitbutler-app/src/virtual_branches/target/writer.rs @@ -14,7 +14,7 @@ pub struct TargetWriter<'writer> { } impl<'writer> TargetWriter<'writer> { - pub fn new(repository: &'writer gb_repository::Repository) -> Self { + pub fn open(repository: &'writer gb_repository::Repository) -> Self { Self { repository, writer: writer::DirWriter::open(repository.root()), @@ -173,10 +173,10 @@ mod tests { last_fetched_ms: Some(1), }; - let branch_writer = branch::Writer::new(&gb_repository); + let branch_writer = branch::Writer::open(&gb_repository); branch_writer.write(&mut branch)?; - let target_writer = TargetWriter::new(&gb_repository); + let target_writer = TargetWriter::open(&gb_repository); target_writer.write(&branch.id, &target)?; let root = gb_repository @@ -271,9 +271,9 @@ mod tests { last_fetched_ms: Some(1), }; - let branch_writer = branch::Writer::new(&gb_repository); + let branch_writer = branch::Writer::open(&gb_repository); branch_writer.write(&mut branch)?; - let target_writer = TargetWriter::new(&gb_repository); + let target_writer = TargetWriter::open(&gb_repository); target_writer.write(&branch.id, &target)?; let updated_target = Target { diff --git a/gitbutler-app/src/virtual_branches/tests.rs b/gitbutler-app/src/virtual_branches/tests.rs index e85086266..4d7a49fa9 100644 --- a/gitbutler-app/src/virtual_branches/tests.rs +++ b/gitbutler-app/src/virtual_branches/tests.rs @@ -35,7 +35,7 @@ pub fn set_test_target( .expect("failed to add remote"); remote.push(&["refs/heads/master:refs/heads/master"], None)?; - target::Writer::new(gb_repo) + target::Writer::open(gb_repo) .write_default(&target::Target { last_fetched_ms: None, branch: "refs/remotes/origin/master".parse().unwrap(), @@ -335,7 +335,7 @@ fn test_create_branch_with_ownership() -> Result<()> { let current_session = gb_repository.get_or_create_current_session().unwrap(); let current_session_reader = sessions::Reader::open(&gb_repository, ¤t_session).unwrap(); - let branch_reader = branch::Reader::new(¤t_session_reader); + let branch_reader = branch::Reader::with_reader(¤t_session_reader); let branch0 = branch_reader.read(&branch0.id).unwrap(); let branch1 = create_virtual_branch( @@ -606,7 +606,7 @@ fn test_updated_ownership_should_bubble_up() -> Result<()> { let current_session = gb_repository.get_or_create_current_session()?; let current_session_reader = sessions::Reader::open(&gb_repository, ¤t_session)?; - let branch_reader = branch::Reader::new(¤t_session_reader); + let branch_reader = branch::Reader::with_reader(¤t_session_reader); let branch1_id = create_virtual_branch( &gb_repository, @@ -734,8 +734,8 @@ fn test_move_hunks_multiple_sources() -> Result<()> { let current_session = gb_repository.get_or_create_current_session()?; let current_session_reader = sessions::Reader::open(&gb_repository, ¤t_session)?; - let branch_reader = branch::Reader::new(¤t_session_reader); - let branch_writer = branch::Writer::new(&gb_repository); + let branch_reader = branch::Reader::with_reader(¤t_session_reader); + let branch_writer = branch::Writer::open(&gb_repository); let mut branch2 = branch_reader.read(&branch2_id)?; branch2.ownership = Ownership { files: vec!["test.txt:1-5".parse()?], @@ -1006,7 +1006,7 @@ fn test_merge_vbranch_upstream_clean() -> Result<()> { )?; set_test_target(&gb_repository, &project_repository)?; - target::Writer::new(&gb_repository).write_default(&target::Target { + target::Writer::open(&gb_repository).write_default(&target::Target { last_fetched_ms: None, branch: "refs/remotes/origin/master".parse().unwrap(), remote_url: "origin".to_string(), @@ -1021,7 +1021,7 @@ fn test_merge_vbranch_upstream_clean() -> Result<()> { )?; let remote_branch: git::RemoteRefname = "refs/remotes/origin/master".parse().unwrap(); - let branch_writer = branch::Writer::new(&gb_repository); + let branch_writer = branch::Writer::open(&gb_repository); let mut branch = create_virtual_branch( &gb_repository, &project_repository, @@ -1136,7 +1136,7 @@ fn test_merge_vbranch_upstream_conflict() -> Result<()> { )?; set_test_target(&gb_repository, &project_repository)?; - target::Writer::new(&gb_repository).write_default(&target::Target { + target::Writer::open(&gb_repository).write_default(&target::Target { last_fetched_ms: None, branch: "refs/remotes/origin/master".parse().unwrap(), remote_url: "origin".to_string(), @@ -1150,7 +1150,7 @@ fn test_merge_vbranch_upstream_conflict() -> Result<()> { )?; let remote_branch: git::RemoteRefname = "refs/remotes/origin/master".parse().unwrap(); - let branch_writer = branch::Writer::new(&gb_repository); + let branch_writer = branch::Writer::open(&gb_repository); let mut branch = create_virtual_branch( &gb_repository, &project_repository, @@ -1564,8 +1564,8 @@ fn test_detect_mergeable_branch() -> Result<()> { let current_session = gb_repository.get_or_create_current_session()?; let current_session_reader = sessions::Reader::open(&gb_repository, ¤t_session)?; - let branch_reader = branch::Reader::new(¤t_session_reader); - let branch_writer = branch::Writer::new(&gb_repository); + let branch_reader = branch::Reader::with_reader(¤t_session_reader); + let branch_writer = branch::Writer::open(&gb_repository); update_branch( &gb_repository, @@ -1759,7 +1759,7 @@ fn test_upstream_integrated_vbranch() -> Result<()> { "update target", )?; - target::Writer::new(&gb_repository).write_default(&target::Target { + target::Writer::open(&gb_repository).write_default(&target::Target { last_fetched_ms: None, branch: "refs/remotes/origin/master".parse().unwrap(), remote_url: "http://origin.com/project".to_string(), diff --git a/gitbutler-app/src/virtual_branches/virtual.rs b/gitbutler-app/src/virtual_branches/virtual.rs index 33483cf5d..919e65a76 100644 --- a/gitbutler-app/src/virtual_branches/virtual.rs +++ b/gitbutler-app/src/virtual_branches/virtual.rs @@ -161,7 +161,7 @@ pub fn normalize_branch_name(name: &str) -> String { pub fn get_default_target( current_session_reader: &sessions::Reader, ) -> Result, reader::Error> { - let target_reader = target::Reader::new(current_session_reader); + let target_reader = target::Reader::with_reader(current_session_reader); match target_reader.read_default() { Ok(target) => Ok(Some(target)), Err(reader::Error::NotFound) => Ok(None), @@ -199,9 +199,9 @@ pub fn apply_branch( }) })?; - let writer = branch::Writer::new(gb_repository); + let writer = branch::Writer::open(gb_repository); - let mut branch = match branch::Reader::new(¤t_session_reader).read(branch_id) { + let mut branch = match branch::Reader::with_reader(¤t_session_reader).read(branch_id) { Ok(branch) => Ok(branch), Err(reader::Error::NotFound) => Err(errors::ApplyBranchError::BranchNotFound( errors::BranchNotFoundError { @@ -477,7 +477,7 @@ pub fn unapply_ownership( .context("failed to get status by branch")?; // remove the ownership from the applied branches, and write them out - let branch_writer = branch::Writer::new(gb_repository); + let branch_writer = branch::Writer::open(gb_repository); let applied_statuses = applied_statuses .into_iter() .map(|(branch, branch_files)| { @@ -564,7 +564,7 @@ pub fn unapply_branch( let current_session_reader = sessions::Reader::open(gb_repository, session).context("failed to open current session")?; - let branch_reader = branch::Reader::new(¤t_session_reader); + let branch_reader = branch::Reader::with_reader(¤t_session_reader); let mut target_branch = branch_reader.read(branch_id).map_err(|error| match error { reader::Error::NotFound => { @@ -597,7 +597,7 @@ pub fn unapply_branch( // when applying branch leads to a conflict, all other branches are unapplied. // this means we can just reset to the default target tree. { - let branch_writer = branch::Writer::new(gb_repository); + let branch_writer = branch::Writer::open(gb_repository); target_branch.applied = false; branch_writer.write(&mut target_branch)?; } @@ -628,7 +628,7 @@ pub fn unapply_branch( .find(|(s, _)| s.id == target_branch.id) .context("failed to find status for branch"); - let branch_writer = branch::Writer::new(gb_repository); + let branch_writer = branch::Writer::open(gb_repository); if let Ok((_, files)) = status { if files.is_empty() { // if there is nothing to unapply, remove the branch straight away @@ -1072,7 +1072,7 @@ pub fn create_virtual_branch( .unwrap_or(all_virtual_branches.len()) .clamp(0, all_virtual_branches.len()); - let branch_writer = branch::Writer::new(gb_repository); + let branch_writer = branch::Writer::open(gb_repository); // make space for the new branch for (i, branch) in all_virtual_branches.iter().enumerate() { @@ -1118,7 +1118,7 @@ pub fn create_virtual_branch( }; if let Some(ownership) = &create.ownership { - let branch_reader = branch::Reader::new(¤t_session_reader); + let branch_reader = branch::Reader::with_reader(¤t_session_reader); set_ownership(&branch_reader, &branch_writer, &mut branch, ownership) .context("failed to set ownership")?; } @@ -1154,7 +1154,7 @@ pub fn merge_virtual_branch_upstream( .context("failed to open current session")?; // get the branch - let branch_reader = branch::Reader::new(¤t_session_reader); + let branch_reader = branch::Reader::with_reader(¤t_session_reader); let mut branch = match branch_reader.read(branch_id) { Ok(branch) => Ok(branch), Err(reader::Error::NotFound) => Err( @@ -1284,7 +1284,7 @@ pub fn merge_virtual_branch_upstream( .context("failed to checkout tree")?; // write the branch data - let branch_writer = branch::Writer::new(gb_repository); + let branch_writer = branch::Writer::open(gb_repository); branch.head = new_branch_head; branch.tree = merge_tree_oid; branch_writer.write(&mut branch)?; @@ -1305,8 +1305,8 @@ pub fn update_branch( .context("failed to get or create currnt session")?; let current_session_reader = sessions::Reader::open(gb_repository, ¤t_session) .context("failed to open current session")?; - let branch_reader = branch::Reader::new(¤t_session_reader); - let branch_writer = branch::Writer::new(gb_repository); + let branch_reader = branch::Reader::with_reader(¤t_session_reader); + let branch_writer = branch::Writer::open(gb_repository); let mut branch = branch_reader .read(&branch_update.id) @@ -1387,8 +1387,8 @@ pub fn delete_branch( .context("failed to get or create currnt session")?; let current_session_reader = sessions::Reader::open(gb_repository, ¤t_session) .context("failed to open current session")?; - let branch_reader = branch::Reader::new(¤t_session_reader); - let branch_writer = branch::Writer::new(gb_repository); + let branch_reader = branch::Reader::with_reader(¤t_session_reader); + let branch_writer = branch::Writer::open(gb_repository); let branch = match branch_reader.read(branch_id) { Ok(branch) => Ok(branch), @@ -1771,7 +1771,7 @@ fn get_applied_status( // write updated state if not resolving if !project_repository.is_resolving() { - let branch_writer = branch::Writer::new(gb_repository); + let branch_writer = branch::Writer::open(gb_repository); for (vbranch, files) in &mut hunks_by_branch { vbranch.tree = write_tree(project_repository, default_target, files)?; branch_writer @@ -1829,7 +1829,7 @@ pub fn reset_branch( }) })?; - let branch_reader = branch::Reader::new(¤t_session_reader); + let branch_reader = branch::Reader::with_reader(¤t_session_reader); let mut branch = match branch_reader.read(branch_id) { Ok(branch) => Ok(branch), Err(reader::Error::NotFound) => Err(errors::ResetBranchError::BranchNotFound( @@ -1856,7 +1856,7 @@ pub fn reset_branch( )); } - let branch_writer = branch::Writer::new(gb_repository); + let branch_writer = branch::Writer::open(gb_repository); branch.head = target_commit_oid; branch_writer .write(&mut branch) @@ -2186,7 +2186,7 @@ pub fn commit( } // update the virtual branch head - let writer = branch::Writer::new(gb_repository); + let writer = branch::Writer::open(gb_repository); branch.tree = tree_oid; branch.head = commit_oid; writer.write(branch).context("failed to write branch")?; @@ -2212,8 +2212,8 @@ pub fn push( .context("failed to open current session") .map_err(errors::PushError::Other)?; - let branch_reader = branch::Reader::new(¤t_session_reader); - let branch_writer = branch::Writer::new(gb_repository); + let branch_reader = branch::Reader::with_reader(¤t_session_reader); + let branch_writer = branch::Writer::open(gb_repository); let mut vbranch = branch_reader.read(branch_id).map_err(|error| match error { reader::Error::NotFound => errors::PushError::BranchNotFound(errors::BranchNotFoundError { @@ -2276,7 +2276,7 @@ pub fn mark_all_unapplied(gb_repository: &gb_repository::Repository) -> Result<( let current_session = gb_repository.get_or_create_current_session()?; let session_reader = sessions::Reader::open(gb_repository, ¤t_session)?; let branch_iterator = super::Iterator::new(&session_reader)?; - let branch_writer = super::branch::Writer::new(gb_repository); + let branch_writer = super::branch::Writer::open(gb_repository); branch_iterator .collect::, _>>() .context("failed to read branches")? @@ -2426,7 +2426,7 @@ pub fn is_virtual_branch_mergeable( .context("failed to get or create currnt session")?; let current_session_reader = sessions::Reader::open(gb_repository, ¤t_session) .context("failed to open current session reader")?; - let branch_reader = branch::Reader::new(¤t_session_reader); + let branch_reader = branch::Reader::with_reader(¤t_session_reader); let branch = match branch_reader.read(branch_id) { Ok(branch) => Ok(branch), Err(reader::Error::NotFound) => Err(errors::IsVirtualBranchMergeable::BranchNotFound( @@ -2655,7 +2655,7 @@ pub fn amend( ) .context("failed to create commit")?; - let branch_writer = branch::Writer::new(gb_repository); + let branch_writer = branch::Writer::open(gb_repository); target_branch.head = commit_oid; branch_writer.write(target_branch)?; @@ -2683,7 +2683,7 @@ pub fn cherry_pick( .context("failed to get or create current session")?; let current_session_reader = sessions::Reader::open(gb_repository, ¤t_session) .context("failed to open current session")?; - let branch_reader = branch::Reader::new(¤t_session_reader); + let branch_reader = branch::Reader::with_reader(¤t_session_reader); let mut branch = branch_reader .read(branch_id) .context("failed to read branch")?; @@ -2837,7 +2837,7 @@ pub fn cherry_pick( .context("failed to checkout final tree")?; // update branch status - let writer = branch::Writer::new(gb_repository); + let writer = branch::Writer::open(gb_repository); branch.head = commit_oid; writer .write(&mut branch) @@ -2872,7 +2872,7 @@ pub fn squash( .context("failed to get or create current session")?; let current_session_reader = sessions::Reader::open(gb_repository, ¤t_session) .context("failed to open current session")?; - let branch_reader = branch::Reader::new(¤t_session_reader); + let branch_reader = branch::Reader::with_reader(¤t_session_reader); let default_target = get_default_target(¤t_session_reader) .context("failed to read default target")? @@ -3029,7 +3029,7 @@ pub fn squash( }; // save new branch head - let writer = branch::Writer::new(gb_repository); + let writer = branch::Writer::open(gb_repository); branch.head = new_head_id; writer .write(&mut branch) @@ -3064,7 +3064,7 @@ pub fn update_commit_message( .context("failed to get or create current session")?; let current_session_reader = sessions::Reader::open(gb_repository, ¤t_session) .context("failed to open current session")?; - let branch_reader = branch::Reader::new(¤t_session_reader); + let branch_reader = branch::Reader::with_reader(¤t_session_reader); let default_target = get_default_target(¤t_session_reader) .context("failed to read default target")? @@ -3205,7 +3205,7 @@ pub fn update_commit_message( }; // save new branch head - let writer = branch::Writer::new(gb_repository); + let writer = branch::Writer::open(gb_repository); branch.head = new_head_id; writer .write(&mut branch) @@ -3346,7 +3346,7 @@ pub fn create_virtual_branch_from_branch( order, }; - let writer = branch::Writer::new(gb_repository); + let writer = branch::Writer::open(gb_repository); writer .write(&mut branch) .context("failed to write branch")?; diff --git a/gitbutler-app/src/watcher/handlers/calculate_deltas_handler.rs b/gitbutler-app/src/watcher/handlers/calculate_deltas_handler.rs index 5706dae6d..3a24c9f5e 100644 --- a/gitbutler-app/src/watcher/handlers/calculate_deltas_handler.rs +++ b/gitbutler-app/src/watcher/handlers/calculate_deltas_handler.rs @@ -115,7 +115,7 @@ impl Handler { Err(err) => Err(err).context("failed to get file content")?, }; - let deltas_reader = deltas::Reader::new(¤t_session_reader); + let deltas_reader = deltas::Reader::with_reader(¤t_session_reader); let current_deltas = deltas_reader .read_file(path) .context("failed to get file deltas")?; @@ -132,7 +132,7 @@ impl Handler { if let Some(new_delta) = new_delta { let deltas = text_doc.get_deltas(); - let writer = deltas::Writer::new(&gb_repository); + let writer = deltas::Writer::open(&gb_repository); writer .write(path, &deltas) .context("failed to write deltas")?; @@ -262,7 +262,7 @@ mod test { let session = gb_repository.get_current_session()?.unwrap(); let session_reader = sessions::Reader::open(&gb_repository, &session)?; - let deltas_reader = deltas::Reader::new(&session_reader); + let deltas_reader = deltas::Reader::with_reader(&session_reader); let deltas = deltas_reader.read_file("test.txt")?.unwrap(); assert_eq!(deltas.len(), 1); assert_eq!(deltas[0].operations.len(), 1); @@ -338,7 +338,7 @@ mod test { let session = gb_repository.get_current_session()?.unwrap(); let session_reader = sessions::Reader::open(&gb_repository, &session)?; - let deltas_reader = deltas::Reader::new(&session_reader); + let deltas_reader = deltas::Reader::with_reader(&session_reader); let deltas = deltas_reader.read_file("test.bin")?.unwrap(); assert_eq!(deltas.len(), 1); @@ -367,7 +367,7 @@ mod test { let session = gb_repository.get_current_session()?.unwrap(); let session_reader = sessions::Reader::open(&gb_repository, &session)?; - let deltas_reader = deltas::Reader::new(&session_reader); + let deltas_reader = deltas::Reader::with_reader(&session_reader); let deltas = deltas_reader.read_file("test.txt")?.unwrap(); assert_eq!(deltas.len(), 1); assert_eq!(deltas[0].operations.len(), 0); @@ -395,7 +395,7 @@ mod test { let session = gb_repository.get_current_session()?.unwrap(); let session_reader = sessions::Reader::open(&gb_repository, &session)?; - let deltas_reader = deltas::Reader::new(&session_reader); + let deltas_reader = deltas::Reader::with_reader(&session_reader); let deltas = deltas_reader.read_file("test.txt")?.unwrap(); assert_eq!(deltas.len(), 1); assert_eq!(deltas[0].operations.len(), 1); @@ -455,7 +455,7 @@ mod test { let session = gb_repository.get_current_session()?.unwrap(); let session_reader = sessions::Reader::open(&gb_repository, &session)?; - let deltas_reader = deltas::Reader::new(&session_reader); + let deltas_reader = deltas::Reader::with_reader(&session_reader); let deltas = deltas_reader.read_file("test.txt")?.unwrap(); assert_eq!(deltas.len(), 1); assert_eq!(deltas[0].operations.len(), 1); @@ -512,7 +512,7 @@ mod test { // current session must have the deltas, but not the file (it didn't exist) let session = gb_repository.get_current_session()?.unwrap(); let session_reader = sessions::Reader::open(&gb_repository, &session)?; - let deltas_reader = deltas::Reader::new(&session_reader); + let deltas_reader = deltas::Reader::with_reader(&session_reader); let deltas = deltas_reader.read_file("test.txt")?.unwrap(); assert_eq!(deltas.len(), 1); assert_eq!(deltas[0].operations.len(), 1); @@ -542,7 +542,7 @@ mod test { reader::Content::UTF8("test".to_string()) ); - let deltas_reader = deltas::Reader::new(&session_reader); + let deltas_reader = deltas::Reader::with_reader(&session_reader); let deltas = deltas_reader.read(None)?; assert!(deltas.is_empty()); @@ -621,7 +621,7 @@ mod test { let mut operations: Vec = vec![]; for session in &mut *sessions_slice { let session_reader = sessions::Reader::open(&gb_repository, session).unwrap(); - let deltas_reader = deltas::Reader::new(&session_reader); + let deltas_reader = deltas::Reader::with_reader(&session_reader); let deltas_by_filepath = deltas_reader.read(None).unwrap(); for deltas in deltas_by_filepath.values() { for delta in deltas { @@ -707,7 +707,7 @@ mod test { let mut operations: Vec = vec![]; for session in &mut *sessions_slice { let session_reader = sessions::Reader::open(&gb_repository, session).unwrap(); - let deltas_reader = deltas::Reader::new(&session_reader); + let deltas_reader = deltas::Reader::with_reader(&session_reader); let deltas_by_filepath = deltas_reader.read(None).unwrap(); for deltas in deltas_by_filepath.values() { for delta in deltas { @@ -770,7 +770,7 @@ mod test { let mut operations: Vec = vec![]; let session = gb_repository.get_current_session()?.unwrap(); let session_reader = sessions::Reader::open(&gb_repository, &session).unwrap(); - let deltas_reader = deltas::Reader::new(&session_reader); + let deltas_reader = deltas::Reader::with_reader(&session_reader); let deltas_by_filepath = deltas_reader.read(None).unwrap(); for deltas in deltas_by_filepath.values() { for delta in deltas { @@ -811,8 +811,8 @@ mod test { )])); let listener = Handler::from(&suite.local_app_data); - let branch_writer = virtual_branches::branch::Writer::new(&gb_repository); - let target_writer = virtual_branches::target::Writer::new(&gb_repository); + let branch_writer = virtual_branches::branch::Writer::open(&gb_repository); + let target_writer = virtual_branches::target::Writer::open(&gb_repository); let default_target = test_target(); target_writer.write_default(&default_target)?; let mut vbranch0 = test_branch(); @@ -845,7 +845,7 @@ mod test { assert!(branch_ids.contains(&vbranch0.id)); assert!(branch_ids.contains(&vbranch1.id)); - let target_reader = virtual_branches::target::Reader::new(&session_reader); + let target_reader = virtual_branches::target::Reader::with_reader(&session_reader); assert_eq!(target_reader.read_default().unwrap(), default_target); assert_eq!(target_reader.read(&vbranch0.id).unwrap(), default_target); assert_eq!(target_reader.read(&vbranch1.id).unwrap(), vbranch1_target); @@ -867,8 +867,8 @@ mod test { )])); let listener = Handler::from(&suite.local_app_data); - let branch_writer = virtual_branches::branch::Writer::new(&gb_repository); - let target_writer = virtual_branches::target::Writer::new(&gb_repository); + let branch_writer = virtual_branches::branch::Writer::open(&gb_repository); + let target_writer = virtual_branches::target::Writer::open(&gb_repository); let default_target = test_target(); target_writer.write_default(&default_target)?; let mut vbranch0 = test_branch(); @@ -904,7 +904,7 @@ mod test { assert!(branch_ids.contains(&vbranch0.id)); assert!(branch_ids.contains(&vbranch1.id)); - let target_reader = virtual_branches::target::Reader::new(&session_reader); + let target_reader = virtual_branches::target::Reader::with_reader(&session_reader); assert_eq!(target_reader.read_default().unwrap(), default_target); assert_eq!(target_reader.read(&vbranch0.id).unwrap(), default_target); assert_eq!(target_reader.read(&vbranch1.id).unwrap(), vbranch1_target); diff --git a/gitbutler-app/src/watcher/handlers/index_handler.rs b/gitbutler-app/src/watcher/handlers/index_handler.rs index bc57f96a4..ed764d743 100644 --- a/gitbutler-app/src/watcher/handlers/index_handler.rs +++ b/gitbutler-app/src/watcher/handlers/index_handler.rs @@ -108,7 +108,7 @@ impl Handler { .context("failed to insert session into database")?; let session_reader = sessions::Reader::open(gb_repository, session)?; - let deltas_reader = deltas::Reader::new(&session_reader); + let deltas_reader = deltas::Reader::with_reader(&session_reader); for (file_path, deltas) in deltas_reader .read(None) .context("could not list deltas for session")? From a19939ea39a215a6048e86fa60c3b4126bfe5631 Mon Sep 17 00:00:00 2001 From: Mattias Granlund Date: Mon, 8 Jan 2024 10:21:45 +0100 Subject: [PATCH 05/12] Fix zoom shortcuts on Linux --- gitbutler-ui/src/routes/+layout.svelte | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/gitbutler-ui/src/routes/+layout.svelte b/gitbutler-ui/src/routes/+layout.svelte index 1ce861691..a5589d824 100644 --- a/gitbutler-ui/src/routes/+layout.svelte +++ b/gitbutler-ui/src/routes/+layout.svelte @@ -32,11 +32,12 @@ events.on('goto', (path: string) => goto(path)), events.on('openSendIssueModal', () => shareIssueModal?.show()), - // Zoom using cmd +, - and = - hotkeys.on('Meta+Equal', () => (zoom = Math.min(zoom + 0.0625, 3))), - hotkeys.on('Meta+Minus', () => (zoom = Math.max(zoom - 0.0625, 0.375))), - hotkeys.on('Meta+Digit0', () => (zoom = 1)), - hotkeys.on('Meta+T', () => { + // Zoom keys using cmd/ctrl depending on platform + hotkeys.on('$mod+Equal', () => (zoom = Math.min(zoom + 0.0625, 3))), + hotkeys.on('$mod+Minus', () => (zoom = Math.max(zoom - 0.0625, 0.375))), + hotkeys.on('$mod+Digit0', () => (zoom = 1)), + + hotkeys.on('$mod+T', () => { userSettings.update((s) => ({ ...s, theme: $userSettings.theme == 'light' ? 'dark' : 'light' From 7c8f8cc68d6a20eec28ceba507c0c2217afa77ee Mon Sep 17 00:00:00 2001 From: Nikita Galaiko Date: Mon, 8 Jan 2024 12:21:45 +0100 Subject: [PATCH 06/12] chore: update notify dependency to version 6.1.1 --- gitbutler-app/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitbutler-app/Cargo.toml b/gitbutler-app/Cargo.toml index f37b9cd03..a55e85722 100644 --- a/gitbutler-app/Cargo.toml +++ b/gitbutler-app/Cargo.toml @@ -41,7 +41,7 @@ itertools = "0.12" lazy_static = "1.4.0" md5 = "0.7.0" nonzero_ext = "0.3.0" -notify = { version = "6.0.1" } +notify = { version = "6.1.1" } notify-debouncer-full = "0.3.1" num_cpus = "1.16.0" once_cell = "1.19" From 2fcc03e939e3ead7ca8785001407016ff9db451b Mon Sep 17 00:00:00 2001 From: Mattias Granlund Date: Mon, 8 Jan 2024 14:15:55 +0100 Subject: [PATCH 07/12] Fix branch change copy - shoud say you've switched _away_ from gitbutler/integration --- gitbutler-ui/src/routes/[projectId]/+layout.svelte | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gitbutler-ui/src/routes/[projectId]/+layout.svelte b/gitbutler-ui/src/routes/[projectId]/+layout.svelte index ebbbe96f0..af59a2d31 100644 --- a/gitbutler-ui/src/routes/[projectId]/+layout.svelte +++ b/gitbutler-ui/src/routes/[projectId]/+layout.svelte @@ -50,7 +50,9 @@
{#if $branchesError$.code === Code.ProjectHead}
-

Looks like you've switched from gitbutler/integration

+

+ Looks like you've switched away from gitbutler/integration +

Due to GitButler managing multiple virtual branches, you cannot switch back and forth From aefeba8c7c26085814d0a8f1219935ffa1b6001b Mon Sep 17 00:00:00 2001 From: Nikita Galaiko Date: Mon, 8 Jan 2024 15:39:22 +0100 Subject: [PATCH 08/12] support unapplying locked ownerships --- gitbutler-app/src/git/diff.rs | 41 +++- gitbutler-app/src/virtual_branches/base.rs | 1 + gitbutler-app/src/virtual_branches/files.rs | 7 +- gitbutler-app/src/virtual_branches/virtual.rs | 184 +++++++++++++++++- gitbutler-app/tests/virtual_branches.rs | 133 +++++++++++++ 5 files changed, 354 insertions(+), 12 deletions(-) diff --git a/gitbutler-app/src/git/diff.rs b/gitbutler-app/src/git/diff.rs index fa42910f2..1d00de04e 100644 --- a/gitbutler-app/src/git/diff.rs +++ b/gitbutler-app/src/git/diff.rs @@ -17,13 +17,44 @@ pub struct Hunk { pub binary: bool, } +impl Hunk { + pub fn reverse(&self) -> Hunk { + let diff = self + .diff + .lines() + .map(|line| { + if let Some(content) = line.strip_prefix('+') { + format!("-{}", content) + } else if let Some(content) = line.strip_prefix('-') { + format!("+{}", content) + } else { + line.to_string() + } + }) + .collect::>() + .join("\n"); + Hunk { + old_start: self.new_start, + old_lines: self.new_lines, + new_start: self.old_start, + new_lines: self.old_lines, + diff, + binary: self.binary, + } + } +} + pub struct Options { + pub reverse: bool, pub context_lines: u32, } impl Default for Options { fn default() -> Self { - Self { context_lines: 3 } + Self { + context_lines: 3, + reverse: false, + } } } @@ -44,7 +75,8 @@ pub fn workdir( .show_binary(true) .show_untracked_content(true) .ignore_submodules(true) - .context_lines(opts.context_lines); + .context_lines(opts.context_lines) + .reverse(opts.reverse); let diff = repository.diff_tree_to_workdir(Some(&tree), Some(&mut diff_opts))?; @@ -55,14 +87,17 @@ pub fn trees( repository: &Repository, old_tree: &git::Tree, new_tree: &git::Tree, + opts: &Options, ) -> Result>> { let mut diff_opts = git2::DiffOptions::new(); diff_opts .recurse_untracked_dirs(true) .include_untracked(true) .show_binary(true) + .show_untracked_content(true) .ignore_submodules(true) - .show_untracked_content(true); + .context_lines(opts.context_lines) + .reverse(opts.reverse); let diff = repository.diff_tree_to_tree(Some(old_tree), Some(new_tree), Some(&mut diff_opts))?; diff --git a/gitbutler-app/src/virtual_branches/base.rs b/gitbutler-app/src/virtual_branches/base.rs index b9b982ad7..ded1b2568 100644 --- a/gitbutler-app/src/virtual_branches/base.rs +++ b/gitbutler-app/src/virtual_branches/base.rs @@ -327,6 +327,7 @@ pub fn update_base_branch( &project_repository.git_repository, &branch_head_tree, &branch_tree, + &diff::Options::default(), )?; if non_commited_files.is_empty() { // if there are no commited files, then the branch is fully merged diff --git a/gitbutler-app/src/virtual_branches/files.rs b/gitbutler-app/src/virtual_branches/files.rs index 0c0cce36e..70e0529b9 100644 --- a/gitbutler-app/src/virtual_branches/files.rs +++ b/gitbutler-app/src/virtual_branches/files.rs @@ -34,7 +34,12 @@ 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, + &diff::Options::default(), + )?; let files = diff .into_iter() diff --git a/gitbutler-app/src/virtual_branches/virtual.rs b/gitbutler-app/src/virtual_branches/virtual.rs index 919e65a76..8f755ce69 100644 --- a/gitbutler-app/src/virtual_branches/virtual.rs +++ b/gitbutler-app/src/virtual_branches/virtual.rs @@ -437,7 +437,7 @@ pub fn apply_branch( pub fn unapply_ownership( gb_repository: &gb_repository::Repository, project_repository: &project_repository::Repository, - ownership: &Ownership, + target_ownership: &Ownership, ) -> Result<(), errors::UnapplyOwnershipError> { if conflicts::is_resolving(project_repository) { return Err(errors::UnapplyOwnershipError::Conflict( @@ -476,18 +476,68 @@ pub fn unapply_ownership( ) .context("failed to get status by branch")?; - // remove the ownership from the applied branches, and write them out + // remove non locked hunks directly from the branch ownership let branch_writer = branch::Writer::open(gb_repository); let applied_statuses = applied_statuses - .into_iter() + .iter() .map(|(branch, branch_files)| { let mut branch = branch.clone(); let mut branch_files = branch_files.clone(); - for file_ownership_to_take in &ownership.files { - let taken_file_ownerships = branch.ownership.take(file_ownership_to_take); + + let non_commited_hunks = calculate_non_commited_diffs( + project_repository, + &branch, + &default_target, + &branch_files, + )?; + + let non_locked_non_commited_hunks = non_commited_hunks + .iter() + .filter_map(|(filepath, hunks)| { + let hunks = hunks + .iter() + .filter(|hunk| { + branch_files.get(filepath).map_or(false, |hunks| { + hunks + .iter() + .any(|branch_hunk| branch_hunk.diff == hunk.diff) + }) + }) + .cloned() + .collect::>(); + if hunks.is_empty() { + None + } else { + Some((filepath.clone(), hunks)) + } + }) + .collect::>(); + + for target_file_ownership in &target_ownership.files { + let taken_file_ownerships = branch.ownership.take(target_file_ownership); if taken_file_ownerships.is_empty() { continue; } + + // only consider non locked, non commited hunks + let taken_file_ownerships = taken_file_ownerships + .iter() + .filter(|taken_file_ownership| { + non_locked_non_commited_hunks.iter().any(|hunk| { + taken_file_ownership.file_path.eq(hunk.0) + && hunk.1.iter().any(|h| { + h.new_start == taken_file_ownership.hunks[0].start + && h.new_start + h.new_lines + == taken_file_ownership.hunks[0].end + }) + }) + }) + .collect::>(); + + if taken_file_ownerships.is_empty() { + continue; + } + branch_writer.write(&mut branch)?; branch_files = branch_files .iter_mut() @@ -517,6 +567,67 @@ pub fn unapply_ownership( }) .collect::>>()?; + let hunks_to_unapply = applied_statuses + .iter() + .map(|(branch, branch_files)| { + let non_commited_hunks = calculate_reverse_non_commited_diffs( + project_repository, + branch, + &default_target, + branch_files, + )?; + + let locked_non_commited_hunks = non_commited_hunks + .iter() + .filter_map(|(filepath, hunks)| { + let hunks = hunks + .iter() + .filter(|hunk| { + branch_files.get(filepath).map_or(false, |hunks| { + !hunks + .iter() + .any(|branch_hunk| branch_hunk.diff == hunk.diff) + }) + }) + .cloned() + .collect::>(); + if hunks.is_empty() { + None + } else { + Some((filepath.clone(), hunks)) + } + }) + .collect::>(); + + let hunks_to_unapply = locked_non_commited_hunks + .into_iter() + .filter_map(|(file_path, hunks)| { + target_ownership + .files + .iter() + .find(|o| o.file_path == *file_path) + .map(|target_hunks| { + let hunks = hunks + .iter() + .filter(|hunk| { + target_hunks.hunks.iter().any(|target_hunk| { + target_hunk.start == hunk.old_start + && target_hunk.end == hunk.old_start + hunk.old_lines + }) + }) + .cloned() + .collect::>(); + (file_path.clone(), hunks) + }) + }) + .collect::>(); + Ok(hunks_to_unapply) + }) + .collect::>>()? + .into_iter() + .flatten() + .collect::>(); + let repo = &project_repository.git_repository; let target_commit = repo @@ -527,7 +638,7 @@ pub fn unapply_ownership( let base_tree = target_commit.tree().context("failed to get target tree")?; // construst a new working directory tree, without the removed ownerships - let final_tree = applied_statuses.into_iter().fold( + let cumulative_tree = applied_statuses.into_iter().fold( target_commit.tree().context("failed to get target tree"), |final_tree, status| { let final_tree = final_tree?; @@ -540,6 +651,12 @@ pub fn unapply_ownership( }, )?; + let final_tree_oid = + write_tree_onto_tree(project_repository, &cumulative_tree, &hunks_to_unapply)?; + let final_tree = repo + .find_tree(final_tree_oid) + .context("failed to find tree")?; + repo.checkout_tree(&final_tree) .force() .remove_untracked() @@ -907,6 +1024,46 @@ fn is_requires_force( Ok(merge_base != upstream_commit.id()) } +// returns a diff to apply to the working directory to cancel out non commited changes +pub fn calculate_reverse_non_commited_diffs( + project_repository: &project_repository::Repository, + branch: &branch::Branch, + default_target: &target::Target, + files: &HashMap>, +) -> Result>> { + if default_target.sha == branch.head && !branch.applied { + return Ok(files.clone()); + }; + + let branch_tree = if branch.applied { + let target_plus_wd_oid = write_tree(project_repository, default_target, files)?; + project_repository + .git_repository + .find_tree(target_plus_wd_oid) + } else { + project_repository.git_repository.find_tree(branch.tree) + }?; + + let branch_head = project_repository + .git_repository + .find_commit(branch.head)? + .tree()?; + + // do a diff between branch.head and the tree we _would_ commit + let non_commited_diff = diff::trees( + &project_repository.git_repository, + &branch_head, + &branch_tree, + &diff::Options { + reverse: true, + ..Default::default() + }, + ) + .context("failed to diff trees")?; + + Ok(non_commited_diff) +} + // given a virtual branch and it's files that are calculated off of a default target, // return files adjusted to the branch's head commit pub fn calculate_non_commited_diffs( @@ -938,6 +1095,7 @@ pub fn calculate_non_commited_diffs( &project_repository.git_repository, &branch_head, &branch_tree, + &diff::Options::default(), ) .context("failed to diff trees")?; @@ -979,6 +1137,7 @@ fn list_virtual_commit_files( &project_repository.git_repository, &parent_tree, &commit_tree, + &diff::Options::default(), )?; let hunks_by_filepath = virtual_hunks_by_filepath(&project_repository.project().path, &diff); Ok(virtual_hunks_to_virtual_files( @@ -1594,6 +1753,7 @@ fn get_non_applied_status( &project_repository.git_repository, &target_tree, &branch_tree, + &diff::Options::default(), )?; Ok((branch, diff)) @@ -1901,11 +2061,18 @@ pub fn write_tree_onto_commit( ) -> Result { // read the base sha into an index let git_repository = &project_repository.git_repository; - let head_commit = git_repository.find_commit(commit_oid)?; let base_tree = head_commit.tree()?; + write_tree_onto_tree(project_repository, &base_tree, files) +} - let mut builder = git_repository.treebuilder(Some(&base_tree)); +pub fn write_tree_onto_tree( + project_repository: &project_repository::Repository, + base_tree: &git::Tree, + files: &HashMap>, +) -> Result { + let git_repository = &project_repository.git_repository; + let mut builder = git_repository.treebuilder(Some(base_tree)); // now update the index with content in the working directory for each file for (filepath, hunks) in files { // convert this string to a Path @@ -3309,6 +3476,7 @@ pub fn create_virtual_branch_from_branch( &project_repository.git_repository, &merge_base_tree, &head_commit_tree, + &diff::Options::default(), ) .context("failed to diff trees")?; diff --git a/gitbutler-app/tests/virtual_branches.rs b/gitbutler-app/tests/virtual_branches.rs index 811fdfe3d..94fe51e5b 100644 --- a/gitbutler-app/tests/virtual_branches.rs +++ b/gitbutler-app/tests/virtual_branches.rs @@ -5305,3 +5305,136 @@ mod create_virtual_branch_from_branch { assert_eq!(branches[0].commits[0].description, "branch commit"); } } + +mod unapply_ownership { + use super::*; + + #[tokio::test] + async fn unapply_locked_hunk() { + let Test { + project_id, + controller, + repository, + .. + } = Test::default(); + + controller + .set_base_branch(&project_id, &"refs/remotes/origin/master".parse().unwrap()) + .await + .unwrap(); + + let branch_id = controller + .create_virtual_branch(&project_id, &branch::BranchCreateRequest::default()) + .await + .unwrap(); + + { + // create a commit + fs::write(repository.path().join("file.txt"), "line1\n").unwrap(); + controller + .create_commit(&project_id, &branch_id, "commit", None, false) + .await + .unwrap(); + } + + let locked_hunk_ownership = { + // change in the committed hunks leads to hunk locking + fs::write(repository.path().join("file.txt"), "line1\nline2\n").unwrap(); + + let branch = controller + .list_virtual_branches(&project_id) + .await + .unwrap() + .into_iter() + .find(|b| b.id == branch_id) + .unwrap(); + assert_eq!(branch.files.len(), 1); + assert_eq!(branch.files[0].path.display().to_string(), "file.txt"); + assert_eq!(branch.files[0].hunks.len(), 1); + assert!(branch.files[0].hunks[0].locked); + + let hunk = &branch.files[0].hunks[0]; + format!("{}:{}-{}", hunk.file_path.display(), hunk.start, hunk.end) + .parse() + .unwrap() + }; + + // unaplly locked hunk + controller + .unapply_ownership(&project_id, &locked_hunk_ownership) + .await + .unwrap(); + + { + // verify no changes + let branch = controller + .list_virtual_branches(&project_id) + .await + .unwrap() + .into_iter() + .find(|b| b.id == branch_id) + .unwrap(); + assert!(branch.files.is_empty()); + } + } + + #[tokio::test] + async fn unapply_non_locked_hunk() { + let Test { + project_id, + controller, + repository, + .. + } = Test::default(); + + controller + .set_base_branch(&project_id, &"refs/remotes/origin/master".parse().unwrap()) + .await + .unwrap(); + + let branch_id = controller + .create_virtual_branch(&project_id, &branch::BranchCreateRequest::default()) + .await + .unwrap(); + + let hunk_ownership = { + // change in the committed hunks leads to hunk locking + fs::write(repository.path().join("file.txt"), "line1\n").unwrap(); + + let branch = controller + .list_virtual_branches(&project_id) + .await + .unwrap() + .into_iter() + .find(|b| b.id == branch_id) + .unwrap(); + assert_eq!(branch.files.len(), 1); + assert_eq!(branch.files[0].path.display().to_string(), "file.txt"); + assert_eq!(branch.files[0].hunks.len(), 1); + assert!(!branch.files[0].hunks[0].locked); + + let hunk = &branch.files[0].hunks[0]; + format!("{}:{}-{}", hunk.file_path.display(), hunk.start, hunk.end) + .parse() + .unwrap() + }; + + // unaplly locked hunk + controller + .unapply_ownership(&project_id, &hunk_ownership) + .await + .unwrap(); + + { + // verify no changes + let branch = controller + .list_virtual_branches(&project_id) + .await + .unwrap() + .into_iter() + .find(|b| b.id == branch_id) + .unwrap(); + assert!(branch.files.is_empty()); + } + } +} From a68064723de5b2caba00d369fceae7e253b816f3 Mon Sep 17 00:00:00 2001 From: Nikita Galaiko Date: Mon, 8 Jan 2024 15:39:31 +0100 Subject: [PATCH 09/12] allow unapplying locked ownerships --- .../src/routes/[projectId]/components/HunkContextMenu.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitbutler-ui/src/routes/[projectId]/components/HunkContextMenu.svelte b/gitbutler-ui/src/routes/[projectId]/components/HunkContextMenu.svelte index 48ec35467..ac44f23ec 100644 --- a/gitbutler-ui/src/routes/[projectId]/components/HunkContextMenu.svelte +++ b/gitbutler-ui/src/routes/[projectId]/components/HunkContextMenu.svelte @@ -20,7 +20,7 @@ - {#if item.hunk !== undefined && !item.hunk.locked} + {#if item.hunk !== undefined} branchController.unapplyHunk(item.hunk)} /> {/if} {#if item.lineNumber} From f481ba53113643e901086461d07f9c5fc9fb22e6 Mon Sep 17 00:00:00 2001 From: Mattias Granlund Date: Mon, 8 Jan 2024 17:07:17 +0100 Subject: [PATCH 10/12] Increase line-height of new/empty branch message --- .../src/routes/[projectId]/components/BranchCard.svelte | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/gitbutler-ui/src/routes/[projectId]/components/BranchCard.svelte b/gitbutler-ui/src/routes/[projectId]/components/BranchCard.svelte index a8816b54a..3ac4b67f9 100644 --- a/gitbutler-ui/src/routes/[projectId]/components/BranchCard.svelte +++ b/gitbutler-ui/src/routes/[projectId]/components/BranchCard.svelte @@ -217,13 +217,15 @@ {/if} {:else if branch.commits.length == 0}

-

This is a new branch. Let's start creating!

-

Get some work done, then throw some files my way!

+

+ This is a new branch. Let's start creating! +

+

Get some work done, then throw some files this way!

{:else}
-

No uncommitted changes on this branch

+

No uncommitted changes on this branch

{/if} Date: Mon, 8 Jan 2024 17:16:18 +0100 Subject: [PATCH 11/12] Fix UI recovery when switching back to gitbutler branch --- gitbutler-ui/src/lib/vbranches/branchStoresCache.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/gitbutler-ui/src/lib/vbranches/branchStoresCache.ts b/gitbutler-ui/src/lib/vbranches/branchStoresCache.ts index becc271a9..802a6469e 100644 --- a/gitbutler-ui/src/lib/vbranches/branchStoresCache.ts +++ b/gitbutler-ui/src/lib/vbranches/branchStoresCache.ts @@ -14,7 +14,8 @@ import { map, firstValueFrom, timeout, - combineLatest + combineLatest, + of } from 'rxjs'; export class VirtualBranchService { @@ -43,11 +44,11 @@ export class VirtualBranchService { }); }); }), + shareReplay(1), catchError((err) => { this.branchesError$.next(UserError.fromError(err)); - return []; - }), - shareReplay(1) + return of([]); + }) ); this.stashedBranches$ = this.branches$.pipe( From 4417510d551844c08d2136d20b5d4debb34052bf Mon Sep 17 00:00:00 2001 From: Nikita Galaiko Date: Tue, 9 Jan 2024 09:59:19 +0100 Subject: [PATCH 12/12] remove sentry api key from source code --- .github/workflows/publish.yaml | 4 ++++ gitbutler-ui/vite.config.ts | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 59a2ffdcf..e6a6a4546 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -35,6 +35,10 @@ jobs: - ubuntu-20.04 # [linux, x64] runs-on: ${{ matrix.platform }} + + env: + SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }} + steps: - uses: actions/checkout@v4 with: diff --git a/gitbutler-ui/vite.config.ts b/gitbutler-ui/vite.config.ts index c36fdef7b..ff01a0d1e 100644 --- a/gitbutler-ui/vite.config.ts +++ b/gitbutler-ui/vite.config.ts @@ -11,7 +11,7 @@ export default defineConfig({ org: 'gitbutler', project: 'app-js', // this is nikita galaiko's personal sentry api token. - authToken: '04c6bc1df15346f39ed2fbeb99c0a8e25bcbedc4aba9461bb3a471733b8c80db', + authToken: process.env.SENTRY_AUTH_TOKEN, include: ['build'], cleanArtifacts: true, setCommits: {