gitea源码

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package gitrepo
  4. import (
  5. "context"
  6. "strings"
  7. "code.gitea.io/gitea/modules/git/gitcmd"
  8. "code.gitea.io/gitea/modules/globallock"
  9. )
  10. func GitConfigGet(ctx context.Context, repo Repository, key string) (string, error) {
  11. result, _, err := gitcmd.NewCommand("config", "--get").
  12. AddDynamicArguments(key).
  13. RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
  14. if err != nil {
  15. return "", err
  16. }
  17. return strings.TrimSpace(result), nil
  18. }
  19. func getRepoConfigLockKey(repoStoragePath string) string {
  20. return "repo-config:" + repoStoragePath
  21. }
  22. // GitConfigAdd add a git configuration key to a specific value for the given repository.
  23. func GitConfigAdd(ctx context.Context, repo Repository, key, value string) error {
  24. return globallock.LockAndDo(ctx, getRepoConfigLockKey(repo.RelativePath()), func(ctx context.Context) error {
  25. _, _, err := gitcmd.NewCommand("config", "--add").
  26. AddDynamicArguments(key, value).
  27. RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
  28. return err
  29. })
  30. }
  31. // GitConfigSet updates a git configuration key to a specific value for the given repository.
  32. // If the key does not exist, it will be created.
  33. // If the key exists, it will be updated to the new value.
  34. func GitConfigSet(ctx context.Context, repo Repository, key, value string) error {
  35. return globallock.LockAndDo(ctx, getRepoConfigLockKey(repo.RelativePath()), func(ctx context.Context) error {
  36. _, _, err := gitcmd.NewCommand("config").
  37. AddDynamicArguments(key, value).
  38. RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
  39. return err
  40. })
  41. }