feat: add git pull to update cached repositories from remote
Deploy PR-Helper / deploy (push) Successful in 34s
Deploy PR-Helper / deploy (push) Successful in 34s
- Replace dead FetchRemote with PullRepo (fetch + merge) in services/git.go - Add POST /api/repos/:id/pull endpoint with DB size/last_used update - Add '更新' button next to each cached repo in the index page
This commit is contained in:
+40
-8
@@ -441,19 +441,51 @@ func GetBranchCommits(repoPath, branchName string, maxCommits int) ([]CommitInfo
|
||||
return GetCommitLog(repo, branchName, maxCommits)
|
||||
}
|
||||
|
||||
// FetchRemote fetches latest changes for a repo.
|
||||
func FetchRemote(repoPath string) error {
|
||||
// PullRepo fetches and merges latest changes from origin into the current branch.
|
||||
func PullRepo(repoPath string) (*CloneResult, error) {
|
||||
repo, err := OpenRepo(repoPath)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
remote, err := repo.Remote("origin")
|
||||
|
||||
w, err := repo.Worktree()
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, fmt.Errorf("worktree: %w", err)
|
||||
}
|
||||
return remote.Fetch(&git.FetchOptions{
|
||||
Force: true,
|
||||
})
|
||||
|
||||
err = w.Pull(&git.PullOptions{})
|
||||
if err != nil && err != git.NoErrAlreadyUpToDate {
|
||||
return nil, fmt.Errorf("git pull: %w", err)
|
||||
}
|
||||
|
||||
result := &CloneResult{RepoPath: repoPath}
|
||||
|
||||
iter, err := repo.CommitObjects()
|
||||
if err == nil {
|
||||
_ = iter.ForEach(func(c *object.Commit) error {
|
||||
result.CommitNum++
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
branches, err := repo.Branches()
|
||||
if err == nil {
|
||||
_ = branches.ForEach(func(ref *plumbing.Reference) error {
|
||||
result.Branches = append(result.Branches, ref.Name().Short())
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
tags, err := repo.Tags()
|
||||
if err == nil {
|
||||
_ = tags.ForEach(func(ref *plumbing.Reference) error {
|
||||
result.Tags = append(result.Tags, ref.Name().Short())
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
result.SizeBytes = dirSize(repoPath)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// dirSize returns the total size of files in a directory.
|
||||
|
||||
Reference in New Issue
Block a user