33
44package git
55
6- import "code.gitea.io/gitea/modules/setting"
6+ import (
7+ "context"
8+ "strings"
9+
10+ "code.gitea.io/gitea/modules/git/gitcmd"
11+ "code.gitea.io/gitea/modules/setting"
12+ )
713
814// Based on https://git-scm.com/docs/git-config#Documentation/git-config.txt-gpgformat
915const (
@@ -24,3 +30,48 @@ func (s *SigningKey) String() string {
2430 setting .PanicInDevOrTesting ("don't call SigningKey.String() - it exposes the KeyID which might be a local file path" )
2531 return "SigningKey:" + s .Format
2632}
33+
34+ // GetSigningKey returns the KeyID and git Signature for the repo
35+ func GetSigningKey (ctx context.Context , repoPath string ) (* SigningKey , * Signature ) {
36+ if setting .Repository .Signing .SigningKey == "none" {
37+ return nil , nil
38+ }
39+
40+ if setting .Repository .Signing .SigningKey == "default" || setting .Repository .Signing .SigningKey == "" {
41+ // Can ignore the error here as it means that commit.gpgsign is not set
42+ value , _ , _ := gitcmd .NewCommand ("config" , "--get" , "commit.gpgsign" ).WithDir (repoPath ).RunStdString (ctx )
43+ sign , valid := ParseBool (strings .TrimSpace (value ))
44+ if ! sign || ! valid {
45+ return nil , nil
46+ }
47+
48+ format , _ , _ := gitcmd .NewCommand ("config" , "--default" , SigningKeyFormatOpenPGP , "--get" , "gpg.format" ).WithDir (repoPath ).RunStdString (ctx )
49+ signingKey , _ , _ := gitcmd .NewCommand ("config" , "--get" , "user.signingkey" ).WithDir (repoPath ).RunStdString (ctx )
50+ signingName , _ , _ := gitcmd .NewCommand ("config" , "--get" , "user.name" ).WithDir (repoPath ).RunStdString (ctx )
51+ signingEmail , _ , _ := gitcmd .NewCommand ("config" , "--get" , "user.email" ).WithDir (repoPath ).RunStdString (ctx )
52+
53+ if strings .TrimSpace (signingKey ) == "" {
54+ return nil , nil
55+ }
56+
57+ return & SigningKey {
58+ KeyID : strings .TrimSpace (signingKey ),
59+ Format : strings .TrimSpace (format ),
60+ }, & Signature {
61+ Name : strings .TrimSpace (signingName ),
62+ Email : strings .TrimSpace (signingEmail ),
63+ }
64+ }
65+
66+ if setting .Repository .Signing .SigningKey == "" {
67+ return nil , nil
68+ }
69+
70+ return & SigningKey {
71+ KeyID : setting .Repository .Signing .SigningKey ,
72+ Format : setting .Repository .Signing .SigningFormat ,
73+ }, & Signature {
74+ Name : setting .Repository .Signing .SigningName ,
75+ Email : setting .Repository .Signing .SigningEmail ,
76+ }
77+ }
0 commit comments