You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo_gpg.go 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package git
  5. import (
  6. "fmt"
  7. "strings"
  8. "code.gitea.io/gitea/modules/process"
  9. )
  10. // LoadPublicKeyContent will load the key from gpg
  11. func (gpgSettings *GPGSettings) LoadPublicKeyContent() error {
  12. content, stderr, err := process.GetManager().Exec(
  13. "gpg -a --export",
  14. "gpg", "-a", "--export", gpgSettings.KeyID)
  15. if err != nil {
  16. return fmt.Errorf("unable to get default signing key: %s, %s, %w", gpgSettings.KeyID, stderr, err)
  17. }
  18. gpgSettings.PublicKeyContent = content
  19. return nil
  20. }
  21. // GetDefaultPublicGPGKey will return and cache the default public GPG settings for this repository
  22. func (repo *Repository) GetDefaultPublicGPGKey(forceUpdate bool) (*GPGSettings, error) {
  23. if repo.gpgSettings != nil && !forceUpdate {
  24. return repo.gpgSettings, nil
  25. }
  26. gpgSettings := &GPGSettings{
  27. Sign: true,
  28. }
  29. value, _, _ := NewCommand(repo.Ctx, "config", "--get", "commit.gpgsign").RunStdString(&RunOpts{Dir: repo.Path})
  30. sign, valid := ParseBool(strings.TrimSpace(value))
  31. if !sign || !valid {
  32. gpgSettings.Sign = false
  33. repo.gpgSettings = gpgSettings
  34. return gpgSettings, nil
  35. }
  36. signingKey, _, _ := NewCommand(repo.Ctx, "config", "--get", "user.signingkey").RunStdString(&RunOpts{Dir: repo.Path})
  37. gpgSettings.KeyID = strings.TrimSpace(signingKey)
  38. defaultEmail, _, _ := NewCommand(repo.Ctx, "config", "--get", "user.email").RunStdString(&RunOpts{Dir: repo.Path})
  39. gpgSettings.Email = strings.TrimSpace(defaultEmail)
  40. defaultName, _, _ := NewCommand(repo.Ctx, "config", "--get", "user.name").RunStdString(&RunOpts{Dir: repo.Path})
  41. gpgSettings.Name = strings.TrimSpace(defaultName)
  42. if err := gpgSettings.LoadPublicKeyContent(); err != nil {
  43. return nil, err
  44. }
  45. repo.gpgSettings = gpgSettings
  46. return repo.gpgSettings, nil
  47. }