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

Function HardReset

pkgs/git/control.go:343–379  ·  view source on GitHub ↗

HardReset hard resets the repo to the given commit and cleans untracked files. If the commit is not available locally, it fetches from origin first.

(repoDir, commit string)

Source from the content-addressed store, hash-verified

341// HardReset hard resets the repo to the given commit and cleans untracked files.
342// If the commit is not available locally, it fetches from origin first.
343func HardReset(repoDir, commit string) error {
344 if !fileio.PathExists(repoDir) {
345 return errors.ErrDirNotExist
346 }
347 if !fileio.PathExists(filepath.Join(repoDir, ".git")) {
348 return errors.ErrNotGitDir
349 }
350
351 nameVersion := filepath.Base(filepath.Dir(repoDir))
352 title := fmt.Sprintf("[reset %s]", nameVersion)
353
354 execute := func(args ...string) error {
355 executor := cmd.NewExecutor(title, "git", args...)
356 executor.SetWorkDir(repoDir)
357 if output, err := executor.ExecuteOutputLive(); err != nil {
358 return fmt.Errorf("%s -> %w", output, err)
359 }
360 return nil
361 }
362
363 // Try reset directly — commit may already be local.
364 if err := execute("reset", "--hard", commit); err != nil {
365 // Commit not found locally, fetch and retry.
366 if err := execute("fetch", "origin"); err != nil {
367 return fmt.Errorf("failed to fetch origin for '%s' -> %w", nameVersion, err)
368 }
369 if err := execute("reset", "--hard", commit); err != nil {
370 return fmt.Errorf("failed to reset --hard '%s' to '%s' -> %w", nameVersion, commit, err)
371 }
372 }
373
374 if err := execute("clean", "-ffdx"); err != nil {
375 return fmt.Errorf("failed to clean '%s' -> %w", nameVersion, err)
376 }
377
378 return nil
379}
380
381// ApplyPatch apply git patch.
382func ApplyPatch(nameVersion, repoDir, patchFile string) error {

Callers 2

CloneMethod · 0.92
UpdateRepoFunction · 0.85

Calls 5

PathExistsFunction · 0.92
NewExecutorFunction · 0.92
SprintfMethod · 0.80
SetWorkDirMethod · 0.80
ExecuteOutputLiveMethod · 0.80

Tested by

no test coverage detected