mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-03 14:27:32 +08:00
@@ -17,6 +17,7 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
@@ -50,7 +51,7 @@ func NewFileCommitService() *FileCommitService {
|
||||
}
|
||||
|
||||
// CreateCommit creates a new commit for a workspace folder
|
||||
func (s *FileCommitService) CreateCommit(folderID, authorID, message string, changes []entity.FileChange) (*entity.FileCommit, error) {
|
||||
func (s *FileCommitService) CreateCommit(ctx context.Context, folderID, authorID, message string, changes []entity.FileChange) (*entity.FileCommit, error) {
|
||||
// 1. Get the latest commit for this folder
|
||||
latestCommit, _ := s.commitDAO.GetLatestByFolderID(folderID)
|
||||
|
||||
@@ -233,22 +234,22 @@ func (s *FileCommitService) CreateCommit(folderID, authorID, message string, cha
|
||||
}
|
||||
|
||||
// ListCommits lists commits for a workspace folder with pagination
|
||||
func (s *FileCommitService) ListCommits(folderID string, page, pageSize int, orderBy string, desc bool) ([]*entity.FileCommit, int64, error) {
|
||||
func (s *FileCommitService) ListCommits(ctx context.Context, folderID string, page, pageSize int, orderBy string, desc bool) ([]*entity.FileCommit, int64, error) {
|
||||
return s.commitDAO.ListByFolderID(folderID, page, pageSize, orderBy, desc)
|
||||
}
|
||||
|
||||
// GetCommit gets a single commit by ID
|
||||
func (s *FileCommitService) GetCommit(commitID string) (*entity.FileCommit, error) {
|
||||
func (s *FileCommitService) GetCommit(ctx context.Context, commitID string) (*entity.FileCommit, error) {
|
||||
return s.commitDAO.GetByID(commitID)
|
||||
}
|
||||
|
||||
// ListCommitFiles lists all file change items for a commit
|
||||
func (s *FileCommitService) ListCommitFiles(commitID string) ([]*entity.FileCommitItem, error) {
|
||||
func (s *FileCommitService) ListCommitFiles(ctx context.Context, commitID string) ([]*entity.FileCommitItem, error) {
|
||||
return s.commitItemDAO.ListByCommitID(commitID)
|
||||
}
|
||||
|
||||
// DiffCommits compares two commits and returns the diff
|
||||
func (s *FileCommitService) DiffCommits(fromID, toID string) ([]entity.DiffEntry, error) {
|
||||
func (s *FileCommitService) DiffCommits(ctx context.Context, fromID, toID string) ([]entity.DiffEntry, error) {
|
||||
fromItems, err := s.commitItemDAO.ListByCommitID(fromID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -348,7 +349,7 @@ func (s *FileCommitService) DiffCommits(fromID, toID string) ([]entity.DiffEntry
|
||||
|
||||
// GetUncommittedChanges gets uncommitted changes for a workspace folder.
|
||||
// Recursively scans all sub-folders.
|
||||
func (s *FileCommitService) GetUncommittedChanges(folderID string) ([]entity.DiffEntry, error) {
|
||||
func (s *FileCommitService) GetUncommittedChanges(ctx context.Context, folderID string) ([]entity.DiffEntry, error) {
|
||||
// Get latest commit tree state
|
||||
latest, err := s.commitDAO.GetLatestByFolderID(folderID)
|
||||
committedFiles := make(map[string]map[string]interface{})
|
||||
@@ -364,7 +365,7 @@ func (s *FileCommitService) GetUncommittedChanges(folderID string) ([]entity.Dif
|
||||
}
|
||||
|
||||
// Get all live files recursively under this folder
|
||||
liveMap := s.collectAllFilesRecursive(folderID)
|
||||
liveMap := s.collectAllFilesRecursive(ctx, folderID)
|
||||
|
||||
var changes []entity.DiffEntry
|
||||
processed := make(map[string]bool)
|
||||
@@ -417,17 +418,17 @@ func (s *FileCommitService) GetUncommittedChanges(folderID string) ([]entity.Dif
|
||||
}
|
||||
|
||||
// collectAllFilesRecursive recursively collects all non-folder files under a folder.
|
||||
func (s *FileCommitService) collectAllFilesRecursive(folderID string) map[string]*entity.File {
|
||||
func (s *FileCommitService) collectAllFilesRecursive(ctx context.Context, folderID string) map[string]*entity.File {
|
||||
result := make(map[string]*entity.File)
|
||||
// Direct files (non-folder)
|
||||
files, _ := s.fileDAO.ListNonFolderByParentID(folderID)
|
||||
files, _ := s.fileDAO.ListNonFolderByParentID(ctx, dao.DB, folderID)
|
||||
for _, f := range files {
|
||||
result[f.ID] = f
|
||||
}
|
||||
// Sub-folders — recurse
|
||||
subFolders, _ := s.fileDAO.ListFolderByParentID(folderID)
|
||||
subFolders, _ := s.fileDAO.ListFolderByParentID(ctx, dao.DB, folderID)
|
||||
for _, sf := range subFolders {
|
||||
sub := s.collectAllFilesRecursive(sf.ID)
|
||||
sub := s.collectAllFilesRecursive(ctx, sf.ID)
|
||||
for k, v := range sub {
|
||||
result[k] = v
|
||||
}
|
||||
@@ -436,7 +437,7 @@ func (s *FileCommitService) collectAllFilesRecursive(folderID string) map[string
|
||||
}
|
||||
|
||||
// GetCommitTree gets the tree state snapshot for a commit as a hierarchical tree.
|
||||
func (s *FileCommitService) GetCommitTree(commitID string) (map[string]interface{}, error) {
|
||||
func (s *FileCommitService) GetCommitTree(ctx context.Context, commitID string) (map[string]interface{}, error) {
|
||||
commit, err := s.commitDAO.GetByID(commitID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -445,15 +446,15 @@ func (s *FileCommitService) GetCommitTree(commitID string) (map[string]interface
|
||||
return map[string]interface{}{"id": commit.FolderID, "name": "", "type": "folder", "children": []interface{}{}}, nil
|
||||
}
|
||||
var flat map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(*commit.TreeState), &flat); err != nil {
|
||||
if err = json.Unmarshal([]byte(*commit.TreeState), &flat); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.buildHierarchicalTree(flat, commit.FolderID), nil
|
||||
return s.buildHierarchicalTree(ctx, flat, commit.FolderID), nil
|
||||
}
|
||||
|
||||
// buildHierarchicalTree builds a recursive tree from a flat tree_state map.
|
||||
// Sub-folder hierarchy is resolved from the File table's parent_id.
|
||||
func (s *FileCommitService) buildHierarchicalTree(flat map[string]interface{}, rootFolderID string) map[string]interface{} {
|
||||
func (s *FileCommitService) buildHierarchicalTree(ctx context.Context, flat map[string]interface{}, rootFolderID string) map[string]interface{} {
|
||||
// Collect all unique folder IDs
|
||||
folderIDs := map[string]bool{rootFolderID: true}
|
||||
for _, v := range flat {
|
||||
@@ -470,7 +471,7 @@ func (s *FileCommitService) buildHierarchicalTree(flat map[string]interface{}, r
|
||||
folderParentMap := make(map[string]string)
|
||||
for fid := range folderIDs {
|
||||
if fid != rootFolderID {
|
||||
if f, err := s.fileDAO.GetByID(fid); err == nil {
|
||||
if f, err := s.fileDAO.GetByID(ctx, dao.DB, fid); err == nil {
|
||||
folderParentMap[fid] = f.ParentID
|
||||
}
|
||||
}
|
||||
@@ -501,7 +502,7 @@ func (s *FileCommitService) buildHierarchicalTree(flat map[string]interface{}, r
|
||||
var buildNode func(nodeID string) map[string]interface{}
|
||||
buildNode = func(nodeID string) map[string]interface{} {
|
||||
nodeName := nodeID
|
||||
if f, err := s.fileDAO.GetByID(nodeID); err == nil {
|
||||
if f, err := s.fileDAO.GetByID(ctx, dao.DB, nodeID); err == nil {
|
||||
nodeName = f.Name
|
||||
}
|
||||
node := map[string]interface{}{
|
||||
@@ -539,7 +540,7 @@ func (s *FileCommitService) buildHierarchicalTree(flat map[string]interface{}, r
|
||||
}
|
||||
|
||||
// GetCommitFileContent gets file content as it existed in a given commit
|
||||
func (s *FileCommitService) GetCommitFileContent(folderID, commitID, fileID string) ([]byte, error) {
|
||||
func (s *FileCommitService) GetCommitFileContent(ctx context.Context, folderID, commitID, fileID string) ([]byte, error) {
|
||||
_, err := s.commitDAO.GetByID(commitID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("commit not found: %w", err)
|
||||
@@ -577,7 +578,7 @@ func (s *FileCommitService) GetCommitFileContent(folderID, commitID, fileID stri
|
||||
}
|
||||
|
||||
// GetFileVersionHistory gets version history for a specific file
|
||||
func (s *FileCommitService) GetFileVersionHistory(fileID string) ([]entity.VersionEntry, error) {
|
||||
func (s *FileCommitService) GetFileVersionHistory(ctx context.Context, fileID string) ([]entity.VersionEntry, error) {
|
||||
items, err := s.commitItemDAO.ListByFileID(fileID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -585,7 +586,8 @@ func (s *FileCommitService) GetFileVersionHistory(fileID string) ([]entity.Versi
|
||||
|
||||
var versions []entity.VersionEntry
|
||||
for _, item := range items {
|
||||
commit, err := s.commitDAO.GetByID(item.CommitID)
|
||||
var commit *entity.FileCommit
|
||||
commit, err = s.commitDAO.GetByID(item.CommitID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user