Skip to content

Commit cb5ff46

Browse files
committed
lib: replace protobuf crate with prost
1 parent 6063fe8 commit cb5ff46

File tree

16 files changed

+627
-204
lines changed

16 files changed

+627
-204
lines changed

.github/workflows/build.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ jobs:
4242
env:
4343
RUST_BACKTRACE: 1
4444

45+
check-protos:
46+
name: Check protos
47+
runs-on: ubuntu-latest
48+
steps:
49+
- uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b
50+
- uses: dtolnay/rust-toolchain@e645b0cf01249a964ec099494d38d2da0f0b349f
51+
with:
52+
toolchain: stable
53+
- run: sudo apt update && sudo apt-get -y install protobuf-compiler
54+
- name: Generate Rust code from .proto files
55+
run: cargo run -p gen-protos
56+
- name: Check for uncommitted changes
57+
run: git diff --exit-code
58+
4559
rustfmt:
4660
name: Check formatting
4761
runs-on: ubuntu-latest

Cargo.lock

Lines changed: 71 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ name = "diff_bench"
3131
harness = false
3232

3333
[workspace]
34-
members = ["lib", "lib/testutils"]
34+
members = ["lib", "lib/testutils", "lib/gen-protos"]
3535

3636
[dependencies]
3737
chrono = { version = "0.4.23", default-features = false, features = ["std", "clock"] }

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ cargo install --git https://github.com/martinvonz/jj.git --bin jj
228228

229229

230230
### Windows
231+
Due to [a protobuf-src
232+
limitation](https://github.com/MaterializeInc/rust-protobuf-native/issues/4),
233+
`protoc` must be provided manually. See ([prost-build's
234+
instructions](https://github.com/tokio-rs/prost/tree/master/prost-build#protoc))
235+
for details.
231236

232237
Run:
233238
```shell script

lib/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ readme = "../README.md"
1414
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1515

1616
[build-dependencies]
17-
protobuf-codegen = "3.2.0"
1817
version_check = "0.9.4"
1918

2019
[dependencies]
@@ -32,7 +31,6 @@ maplit = "1.0.2"
3231
once_cell = "1.16.0"
3332
pest = "2.5.1"
3433
pest_derive = "2.5.1"
35-
protobuf = { version = "3.0.1", features = ["with-bytes"] }
3634
regex = "1.7.0"
3735
serde_json = "1.0.91"
3836
tempfile = "3.3.0"
@@ -42,6 +40,7 @@ uuid = { version = "1.2.2", features = ["v4"] }
4240
whoami = "1.2.3"
4341
zstd = "0.12.1"
4442
tracing = "0.1.37"
43+
prost = "0.11.5"
4544

4645
[dev-dependencies]
4746
assert_matches = "1.5.0"

lib/build.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,12 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
fn main() {
16-
let input = &[
17-
"src/protos/op_store.proto",
18-
"src/protos/store.proto",
19-
"src/protos/working_copy.proto",
20-
];
21-
protobuf_codegen::Codegen::new()
22-
.pure()
23-
.inputs(input)
24-
.include("src/protos")
25-
.cargo_out_dir("protos")
26-
.run_from_script();
15+
fn main() -> std::io::Result<()> {
2716
println!("cargo:rerun-if-changed=build.rs");
28-
for file in input {
29-
println!("cargo:rerun-if-changed={file}");
30-
}
3117

3218
if let Some(true) = version_check::supports_feature("map_first_last") {
3319
println!("cargo:rustc-cfg=feature=\"map_first_last\"");
3420
}
21+
22+
Ok(())
3523
}

lib/gen-protos/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "gen-protos"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[dependencies]
8+
prost-build = "0.11.5"

lib/gen-protos/src/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::io::Result;
2+
use std::path::Path;
3+
4+
fn main() -> Result<()> {
5+
let input = ["op_store.proto", "store.proto", "working_copy.proto"];
6+
7+
let root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
8+
let protos_dir = root.join("src").join("protos");
9+
10+
prost_build::Config::new()
11+
.out_dir(&protos_dir)
12+
.include_file("mod.rs")
13+
.compile_protos(
14+
&input
15+
.into_iter()
16+
.map(|x| protos_dir.join(x))
17+
.collect::<Vec<_>>(),
18+
&[protos_dir],
19+
)
20+
}

lib/src/git_backend.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::sync::{Arc, Mutex};
2020

2121
use git2::Oid;
2222
use itertools::Itertools;
23-
use protobuf::Message;
23+
use prost::Message;
2424
use uuid::Uuid;
2525

2626
use crate::backend::{
@@ -126,17 +126,18 @@ fn signature_to_git(signature: &Signature) -> git2::Signature {
126126
}
127127

128128
fn serialize_extras(commit: &Commit) -> Vec<u8> {
129-
let mut proto = crate::protos::store::Commit::new();
130-
proto.change_id = commit.change_id.to_bytes();
129+
let mut proto = crate::protos::store::Commit {
130+
change_id: commit.change_id.to_bytes(),
131+
..Default::default()
132+
};
131133
for predecessor in &commit.predecessors {
132134
proto.predecessors.push(predecessor.to_bytes());
133135
}
134-
proto.write_to_bytes().unwrap()
136+
proto.encode_to_vec()
135137
}
136138

137139
fn deserialize_extras(commit: &mut Commit, bytes: &[u8]) {
138-
let mut cursor = Cursor::new(bytes);
139-
let proto: crate::protos::store::Commit = Message::parse_from_reader(&mut cursor).unwrap();
140+
let proto = crate::protos::store::Commit::decode(bytes).unwrap();
140141
commit.change_id = ChangeId::new(proto.change_id);
141142
for predecessor in &proto.predecessors {
142143
commit.predecessors.push(CommitId::from_bytes(predecessor));

0 commit comments

Comments
 (0)