Skip to content

Commit be31337

Browse files
committed
self-referential-git/commit/be31337
18364853
0 parents  commit be31337

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# self-referential-git be31337
2+
3+
[self-referential-git/commit/be31337](https://github.com/blaufish/self-referential-git/commit/be31337)
4+
5+
I just wanted to create a silly git that references itself...
6+
7+
i.e.
8+
`README.md`
9+
and commit message should both
10+
include the truncated `SHA1` hash.
11+
12+
* [self.bash](self.bash) - a small wrapper for modifying local `.git` if successfull.
13+
* [self.py](self.py) - a small bruteforce searching for a commit that meets our success condition.

self.bash

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
GUESS="be31337"
4+
FIRST=$(git rev-parse HEAD | grep -o '^..')
5+
LAST=$(git rev-parse HEAD | grep -o '......................................$')
6+
7+
if git rev-parse HEAD | grep $GUESS
8+
then
9+
echo "Success allready: "
10+
git rev-parse HEAD
11+
exit
12+
fi
13+
14+
rm -- hack.msg hack.msg.4 hack.msg.trunc hack.msg.zlib
15+
16+
./self.py .git/objects/$FIRST/$LAST $GUESS hack.msg hack.msg.zlib
17+
18+
set -x
19+
20+
SHA=$(shasum hack.msg | awk '{print $1}' )
21+
FIRST=$(shasum hack.msg | awk '{print $1}' | grep -o '^..')
22+
LAST=$(shasum hack.msg | awk '{print $1}' | grep -o '......................................$')
23+
24+
git rev-parse HEAD
25+
mkdir -p ".git/objects/$FIRST"
26+
file hack.msg.zlib
27+
cp -- hack.msg.zlib ".git/objects/$FIRST/$LAST"
28+
29+
set -e
30+
git cat-file -p "$SHA"
31+
32+
shasum hack.msg > .git/HEAD
33+
git rev-parse HEAD

self.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python3
2+
3+
import hashlib
4+
import os
5+
import sys
6+
import threading
7+
import zlib
8+
9+
lock = threading.Lock()
10+
terminate = False;
11+
12+
def hack(git_object, counter_start, counter_increment, goal, destination_clear, destination_compressed):
13+
global lock
14+
global terminate
15+
print(f"hack(..., {counter_start}, {counter_increment}, {goal})", file=sys.stderr);
16+
header, content = git_object.split(b'\x00', 1)
17+
hack_str = content.decode("utf-8")
18+
counter = counter_start;
19+
while True:
20+
with lock:
21+
if terminate:
22+
return
23+
commit_content = hack_str + "\n" + str(counter) + "\n"
24+
commit_content_bytes = commit_content.encode("utf-8")
25+
commit_content_bytes_len = len(commit_content_bytes)
26+
header = f"commit {commit_content_bytes_len}\x00"
27+
header_content = header + commit_content
28+
to_be_hashed = header_content.encode("utf-8")
29+
s = hashlib.sha1(to_be_hashed).hexdigest()
30+
if s.startswith(goal):
31+
with lock:
32+
terminate = True
33+
print(f"SHA1: {s}", file=sys.stderr);
34+
print(f"Write to: {destination_clear} (plain)", file=sys.stderr);
35+
with open(destination_clear, "wb") as f:
36+
f.write(to_be_hashed)
37+
print(f"Write to: {destination_compressed} (compressed)", file=sys.stderr);
38+
with open(destination_compressed, "wb") as f:
39+
bb = zlib.compress(to_be_hashed)
40+
f.write(bb)
41+
return
42+
counter = counter + counter_increment
43+
44+
filename = sys.argv[1]
45+
compressed_contents = open(filename, 'rb').read()
46+
decompressed_contents = zlib.decompress(compressed_contents)
47+
#print(f"------\n{s}\n------)", file=sys.stderr);
48+
49+
cpus = len(os.sched_getaffinity(0))
50+
print(f"\rCPUs detected: {cpus}", file=sys.stderr)
51+
52+
threads = [threading.Thread(target=hack, args=(decompressed_contents,i,cpus,sys.argv[2], sys.argv[3], sys.argv[4])) for i in range(0, cpus)]
53+
for thread in threads:
54+
thread.start()
55+
56+
for thread in threads:
57+
thread.join()

0 commit comments

Comments
 (0)