Computes the git-sha1 hash of the given bytes, using the same algorithm as git. 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 suppo
(data: bytes)
| 371 | |
| 372 | |
| 373 | def git_hash(data: bytes) -> str: |
| 374 | """ |
| 375 | Computes the git-sha1 hash of the given bytes, using the same algorithm as git. |
| 376 | |
| 377 | This is equivalent to running `git hash-object`. See https://git-scm.com/docs/git-hash-object |
| 378 | for more details. |
| 379 | |
| 380 | Note: this method is valid for regular files. For LFS files, the proper git hash is supposed to be computed on the |
| 381 | pointer file content, not the actual file content. However, for simplicity, we directly compare the sha256 of |
| 382 | the LFS file content when we want to compare LFS files. |
| 383 | |
| 384 | Args: |
| 385 | data (`bytes`): |
| 386 | The data to compute the git-hash for. |
| 387 | |
| 388 | Returns: |
| 389 | `str`: the git-hash of `data` as an hexadecimal string. |
| 390 | """ |
| 391 | _kwargs = {'usedforsecurity': False} if sys.version_info >= (3, 9) else {} |
| 392 | sha1 = functools.partial(hashlib.sha1, **_kwargs) |
| 393 | sha = sha1() |
| 394 | sha.update(b'blob ') |
| 395 | sha.update(str(len(data)).encode()) |
| 396 | sha.update(b'\0') |
| 397 | sha.update(data) |
| 398 | return sha.hexdigest() |
| 399 | |
| 400 | |
| 401 | @dataclass |
no test coverage detected
searching dependent graphs…