-
Notifications
You must be signed in to change notification settings - Fork 570
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
Prevent empty commits if files did not change #2389
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c849c15
Prevent empty commits
Wauplin f4653e1
remove warnings in tests
Wauplin b9facdb
Merge branch 'main' into 1411-prevent-empty-commmits
Wauplin 638d568
no newline in echo test
Wauplin 8db64cc
fix windows?
Wauplin d17dc63
style
Wauplin db05a98
Apply suggestions from code review
Wauplin 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
from typing import BinaryIO, Optional | ||
|
||
from .insecure_hashlib import sha256 | ||
from .insecure_hashlib import sha1, sha256 | ||
|
||
|
||
def sha_fileobj(fileobj: BinaryIO, chunk_size: Optional[int] = None) -> bytes: | ||
|
@@ -27,3 +27,38 @@ def sha_fileobj(fileobj: BinaryIO, chunk_size: Optional[int] = None) -> bytes: | |
if not chunk: | ||
break | ||
return sha.digest() | ||
|
||
|
||
def git_hash(data: bytes) -> str: | ||
""" | ||
Computes the sha1 hash of the given bytes, using the same algorithm as git. | ||
Wauplin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This is equivalent to running `git hash-object`. See https://git-scm.com/docs/git-hash-object | ||
for more details. | ||
|
||
Note: this method is valid for regular files. For LFS files, the proper git hash is supposed to be computed on the | ||
pointer file content, not the actual file content. However, for simplicity, we directly compare the sha256 of | ||
the LFS file content when we want to compare LFS files. | ||
|
||
Args: | ||
data (`bytes`): | ||
The data to compute the git-hash for. | ||
|
||
Returns: | ||
`str`: the git-hash of `data` as an hexadecimal string. | ||
|
||
Example: | ||
```python | ||
>>> from huggingface_hub.utils.sha import git_hash | ||
>>> git_hash(b"Hello, World!") | ||
'b45ef6fec89518d314f546fd6c3025367b721684' | ||
``` | ||
""" | ||
# Taken from https://gist.github.com/msabramo/763200 | ||
# Note: no need to optimize by reading the file in chunks as we're not supposed to hash huge files (5MB maximum). | ||
sha = sha1() | ||
sha.update(b"blob ") | ||
sha.update(str(len(data)).encode()) | ||
sha.update(b"\0") | ||
sha.update(data) | ||
Comment on lines
+60
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
return sha.hexdigest() |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
out of curiosity, how long does this take to compute on larger files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Asking as large file uploads (without going through your fantastic large-file-upload method) are already taking quite a bit of time before uploading anything
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For LFS files (so any file larger than 10MB for text files or 1MB for binary files) it won't add any time as we already compute and have the sha256.
For non-LFS files i expect this to be quite fast unless there are many many small files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @julien-c said, it should be neglectable on regular files since they are small. In any case, all regular files are loaded in memory in the
/commit
payload and cannot be above 1GB so having 1000s of files is not a use case to optimize for.