Files
useful/git/find_bulk.py
2024-07-13 00:53:45 +03:00

69 lines
2.0 KiB
Python

import hashlib
import zlib
import sys
import os
import shutil
def calculate_git_hash(file_path):
# Read the file content
with open(file_path, 'rb') as f:
content = f.read()
# Decompress the zlib-compressed content
decompressed_content = zlib.decompress(content)
# Calculate the SHA-1 hash of the decompressed content
sha1 = hashlib.sha1(decompressed_content).hexdigest()
return sha1
def main(hashes):
files_processed = 0
skipped = 0
remaining_hashes = set(hashes)
for root, dirs, files in os.walk('./writable'):
for file in files:
# if 'objects' not in root:
# skipped += 1
# continue
if '.' in file:
continue
file_path = os.path.join(root, file)
if os.path.islink(file_path):
skipped += 1
continue
print(file_path)
try:
file_hash = calculate_git_hash(file_path)
if file_hash in remaining_hashes:
print(f"File with hash {file_hash} found at {file_path}")
remaining_hashes.remove(file_hash)
destination_file = f'/var/www/test/useless/contests.git/objects/{file_hash}'
shutil.copyfile(file_path, destination_file)
if len(remaining_hashes) == 0:
print('All hashes found.')
sys.exit(0)
except Exception as e:
# print(f"Error processing {file_path}: {e}")
print(f"Error processing {file_path}")
files_processed += 1
print(files_processed)
print(f'Not found. Processed: {files_processed}. Skipped: {skipped}')
if __name__ == "__main__":
hashes = [
"1b38afd1a39bc0c2de0d01e513499d38a08e12b7",
"84ee1354e2182ccb5445d103f3dcc54dbf682ec8",
"e44d1404d642858babb9d45da0eba9ec410078f0",
"9c16b13a81f70cba6e76195664c7da28f3b93dc6",
]
main(hashes)