Skip to content

Commit

Permalink
backend: move constant functions first
Browse files Browse the repository at this point in the history
`root_commit_id()`, `root_change_id()`, and `empty_tree_id()` were
strangely ordered between `write_symlink()` and `read_tree().
  • Loading branch information
martinvonz committed Sep 19, 2023
1 parent 742df27 commit d575aae
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 50 deletions.
24 changes: 12 additions & 12 deletions cli/examples/custom-backend/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ impl Backend for JitBackend {
self.inner.change_id_length()
}

fn root_commit_id(&self) -> &CommitId {
self.inner.root_commit_id()
}

fn root_change_id(&self) -> &ChangeId {
self.inner.root_change_id()
}

fn empty_tree_id(&self) -> &TreeId {
self.inner.empty_tree_id()
}

fn read_file(&self, path: &RepoPath, id: &FileId) -> BackendResult<Box<dyn Read>> {
self.inner.read_file(path, id)
}
Expand All @@ -119,18 +131,6 @@ impl Backend for JitBackend {
self.inner.write_symlink(path, target)
}

fn root_commit_id(&self) -> &CommitId {
self.inner.root_commit_id()
}

fn root_change_id(&self) -> &ChangeId {
self.inner.root_change_id()
}

fn empty_tree_id(&self) -> &TreeId {
self.inner.empty_tree_id()
}

fn read_tree(&self, path: &RepoPath, id: &TreeId) -> BackendResult<Tree> {
self.inner.read_tree(path, id)
}
Expand Down
12 changes: 6 additions & 6 deletions lib/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,12 @@ pub trait Backend: Send + Sync + Debug {
/// The length of change IDs in bytes.
fn change_id_length(&self) -> usize;

fn root_commit_id(&self) -> &CommitId;

fn root_change_id(&self) -> &ChangeId;

fn empty_tree_id(&self) -> &TreeId;

fn read_file(&self, path: &RepoPath, id: &FileId) -> BackendResult<Box<dyn Read>>;

fn write_file(&self, path: &RepoPath, contents: &mut dyn Read) -> BackendResult<FileId>;
Expand All @@ -486,12 +492,6 @@ pub trait Backend: Send + Sync + Debug {

fn write_symlink(&self, path: &RepoPath, target: &str) -> BackendResult<SymlinkId>;

fn root_commit_id(&self) -> &CommitId;

fn root_change_id(&self) -> &ChangeId;

fn empty_tree_id(&self) -> &TreeId;

fn read_tree(&self, path: &RepoPath, id: &TreeId) -> BackendResult<Tree>;

fn write_tree(&self, path: &RepoPath, contents: &Tree) -> BackendResult<TreeId>;
Expand Down
24 changes: 12 additions & 12 deletions lib/src/git_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,18 @@ impl Backend for GitBackend {
CHANGE_ID_LENGTH
}

fn root_commit_id(&self) -> &CommitId {
&self.root_commit_id
}

fn root_change_id(&self) -> &ChangeId {
&self.root_change_id
}

fn empty_tree_id(&self) -> &TreeId {
&self.empty_tree_id
}

fn read_file(&self, _path: &RepoPath, id: &FileId) -> BackendResult<Box<dyn Read>> {
let git_blob_id = validate_git_object_id(id)?;
let locked_repo = self.repo.lock().unwrap();
Expand Down Expand Up @@ -530,18 +542,6 @@ impl Backend for GitBackend {
Ok(SymlinkId::new(oid.as_bytes().to_vec()))
}

fn root_commit_id(&self) -> &CommitId {
&self.root_commit_id
}

fn root_change_id(&self) -> &ChangeId {
&self.root_change_id
}

fn empty_tree_id(&self) -> &TreeId {
&self.empty_tree_id
}

fn read_tree(&self, _path: &RepoPath, id: &TreeId) -> BackendResult<Tree> {
if id == &self.empty_tree_id {
return Ok(Tree::default());
Expand Down
24 changes: 12 additions & 12 deletions lib/src/local_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ impl Backend for LocalBackend {
CHANGE_ID_LENGTH
}

fn root_commit_id(&self) -> &CommitId {
&self.root_commit_id
}

fn root_change_id(&self) -> &ChangeId {
&self.root_change_id
}

fn empty_tree_id(&self) -> &TreeId {
&self.empty_tree_id
}

fn read_file(&self, _path: &RepoPath, id: &FileId) -> BackendResult<Box<dyn Read>> {
let path = self.file_path(id);
let file = File::open(path).map_err(|err| map_not_found_err(err, id))?;
Expand Down Expand Up @@ -179,18 +191,6 @@ impl Backend for LocalBackend {
Ok(id)
}

fn root_commit_id(&self) -> &CommitId {
&self.root_commit_id
}

fn root_change_id(&self) -> &ChangeId {
&self.root_change_id
}

fn empty_tree_id(&self) -> &TreeId {
&self.empty_tree_id
}

fn read_tree(&self, _path: &RepoPath, id: &TreeId) -> BackendResult<Tree> {
let path = self.tree_path(id);
let buf = fs::read(path).map_err(|err| map_not_found_err(err, id))?;
Expand Down
16 changes: 8 additions & 8 deletions lib/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,6 @@ impl Store {
self.backend.change_id_length()
}

pub fn empty_tree_id(&self) -> &TreeId {
self.backend.empty_tree_id()
}

pub fn empty_merged_tree_id(&self) -> MergedTreeId {
MergedTreeId::Legacy(self.backend.empty_tree_id().clone())
}

pub fn root_commit_id(&self) -> &CommitId {
self.backend.root_commit_id()
}
Expand All @@ -92,6 +84,14 @@ impl Store {
self.backend.root_change_id()
}

pub fn empty_tree_id(&self) -> &TreeId {
self.backend.empty_tree_id()
}

pub fn empty_merged_tree_id(&self) -> MergedTreeId {
MergedTreeId::Legacy(self.backend.empty_tree_id().clone())
}

pub fn root_commit(self: &Arc<Self>) -> Commit {
self.get_commit(self.backend.root_commit_id()).unwrap()
}
Expand Down

0 comments on commit d575aae

Please sign in to comment.