UpdateRepo update git repo.
(target, repoRef, repoDir string, force bool)
| 156 | |
| 157 | // UpdateRepo update git repo. |
| 158 | func UpdateRepo(target, repoRef, repoDir string, force bool) error { |
| 159 | if !fileio.PathExists(repoDir) { |
| 160 | return errors.ErrDirNotExist |
| 161 | } |
| 162 | if !fileio.PathExists(filepath.Join(repoDir, ".git")) { |
| 163 | return errors.ErrNotGitDir |
| 164 | } |
| 165 | |
| 166 | // Check if repo is modified. |
| 167 | modified, err := IsModified(repoDir) |
| 168 | if err != nil { |
| 169 | return err |
| 170 | } |
| 171 | if modified { |
| 172 | if !force { |
| 173 | return fmt.Errorf("repository has local modifications, update is skipped - you can update forcibly with -f/--force") |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // Get default branch if repoRef is empty. |
| 178 | if repoRef == "" { |
| 179 | branch, err := GetDefaultBranch(target, repoDir) |
| 180 | if err != nil { |
| 181 | return err |
| 182 | } |
| 183 | repoRef = branch |
| 184 | } |
| 185 | |
| 186 | // Get repo URL. |
| 187 | repoUrl, err := GetRepoUrl(repoDir) |
| 188 | if err != nil { |
| 189 | return err |
| 190 | } |
| 191 | |
| 192 | // Update to a specific commit hash: fetch then hard-reset. |
| 193 | if CheckIsCommitHash(repoRef) { |
| 194 | if err := HardReset(repoDir, repoRef); err != nil { |
| 195 | return fmt.Errorf("failed to update %s to commit '%s' -> %w", target, repoRef, err) |
| 196 | } |
| 197 | return nil |
| 198 | } |
| 199 | |
| 200 | // Update to branch. |
| 201 | isBranch, err := CheckIfRemoteBranch(target, repoUrl, repoRef) |
| 202 | if err != nil { |
| 203 | return err |
| 204 | } |
| 205 | if isBranch { |
| 206 | commands := []string{ |
| 207 | "git reset --hard", |
| 208 | "git clean -ffdx", |
| 209 | "git fetch origin " + repoRef, |
| 210 | "git checkout -B " + repoRef + " origin/" + repoRef, |
| 211 | "git pull origin " + repoRef, |
| 212 | } |
| 213 | commandLine := strings.Join(commands, " && ") |
| 214 | executor := cmd.NewExecutor("[update "+target+"]", commandLine) |
| 215 | executor.SetWorkDir(repoDir) |
no test coverage detected