Skip to content
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

Improve ObjectFormat interface #28496

Merged
merged 7 commits into from
Dec 19, 2023
Merged

Conversation

lunny
Copy link
Member

@lunny lunny commented Dec 17, 2023

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.

@lunny lunny added the type/refactoring Existing code has been cleaned up. There should be no new functionality. label Dec 17, 2023
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Dec 17, 2023
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Dec 17, 2023
@github-actions github-actions bot added modifies/api This PR adds API routes or modifies them modifies/cli PR changes something on the CLI, i.e. gitea doctor or gitea admin labels Dec 17, 2023
wxiaoguang
wxiaoguang previously approved these changes Dec 17, 2023
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Dec 17, 2023
@GiteaBot GiteaBot added lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Dec 17, 2023
@wxiaoguang
Copy link
Contributor

wxiaoguang commented Dec 17, 2023

Not sure whether it is right to remove objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName); objectFormat.MustIDFromString(...)

@lunny
Copy link
Member Author

lunny commented Dec 18, 2023

Not sure whether it is right to remove objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName); objectFormat.MustIDFromString(...)

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.

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Dec 18, 2023

Not sure whether it is right to remove objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName); objectFormat.MustIDFromString(...)

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.

I do not have full understanding, the only question is it good to "guess" the object format by length. I can accept if it is fine to do so.

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Dec 18, 2023
@AdamMajer
Copy link
Contributor

My thought was to move "the other way" when it comes to refactoring. That is, to move away from string and toward ObjectID and ObjectFormat to store these references everywhere. Then the string to ObjectID helper functions would go away naturally. Is this something that would be beneficial?

The only problem here is that you are removing objectFormat and assuming that hashes will always be in correct format, there will be an issue when you pass a partial hash the length of SHA1 into a SHA256 repository. Any user input cannot be assumed to be full hash just by looking at length, for example. So if we take care that user input never hits these functions, there should be no issue.

@lunny
Copy link
Member Author

lunny commented Dec 18, 2023

My thought was to move "the other way" when it comes to refactoring. That is, to move away from string and toward ObjectID and ObjectFormat to store these references everywhere. Then the string to ObjectID helper functions would go away naturally. Is this something that would be beneficial?

The only problem here is that you are removing objectFormat and assuming that hashes will always be in correct format, there will be an issue when you pass a partial hash the length of SHA1 into a SHA256 repository. Any user input cannot be assumed to be full hash just by looking at length, for example. So if we take care that user input never hits these functions, there should be no issue.

That's what GetCommit to do which convert a partial hash to a full one. And if it's a partial hash, the previous implemention of NewIDFromString still cannot resolve the problem. So we should not expect the new implemention can resolve the problem.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Dec 19, 2023
@lunny lunny added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Dec 19, 2023
@lunny lunny enabled auto-merge (squash) December 19, 2023 06:47
@lunny lunny merged commit 4eb2a29 into go-gitea:main Dec 19, 2023
25 checks passed
@GiteaBot GiteaBot added this to the 1.22.0 milestone Dec 19, 2023
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Dec 19, 2023
@lunny lunny deleted the lunny/adjust_object_format3 branch December 19, 2023 08:40
zjjhot added a commit to zjjhot/gitea that referenced this pull request Dec 22, 2023
* giteaofficial/main:
  Add more ways to try (go-gitea#28581)
  Convert to url auth to header auth in tests (go-gitea#28484)
  Fix 500 error of searching commits (go-gitea#28576)
  improve possible performance bottleneck (go-gitea#28547)
  Use information from previous blame parts (go-gitea#28572)
  Make offline mode as default to no connect external avatar service by default (go-gitea#28548)
  Fix merging artifact chunks error when minio storage basepath is set (go-gitea#28555)
  feat: bump `dessant/lock-threads` and `actions/setup-go` to use nodejs20 runtime (go-gitea#28565)
  Update actions document about comparsion as Github Actions (go-gitea#28560)
  Fix inperformant query on retrifing review from database. (go-gitea#28552)
  Fix the issue ref rendering for wiki (go-gitea#28556)
  Add missing head of lfs client batch (go-gitea#28550)
  [skip ci] Updated translations via Crowdin
  Remove deadcode under models/issues (go-gitea#28536)
  Always enable caches (go-gitea#28527)
  Improve ObjectFormat interface (go-gitea#28496)
fuxiaohei pushed a commit to fuxiaohei/gitea that referenced this pull request Jan 17, 2024
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.
AdamMajer pushed a commit to AdamMajer/gitea that referenced this pull request Jan 22, 2024
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.
silverwind pushed a commit to silverwind/gitea that referenced this pull request Feb 20, 2024
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.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/api This PR adds API routes or modifies them modifies/cli PR changes something on the CLI, i.e. gitea doctor or gitea admin size/L Denotes a PR that changes 100-499 lines, ignoring generated files. type/refactoring Existing code has been cleaned up. There should be no new functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants