MCPcopy Create free account
hub / github.com/OpenAtomFoundation/SmartIDE / SparseCheckout

Method SparseCheckout

cli/pkg/common/git.go:135–191  ·  view source on GitHub ↗

使用git下载指定的文件

(rootDir string, gitCloneUrl string, fileExpression string, branch string)

Source from the content-addressed store, hash-verified

133
134// 使用git下载指定的文件
135func (g gitOperation) SparseCheckout(rootDir string, gitCloneUrl string, fileExpression string, branch string) ([]string, error) {
136 if gitCloneUrl == "" {
137 return []string{}, errors.New("actual git repo url is null")
138 }
139 repoName := GetRepoName(gitCloneUrl)
140
141 //1. 配置
142 //1.1. command
143 sparseCheckout := fmt.Sprintf("echo \"%v\" >> .git/info/sparse-checkout", fileExpression) //TODO 会插入多条
144 if runtime.GOOS == "windows" {
145 sparseCheckout = fmt.Sprintf(`$content = "%v"
146$checkoutFilePath = ".git\\info\\sparse-checkout"
147if (Test-Path $checkoutFilePath) { $content = (Get-Content $checkoutFilePath)+"%v" }
148Set-Content $checkoutFilePath -Value $content -Encoding Ascii`,
149 "`n"+fileExpression, "`n"+fileExpression)
150 }
151 command := fmt.Sprintf(`
152 git init %v
153 cd %v
154 git config core.sparsecheckout true
155 %v
156 git remote add -f origin %v
157 git remote set-url origin %v
158 git fetch
159`, repoName, repoName, sparseCheckout, gitCloneUrl, gitCloneUrl)
160
161 //1.2. exec
162 err := EXEC.Realtime(command, rootDir)
163 if err != nil {
164 return []string{}, err
165 }
166
167 //2. checkout
168 repoDirPath := filepath.Join(rootDir, repoName)
169 if branch == "" {
170 branch = g.GetMainBranchName(repoDirPath)
171 }
172 remoteName := g.GetRemoteName(repoDirPath)
173 branchCommand := fmt.Sprintf(`
174 git checkout %v
175 git reset --hard %v/%v
176 git pull
177 `, branch, remoteName, branch)
178 err = EXEC.Realtime(branchCommand, repoDirPath)
179 if err != nil {
180 SmartIDELog.Warning(err.Error())
181 }
182
183 //3. 获取下载的文件列表
184 tempExpression := filepath.Join(rootDir, repoName, fileExpression)
185 files, err := filepath.Glob(tempExpression)
186 if err != nil {
187 return []string{}, err
188 }
189
190 return files, nil
191}
192

Callers 1

DownloadFilesByGitMethod · 0.80

Calls 8

GetMainBranchNameMethod · 0.95
GetRemoteNameMethod · 0.95
GetRepoNameFunction · 0.85
RealtimeMethod · 0.80
WarningMethod · 0.80
GlobMethod · 0.80
JoinMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected