-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for sha256 repositories (#23894)
Currently only SHA1 repositories are supported by Gitea. This adds support for alternate SHA256 with the additional aim of easier support for additional hash types in the future. Fixes: #13794 Limited by: go-git/go-git#899 Depend on: #28138 <img width="776" alt="图片" src="https://github.com/go-gitea/gitea/assets/81045/5448c9a7-608e-4341-a149-5dd0069c9447"> --------- Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: 6543 <[email protected]>
- Loading branch information
1 parent
07ba4d9
commit d68a613
Showing
98 changed files
with
834 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
models/migrations/fixtures/Test_RepositoryFormat/repository.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# type Repository struct { | ||
# ID int64 `xorm:"pk autoincr"` | ||
# } | ||
- | ||
id: 1 | ||
- | ||
id: 2 | ||
- | ||
id: 3 | ||
- | ||
id: 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
package v1_22 //nolint | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/setting" | ||
|
||
"xorm.io/xorm" | ||
) | ||
|
||
func expandHashReferencesToSha256(x *xorm.Engine) error { | ||
alteredTables := [][2]string{ | ||
{"commit_status", "context_hash"}, | ||
{"comment", "commit_sha"}, | ||
{"pull_request", "merge_base"}, | ||
{"pull_request", "merged_commit_id"}, | ||
{"review", "commit_id"}, | ||
{"review_state", "commit_sha"}, | ||
{"repo_archiver", "commit_id"}, | ||
{"release", "sha1"}, | ||
{"repo_indexer_status", "commit_sha"}, | ||
} | ||
|
||
db := x.NewSession() | ||
defer db.Close() | ||
|
||
if err := db.Begin(); err != nil { | ||
return err | ||
} | ||
|
||
if !setting.Database.Type.IsSQLite3() { | ||
if setting.Database.Type.IsMSSQL() { | ||
// drop indexes that need to be re-created afterwards | ||
droppedIndexes := []string{ | ||
"DROP INDEX commit_status.IDX_commit_status_context_hash", | ||
"DROP INDEX review_state.UQE_review_state_pull_commit_user", | ||
"DROP INDEX repo_archiver.UQE_repo_archiver_s", | ||
} | ||
for _, s := range droppedIndexes { | ||
_, err := db.Exec(s) | ||
if err != nil { | ||
return errors.New(s + " " + err.Error()) | ||
} | ||
} | ||
} | ||
|
||
for _, alts := range alteredTables { | ||
var err error | ||
if setting.Database.Type.IsMySQL() { | ||
_, err = db.Exec(fmt.Sprintf("ALTER TABLE `%s` MODIFY COLUMN `%s` VARCHAR(64)", alts[0], alts[1])) | ||
} else if setting.Database.Type.IsMSSQL() { | ||
_, err = db.Exec(fmt.Sprintf("ALTER TABLE `%s` ALTER COLUMN `%s` VARCHAR(64)", alts[0], alts[1])) | ||
} else { | ||
_, err = db.Exec(fmt.Sprintf("ALTER TABLE `%s` ALTER COLUMN `%s` TYPE VARCHAR(64)", alts[0], alts[1])) | ||
} | ||
if err != nil { | ||
return fmt.Errorf("alter column '%s' of table '%s' failed: %w", alts[1], alts[0], err) | ||
} | ||
} | ||
|
||
if setting.Database.Type.IsMSSQL() { | ||
recreateIndexes := []string{ | ||
"CREATE INDEX IDX_commit_status_context_hash ON commit_status(context_hash)", | ||
"CREATE UNIQUE INDEX UQE_review_state_pull_commit_user ON review_state(user_id, pull_id, commit_sha)", | ||
"CREATE UNIQUE INDEX UQE_repo_archiver_s ON repo_archiver(repo_id, type, commit_id)", | ||
} | ||
for _, s := range recreateIndexes { | ||
_, err := db.Exec(s) | ||
if err != nil { | ||
return errors.New(s + " " + err.Error()) | ||
} | ||
} | ||
} | ||
} | ||
log.Debug("Updated database tables to hold SHA256 git hash references") | ||
|
||
return db.Commit() | ||
} | ||
|
||
func addObjectFormatNameToRepository(x *xorm.Engine) error { | ||
type Repository struct { | ||
ObjectFormatName string `xorm:"VARCHAR(6) NOT NULL DEFAULT 'sha1'"` | ||
} | ||
|
||
if err := x.Sync(new(Repository)); err != nil { | ||
return err | ||
} | ||
|
||
// Here to catch weird edge-cases where column constraints above are | ||
// not applied by the DB backend | ||
_, err := x.Exec("UPDATE repository set object_format_name = 'sha1' WHERE object_format_name = '' or object_format_name IS NULL") | ||
return err | ||
} | ||
|
||
func AdjustDBForSha256(x *xorm.Engine) error { | ||
if err := expandHashReferencesToSha256(x); err != nil { | ||
return err | ||
} | ||
return addObjectFormatNameToRepository(x) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package v1_22 //nolint | ||
|
||
import ( | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models/migrations/base" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"xorm.io/xorm" | ||
) | ||
|
||
func PrepareOldRepository(t *testing.T) (*xorm.Engine, func()) { | ||
type Repository struct { // old struct | ||
ID int64 `xorm:"pk autoincr"` | ||
} | ||
|
||
// Prepare and load the testing database | ||
return base.PrepareTestEnv(t, 0, new(Repository)) | ||
} | ||
|
||
func Test_RepositoryFormat(t *testing.T) { | ||
x, deferable := PrepareOldRepository(t) | ||
defer deferable() | ||
|
||
type Repository struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
ObjectFormatName string `xorg:"not null default('sha1')"` | ||
} | ||
|
||
repo := new(Repository) | ||
|
||
// check we have some records to migrate | ||
count, err := x.Count(new(Repository)) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 4, count) | ||
|
||
assert.NoError(t, AdjustDBForSha256(x)) | ||
|
||
repo.ID = 20 | ||
repo.ObjectFormatName = "sha256" | ||
_, err = x.Insert(repo) | ||
assert.NoError(t, err) | ||
|
||
count, err = x.Count(new(Repository)) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 5, count) | ||
|
||
repo = new(Repository) | ||
ok, err := x.ID(2).Get(repo) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, true, ok) | ||
assert.EqualValues(t, "sha1", repo.ObjectFormatName) | ||
|
||
repo = new(Repository) | ||
ok, err = x.ID(20).Get(repo) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, true, ok) | ||
assert.EqualValues(t, "sha256", repo.ObjectFormatName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.