功能开发完成

This commit is contained in:
2025-12-31 16:23:40 +08:00
parent 2b51050ca8
commit 6f0598a859
28 changed files with 5463 additions and 118 deletions

View File

@@ -88,7 +88,7 @@ func (m *CmdGitManager) Pull(ctx context.Context, localPath string, cred *models
// Checkout 切换分支
func (m *CmdGitManager) Checkout(ctx context.Context, localPath, branch string) error {
cmd := exec.CommandContext(ctx, m.gitPath, "-C", localPath, "checkout", branch)
output, err := cmd.CombinedOutput()
if err != nil {
logger.Logger.Error().
@@ -111,7 +111,7 @@ func (m *CmdGitManager) Checkout(ctx context.Context, localPath, branch string)
// GetCurrentBranch 获取当前分支
func (m *CmdGitManager) GetCurrentBranch(ctx context.Context, localPath string) (string, error) {
cmd := exec.CommandContext(ctx, m.gitPath, "-C", localPath, "rev-parse", "--abbrev-ref", "HEAD")
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("failed to get current branch: %w", err)
@@ -124,7 +124,7 @@ func (m *CmdGitManager) GetCurrentBranch(ctx context.Context, localPath string)
// GetHeadCommitHash 获取HEAD commit hash
func (m *CmdGitManager) GetHeadCommitHash(ctx context.Context, localPath string) (string, error) {
cmd := exec.CommandContext(ctx, m.gitPath, "-C", localPath, "rev-parse", "HEAD")
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("failed to get HEAD commit hash: %w", err)
@@ -137,15 +137,15 @@ func (m *CmdGitManager) GetHeadCommitHash(ctx context.Context, localPath string)
// CountCommits 统计提交次数
func (m *CmdGitManager) CountCommits(ctx context.Context, localPath, branch, fromDate string) (int, error) {
args := []string{"-C", localPath, "rev-list", "--count"}
if fromDate != "" {
args = append(args, "--since="+fromDate)
}
args = append(args, branch)
cmd := exec.CommandContext(ctx, m.gitPath, args...)
output, err := cmd.Output()
if err != nil {
return 0, fmt.Errorf("failed to count commits: %w", err)
@@ -160,6 +160,46 @@ func (m *CmdGitManager) CountCommits(ctx context.Context, localPath, branch, fro
return count, nil
}
// ListBranches 获取仓库分支列表
func (m *CmdGitManager) ListBranches(ctx context.Context, localPath string) ([]string, error) {
// 首先获取远程分支
cmd := exec.CommandContext(ctx, m.gitPath, "-C", localPath, "branch", "-r")
output, err := cmd.Output()
if err != nil {
logger.Logger.Error().
Err(err).
Str("local_path", localPath).
Msg("failed to list remote branches")
return nil, fmt.Errorf("failed to list branches: %w", err)
}
// 解析分支列表
lines := strings.Split(string(output), "\n")
branches := make([]string, 0)
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" || strings.Contains(line, "->") {
// 跳过空行和HEAD指针
continue
}
// 移除 origin/ 前缀
branch := strings.TrimPrefix(line, "origin/")
if branch != "" {
branches = append(branches, branch)
}
}
logger.Logger.Debug().
Str("local_path", localPath).
Int("count", len(branches)).
Msg("branches listed successfully")
return branches, nil
}
// injectCredentials 注入凭据到URL
func (m *CmdGitManager) injectCredentials(url string, cred *models.Credential) string {
if cred == nil || cred.Username == "" {

View File

@@ -10,22 +10,25 @@ import (
type Manager interface {
// Clone 克隆仓库
Clone(ctx context.Context, url, localPath string, cred *models.Credential) error
// Pull 拉取更新
Pull(ctx context.Context, localPath string, cred *models.Credential) error
// Checkout 切换分支
Checkout(ctx context.Context, localPath, branch string) error
// GetCurrentBranch 获取当前分支
GetCurrentBranch(ctx context.Context, localPath string) (string, error)
// GetHeadCommitHash 获取HEAD commit hash
GetHeadCommitHash(ctx context.Context, localPath string) (string, error)
// CountCommits 统计提交次数
CountCommits(ctx context.Context, localPath, branch, fromDate string) (int, error)
// ListBranches 获取分支列表
ListBranches(ctx context.Context, localPath string) ([]string, error)
// IsAvailable 检查Git是否可用
IsAvailable() bool
}