diff options
author | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-03-19 13:51:52 +0700 |
---|---|---|
committer | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-03-19 13:51:52 +0700 |
commit | 7edf12c3f46522071d7b163d188b632ba231e2ef (patch) | |
tree | 11b80d479b193c99d2f24db21390520f7baa5486 | |
parent | 9f129580b53a11d7f0f2d10d3c62e4f3a596fd35 (diff) |
upd: tree serializationmain
-rw-r--r-- | src/object.rs | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/src/object.rs b/src/object.rs index b42412d..591c540 100644 --- a/src/object.rs +++ b/src/object.rs @@ -163,7 +163,7 @@ struct GitLeaf { sha: String, } -struct GitTree { +pub struct GitTree { records: Vec<GitLeaf>, } @@ -209,7 +209,7 @@ impl GitTree { } } - fn parse(data: &Vec<u8>) -> Self { + pub fn deserialize(data: &Vec<u8>) -> Self { let mut records: Vec<GitLeaf> = Vec::new(); let mut offset: usize = 0; while offset < data.len() { @@ -218,4 +218,31 @@ impl GitTree { } Self { records } } + + fn sort(&mut self) { + self.records.sort_by(|a, b| { + let mut a_path = a.path.clone(); + if PathBuf::from(&a_path).is_dir() { + a_path.push('/'); + } + let mut b_path = b.path.clone(); + if PathBuf::from(&b_path).is_dir() { + b_path.push('/'); + } + a_path.cmp(&b_path) + }) + } + + pub fn serialize(&mut self) -> Vec<u8> { + self.sort(); + let mut result: Vec<u8> = Vec::new(); + for record in &self.records { + result.extend(record.mode.as_bytes().to_vec()); + result.push(b' '); + result.extend(record.path.as_bytes().to_vec()); + result.push(0); + result.extend(record.sha.as_bytes().to_vec()); + } + result + } }
\ No newline at end of file |