|
|
|
@@ -6,6 +6,7 @@ |
|
|
|
package models |
|
|
|
|
|
|
|
import ( |
|
|
|
"code.gitea.io/gitea/modules/git" |
|
|
|
"context" |
|
|
|
"crypto/md5" |
|
|
|
"errors" |
|
|
|
@@ -2519,3 +2520,53 @@ func UpdateRepositoryCommitNum(repo *Repository) error { |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
type RepoFile struct { |
|
|
|
CommitId string |
|
|
|
Content []byte |
|
|
|
} |
|
|
|
|
|
|
|
// ReadLatestFileRepo read latest version of file in repository |
|
|
|
// return commitId and file content |
|
|
|
func ReadLatestFileRepo(userName, repoName, refName, treePath string) (*RepoFile, error) { |
|
|
|
var err error |
|
|
|
repoPath := RepoPath(userName, repoName) |
|
|
|
gitRepo, err := git.OpenRepository(repoPath) |
|
|
|
if err != nil { |
|
|
|
log.Error("ReadLatestFileRepo error when OpenRepository,error=%v", err) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
commitID, err := gitRepo.GetBranchCommitID(refName) |
|
|
|
if err != nil { |
|
|
|
log.Error("ReadLatestFileRepo error when GetBranchCommitID,error=%v", err) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
commit, err := gitRepo.GetBranchCommit(refName) |
|
|
|
if err != nil { |
|
|
|
log.Error("ReadLatestFileRepo error when GetBranchCommit,error=%v", err) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
blob, err := commit.GetBlobByPath(treePath) |
|
|
|
if err != nil { |
|
|
|
log.Error("ReadLatestFileRepo error when GetBlobByPath,error=%v", err) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
reader, err := blob.DataAsync() |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
defer func() { |
|
|
|
if err = reader.Close(); err != nil { |
|
|
|
log.Error("ReadLatestFileRepo: Close: %v", err) |
|
|
|
} |
|
|
|
}() |
|
|
|
|
|
|
|
buf := make([]byte, 1024) |
|
|
|
n, _ := reader.Read(buf) |
|
|
|
if n >= 0 { |
|
|
|
buf = buf[:n] |
|
|
|
} |
|
|
|
return &RepoFile{CommitId: commitID, Content: buf}, nil |
|
|
|
} |