From d89e196bd0ca0adee29497a707e138689385e5b5 Mon Sep 17 00:00:00 2001 From: Alexandre de Souza Date: Wed, 1 Oct 2025 17:19:54 -0300 Subject: [PATCH] Streams the file content into a collectable --- lib/livebook/file_system/git.ex | 8 +++++++- lib/livebook/file_system/git/client.ex | 19 ++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/livebook/file_system/git.ex b/lib/livebook/file_system/git.ex index dbd007ce4..eeaf1cd32 100644 --- a/lib/livebook/file_system/git.ex +++ b/lib/livebook/file_system/git.ex @@ -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, %{ diff --git a/lib/livebook/file_system/git/client.ex b/lib/livebook/file_system/git/client.ex index 9b7d9aab9..c135490a5 100644 --- a/lib/livebook/file_system/git/client.ex +++ b/lib/livebook/file_system/git/client.ex @@ -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)}