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