Add function to read files from repository

This commit is contained in:
Patrick Lühne 2018-09-17 23:45:40 +02:00
parent dd333768b0
commit 094cb5a3bb
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
2 changed files with 38 additions and 2 deletions

View File

@ -3,6 +3,7 @@ extern crate pretty_env_logger;
use benchmark_repository::{BenchmarkRepository, TargetPath};
use std::io::{self, copy};
use std::path::Path;
fn main()
@ -11,11 +12,15 @@ fn main()
let benchmark_repository = BenchmarkRepository::new("git@git.luehne.de:patrick/tplp-planning-benchmark.git", Path::new("storage"), "git", "Potassco Bot", "bot@potassco.org");
let files = vec!
/*let files = vec!
[
TargetPath{source: &Path::new("/tmp/test"), destination: &Path::new("foobar/test")},
TargetPath{source: &Path::new("/tmp/test2"), destination: &Path::new("foobar/test-2")},
];
benchmark_repository.commit_files(&files[..], "test-results");
benchmark_repository.commit_files(&files[..], "test-results");*/
let mut file = benchmark_repository.read_file(Path::new("configurations.yml"), "test-config").unwrap();
copy(&mut file, &mut io::stdout());
}

View File

@ -7,6 +7,7 @@ extern crate indicatif;
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::io::Cursor;
use std::path::Path;
use std::string::String;
use std::str;
@ -326,6 +327,36 @@ impl BenchmarkRepository
let mut remote = self.repository.find_remote("origin").expect("");
remote.push(&[&push_refspec], Some(&mut push_options)).expect("couldnt push");
}
pub fn read_file(&self, file_path: &Path, branch_name: &str) -> Option<Cursor<Vec<u8>>>
{
let tip_reference_name = format!("refs/remotes/origin/{}", branch_name);
let tip_reference = match self.repository.find_reference(&tip_reference_name)
{
Ok(value) => value,
Err(error) => panic!("Could not find reference “{}”: {}", tip_reference_name, error),
};
let tree = match tip_reference.peel_to_tree()
{
Ok(value) => value,
Err(error) => panic!("Could not peel reference to tree: {}", error),
};
let object_id = match tree.get_path(file_path)
{
Ok(tree_entry) => tree_entry.id(),
Err(error) => return None,
};
let blob = match self.repository.find_blob(object_id)
{
Ok(blob) => blob,
Err(error) => return None,
};
Some(Cursor::new(blob.content().to_owned()))
}
}
#[cfg(test)]