HfHub.Commit.Operation (HfHub v0.2.0)

Copy Markdown View Source

Commit operation types for file manipulation.

Operations represent changes to be made in a single commit:

  • Add - Upload or update a file
  • Delete - Remove a file or folder
  • Copy - Copy an existing LFS file (efficient, no re-upload)

Examples

# Add a file from disk
add_op = HfHub.Commit.Operation.add("model.bin", "/path/to/model.bin")

# Add from binary content
add_op = HfHub.Commit.Operation.add("config.json", ~s({"hidden_size": 768}))

# Delete a file
del_op = HfHub.Commit.Operation.delete("old_model.bin")

# Delete a folder
del_op = HfHub.Commit.Operation.delete("old_weights/", is_folder: true)

# Copy an LFS file
copy_op = HfHub.Commit.Operation.copy("v1/model.bin", "v2/model.bin")

Summary

Functions

Creates an add operation from a file path or binary content.

Gets base64-encoded content (for regular uploads).

Creates a copy operation for an existing LFS file.

Creates a delete operation.

Checks if content is from a file path vs binary data.

Gets the content as binary (reads file if path).

Types

add()

@type add() :: %HfHub.Commit.Operation.Add{
  content: term(),
  is_committed: term(),
  is_uploaded: term(),
  path_in_repo: term(),
  upload_info: term(),
  upload_mode: term()
}

copy()

@type copy() :: %HfHub.Commit.Operation.Copy{
  dst_path: term(),
  src_path: term(),
  src_revision: term()
}

delete()

@type delete() :: %HfHub.Commit.Operation.Delete{
  is_folder: term(),
  path_in_repo: term()
}

t()

@type t() :: add() | delete() | copy()

Functions

add(path_in_repo, content, opts \\ [])

Creates an add operation from a file path or binary content.

Automatically computes UploadInfo (SHA256, size, sample) for the content.

Options

  • :upload_info - Pre-computed upload info (skips computation)

Examples

# From file path
op = Operation.add("model.safetensors", "/path/to/model.safetensors")

# From binary
op = Operation.add("config.json", Jason.encode!(%{hidden_size: 768}))

base64_content(add_op)

@spec base64_content(HfHub.Commit.Operation.Add.t()) ::
  {:ok, String.t()} | {:error, term()}

Gets base64-encoded content (for regular uploads).

copy(src_path, dst_path, opts \\ [])

Creates a copy operation for an existing LFS file.

Copy operations are efficient because they don't re-upload the file content. The file must already exist in the repository (or at src_revision).

Options

  • :src_revision - Source revision (default: current HEAD)

Examples

Operation.copy("v1/model.bin", "v2/model.bin")
Operation.copy("model.bin", "archive/model.bin", src_revision: "v1.0")

delete(path_in_repo, opts \\ [])

Creates a delete operation.

Options

  • :is_folder - Set to true to delete a folder and contents

Examples

Operation.delete("old_model.bin")
Operation.delete("old_weights/", is_folder: true)

file_path?(add)

@spec file_path?(HfHub.Commit.Operation.Add.t()) :: boolean()

Checks if content is from a file path vs binary data.

get_content(add_op)

@spec get_content(HfHub.Commit.Operation.Add.t()) ::
  {:ok, binary()} | {:error, term()}

Gets the content as binary (reads file if path).