summaryrefslogtreecommitdiffstats
path: root/modules/git/commit.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/git/commit.go')
-rw-r--r--modules/git/commit.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/modules/git/commit.go b/modules/git/commit.go
index 8337e54fef..8c194ef502 100644
--- a/modules/git/commit.go
+++ b/modules/git/commit.go
@@ -17,6 +17,7 @@ import (
"strings"
"code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/util"
)
// Commit represents a git commit.
@@ -306,6 +307,35 @@ func (c *Commit) HasFile(filename string) (bool, error) {
return true, nil
}
+// GetFileContent reads a file content as a string or returns false if this was not possible
+func (c *Commit) GetFileContent(filename string, limit int) (string, error) {
+ entry, err := c.GetTreeEntryByPath(filename)
+ if err != nil {
+ return "", err
+ }
+
+ r, err := entry.Blob().DataAsync()
+ if err != nil {
+ return "", err
+ }
+ defer r.Close()
+
+ if limit > 0 {
+ bs := make([]byte, limit)
+ n, err := util.ReadAtMost(r, bs)
+ if err != nil {
+ return "", err
+ }
+ return string(bs[:n]), nil
+ }
+
+ bytes, err := io.ReadAll(r)
+ if err != nil {
+ return "", err
+ }
+ return string(bytes), nil
+}
+
// GetSubModules get all the sub modules of current revision git tree
func (c *Commit) GetSubModules() (*ObjectCache, error) {
if c.submoduleCache != nil {