diff options
author | Mx Kookie <kookie@spacekookie.de> | 2021-01-06 23:04:53 +0100 |
---|---|---|
committer | Mx Kookie <kookie@spacekookie.de> | 2021-01-06 23:04:53 +0100 |
commit | c8f3364c742c3528ee0709a44e82643fee2f8afe (patch) | |
tree | 8c63dc68b0b9af25a138083825de424546871f6f /apps/servers/octopus/supergit/src/files/tree_utils.rs | |
parent | fcc89b1b5bcbcb00ce02cb461b186ef0337312e3 (diff) |
octopus: supergit: add interface to query specific store paths
Diffstat (limited to '')
-rw-r--r-- | apps/servers/octopus/supergit/src/files/tree_utils.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/apps/servers/octopus/supergit/src/files/tree_utils.rs b/apps/servers/octopus/supergit/src/files/tree_utils.rs new file mode 100644 index 000000000000..55a6c2e0ffab --- /dev/null +++ b/apps/servers/octopus/supergit/src/files/tree_utils.rs @@ -0,0 +1,61 @@ +//! A set of tree utilities used internally in the tree.rs module + +use crate::HashId; +use git2::{Repository, Tree}; +use std::{path::PathBuf, sync::Arc}; + +/// Take a vector of path segments, and turn it into a valid offset path +/// +/// There are tests to make sure this function works properly. +/// Following are some example transformations. +/// +/// * vec![] -> "" +/// * vec!["foo"] -> "foo" +/// * vec!["foo", "bar", "baz"] -> "foo/bar/baz" +pub(super) fn path_segs_join(segments: Vec<&str>) -> String { + segments + .into_iter() + .fold(PathBuf::new(), |buf, seg| buf.join(seg)) + .as_path() + .to_str() + .unwrap() + .to_owned() +} + +/// Take an offset path inside the repository and split it into a vec +pub(super) fn path_split(path: &str) -> Vec<&str> { + path.split("/").filter(|s| s != &"").collect() +} + +/// Compare a path and entry to a target buffer +pub(super) fn path_cmp(target: &Vec<&str>, path: &str, entry: &str) -> bool { + let mut buf = path_split(path); + buf.push(entry); + + eprintln!("{:?}", buf); + + target == &buf +} + +/// Open a tree for a particular commit +pub(super) fn open_tree<'r>(repo: &'r Arc<Repository>, c: &HashId) -> Option<Tree<'r>> { + repo.find_commit(c.to_oid()).unwrap().tree().ok() +} + +#[test] +fn empty_path() { + assert_eq!(path_segs_join(vec![]), String::from("")); +} + +#[test] +fn one_path() { + assert_eq!(path_segs_join(vec!["foo".into()]), String::from("foo")); +} + +#[test] +fn nested_path() { + assert_eq!( + path_segs_join(vec!["foo".into(), "bar".into(), "baz".into()]), + String::from("foo/bar/baz") + ); +} |