git_backend: use file mode extensively in read_tree()

Both filemode() and kind() are calculated from the same underlying data,
and kind() is libgit2-specific API.
This commit is contained in:
Yuya Nishihara 2023-10-30 14:44:22 +09:00
parent b3c9cab12d
commit 9bd84c55e0

View File

@ -597,50 +597,47 @@ impl Backend for GitBackend {
for entry in git_tree.iter() { for entry in git_tree.iter() {
let name = let name =
str::from_utf8(entry.name_bytes()).map_err(|err| to_invalid_utf8_err(err, id))?; str::from_utf8(entry.name_bytes()).map_err(|err| to_invalid_utf8_err(err, id))?;
let (name, value) = match entry.kind().unwrap() { let (name, value) = match entry.filemode() {
git2::ObjectType::Tree => { 0o040000 => {
let id = TreeId::from_bytes(entry.id().as_bytes()); let id = TreeId::from_bytes(entry.id().as_bytes());
(name, TreeValue::Tree(id)) (name, TreeValue::Tree(id))
} }
git2::ObjectType::Blob => match entry.filemode() { 0o100644 => {
0o100644 => { let id = FileId::from_bytes(entry.id().as_bytes());
let id = FileId::from_bytes(entry.id().as_bytes()); if name.ends_with(CONFLICT_SUFFIX) {
if name.ends_with(CONFLICT_SUFFIX) { (
( &name[0..name.len() - CONFLICT_SUFFIX.len()],
&name[0..name.len() - CONFLICT_SUFFIX.len()], TreeValue::Conflict(ConflictId::from_bytes(entry.id().as_bytes())),
TreeValue::Conflict(ConflictId::from_bytes(entry.id().as_bytes())), )
) } else {
} else {
(
name,
TreeValue::File {
id,
executable: false,
},
)
}
}
0o100755 => {
let id = FileId::from_bytes(entry.id().as_bytes());
( (
name, name,
TreeValue::File { TreeValue::File {
id, id,
executable: true, executable: false,
}, },
) )
} }
0o120000 => { }
let id = SymlinkId::from_bytes(entry.id().as_bytes()); 0o100755 => {
(name, TreeValue::Symlink(id)) let id = FileId::from_bytes(entry.id().as_bytes());
} (
mode => panic!("unexpected file mode {mode:?}"), name,
}, TreeValue::File {
git2::ObjectType::Commit => { id,
executable: true,
},
)
}
0o120000 => {
let id = SymlinkId::from_bytes(entry.id().as_bytes());
(name, TreeValue::Symlink(id))
}
0o160000 => {
let id = CommitId::from_bytes(entry.id().as_bytes()); let id = CommitId::from_bytes(entry.id().as_bytes());
(name, TreeValue::GitSubmodule(id)) (name, TreeValue::GitSubmodule(id))
} }
kind => panic!("unexpected object type {kind:?}"), mode => panic!("unexpected file mode {mode:?}"),
}; };
tree.set(RepoPathComponent::from(name), value); tree.set(RepoPathComponent::from(name), value);
} }