Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: replace protobuf crate with prost #932

Merged
merged 1 commit into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ jobs:
env:
RUST_BACKTRACE: 1

check-protos:
name: Check protos
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b
- uses: dtolnay/rust-toolchain@e645b0cf01249a964ec099494d38d2da0f0b349f
with:
toolchain: stable
- run: sudo apt update && sudo apt-get -y install protobuf-compiler
- name: Generate Rust code from .proto files
run: cargo run -p gen-protos
- name: Check for uncommitted changes
run: git diff --exit-code

rustfmt:
name: Check formatting
runs-on: ubuntu-latest
Expand Down
101 changes: 71 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ name = "diff_bench"
harness = false

[workspace]
members = ["lib", "lib/testutils"]
members = ["lib", "lib/testutils", "lib/gen-protos"]

[dependencies]
chrono = { version = "0.4.23", default-features = false, features = ["std", "clock"] }
Expand Down
3 changes: 1 addition & 2 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ readme = "../README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
protobuf-codegen = "3.2.0"
version_check = "0.9.4"

[dependencies]
Expand All @@ -32,7 +31,6 @@ maplit = "1.0.2"
once_cell = "1.16.0"
pest = "2.5.1"
pest_derive = "2.5.1"
protobuf = { version = "3.0.1", features = ["with-bytes"] }
regex = "1.7.0"
serde_json = "1.0.91"
tempfile = "3.3.0"
Expand All @@ -42,6 +40,7 @@ uuid = { version = "1.2.2", features = ["v4"] }
whoami = "1.2.3"
zstd = "0.12.1"
tracing = "0.1.37"
prost = "0.11.5"

[dev-dependencies]
assert_matches = "1.5.0"
Expand Down
18 changes: 3 additions & 15 deletions lib/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

fn main() {
let input = &[
"src/protos/op_store.proto",
"src/protos/store.proto",
"src/protos/working_copy.proto",
];
protobuf_codegen::Codegen::new()
.pure()
.inputs(input)
.include("src/protos")
.cargo_out_dir("protos")
.run_from_script();
fn main() -> std::io::Result<()> {
println!("cargo:rerun-if-changed=build.rs");
for file in input {
println!("cargo:rerun-if-changed={file}");
}

if let Some(true) = version_check::supports_feature("map_first_last") {
println!("cargo:rustc-cfg=feature=\"map_first_last\"");
}

Ok(())
}
8 changes: 8 additions & 0 deletions lib/gen-protos/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "gen-protos"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
prost-build = "0.11.5"
34 changes: 34 additions & 0 deletions lib/gen-protos/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2022 The Jujutsu Authors
//
// 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::io::Result;
Ralith marked this conversation as resolved.
Show resolved Hide resolved
use std::path::Path;

fn main() -> Result<()> {
let input = ["op_store.proto", "store.proto", "working_copy.proto"];

let root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
let protos_dir = root.join("src").join("protos");

prost_build::Config::new()
.out_dir(&protos_dir)
.include_file("mod.rs")
.compile_protos(
&input
.into_iter()
.map(|x| protos_dir.join(x))
.collect::<Vec<_>>(),
&[protos_dir],
)
}
13 changes: 7 additions & 6 deletions lib/src/git_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::sync::{Arc, Mutex};

use git2::Oid;
use itertools::Itertools;
use protobuf::Message;
use prost::Message;
use uuid::Uuid;

use crate::backend::{
Expand Down Expand Up @@ -126,17 +126,18 @@ fn signature_to_git(signature: &Signature) -> git2::Signature {
}

fn serialize_extras(commit: &Commit) -> Vec<u8> {
let mut proto = crate::protos::store::Commit::new();
proto.change_id = commit.change_id.to_bytes();
let mut proto = crate::protos::store::Commit {
change_id: commit.change_id.to_bytes(),
..Default::default()
};
for predecessor in &commit.predecessors {
proto.predecessors.push(predecessor.to_bytes());
}
proto.write_to_bytes().unwrap()
proto.encode_to_vec()
}

fn deserialize_extras(commit: &mut Commit, bytes: &[u8]) {
let mut cursor = Cursor::new(bytes);
let proto: crate::protos::store::Commit = Message::parse_from_reader(&mut cursor).unwrap();
let proto = crate::protos::store::Commit::decode(bytes).unwrap();
commit.change_id = ChangeId::new(proto.change_id);
for predecessor in &proto.predecessors {
commit.predecessors.push(CommitId::from_bytes(predecessor));
Expand Down
Loading