mirror of
https://github.com/go-gitea/gitea
synced 2025-07-03 09:07:19 +00:00
Improve ObjectFormat interface (#28496)
The 4 functions are duplicated, especially as interface methods. I think we just need to keep `MustID` the only one and remove other 3. ``` MustID(b []byte) ObjectID MustIDFromString(s string) ObjectID NewID(b []byte) (ObjectID, error) NewIDFromString(s string) (ObjectID, error) ``` Introduced the new interfrace method `ComputeHash` which will replace the interface `HasherInterface`. Now we don't need to keep two interfaces. Reintroduced `git.NewIDFromString` and `git.MustIDFromString`. The new function will detect the hash length to decide which objectformat of it. If it's 40, then it's SHA1. If it's 64, then it's SHA256. This will be right if the commitID is a full one. So the parameter should be always a full commit id. @AdamMajer Please review.
This commit is contained in:
@ -144,7 +144,7 @@ func TestCommitToPushCommit(t *testing.T) {
|
||||
When: now,
|
||||
}
|
||||
const hexString = "0123456789abcdef0123456789abcdef01234567"
|
||||
sha1, err := git.IDFromString(hexString)
|
||||
sha1, err := git.NewIDFromString(hexString)
|
||||
assert.NoError(t, err)
|
||||
pushCommit := CommitToPushCommit(&git.Commit{
|
||||
ID: sha1,
|
||||
@ -169,12 +169,11 @@ func TestListToPushCommits(t *testing.T) {
|
||||
When: now,
|
||||
}
|
||||
|
||||
hashType := git.Sha1ObjectFormat
|
||||
const hexString1 = "0123456789abcdef0123456789abcdef01234567"
|
||||
hash1, err := hashType.NewIDFromString(hexString1)
|
||||
hash1, err := git.NewIDFromString(hexString1)
|
||||
assert.NoError(t, err)
|
||||
const hexString2 = "fedcba9876543210fedcba9876543210fedcba98"
|
||||
hash2, err := hashType.NewIDFromString(hexString2)
|
||||
hash2, err := git.NewIDFromString(hexString2)
|
||||
assert.NoError(t, err)
|
||||
|
||||
l := []*git.Commit{
|
||||
|
@ -20,13 +20,13 @@ type PushUpdateOptions struct {
|
||||
|
||||
// IsNewRef return true if it's a first-time push to a branch, tag or etc.
|
||||
func (opts *PushUpdateOptions) IsNewRef() bool {
|
||||
commitID, err := git.IDFromString(opts.OldCommitID)
|
||||
commitID, err := git.NewIDFromString(opts.OldCommitID)
|
||||
return err == nil && commitID.IsZero()
|
||||
}
|
||||
|
||||
// IsDelRef return true if it's a deletion to a branch or tag
|
||||
func (opts *PushUpdateOptions) IsDelRef() bool {
|
||||
commitID, err := git.IDFromString(opts.NewCommitID)
|
||||
commitID, err := git.NewIDFromString(opts.NewCommitID)
|
||||
return err == nil && commitID.IsZero()
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user