Streams the file content into a collectable

This commit is contained in:
Alexandre de Souza 2025-10-01 17:19:54 -03:00
parent 362a27f850
commit d89e196bd0
No known key found for this signature in database
GPG key ID: E39228FFBA346545
2 changed files with 21 additions and 6 deletions

View file

@ -116,7 +116,13 @@ defimpl Livebook.FileSystem, for: Livebook.FileSystem.Git do
def write_stream_halt(_file_system, _state), do: raise("not implemented")
def read_stream_into(_file_system, _path, _collectable), do: raise("not implemented")
def read_stream_into(file_system, path, collectable) do
try do
Git.Client.stream_file(file_system, path, collectable)
rescue
error in File.Error -> FileSystem.Utils.posix_error(error.reason)
end
end
def load(file_system, %{"hub_id" => _} = fields) do
load(file_system, %{

View file

@ -43,7 +43,20 @@ defmodule Livebook.FileSystem.Git.Client do
path = relative_path(path)
with {:ok, git_dir} <- fetch_repository(file_system) do
show(git_dir, file_system.branch, path)
git(git_dir, ["show", "#{file_system.branch}:#{path}"])
end
end
@doc """
Streams the content of the given file from given repository.
"""
@spec stream_file(FileSystem.Git.t(), String.t(), Collectable.t()) ::
{:ok, String.t()} | {:error, FileSystem.error()}
def stream_file(%FileSystem.Git{} = file_system, path, collectable) do
path = relative_path(path)
with {:ok, git_dir} <- fetch_repository(file_system) do
git(git_dir, ["show", "#{file_system.branch}:#{path}"], into: collectable)
end
end
@ -90,10 +103,6 @@ defmodule Livebook.FileSystem.Git.Client do
end
end
defp show(git_dir, branch, path) do
git(git_dir, ["show", "#{branch}:#{path}"])
end
defp rev_parse(git_dir, branch, path) do
with {:ok, etag} <- git(git_dir, ["rev-parse", "#{branch}:#{path}"]) do
{:ok, String.trim(etag)}