1
0
Fork 0

Use index for writing trees

This commit is contained in:
Patrick Lühne 2018-09-17 17:14:47 +02:00
parent ec0feab38d
commit be2e15384b
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
2 changed files with 50 additions and 20 deletions

View File

@ -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");
}

View File

@ -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)