/
pull.go
155 lines (148 loc) · 5.6 KB
/
pull.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
File: pull.go
Author: YJ
Email: yj1516268@outlook.com
Created Time: 2024-03-05 14:22:54
Description: 子命令 'pull' 的实现
*/
package cli
import (
"path/filepath"
"sort"
"strings"
"github.com/go-git/go-git/v5"
"github.com/gookit/color"
"github.com/pelletier/go-toml"
"github.com/yhyj/curator/general"
)
// RollingPullRepos 遍历拉取远端仓库的更改到本地
//
// 参数:
// - configTree: 解析 toml 配置文件得到的配置树
// - source: 远端仓库源,支持 'github' 和 'gitea',默认为 'github'
func RollingPullRepos(configTree *toml.Tree, source string) {
// 获取配置项
// pemfile := conf.Get("ssh.rsa_file")
// storagePath := conf.Get("storage.path").(string)
// repoNames := conf.Get("git.repos").([]interface{})
// 获取配置项
config, err := general.LoadConfigToStruct(configTree)
if err != nil {
color.Error.Println(err)
return
}
// 获取公钥
publicKeys, err := general.GetPublicKeysByGit(config.SSH.RsaFile)
if err != nil {
color.Error.Println(err)
return
}
// 拉取
color.Info.Tips("%s %s", general.FgWhiteText("Fetch from and merge with"), general.FgGreenText(source))
color.Info.Tips("%s: %s", general.FgWhiteText("Repository root"), general.PrimaryText(config.Storage.Path))
// 让用户选择需要 Pull 的存储库
selectedRepos, err := general.MultipleSelectionFilter(config.Git.Repos)
if err != nil {
color.Error.Println(err)
}
// 对所选的存储库进行排序
sort.Strings(selectedRepos)
// 遍历所选存储库名
for _, repoName := range selectedRepos {
repoPath := filepath.Join(config.Storage.Path, repoName)
// 开始拉取
color.Printf("%s %s %s: ", general.RunFlag, general.FgWhiteText("Pulling"), general.FgCyanText(repoName))
// 拉取前检测本地仓库是否存在
if general.FileExist(repoPath) {
isRepo, repo := general.IsLocalRepo(repoPath)
if isRepo {
worktree, leftCommit, rightCommit, err := general.PullRepo(repo, publicKeys)
if err != nil {
if err == git.NoErrAlreadyUpToDate {
color.Printf("%s %s\n", general.FgBlueText(general.LatestFlag), general.SecondaryText("Already up-to-date"))
// 尝试拉取子模块
submodules, err := general.GetLocalRepoSubmoduleInfo(worktree)
if err != nil {
color.Error.Println(err)
continue
}
if len(submodules) != 0 {
length := len(general.RunFlag) + len("Pulling") // 子模块缩进长度
for index, submodule := range submodules {
// 创建和主模块的连接符
joiner := func() string {
if index == len(submodules)-1 {
return general.JoinerFinish
}
return general.JoinerIng
}()
color.Printf("%s%s %s %s: ", strings.Repeat(" ", length), joiner, general.SubmoduleFlag, general.FgMagentaText(submodule.Config().Name))
submoduleRepo, err := submodule.Repository()
if err != nil {
color.Error.Println(err)
} else {
_, submoduleLeftCommit, submoduleRightCommit, err := general.PullRepo(submoduleRepo, publicKeys)
if err != nil {
if err == git.NoErrAlreadyUpToDate {
color.Printf("%s %s", general.FgBlueText(general.LatestFlag), general.SecondaryText("Already up-to-date"))
} else {
color.Error.Println(err)
}
} else {
color.Printf("%s %s %s %s", general.SuccessFlag, general.FgBlueText(submoduleLeftCommit.Hash.String()[:6]), general.FgWhiteText("-->"), general.FgGreenText(submoduleRightCommit.Hash.String()[:6]))
}
}
color.Println() // 子模块处理完成,换行
}
}
} else {
color.Error.Println(err)
}
} else {
color.Printf("%s %s %s %s\n", general.SuccessFlag, general.FgBlueText(leftCommit.Hash.String()[:6]), general.FgWhiteText("-->"), general.FgGreenText(rightCommit.Hash.String()[:6]))
// 尝试拉取子模块
submodules, err := general.GetLocalRepoSubmoduleInfo(worktree)
if err != nil {
color.Error.Println(err)
continue
}
if len(submodules) != 0 {
length := len(general.RunFlag) + len("Pulling") // 子模块缩进长度
for index, submodule := range submodules {
// 创建和主模块的连接符
joiner := func() string {
if index == len(submodules)-1 {
return general.JoinerFinish
}
return general.JoinerIng
}()
color.Printf("%s%s %s %s: ", strings.Repeat(" ", length), joiner, general.SubmoduleFlag, general.FgMagentaText(submodule.Config().Name))
submoduleRepo, err := submodule.Repository()
if err != nil {
color.Error.Println(err)
} else {
_, submoduleLeftCommit, submoduleRightCommit, err := general.PullRepo(submoduleRepo, publicKeys)
if err != nil {
if err == git.NoErrAlreadyUpToDate {
color.Printf("%s %s", general.FgBlueText(general.LatestFlag), general.SecondaryText("Already up-to-date"))
} else {
color.Error.Println(err)
}
} else {
color.Printf("%s %s %s %s", general.SuccessFlag, general.FgBlueText(submoduleLeftCommit.Hash.String()[:6]), general.FgWhiteText("-->"), general.FgGreenText(submoduleRightCommit.Hash.String()[:6]))
}
}
color.Println() // 子模块处理完成,换行
}
}
}
} else { // 非本地仓库
color.Printf("%s %s\n", general.ErrorFlag, general.ErrorText("Folder is not a local repository"))
}
} else {
color.Printf("%s %s\n", general.ErrorFlag, general.ErrorText("The local repository does not exist"))
}
// 添加一个延时,使输出更加顺畅
general.Delay(0.1)
}
}