code.gitea.io/gitea@v1.19.3/modules/git/repo_gpg.go (about)

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