Skip to content

Commit

Permalink
Initial gitkeyfind script
Browse files Browse the repository at this point in the history
Bug: #8
  • Loading branch information
hannob committed Nov 21, 2024
1 parent 66d2156 commit 77d2b66
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
63 changes: 63 additions & 0 deletions gitkeyfinder
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/python3

import argparse
import shutil
import tempfile

import pygit2

import keyfinder

ap = argparse.ArgumentParser()
ap.add_argument("repo", help="URL or local path of git repository")
ap.add_argument("--keep", action="store_true", help="Don't delete tmpdir")
args = ap.parse_args()

tmpdir = tempfile.mkdtemp(prefix="gitkf")


if "://" in args.repo:
rep = pygit2.clone_repository(args.repo, tmpdir)
else:
rep = pygit2.Repository(args.repo)

dd = rep.odb

count = 0
results = {}
for oid in dd:
o = rep.get(oid)
if isinstance(o, pygit2.Blob):
keys = keyfinder.findkeys(o.read_raw(), usebk=True)
if keys:
results[str(oid)] = keys
count += 1
if count % 1000 == 0:
print(f"pass1: {count} objects...")
print(f"found key(s) in {oid}")

fcount = 0
fnames = {}
for oid in dd:
o = rep.get(oid)
if isinstance(o, pygit2.Tree):
for x in o:
gid = str(x.id)
if gid in results and id not in fnames:
fnames[gid] = x.name
fcount += 1
if fcount >= count:
break
else:
continue
break

for k, v in results.items():
for spki, key in v.items():
fn = fnames.get(k, k)
keyfinder.writekey(key, fn, "out/", spki)

if args.keep:
print(f"Git clone / tmpdir not removed: {tmpdir}")
else:
shutil.rmtree(tmpdir)
2 changes: 1 addition & 1 deletion runci.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
set -euo pipefail

ruff check --select=ALL --ignore=PTH,ANN,D,ERA,S310,T201,C,PLR,S501,FIX,TD,FBT,I001 keyfinder.py
ruff check --select=ALL --ignore=PTH,ANN,D,ERA,S310,T201,C,PLR,S501,FIX,TD,FBT,I001 keyfinder.py gitkeyfind
python -m unittest -v

0 comments on commit 77d2b66

Please sign in to comment.