2019-10-16 19:12:42 +05:30
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-10-16 19:12:42 +05:30
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/process"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LoadPublicKeyContent will load the key from gpg
|
|
|
|
func (gpgSettings *GPGSettings) LoadPublicKeyContent() error {
|
|
|
|
content, stderr, err := process.GetManager().Exec(
|
|
|
|
"gpg -a --export",
|
|
|
|
"gpg", "-a", "--export", gpgSettings.KeyID)
|
|
|
|
if err != nil {
|
2023-12-14 02:32:00 +05:30
|
|
|
return fmt.Errorf("unable to get default signing key: %s, %s, %w", gpgSettings.KeyID, stderr, err)
|
2019-10-16 19:12:42 +05:30
|
|
|
}
|
|
|
|
gpgSettings.PublicKeyContent = content
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDefaultPublicGPGKey will return and cache the default public GPG settings for this repository
|
|
|
|
func (repo *Repository) GetDefaultPublicGPGKey(forceUpdate bool) (*GPGSettings, error) {
|
|
|
|
if repo.gpgSettings != nil && !forceUpdate {
|
|
|
|
return repo.gpgSettings, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgSettings := &GPGSettings{
|
|
|
|
Sign: true,
|
|
|
|
}
|
|
|
|
|
2022-04-01 08:25:30 +05:30
|
|
|
value, _, _ := NewCommand(repo.Ctx, "config", "--get", "commit.gpgsign").RunStdString(&RunOpts{Dir: repo.Path})
|
2019-10-16 19:12:42 +05:30
|
|
|
sign, valid := ParseBool(strings.TrimSpace(value))
|
|
|
|
if !sign || !valid {
|
|
|
|
gpgSettings.Sign = false
|
|
|
|
repo.gpgSettings = gpgSettings
|
|
|
|
return gpgSettings, nil
|
|
|
|
}
|
|
|
|
|
2022-04-01 08:25:30 +05:30
|
|
|
signingKey, _, _ := NewCommand(repo.Ctx, "config", "--get", "user.signingkey").RunStdString(&RunOpts{Dir: repo.Path})
|
2019-10-16 19:12:42 +05:30
|
|
|
gpgSettings.KeyID = strings.TrimSpace(signingKey)
|
|
|
|
|
2022-04-01 08:25:30 +05:30
|
|
|
defaultEmail, _, _ := NewCommand(repo.Ctx, "config", "--get", "user.email").RunStdString(&RunOpts{Dir: repo.Path})
|
2019-10-16 19:12:42 +05:30
|
|
|
gpgSettings.Email = strings.TrimSpace(defaultEmail)
|
|
|
|
|
2022-04-01 08:25:30 +05:30
|
|
|
defaultName, _, _ := NewCommand(repo.Ctx, "config", "--get", "user.name").RunStdString(&RunOpts{Dir: repo.Path})
|
2019-10-16 19:12:42 +05:30
|
|
|
gpgSettings.Name = strings.TrimSpace(defaultName)
|
|
|
|
|
|
|
|
if err := gpgSettings.LoadPublicKeyContent(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
repo.gpgSettings = gpgSettings
|
|
|
|
return repo.gpgSettings, nil
|
|
|
|
}
|