Skip to content

Commit 51081d9

Browse files
committed
refactor
1 parent e0bcf2b commit 51081d9

File tree

9 files changed

+67
-52
lines changed

9 files changed

+67
-52
lines changed

models/asymkey/display_key.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

models/asymkey/gpg_key_commit_verification.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type CommitVerification struct {
2525
SigningUser *user_model.User // if Verified, then SigningUser is non-nil
2626
CommittingUser *user_model.User // if Verified, then CommittingUser is non-nil
2727
SigningEmail string
28-
SigningKey *GPGKey
28+
SigningKey *GPGKey // FIXME: need to refactor it to a new name like "SigningGPGKey", it is also used in some templates
2929
SigningSSHKey *PublicKey
3030
TrustStatus string
3131
}

models/asymkey/key_display.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package asymkey
5+
6+
import (
7+
"os"
8+
9+
"code.gitea.io/gitea/modules/git"
10+
"code.gitea.io/gitea/modules/log"
11+
"code.gitea.io/gitea/modules/setting"
12+
)
13+
14+
func GetDisplaySigningKey(key *git.SigningKey) string {
15+
if key == nil || key.Format == "" {
16+
return ""
17+
}
18+
19+
switch key.Format {
20+
case git.SigningKeyFormatOpenPGP:
21+
return key.KeyID
22+
case git.SigningKeyFormatSSH:
23+
content, err := os.ReadFile(key.KeyID)
24+
if err != nil {
25+
log.Error("Unable to read SSH key %s: %v", key.KeyID, err)
26+
return "(Unable to read SSH key)"
27+
}
28+
display, err := CalcFingerprint(string(content))
29+
if err != nil {
30+
log.Error("Unable to calculate fingerprint for SSH key %s: %v", key.KeyID, err)
31+
return "(Unable to calculate fingerprint for SSH key)"
32+
}
33+
return display
34+
}
35+
setting.PanicInDevOrTesting("Unknown signing key format: %s", key.Format)
36+
return "(Unknown key format)"
37+
}

modules/git/key.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,24 @@
33

44
package git
55

6+
import "code.gitea.io/gitea/modules/setting"
7+
68
// Based on https://git-scm.com/docs/git-config#Documentation/git-config.txt-gpgformat
79
const (
810
SigningKeyFormatOpenPGP = "openpgp" // for GPG keys, the expected default of git cli
911
SigningKeyFormatSSH = "ssh"
1012
)
1113

14+
// SigningKey represents an instance key info which will be used to sign git commits.
15+
// FIXME: need to refactor it to a new name, this name conflicts with the variable names for "asymkey.GPGKey" in many places.
1216
type SigningKey struct {
1317
KeyID string
1418
Format string
1519
}
20+
21+
func (s *SigningKey) String() string {
22+
// Do not expose KeyID
23+
// In case the key is a file path and the struct is rendered in a template, then the server path will be exposed.
24+
setting.PanicInDevOrTesting("don't call SigningKey.String() - it exposes the KeyID which might be a local file path")
25+
return "SigningKey:" + s.Format
26+
}

routers/web/repo/issue_view.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,11 +495,7 @@ func preparePullViewSigning(ctx *context.Context, issue *issues_model.Issue) {
495495
if ctx.Doer != nil {
496496
sign, key, _, err := asymkey_service.SignMerge(ctx, pull, ctx.Doer, pull.BaseRepo.RepoPath(), pull.BaseBranch, pull.GetGitHeadRefName())
497497
ctx.Data["WillSign"] = sign
498-
displayKeyID, displayKeyIDErr := asymkey_model.GetDisplaySigningKey(key)
499-
if displayKeyIDErr != nil {
500-
log.Error("Error whilst getting the display keyID: %s", displayKeyIDErr.Error())
501-
}
502-
ctx.Data["SigningKey"] = displayKeyID
498+
ctx.Data["SigningKeyMergeDisplay"] = asymkey_model.GetDisplaySigningKey(key)
503499
if err != nil {
504500
if asymkey_service.IsErrWontSign(err) {
505501
ctx.Data["WontSignReason"] = err.(*asymkey_service.ErrWontSign).Reason

services/context/repo.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ type CommitFormOptions struct {
100100
UserCanPush bool
101101
RequireSigned bool
102102
WillSign bool
103-
SigningKey string
103+
SigningKeyFormDisplay string
104104
WontSignReason string
105105
CanCreatePullRequest bool
106106
CanCreateBasePullRequest bool
@@ -156,20 +156,15 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
156156
canCreateBasePullRequest := targetRepo.BaseRepo != nil && targetRepo.BaseRepo.UnitEnabled(ctx, unit_model.TypePullRequests)
157157
canCreatePullRequest := targetRepo.UnitEnabled(ctx, unit_model.TypePullRequests) || canCreateBasePullRequest
158158

159-
displayKeyID, displayKeyIDErr := asymkey_model.GetDisplaySigningKey(signKey)
160-
if displayKeyIDErr != nil {
161-
log.Error("Error whilst getting the display keyID: %s", displayKeyIDErr.Error())
162-
}
163-
164159
opts := &CommitFormOptions{
165-
TargetRepo: targetRepo,
166-
WillSubmitToFork: submitToForkedRepo,
167-
CanCommitToBranch: canCommitToBranch,
168-
UserCanPush: canPushWithProtection,
169-
RequireSigned: protectionRequireSigned,
170-
WillSign: willSign,
171-
SigningKey: displayKeyID,
172-
WontSignReason: wontSignReason,
160+
TargetRepo: targetRepo,
161+
WillSubmitToFork: submitToForkedRepo,
162+
CanCommitToBranch: canCommitToBranch,
163+
UserCanPush: canPushWithProtection,
164+
RequireSigned: protectionRequireSigned,
165+
WillSign: willSign,
166+
SigningKeyFormDisplay: asymkey_model.GetDisplaySigningKey(signKey),
167+
WontSignReason: wontSignReason,
173168

174169
CanCreatePullRequest: canCreatePullRequest,
175170
CanCreateBasePullRequest: canCreateBasePullRequest,

templates/repo/commit_sign_badge.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ so this template should be kept as small as possbile, DO NOT put large component
88
*/}}
99
{{- $commit := $.Commit -}}
1010
{{- $commitBaseLink := $.CommitBaseLink -}}
11-
{{- $verification := $.CommitSignVerification -}}
11+
{{- $verification := $.CommitSignVerification -}}{{- /* asymkey.CommitVerification */ -}}
1212

1313
{{- $extraClass := "" -}}
1414
{{- $verified := false -}}
@@ -50,7 +50,7 @@ so this template should be kept as small as possbile, DO NOT put large component
5050

5151
{{- if $verification.SigningSSHKey -}}
5252
{{- $msgSigningKey = print (ctx.Locale.Tr "repo.commits.ssh_key_fingerprint") ": " $verification.SigningSSHKey.Fingerprint -}}
53-
{{- else if $verification.SigningKey -}}
53+
{{- else if $verification.SigningKey -}}{{- /* asymkey.GPGKey */ -}}
5454
{{- $msgSigningKey = print (ctx.Locale.Tr "repo.commits.gpg_key_id") ": " $verification.SigningKey.PaddedKeyID -}}
5555
{{- end -}}
5656
{{- end -}}

templates/repo/editor/commit_form.tmpl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<div class="commit-form-wrapper">
22
{{ctx.AvatarUtils.Avatar .SignedUser 40 "commit-avatar"}}
33
<div class="commit-form avatar-content-left-arrow">
4-
<h3>{{- if .CommitFormOptions.WillSign}}
5-
<span title="{{ctx.Locale.Tr "repo.signing.will_sign" .CommitFormOptions.SigningKey}}">{{svg "octicon-lock" 24}}</span>
4+
<h3>
5+
{{- if .CommitFormOptions.WillSign}}
6+
<span data-tooltip-content="{{ctx.Locale.Tr "repo.signing.will_sign" .CommitFormOptions.SigningKeyFormDisplay}}">{{svg "octicon-lock" 24}}</span>
67
{{ctx.Locale.Tr "repo.editor.commit_signed_changes"}}
78
{{- else}}
89
<span title="{{ctx.Locale.Tr (printf "repo.signing.wont_sign.%s" .CommitFormOptions.WontSignReason)}}">{{svg "octicon-unlock" 24}}</span>
910
{{ctx.Locale.Tr "repo.editor.commit_changes"}}
10-
{{- end}}</h3>
11+
{{- end}}
12+
</h3>
1113
<div class="field">
1214
<input name="commit_summary" maxlength="100" placeholder="{{if .PageIsDelete}}{{ctx.Locale.Tr "repo.editor.delete" .TreePath}}{{else if .PageIsUpload}}{{ctx.Locale.Tr "repo.editor.upload_files_to_dir" .TreePath}}{{else if .IsNewFile}}{{ctx.Locale.Tr "repo.editor.add_tmpl"}}{{else if .PageIsPatch}}{{ctx.Locale.Tr "repo.editor.patch"}}{{else}}{{ctx.Locale.Tr "repo.editor.update" .TreePath}}{{end}}" value="{{.commit_summary}}" autofocus>
1315
</div>

templates/repo/issue/view_content/pull_merge_box.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@
188188
{{if .WillSign}}
189189
<div class="item">
190190
{{svg "octicon-lock" 16 "text green"}}
191-
{{ctx.Locale.Tr "repo.signing.will_sign" .SigningKey}}
191+
{{ctx.Locale.Tr "repo.signing.will_sign" .SigningKeyMergeDisplay}}
192192
</div>
193193
{{else if .IsSigned}}
194194
<div class="item">

0 commit comments

Comments
 (0)