InitAsLocalRepo init folder as a local repo.
(repoDir, message string)
| 316 | |
| 317 | // InitAsLocalRepo init folder as a local repo. |
| 318 | func InitAsLocalRepo(repoDir, message string) error { |
| 319 | // Check if repo directory exists |
| 320 | if _, err := os.Stat(repoDir); err != nil { |
| 321 | return fmt.Errorf("directory error: %w", err) |
| 322 | } |
| 323 | |
| 324 | // Set up environment variables for git commits |
| 325 | gitEnv := append(os.Environ(), |
| 326 | "GIT_AUTHOR_NAME=CI Robot", |
| 327 | "GIT_AUTHOR_EMAIL=ci@celer.com", |
| 328 | "GIT_COMMITTER_NAME=CI Robot", |
| 329 | "GIT_COMMITTER_EMAIL=ci@celer.com", |
| 330 | ) |
| 331 | |
| 332 | // git init |
| 333 | color.Printf(color.Hint, "- git -C %s init", repoDir) |
| 334 | cmd := exec.Command("git", "-C", repoDir, "init") |
| 335 | cmd.Env = gitEnv |
| 336 | if output, err := cmd.CombinedOutput(); err != nil { |
| 337 | return fmt.Errorf("failed to git init repo: %w (output: %s)", err, output) |
| 338 | } |
| 339 | color.PrintInline(color.Hint, "✔ git -C %s init\n", repoDir) |
| 340 | |
| 341 | // git add |
| 342 | color.Printf(color.Hint, "- git -C %s add -A", repoDir) |
| 343 | cmd = exec.Command("git", "-C", repoDir, "add", "-A") |
| 344 | cmd.Env = gitEnv |
| 345 | if output, err := cmd.CombinedOutput(); err != nil { |
| 346 | return fmt.Errorf("failed to git add -A: %w (output: %s)", err, output) |
| 347 | } |
| 348 | color.PrintInline(color.Hint, "✔ git -C %s add -A\n", repoDir) |
| 349 | |
| 350 | // git commit |
| 351 | color.Printf(color.Hint, "- git -C %s commit -m %s", repoDir, message) |
| 352 | cmd = exec.Command("git", "-C", repoDir, "commit", "-m", message) |
| 353 | cmd.Env = gitEnv |
| 354 | if output, err := cmd.CombinedOutput(); err != nil { |
| 355 | return fmt.Errorf("failed to git commit: %w (output: %s)", err, output) |
| 356 | } |
| 357 | color.PrintInline(color.Hint, "✔ git -C %s commit -m %s\n", repoDir, message) |
| 358 | |
| 359 | return nil |
| 360 | } |
| 361 | |
| 362 | // RevParseRepoRef return full commit hash with repo ref, if repo ref is not found in remote, |
| 363 | // then find it in local repo, the ref can be any valid git revision (branch, tag, HEAD, commit hash, etc.). |