feat: remove file from index
This commit is contained in:
+54
-14
@@ -58,7 +58,7 @@ impl IndexEntry {
|
||||
let cname = CStr::from_bytes_until_nul(&bytes[62..])?;
|
||||
let name = String::from(cname.to_str()?);
|
||||
|
||||
let entry_size = usize::div_ceil(62 + cname.count_bytes(), 8) * 8;
|
||||
let entry_size = usize::div_ceil(62 + cname.count_bytes() + 1, 8) * 8;
|
||||
|
||||
Ok((
|
||||
IndexEntry {
|
||||
@@ -159,16 +159,6 @@ impl Index {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn insert_entry(&mut self, entry: IndexEntry) {
|
||||
match self
|
||||
.entries
|
||||
.binary_search_by_key(&entry.name, |e| e.name.clone())
|
||||
{
|
||||
Ok(pos) => self.entries[pos] = entry,
|
||||
Err(pos) => self.entries.insert(pos, entry),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_file(&mut self, name: String, hash: [u8; 20]) -> Result<()> {
|
||||
let metadata = fs::metadata(&name)?;
|
||||
if metadata.is_dir() {
|
||||
@@ -212,6 +202,16 @@ impl Index {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn insert_entry(&mut self, entry: IndexEntry) {
|
||||
match self
|
||||
.entries
|
||||
.binary_search_by_key(&entry.name, |e| e.name.clone())
|
||||
{
|
||||
Ok(pos) => self.entries[pos] = entry,
|
||||
Err(pos) => self.entries.insert(pos, entry),
|
||||
}
|
||||
}
|
||||
|
||||
fn from_bytes(bytes: Vec<u8>) -> Result<Self> {
|
||||
match &bytes[0..4] {
|
||||
b"DIRC" => (),
|
||||
@@ -328,7 +328,7 @@ mod tests {
|
||||
hash: [0x19u8; 20],
|
||||
name: String::from("src/git_fs/head.rs"),
|
||||
};
|
||||
let (entry1, _) = IndexEntry::from_bytes(&vec![
|
||||
let (entry1, len1) = IndexEntry::from_bytes(&vec![
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0xa4,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x19,
|
||||
@@ -345,7 +345,7 @@ mod tests {
|
||||
hash: [0x19u8; 20],
|
||||
name: String::from("src/git_fs/head.r"),
|
||||
};
|
||||
let (entry2, _) = IndexEntry::from_bytes(&vec![
|
||||
let (entry2, len2) = IndexEntry::from_bytes(&vec![
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x19,
|
||||
@@ -361,7 +361,7 @@ mod tests {
|
||||
hash: [0x19u8; 20],
|
||||
name: String::from("src/git_fs/head.rst"),
|
||||
};
|
||||
let (entry3, _) = IndexEntry::from_bytes(&vec![
|
||||
let (entry3, len3) = IndexEntry::from_bytes(&vec![
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x19,
|
||||
@@ -375,6 +375,10 @@ mod tests {
|
||||
assert_eq!(entry1, expected1);
|
||||
assert_eq!(entry2, expected2);
|
||||
assert_eq!(entry3, expected3);
|
||||
|
||||
assert_eq!(len1, 88);
|
||||
assert_eq!(len2, 80);
|
||||
assert_eq!(len3, 88);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -418,4 +422,40 @@ mod tests {
|
||||
|
||||
assert_eq!(index, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn index_remove_entry() {
|
||||
let entry1 = IndexEntry {
|
||||
object_type: ObjectType::Regular,
|
||||
permissions: 0o644u16,
|
||||
hash: [0x19u8; 20],
|
||||
name: String::from("a"),
|
||||
};
|
||||
let entry2 = IndexEntry {
|
||||
object_type: ObjectType::Regular,
|
||||
permissions: 0o644u16,
|
||||
hash: [0x19u8; 20],
|
||||
name: String::from("b"),
|
||||
};
|
||||
let entry3 = IndexEntry {
|
||||
object_type: ObjectType::SymLink,
|
||||
permissions: 0,
|
||||
hash: [0x19u8; 20],
|
||||
name: String::from(".hello"),
|
||||
};
|
||||
|
||||
let mut index = Index {
|
||||
entries: vec![entry3.clone(), entry1, entry2.clone()],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
index.remove_file(String::from("a")).unwrap();
|
||||
|
||||
let expected = Index {
|
||||
entries: vec![entry3, entry2],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
assert_eq!(index, expected);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user