Repository navigation
Expand file tree
/
Copy pathdatabase.rs
More file actions
145 lines (118 loc) · 3.32 KB
/
Copy pathdatabase.rs
File metadata and controls
145 lines (118 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use crypto::digest::Digest;
use crypto::sha1::Sha1;
use std::collections::HashMap;
pub struct Database {
pub entries: HashMap<ObjectID, Record>,
}
impl Database {
pub fn new() -> Database {
Database {
entries: HashMap::new(),
}
}
pub fn insert(&mut self, record: Record) -> Option<ObjectID> {
let object_id_str = get_object_id(&record);
let object_id = ObjectID::from_oid_string(object_id_str);
self.entries.insert(object_id.clone(), record.clone());
Some(object_id)
}
pub fn fetch(&self, oid: &ObjectID) -> Option<&Record> {
self.entries.get(oid)
}
pub fn object_count(&self) -> usize {
self.entries.len() as usize
}
pub fn dump(&self) {
for (object_id, _) in &self.entries {
eprintln!("{}", object_id);
}
}
}
#[derive(Clone)]
pub struct ObjectID {
oid_bytes: [u8; 20],
oid_string: String,
}
impl ObjectID {
pub fn from_oid_bytes(bytes: [u8; 20]) -> ObjectID {
ObjectID {
oid_bytes: bytes,
oid_string: hex::encode(bytes),
}
}
pub fn from_oid_string(string: String) -> ObjectID {
let oid_bytes_vec = string.as_bytes();
let oid_bytes_slice = hex::decode(oid_bytes_vec).unwrap();
let mut bytes: [u8; 20] = [0; 20];
bytes.copy_from_slice(&oid_bytes_slice);
ObjectID {
oid_bytes: bytes,
oid_string: string,
}
}
}
impl std::cmp::PartialEq for ObjectID {
fn eq(&self, other: &Self) -> bool {
self.oid_string == other.oid_string
}
}
impl Eq for ObjectID {}
impl std::hash::Hash for ObjectID {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.oid_string.hash(state);
}
}
impl std::fmt::Display for ObjectID {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "ObjectID({})", self.oid_string)
}
}
#[derive(Clone, Debug)]
pub enum Record {
Commit {
data: Vec<u8>,
commit_info: String,
},
Tree {
data: Vec<u8>,
entries: Vec<crate::git::database::TreeEntry>,
},
Blob {
data: Vec<u8>,
},
}
#[derive(Clone, Debug)]
pub struct TreeEntry {
pub mode: Vec<u8>,
pub name: String,
pub oid: String,
}
pub fn get_object_id(record: &Record) -> String {
let (name, data) = match record {
Record::Commit { data, .. } => ("commit", data),
Record::Tree { data, .. } => ("tree", data),
Record::Blob { data, .. } => ("blob", data),
};
let size = data.len();
let mut hash_data: Vec<u8> = Vec::new();
let mut name_data = name.as_bytes().to_vec();
hash_data.append(&mut name_data);
hash_data.push(b' ');
let mut size_data = format!("{}", size).as_bytes().to_vec();
hash_data.append(&mut size_data);
hash_data.push(b'\0');
hash_data.append(&mut data.clone());
let mut hasher = Sha1::new();
hasher.input(&hash_data);
// let mut f = std::fs::File::open("dumped_object").unwrap();
// f.write(&hash_data);
if size == 377 {
std::fs::write("dumped_object", &hash_data).unwrap();
eprintln!(
"{} is \n{:?}\n\n",
hasher.result_str(),
hex::encode(hash_data)
);
}
hasher.result_str()
}