diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2017-10-14 13:36:04 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2017-10-14 13:36:04 +0200 |
commit | 6c387e893c3421cd68ca8b5251d09614fc68f5ba (patch) | |
tree | 87b90582f0f92b0d9fa13d29e2890ef6a4cf80f3 | |
parent | e74745c9fcbc95ed0ea551643325d82940b1bea1 (diff) |
Add RefStore::get_by_partial_hash()
-rw-r--r-- | lib/entry/libimagentryref/src/refstore.rs | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/lib/entry/libimagentryref/src/refstore.rs b/lib/entry/libimagentryref/src/refstore.rs index acfb2b8c..b1ac9381 100644 --- a/lib/entry/libimagentryref/src/refstore.rs +++ b/lib/entry/libimagentryref/src/refstore.rs @@ -22,7 +22,6 @@ use std::collections::BTreeMap; use std::fs::File; use libimagstore::store::FileLockEntry; -use libimagstore::storeid::StoreId; use libimagstore::storeid::IntoStoreId; use libimagstore::storeid::StoreIdIterator; use libimagstore::store::Store; @@ -48,6 +47,12 @@ pub trait RefStore { /// Returns None if the hash cannot be found. fn get_by_hash<'a>(&'a self, hash: String) -> Result<Option<FileLockEntry<'a>>>; + /// Get a Ref object from the store by (eventually partial) hash. + /// + /// If the hash is complete, `RefStore::get_by_hash()` should be used as it is cheaper. + /// If the hash comes from user input and thus might be abbreviated, this function can be used. + fn get_by_partitial_hash<'a>(&'a self, hash: &String) -> Result<Option<FileLockEntry<'a>>>; + /// Delete a ref by hash /// /// If the returned Result contains an error, the ref might not be deleted. @@ -118,6 +123,23 @@ impl RefStore for Store { .map_err(From::from) } + /// Get a Ref object from the store by (eventually partial) hash. + /// + /// If the hash is complete, `RefStore::get_by_hash()` should be used as it is cheaper. + /// If the hash comes from user input and thus might be abbreviated, this function can be used. + fn get_by_partitial_hash<'a>(&'a self, hash: &String) -> Result<Option<FileLockEntry<'a>>> { + for id in self.retrieve_for_module("ref")? { + let components_have_hash = id + .components() + .any(|c| c.as_os_str().to_str().map(|s| s.contains(hash)).unwrap_or(false)); + + if components_have_hash { + return self.get(id).map_err(From::from); + } + } + Ok(None) + } + /// Delete a ref by hash /// /// If the returned Result contains an error, the ref might not be deleted. |