# HG changeset patch
# User Georges Racinet <georges.racinet@octobus.net>
# Date 1691434726 -7200
#      Mon Aug 07 20:58:46 2023 +0200
# Branch oldstable
# Node ID 87b6970c4ac6ac601b14be97c77d84ec42215630
# Parent  562c5d46b6d04d33ee9f3715c385f35a61c5fcb0
RHGitaly: allowing to stream TreeEntryResponse from BlobResponseChunk

We will soon implement `CommitService.TreeEntry`, which has to
behave like `GetBlobs` on one `RevisionPath` in case it turns out
the path is a blob.

diff --git a/rust/rhgitaly/src/message.rs b/rust/rhgitaly/src/message.rs
--- a/rust/rhgitaly/src/message.rs
+++ b/rust/rhgitaly/src/message.rs
@@ -12,6 +12,7 @@
 
 use crate::gitaly::{
     get_blobs_request::RevisionPath, CommitAuthor, GetBlobResponse, GetBlobsResponse, GitCommit,
+    TreeEntryResponse,
 };
 use crate::mercurial::ObjectMetadata;
 use prost_types::Timestamp;
@@ -306,6 +307,26 @@
     }
 }
 
+impl BlobResponseChunk for TreeEntryResponse {
+    fn with_metadata(chunk: &[u8], md: ObjectMetadata) -> Self {
+        TreeEntryResponse {
+            data: chunk.to_vec(),
+            mode: md.mode,
+            oid: md.oid,
+            size: md.size,
+            // TreeEntryResponse.ObjectType and shared.proto's ObjectType
+            // do differ by a shift of 1 (because of `UNKNOWN = 0` in the latter)
+            r#type: md.obj_type as i32 - 1,
+        }
+    }
+    fn only_data(chunk: &[u8]) -> Self {
+        TreeEntryResponse {
+            data: chunk.to_vec(),
+            ..Default::default()
+        }
+    }
+}
+
 /// Return a suitable empty [`GetBlobsResponse`] for unresolved [`RevisionPath`]
 ///
 /// The protocol specifies that all fields are to be empty, except for `revision` and `path`
diff --git a/rust/rhgitaly/src/service/blob.rs b/rust/rhgitaly/src/service/blob.rs
--- a/rust/rhgitaly/src/service/blob.rs
+++ b/rust/rhgitaly/src/service/blob.rs
@@ -200,7 +200,7 @@
     Ok(())
 }
 
-fn stream_blob<R: BlobResponseChunk>(
+pub fn stream_blob<R: BlobResponseChunk>(
     tx: &BlockingResponseSender<R>,
     data: Vec<u8>,
     metadata: ObjectMetadata,