2020-12-12 08:00:42 +00:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::fs;
|
|
|
|
use std::fs::File;
|
2021-03-14 17:46:35 +00:00
|
|
|
use std::io::{ErrorKind, Read, Write};
|
2022-09-23 21:03:43 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2022-02-10 06:01:13 +00:00
|
|
|
use blake2::{Blake2b512, Digest};
|
2020-12-12 08:00:42 +00:00
|
|
|
use tempfile::{NamedTempFile, PersistError};
|
2022-11-04 05:23:24 +00:00
|
|
|
use thrift::protocol::{TCompactInputProtocol, TCompactOutputProtocol, TSerializable};
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
use crate::backend::{
|
2022-09-19 00:33:39 +00:00
|
|
|
make_root_commit, Backend, BackendError, BackendResult, ChangeId, Commit, CommitId, Conflict,
|
|
|
|
ConflictId, ConflictPart, FileId, MillisSinceEpoch, Signature, SymlinkId, Timestamp, Tree,
|
|
|
|
TreeId, TreeValue,
|
2021-09-12 06:52:38 +00:00
|
|
|
};
|
2022-11-12 19:19:03 +00:00
|
|
|
use crate::content_hash::ContentHash;
|
2021-06-14 07:27:42 +00:00
|
|
|
use crate::file_util::persist_content_addressed_temp_file;
|
2022-11-04 05:23:24 +00:00
|
|
|
use crate::local_backend_model;
|
2021-06-06 05:50:14 +00:00
|
|
|
use crate::repo_path::{RepoPath, RepoPathComponent};
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
impl From<std::io::Error> for BackendError {
|
2020-12-12 08:00:42 +00:00
|
|
|
fn from(err: std::io::Error) -> Self {
|
2021-09-12 06:52:38 +00:00
|
|
|
BackendError::Other(err.to_string())
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
impl From<PersistError> for BackendError {
|
2020-12-12 08:00:42 +00:00
|
|
|
fn from(err: PersistError) -> Self {
|
2021-09-12 06:52:38 +00:00
|
|
|
BackendError::Other(err.to_string())
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 05:23:24 +00:00
|
|
|
impl From<thrift::Error> for BackendError {
|
|
|
|
fn from(err: thrift::Error) -> Self {
|
2021-09-12 06:52:38 +00:00
|
|
|
BackendError::Other(err.to_string())
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2021-09-12 06:52:38 +00:00
|
|
|
pub struct LocalBackend {
|
2020-12-12 08:00:42 +00:00
|
|
|
path: PathBuf,
|
2022-09-19 00:33:39 +00:00
|
|
|
root_commit_id: CommitId,
|
2020-12-12 08:00:42 +00:00
|
|
|
empty_tree_id: TreeId,
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
impl LocalBackend {
|
2022-09-23 21:03:43 +00:00
|
|
|
pub fn init(store_path: &Path) -> Self {
|
2020-12-12 08:00:42 +00:00
|
|
|
fs::create_dir(store_path.join("commits")).unwrap();
|
|
|
|
fs::create_dir(store_path.join("trees")).unwrap();
|
|
|
|
fs::create_dir(store_path.join("files")).unwrap();
|
|
|
|
fs::create_dir(store_path.join("symlinks")).unwrap();
|
|
|
|
fs::create_dir(store_path.join("conflicts")).unwrap();
|
2021-09-12 06:52:38 +00:00
|
|
|
let backend = Self::load(store_path);
|
|
|
|
let empty_tree_id = backend
|
2021-05-19 16:41:25 +00:00
|
|
|
.write_tree(&RepoPath::root(), &Tree::default())
|
2020-12-12 08:00:42 +00:00
|
|
|
.unwrap();
|
2021-09-12 06:52:38 +00:00
|
|
|
assert_eq!(empty_tree_id, backend.empty_tree_id);
|
|
|
|
backend
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 21:03:43 +00:00
|
|
|
pub fn load(store_path: &Path) -> Self {
|
2022-09-19 00:33:39 +00:00
|
|
|
let root_commit_id = CommitId::from_bytes(&[0; 64]);
|
2022-11-12 19:19:03 +00:00
|
|
|
let empty_tree_id = TreeId::from_hex("482ae5a29fbe856c7272f2071b8b0f0359ee2d89ff392b8a900643fbd0836eccd067b8bf41909e206c90d45d6e7d8b6686b93ecaee5fe1a9060d87b672101310");
|
2021-09-12 06:52:38 +00:00
|
|
|
LocalBackend {
|
2022-09-23 21:03:43 +00:00
|
|
|
path: store_path.to_path_buf(),
|
2022-09-19 00:33:39 +00:00
|
|
|
root_commit_id,
|
2020-12-12 08:00:42 +00:00
|
|
|
empty_tree_id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn file_path(&self, id: &FileId) -> PathBuf {
|
|
|
|
self.path.join("files").join(id.hex())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn symlink_path(&self, id: &SymlinkId) -> PathBuf {
|
|
|
|
self.path.join("symlinks").join(id.hex())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tree_path(&self, id: &TreeId) -> PathBuf {
|
|
|
|
self.path.join("trees").join(id.hex())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn commit_path(&self, id: &CommitId) -> PathBuf {
|
|
|
|
self.path.join("commits").join(id.hex())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn conflict_path(&self, id: &ConflictId) -> PathBuf {
|
|
|
|
self.path.join("conflicts").join(id.hex())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
fn not_found_to_backend_error(err: std::io::Error) -> BackendError {
|
2020-12-12 08:00:42 +00:00
|
|
|
if err.kind() == ErrorKind::NotFound {
|
2021-09-12 06:52:38 +00:00
|
|
|
BackendError::NotFound
|
2020-12-12 08:00:42 +00:00
|
|
|
} else {
|
2021-09-12 06:52:38 +00:00
|
|
|
BackendError::from(err)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
impl Backend for LocalBackend {
|
2022-09-23 04:01:38 +00:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"local"
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
fn hash_length(&self) -> usize {
|
|
|
|
64
|
|
|
|
}
|
|
|
|
|
2020-12-29 07:38:20 +00:00
|
|
|
fn git_repo(&self) -> Option<git2::Repository> {
|
2020-12-27 02:24:29 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
fn read_file(&self, _path: &RepoPath, id: &FileId) -> BackendResult<Box<dyn Read>> {
|
2021-06-14 07:18:38 +00:00
|
|
|
let path = self.file_path(id);
|
2021-09-12 06:52:38 +00:00
|
|
|
let file = File::open(path).map_err(not_found_to_backend_error)?;
|
2020-12-12 08:00:42 +00:00
|
|
|
Ok(Box::new(zstd::Decoder::new(file)?))
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
fn write_file(&self, _path: &RepoPath, contents: &mut dyn Read) -> BackendResult<FileId> {
|
2020-12-12 08:00:42 +00:00
|
|
|
let temp_file = NamedTempFile::new_in(&self.path)?;
|
|
|
|
let mut encoder = zstd::Encoder::new(temp_file.as_file(), 0)?;
|
2022-02-10 06:01:13 +00:00
|
|
|
let mut hasher = Blake2b512::new();
|
2020-12-12 08:00:42 +00:00
|
|
|
loop {
|
|
|
|
let mut buff: Vec<u8> = Vec::with_capacity(1 << 14);
|
|
|
|
let bytes_read;
|
|
|
|
unsafe {
|
|
|
|
buff.set_len(1 << 14);
|
|
|
|
bytes_read = contents.read(&mut buff)?;
|
|
|
|
buff.set_len(bytes_read);
|
|
|
|
}
|
|
|
|
if bytes_read == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
encoder.write_all(&buff)?;
|
2020-12-24 09:15:38 +00:00
|
|
|
hasher.update(&buff);
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
encoder.finish()?;
|
2021-11-10 16:43:17 +00:00
|
|
|
let id = FileId::new(hasher.finalize().to_vec());
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-06-14 07:27:42 +00:00
|
|
|
persist_content_addressed_temp_file(temp_file, self.file_path(&id))?;
|
2020-12-12 08:00:42 +00:00
|
|
|
Ok(id)
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
fn read_symlink(&self, _path: &RepoPath, id: &SymlinkId) -> Result<String, BackendError> {
|
2021-06-14 07:18:38 +00:00
|
|
|
let path = self.symlink_path(id);
|
2021-09-12 06:52:38 +00:00
|
|
|
let mut file = File::open(path).map_err(not_found_to_backend_error)?;
|
2020-12-12 08:00:42 +00:00
|
|
|
let mut target = String::new();
|
|
|
|
file.read_to_string(&mut target).unwrap();
|
|
|
|
Ok(target)
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
fn write_symlink(&self, _path: &RepoPath, target: &str) -> Result<SymlinkId, BackendError> {
|
2020-12-12 08:00:42 +00:00
|
|
|
let mut temp_file = NamedTempFile::new_in(&self.path)?;
|
2021-06-14 05:35:10 +00:00
|
|
|
temp_file.write_all(target.as_bytes())?;
|
2022-02-10 06:01:13 +00:00
|
|
|
let mut hasher = Blake2b512::new();
|
2022-10-09 17:08:12 +00:00
|
|
|
hasher.update(target.as_bytes());
|
2021-11-10 16:43:17 +00:00
|
|
|
let id = SymlinkId::new(hasher.finalize().to_vec());
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-06-14 07:27:42 +00:00
|
|
|
persist_content_addressed_temp_file(temp_file, self.symlink_path(&id))?;
|
2020-12-12 08:00:42 +00:00
|
|
|
Ok(id)
|
|
|
|
}
|
|
|
|
|
2022-09-19 00:33:39 +00:00
|
|
|
fn root_commit_id(&self) -> &CommitId {
|
|
|
|
&self.root_commit_id
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
fn empty_tree_id(&self) -> &TreeId {
|
|
|
|
&self.empty_tree_id
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
fn read_tree(&self, _path: &RepoPath, id: &TreeId) -> BackendResult<Tree> {
|
2021-06-14 07:18:38 +00:00
|
|
|
let path = self.tree_path(id);
|
2021-09-12 06:52:38 +00:00
|
|
|
let mut file = File::open(path).map_err(not_found_to_backend_error)?;
|
2022-11-04 05:23:24 +00:00
|
|
|
let thrift_tree = read_thrift(&mut file).unwrap();
|
|
|
|
Ok(tree_from_thrift(&thrift_tree))
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
fn write_tree(&self, _path: &RepoPath, tree: &Tree) -> BackendResult<TreeId> {
|
2020-12-12 08:00:42 +00:00
|
|
|
let temp_file = NamedTempFile::new_in(&self.path)?;
|
|
|
|
|
2022-11-04 05:23:24 +00:00
|
|
|
let thrift_tree = tree_to_thrift(tree);
|
|
|
|
write_thrift(&thrift_tree, &mut temp_file.as_file())?;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2022-11-12 19:19:03 +00:00
|
|
|
let id = TreeId::new(hash(tree).to_vec());
|
2021-06-14 07:27:42 +00:00
|
|
|
persist_content_addressed_temp_file(temp_file, self.tree_path(&id))?;
|
2020-12-12 08:00:42 +00:00
|
|
|
Ok(id)
|
|
|
|
}
|
|
|
|
|
2022-04-28 20:31:28 +00:00
|
|
|
fn read_conflict(&self, _path: &RepoPath, id: &ConflictId) -> BackendResult<Conflict> {
|
|
|
|
let path = self.conflict_path(id);
|
2021-09-12 06:52:38 +00:00
|
|
|
let mut file = File::open(path).map_err(not_found_to_backend_error)?;
|
2022-11-04 05:23:24 +00:00
|
|
|
let thrift_conflict = read_thrift(&mut file)?;
|
|
|
|
Ok(conflict_from_thrift(&thrift_conflict))
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 20:31:28 +00:00
|
|
|
fn write_conflict(&self, _path: &RepoPath, conflict: &Conflict) -> BackendResult<ConflictId> {
|
2020-12-12 08:00:42 +00:00
|
|
|
let temp_file = NamedTempFile::new_in(&self.path)?;
|
|
|
|
|
2022-11-04 05:23:24 +00:00
|
|
|
let thrift_conflict = conflict_to_thrift(conflict);
|
|
|
|
write_thrift(&thrift_conflict, &mut temp_file.as_file())?;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2022-11-12 19:19:03 +00:00
|
|
|
let id = ConflictId::new(hash(conflict).to_vec());
|
2022-04-28 20:31:28 +00:00
|
|
|
persist_content_addressed_temp_file(temp_file, self.conflict_path(&id))?;
|
2020-12-12 08:00:42 +00:00
|
|
|
Ok(id)
|
|
|
|
}
|
|
|
|
|
2022-04-28 20:31:28 +00:00
|
|
|
fn read_commit(&self, id: &CommitId) -> BackendResult<Commit> {
|
2022-09-19 00:33:39 +00:00
|
|
|
if *id == self.root_commit_id {
|
|
|
|
return Ok(make_root_commit(self.empty_tree_id.clone()));
|
|
|
|
}
|
|
|
|
|
2022-04-28 20:31:28 +00:00
|
|
|
let path = self.commit_path(id);
|
2021-09-12 06:52:38 +00:00
|
|
|
let mut file = File::open(path).map_err(not_found_to_backend_error)?;
|
2022-11-04 05:23:24 +00:00
|
|
|
let thrift_commit = read_thrift(&mut file).unwrap();
|
|
|
|
Ok(commit_from_thrift(&thrift_commit))
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 20:31:28 +00:00
|
|
|
fn write_commit(&self, commit: &Commit) -> BackendResult<CommitId> {
|
2020-12-12 08:00:42 +00:00
|
|
|
let temp_file = NamedTempFile::new_in(&self.path)?;
|
|
|
|
|
2022-11-04 05:23:24 +00:00
|
|
|
let thrift_commit = commit_to_thrift(commit);
|
|
|
|
write_thrift(&thrift_commit, &mut temp_file.as_file())?;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2022-11-12 19:19:03 +00:00
|
|
|
let id = CommitId::new(hash(commit).to_vec());
|
2022-04-28 20:31:28 +00:00
|
|
|
persist_content_addressed_temp_file(temp_file, self.commit_path(&id))?;
|
2020-12-12 08:00:42 +00:00
|
|
|
Ok(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 05:23:24 +00:00
|
|
|
fn read_thrift<T: TSerializable>(input: &mut impl Read) -> BackendResult<T> {
|
|
|
|
let mut protocol = TCompactInputProtocol::new(input);
|
|
|
|
Ok(TSerializable::read_from_in_protocol(&mut protocol).unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_thrift<T: TSerializable>(thrift_object: &T, output: &mut impl Write) -> BackendResult<()> {
|
|
|
|
let mut protocol = TCompactOutputProtocol::new(output);
|
|
|
|
thrift_object.write_to_out_protocol(&mut protocol)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn commit_to_thrift(commit: &Commit) -> local_backend_model::Commit {
|
|
|
|
let mut parents = vec![];
|
2020-12-12 08:00:42 +00:00
|
|
|
for parent in &commit.parents {
|
2022-11-04 05:23:24 +00:00
|
|
|
parents.push(parent.to_bytes());
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
2022-11-04 05:23:24 +00:00
|
|
|
let mut predecessors = vec![];
|
2020-12-12 08:00:42 +00:00
|
|
|
for predecessor in &commit.predecessors {
|
2022-11-04 05:23:24 +00:00
|
|
|
predecessors.push(predecessor.to_bytes());
|
|
|
|
}
|
|
|
|
let root_tree = commit.root_tree.to_bytes();
|
|
|
|
let change_id = commit.change_id.to_bytes();
|
|
|
|
let description = commit.description.clone();
|
|
|
|
let author = signature_to_thrift(&commit.author);
|
|
|
|
let committer = signature_to_thrift(&commit.committer);
|
|
|
|
local_backend_model::Commit::new(
|
|
|
|
parents,
|
|
|
|
predecessors,
|
|
|
|
root_tree,
|
|
|
|
change_id,
|
|
|
|
description,
|
|
|
|
author,
|
|
|
|
committer,
|
|
|
|
)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 05:23:24 +00:00
|
|
|
fn commit_from_thrift(thrift_commit: &local_backend_model::Commit) -> Commit {
|
|
|
|
let commit_id_from_thrift = |parent: &Vec<u8>| CommitId::new(parent.clone());
|
|
|
|
let parents = thrift_commit
|
|
|
|
.parents
|
|
|
|
.iter()
|
|
|
|
.map(commit_id_from_thrift)
|
|
|
|
.collect();
|
|
|
|
let predecessors = thrift_commit
|
2020-12-12 08:00:42 +00:00
|
|
|
.predecessors
|
|
|
|
.iter()
|
2022-11-04 05:23:24 +00:00
|
|
|
.map(commit_id_from_thrift)
|
2020-12-12 08:00:42 +00:00
|
|
|
.collect();
|
2022-11-04 05:23:24 +00:00
|
|
|
let root_tree = TreeId::new(thrift_commit.root_tree.to_vec());
|
|
|
|
let change_id = ChangeId::new(thrift_commit.change_id.to_vec());
|
2020-12-12 08:00:42 +00:00
|
|
|
Commit {
|
|
|
|
parents,
|
|
|
|
predecessors,
|
|
|
|
root_tree,
|
|
|
|
change_id,
|
2022-11-04 05:23:24 +00:00
|
|
|
description: thrift_commit.description.clone(),
|
|
|
|
author: signature_from_thrift(&thrift_commit.author),
|
|
|
|
committer: signature_from_thrift(&thrift_commit.committer),
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 05:23:24 +00:00
|
|
|
fn tree_to_thrift(tree: &Tree) -> local_backend_model::Tree {
|
|
|
|
let mut entries = vec![];
|
2020-12-12 08:00:42 +00:00
|
|
|
for entry in tree.entries() {
|
2022-11-04 05:23:24 +00:00
|
|
|
let name = entry.name().string();
|
|
|
|
let value = tree_value_to_thrift(entry.value());
|
|
|
|
let thrift_entry = local_backend_model::TreeEntry::new(name, value);
|
|
|
|
entries.push(thrift_entry);
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
2022-11-04 05:23:24 +00:00
|
|
|
local_backend_model::Tree::new(entries)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 05:23:24 +00:00
|
|
|
fn tree_from_thrift(thrift_tree: &local_backend_model::Tree) -> Tree {
|
2020-12-12 08:00:42 +00:00
|
|
|
let mut tree = Tree::default();
|
2022-11-04 05:23:24 +00:00
|
|
|
for thrift_tree_entry in &thrift_tree.entries {
|
|
|
|
let value = tree_value_from_thrift(&thrift_tree_entry.value);
|
|
|
|
tree.set(
|
|
|
|
RepoPathComponent::from(thrift_tree_entry.name.as_str()),
|
|
|
|
value,
|
|
|
|
);
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
tree
|
|
|
|
}
|
|
|
|
|
2022-11-04 05:23:24 +00:00
|
|
|
fn tree_value_to_thrift(value: &TreeValue) -> local_backend_model::TreeValue {
|
2020-12-12 08:00:42 +00:00
|
|
|
match value {
|
|
|
|
TreeValue::Normal { id, executable } => {
|
2022-11-04 05:23:24 +00:00
|
|
|
let file = local_backend_model::NormalFile::new(id.to_bytes(), *executable);
|
|
|
|
local_backend_model::TreeValue::NormalFile(file)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
2022-11-04 05:23:24 +00:00
|
|
|
TreeValue::Symlink(id) => local_backend_model::TreeValue::SymlinkId(id.to_bytes()),
|
2020-12-12 08:00:42 +00:00
|
|
|
TreeValue::GitSubmodule(_id) => {
|
|
|
|
panic!("cannot store git submodules");
|
|
|
|
}
|
2022-11-04 05:23:24 +00:00
|
|
|
TreeValue::Tree(id) => local_backend_model::TreeValue::TreeId(id.to_bytes()),
|
|
|
|
TreeValue::Conflict(id) => local_backend_model::TreeValue::ConflictId(id.to_bytes()),
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 05:23:24 +00:00
|
|
|
fn tree_value_from_thrift(thrift_tree_value: &local_backend_model::TreeValue) -> TreeValue {
|
|
|
|
match thrift_tree_value {
|
|
|
|
local_backend_model::TreeValue::NormalFile(file) => TreeValue::Normal {
|
|
|
|
id: FileId::from_bytes(&file.id),
|
|
|
|
executable: file.executable,
|
2020-12-12 08:00:42 +00:00
|
|
|
},
|
2022-11-04 05:23:24 +00:00
|
|
|
local_backend_model::TreeValue::SymlinkId(id) => {
|
|
|
|
TreeValue::Symlink(SymlinkId::from_bytes(id))
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
2022-11-04 05:23:24 +00:00
|
|
|
local_backend_model::TreeValue::TreeId(id) => TreeValue::Tree(TreeId::from_bytes(id)),
|
|
|
|
local_backend_model::TreeValue::ConflictId(id) => {
|
|
|
|
TreeValue::Conflict(ConflictId::from_bytes(id))
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 05:23:24 +00:00
|
|
|
fn signature_to_thrift(signature: &Signature) -> local_backend_model::Signature {
|
|
|
|
let timestamp = local_backend_model::Timestamp::new(
|
|
|
|
signature.timestamp.timestamp.0,
|
|
|
|
signature.timestamp.tz_offset,
|
|
|
|
);
|
|
|
|
local_backend_model::Signature::new(signature.name.clone(), signature.email.clone(), timestamp)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 05:23:24 +00:00
|
|
|
fn signature_from_thrift(thrift: &local_backend_model::Signature) -> Signature {
|
|
|
|
let timestamp = &thrift.timestamp;
|
2020-12-12 08:00:42 +00:00
|
|
|
Signature {
|
2022-11-04 05:23:24 +00:00
|
|
|
name: thrift.name.clone(),
|
|
|
|
email: thrift.email.clone(),
|
2020-12-12 08:00:42 +00:00
|
|
|
timestamp: Timestamp {
|
|
|
|
timestamp: MillisSinceEpoch(timestamp.millis_since_epoch),
|
|
|
|
tz_offset: timestamp.tz_offset,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 05:23:24 +00:00
|
|
|
fn conflict_to_thrift(conflict: &Conflict) -> local_backend_model::Conflict {
|
|
|
|
let mut removes = vec![];
|
2020-12-12 08:00:42 +00:00
|
|
|
for part in &conflict.removes {
|
2022-11-04 05:23:24 +00:00
|
|
|
removes.push(conflict_part_to_thrift(part));
|
|
|
|
}
|
|
|
|
let mut adds = vec![];
|
|
|
|
for part in &conflict.adds {
|
|
|
|
adds.push(conflict_part_to_thrift(part));
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
2022-11-04 05:23:24 +00:00
|
|
|
local_backend_model::Conflict::new(removes, adds)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 05:23:24 +00:00
|
|
|
fn conflict_from_thrift(thrift: &local_backend_model::Conflict) -> Conflict {
|
2020-12-12 08:00:42 +00:00
|
|
|
let mut conflict = Conflict::default();
|
2022-11-04 05:23:24 +00:00
|
|
|
for part in &thrift.removes {
|
|
|
|
conflict.removes.push(conflict_part_from_thrift(part))
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
2022-11-04 05:23:24 +00:00
|
|
|
for part in &thrift.adds {
|
|
|
|
conflict.adds.push(conflict_part_from_thrift(part))
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
conflict
|
|
|
|
}
|
|
|
|
|
2022-11-04 05:23:24 +00:00
|
|
|
fn conflict_part_from_thrift(thrift: &local_backend_model::ConflictPart) -> ConflictPart {
|
2020-12-12 08:00:42 +00:00
|
|
|
ConflictPart {
|
2022-11-04 05:23:24 +00:00
|
|
|
value: tree_value_from_thrift(&thrift.content),
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 05:23:24 +00:00
|
|
|
fn conflict_part_to_thrift(part: &ConflictPart) -> local_backend_model::ConflictPart {
|
|
|
|
local_backend_model::ConflictPart::new(tree_value_to_thrift(&part.value))
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
2022-11-12 19:19:03 +00:00
|
|
|
|
|
|
|
fn hash(x: &impl ContentHash) -> digest::Output<Blake2b512> {
|
|
|
|
let mut hasher = Blake2b512::default();
|
|
|
|
x.hash(&mut hasher);
|
|
|
|
hasher.finalize()
|
|
|
|
}
|