pigui/src/pijul_glue/patches.rs

80 lines
2.1 KiB
Rust

use libpijul;
use libpijul::patch;
use libpijul::fs_representation::*;
use std::path::Path;
use errors::*;
use base64;
pub struct Patch {
pub name: String,
pub authors: String,
pub created: String,
pub signed: bool,
hash: libpijul::Hash,
}
impl Patch {
pub fn from_patch(patch: patch::Patch, hash: libpijul::Hash) -> Self {
let p = Patch {
name: patch.header().name.clone(),
authors: patch.header().authors.join(", "),
created: format!("{}", patch.header().timestamp.format("%F %T")),
signed: patch_is_signed(patch),
hash: hash,
};
return p;
}
pub fn hash_to_str(&self) -> String {
let config = base64::Config::new(base64::CharacterSet::Standard,
false,
false,
base64::LineWrap::NoWrap);
let s = self.hash.to_base64(config);
return s;
}
}
pub fn str_to_hash(s: String) -> Option<libpijul::Hash> {
return libpijul::Hash::from_base64(s.as_ref());
}
pub fn get_patch_headers(path: &str, branch: &str) -> Result<Vec<Patch>> {
let mut vec = Vec::new();
let repo = libpijul::Repository::open(pristine_dir(path), None)?;
let txn = repo.txn_begin().unwrap();
let branch = txn.get_branch(branch).unwrap();
let patches_ids = txn.iter_patches(&branch, None).map(|x| x.0);
for pid in patches_ids {
let hash_ref = txn.external_hash(pid);
let mp = libpijul::fs_representation::read_patch(Path::new(path),
hash_ref);
match mp {
Ok(p) => {
vec.push(Patch::from_patch(p, hash_ref.to_owned()));
},
Err(e) => warn!("Could not get patch for patch_id {:?}\nError: {:?}", pid, e),
}
}
return Ok(vec);
}
pub fn patch_is_signed(patch: patch::Patch) -> bool {
match patch {
patch::Patch::Unsigned(_) => return false,
patch::Patch::Signed{patch: _, signature: _ } => return true,
}
}