Initial commit
This commit is contained in:
58
git/find.py
Normal file
58
git/find.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import hashlib
|
||||
import zlib
|
||||
import sys
|
||||
import os
|
||||
|
||||
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(hash_to_find):
|
||||
files_processed = 0
|
||||
skipped = 0
|
||||
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 == hash_to_find:
|
||||
print(f"File with hash {hash_to_find} found at {file_path}")
|
||||
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__":
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: python3 find_obj_file.py <hash_to_find>")
|
||||
sys.exit(1)
|
||||
|
||||
hash_to_find = sys.argv[1]
|
||||
main(hash_to_find)
|
||||
68
git/find_bulk.py
Normal file
68
git/find_bulk.py
Normal file
@@ -0,0 +1,68 @@
|
||||
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)
|
||||
32
git/find_commits.sh
Normal file
32
git/find_commits.sh
Normal file
@@ -0,0 +1,32 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Location of the Git objects directory.
|
||||
# Change this if your setup is different.
|
||||
OBJECTS_DIR="./objects"
|
||||
|
||||
# Loop over the 256 possible first two characters of the SHA-1 hash.
|
||||
for dir in $(ls -1 $OBJECTS_DIR); do
|
||||
# Ignore the 'info' and 'pack' special directories.
|
||||
if [ "$dir" == "info" ] || [ "$dir" == "pack" ]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
# Loop over the object files in each directory.
|
||||
for file in $(ls -1 $OBJECTS_DIR/$dir); do
|
||||
# Concatenate the directory and file names to get the full SHA-1 hash.
|
||||
full_hash="${dir}${file}"
|
||||
|
||||
# Use git cat-file to find the type of the object.
|
||||
obj_type=$(git cat-file -t $full_hash 2> /dev/null)
|
||||
|
||||
# If git cat-file fails, skip this object.
|
||||
if [ $? -ne 0 ]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
# If the object is a commit, print its hash.
|
||||
if [ "$obj_type" == "commit" ]; then
|
||||
echo "Found commit: $full_hash"
|
||||
fi
|
||||
done
|
||||
done
|
||||
25
git/get_hash.py
Normal file
25
git/get_hash.py
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
import hashlib
|
||||
import zlib
|
||||
import sys
|
||||
|
||||
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
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: python get_hash.py <path_to_git_object_file>")
|
||||
else:
|
||||
file_path = sys.argv[1]
|
||||
hash_value = calculate_git_hash(file_path)
|
||||
print(f"{hash_value}")
|
||||
Reference in New Issue
Block a user