MCPcopy Create free account
hub / github.com/celer-pkg/celer / UpdateRepo

Function UpdateRepo

pkgs/git/control.go:158–252  ·  view source on GitHub ↗

UpdateRepo update git repo.

(target, repoRef, repoDir string, force bool)

Source from the content-addressed store, hash-verified

156
157// UpdateRepo update git repo.
158func 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)

Callers 4

CloneConfMethod · 0.92
updateConfRepoMethod · 0.92
updatePortsRepoMethod · 0.92
updatePortRepoMethod · 0.92

Calls 12

PathExistsFunction · 0.92
NewExecutorFunction · 0.92
IsModifiedFunction · 0.85
GetDefaultBranchFunction · 0.85
GetRepoUrlFunction · 0.85
CheckIsCommitHashFunction · 0.85
HardResetFunction · 0.85
CheckIfRemoteBranchFunction · 0.85
CheckIfRemoteTagFunction · 0.85
SetWorkDirMethod · 0.80
ExecuteOutputLiveMethod · 0.80
ExecuteMethod · 0.80

Tested by

no test coverage detected