fix(migrations): preserve SHA-256 pull request commit IDs (#39343)

* Fixes #39339

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
breken
2026-09-19 12:41:41 +00:00
committed by GitHub
co-authored by wxiaoguang
parent 2a3d047339
commit cc34c26172
17 changed files with 105 additions and 128 deletions
+36
View File
@@ -7,6 +7,8 @@ import (
"bytes"
"encoding/hex"
"fmt"
"gitea.dev/modules/util"
)
type ObjectID interface {
@@ -104,3 +106,37 @@ func IsEmptyCommitID(commitID string) bool {
func ComputeBlobHash(hashType ObjectFormat, content []byte) ObjectID {
return hashType.ComputeHash(ObjectBlob, content)
}
func IsStringValidObjectID(objFmt ObjectFormat, s string, optMinLen ...int) bool {
if objFmt == invalidObjectFormat {
return false
}
var minLen, maxLen int
if objFmt != nil {
maxLen = objFmt.FullLength()
minLen = util.OptionalArg(optMinLen, maxLen)
} else {
if len(optMinLen) == 0 {
// if no "min length" is applied, then the length must exactly match one of the formats
if len(s) != Sha1ObjectFormat.FullLength() && len(s) != Sha256ObjectFormat.FullLength() {
return false
}
}
maxLen = Sha256ObjectFormat.FullLength()
minLen = util.OptionalArg(optMinLen, Sha1ObjectFormat.FullLength())
}
if len(s) < minLen || len(s) > maxLen {
return false
}
return isStringLowerHex(s)
}
func isStringLowerHex(s string) bool {
for _, c := range s {
isHex := (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')
if !isHex {
return false
}
}
return len(s) > 0 // it accepts odd length because "shorten commit id" can be 7-chars
}