使用git下载指定的文件
(rootDir string, gitCloneUrl string, fileExpression string, branch string)
| 133 | |
| 134 | // 使用git下载指定的文件 |
| 135 | func (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" |
| 147 | if (Test-Path $checkoutFilePath) { $content = (Get-Content $checkoutFilePath)+"%v" } |
| 148 | Set-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 |
no test coverage detected