Skip to content

file-contents API returns size: 0 for directories, should return Git tree object size #34872

Closed
@rickyma

Description

@rickyma

Bug Description

When using the file-contents API to request information about a directory, the returned JSON always sets the "size" field to 0 for directories. This is misleading, as the Git tree object for the directory does have a size, which can be retrieved using native Git commands.

Example response:

[
  {
    "name": "batch_test",
    "path": "batch_test",
    "sha": "46b22fae3e0ac92b73989ca912a6d7dacd3975ab",
    "type": "dir",
    "size": 0,
    ...
  }
]

Expected Behavior

The "size" field for a directory should reflect the actual size of the corresponding Git tree object, which can be obtained using the following Git command:

git cat-file -s $(git rev-parse HEAD:batch_test)

This would provide the real byte size of the tree object, rather than always returning 0.

Why is this important?

  • Returning the actual tree object size is more accurate and useful for clients that want to understand the underlying Git data structure.
  • It aligns the API response with the real state of the repository, and is consistent with how file blobs are handled.

Steps to Reproduce

  1. Create a directory in a repository.
  2. Query the file-contents API for that directory.
  3. Observe that "size" is always 0, regardless of the actual tree object size.

Suggested Fix

  • For entries of "type": "dir", set the "size" field to the size of the corresponding Git tree object, as returned by git cat-file -s <tree-oid>.

Additional Information

  • Gitea version: Latest
  • Example API endpoint: /repos/{owner}/{repo}/file-contents

Thank you for your attention!

Metadata

Metadata

Assignees

No one assigned

    Labels

    issue/needs-feedbackFor bugs, we need more details. For features, the feature must be described in more detailtopic/apiConcerns mainly the APItype/proposalThe new feature has not been accepted yet but needs to be discussed first.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions