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)
| 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. |
| 343 | func 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. |
| 382 | func ApplyPatch(nameVersion, repoDir, patchFile string) error { |
no test coverage detected