-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add support for sha256 repositories #23894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 34 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
eaf23fb
Add SHA256 support
AdamMajer 55d0b3b
Need to retain default hash format as SHA1
AdamMajer 55203b1
name fixes
AdamMajer 128fa52
json name fix
AdamMajer c766f17
swagger template updates
AdamMajer 7165cd1
some sha1 -> hash renames
AdamMajer 7082b52
revert unnecessary change
AdamMajer c95a42d
use git.SupportedObjectFormats for object formats in create repo form
AdamMajer 3371a65
variable name fix
AdamMajer ad02eb3
unnecessary stuff
AdamMajer fba9ca1
hash_type to object_format in template
AdamMajer e00b9c9
enable sha256 object format support only for git 2.42
lunny 81ddba5
Fix test
lunny 130e8bf
Fix migrations and remove unnecessary change
lunny eae8956
disable sha256 if it's gogit and display sha256 label for repository
lunny 0e8db0b
small warning fix
AdamMajer 6b5fa15
test column migration
AdamMajer 2ae9909
first sha256 unit tests
AdamMajer ae5b68e
repo fork should save object_format
AdamMajer 8d54aca
Merge branch 'main' into sha256
AdamMajer 1d96ab1
linting fixes
AdamMajer 24929f2
yaml lint
AdamMajer 367d81c
Apply suggestions from code review
6543 5584c89
Update routers/web/web.go
6543 e3cc200
Update modules/structs/repo.go
6543 0e1713b
update swagger docs
6543 73c67e4
one db migration
AdamMajer 689f48c
Merge branch 'sha256' of github.com:AdamMajer/gitea into sha256
AdamMajer 9b57a7f
Merge branch 'main' into sha256
6543 50c5488
address some suggestions
6543 d580b97
wrapp error istead of loosing type
6543 2300b55
fix-test-again
6543 e6d92c5
unexport internal func
6543 63c1d68
just sync in this case then
6543 d87dad4
partial revert -- for safely
AdamMajer 8b85ba9
comment only
AdamMajer f47bc36
Merge branch 'main' into sha256
6543 01561b4
fix from merge main
6543 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,97 @@ | ||
// 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() | ||
AdamMajer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func addObjectFormatNameToRepository(x *xorm.Engine) error { | ||
type Repository struct { | ||
ObjectFormatName string `xorm:"VARCHAR(6) NOT NULL DEFAULT 'sha1'"` | ||
} | ||
|
||
return x.Sync(new(Repository)) | ||
AdamMajer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func AdjustDBForSha256(x *xorm.Engine) error { | ||
if err := expandHashReferencesToSha256(x); err != nil { | ||
return err | ||
} | ||
return addObjectFormatNameToRepository(x) | ||
} |
This file contains hidden or 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()) { | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) | ||
|
||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.