From be2e15384b80582fb96e6883f3eb89c3675cd874 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Mon, 17 Sep 2018 17:14:47 +0200 Subject: [PATCH] Use index for writing trees --- .../benchmark_repository/examples/test.rs | 2 +- benchmark-new/benchmark_repository/src/lib.rs | 68 +++++++++++++------ 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/benchmark-new/benchmark_repository/examples/test.rs b/benchmark-new/benchmark_repository/examples/test.rs index 1cd10335e..22621e5c2 100644 --- a/benchmark-new/benchmark_repository/examples/test.rs +++ b/benchmark-new/benchmark_repository/examples/test.rs @@ -10,5 +10,5 @@ fn main() pretty_env_logger::init(); let benchmark_repository = BenchmarkRepository::new("git@git.luehne.de:patrick/tplp-planning-benchmark.git", Path::new("storage"), "git", "Potassco Bot", "bot@potassco.org"); - benchmark_repository.commit_file(Path::new("/tmp/test"), Path::new("test-result/test"), "test-results"); + benchmark_repository.commit_file(Path::new("/tmp/test"), Path::new("test-new/fourth"), "test-results"); } diff --git a/benchmark-new/benchmark_repository/src/lib.rs b/benchmark-new/benchmark_repository/src/lib.rs index a467887d4..dc1c9cbfa 100644 --- a/benchmark-new/benchmark_repository/src/lib.rs +++ b/benchmark-new/benchmark_repository/src/lib.rs @@ -4,7 +4,7 @@ extern crate pretty_env_logger; extern crate log; extern crate indicatif; -use git2::{Cred, Error, FetchOptions, Progress, PushOptions, RemoteCallbacks, ResetType, Repository, Signature, TreeBuilder}; +use git2::{Cred, Error, FetchOptions, Index, IndexEntry, IndexTime, Progress, PushOptions, RemoteCallbacks, ResetType, Repository, Signature, TreeBuilder}; use git2::build::{CheckoutBuilder, RepoBuilder}; use std::fs::read; use std::path::Path; @@ -202,15 +202,8 @@ impl BenchmarkRepository benchmark_repository } - pub fn commit_file(&self, file_path: &Path, result_file_path: &Path, branch_name: &str) + pub fn read_file_as_index_entry(&self, file_path: &Path, result_file_path: &Path) -> IndexEntry { - let reference_name = format!("refs/remotes/origin/{}", branch_name); - let reference = match self.repository.find_reference(&reference_name) - { - Ok(value) => value, - Err(error) => panic!("Could not find reference “{}”: {}", reference_name, error), - }; - // create a new blob with the file contents let object_id = match self.repository.blob_path(file_path) { @@ -220,24 +213,61 @@ impl BenchmarkRepository info!("Created object “{}” from “{}”", object_id, file_path.display()); + IndexEntry + { + ctime: IndexTime::new(0, 0), + mtime: IndexTime::new(0, 0), + dev: 0, + ino: 0, + mode: 0o100644, + uid: 0, + gid: 0, + file_size: 0, + id: object_id, + flags: 0, + flags_extended: 0, + path: result_file_path.to_string_lossy().as_bytes().to_owned(), + } + } + + pub fn commit_file(&self, file_path: &Path, result_file_path: &Path, branch_name: &str) + { + let reference_name = format!("refs/remotes/origin/{}", branch_name); + let reference = match self.repository.find_reference(&reference_name) + { + Ok(value) => value, + Err(error) => panic!("Could not find reference “{}”: {}", reference_name, error), + }; + let parent_tree = match reference.peel_to_tree() { Ok(value) => value, Err(error) => panic!("Could not peel reference to tree: {}", error), }; - let mut tree_builder = match self.repository.treebuilder(Some(&parent_tree)) - { - Ok(tree_builder) => tree_builder, - Err(error) => panic!("Could not obtain tree builder: {}", error), - }; - - tree_builder.insert("foobartest/test2/foobar", object_id, 0o100644); - - let tree_object_id = match tree_builder.write() + let mut index = match Index::new() { Ok(value) => value, - Err(error) => panic!("Could not write tree: {}", error), + Err(error) => panic!("Could not create index: {}", error), + }; + + match index.read_tree(&parent_tree) + { + Ok(value) => value, + Err(error) => panic!("Could not read parent tree into index: {}", error), + }; + + let index_entry = self.read_file_as_index_entry(file_path, result_file_path); + + if let Err(error) = index.add(&index_entry) + { + panic!("Could not add index entry: {}", error); + } + + let tree_object_id = match index.write_tree_to(&self.repository) + { + Ok(value) => value, + Err(error) => panic!("Could not write index to tree: {}", error), }; let tree = match self.repository.find_tree(tree_object_id)