mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-21 04:03:38 +09:00
fix(migrations): preserve SHA-256 pull request commit IDs (#39343)
* Fixes #39339 --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user