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.

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