-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitsearch.py
More file actions
192 lines (155 loc) · 6.29 KB
/
Copy pathgitsearch.py
File metadata and controls
192 lines (155 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python3
"""
File search tool: git history + filesystem with pattern matching.
Searches for files matching patterns (e.g., *.env, secrets, apikey) in:
1. Git commit history (to find accidentally committed files)
2. Current filesystem
"""
import subprocess
import re
import sys
import os
from pathlib import Path
from typing import List, Dict
import argparse
import json
def search_git_history(pattern: str, repo_path: str = ".", timeout: int = 15) -> List[str]:
"""Search for files in git history matching the pattern.
This uses a single `git log --all --name-only --pretty=format:` call
to avoid iterating commits one-by-one which is slow on large repos.
"""
results: Dict[str, None] = {}
try:
output = subprocess.check_output(
["git", "log", "--all", "--name-only", "--pretty=format:"],
cwd=repo_path,
text=True,
stderr=subprocess.DEVNULL,
timeout=timeout,
)
except (subprocess.CalledProcessError, FileNotFoundError, subprocess.TimeoutExpired):
return []
for line in output.splitlines():
fn = line.strip()
if not fn:
continue
if _matches_pattern(fn, pattern):
results[fn] = None
return sorted(results.keys())
def search_filesystem(pattern: str, root_path: str = ".") -> List[str]:
"""Search filesystem for files matching the pattern.
Hidden directories (starting with `.`) and `.git` are skipped.
"""
results = []
try:
root = Path(root_path).resolve()
for item in root.rglob("*"):
# Skip .git and hidden directories
if ".git" in item.parts or any(p.startswith(".") for p in item.parts[len(root.parts):]):
continue
try:
name = item.name
except OSError:
continue
if _matches_pattern(name, pattern):
try:
results.append(str(item.relative_to(root)))
except Exception:
results.append(str(item))
except (OSError, PermissionError):
pass
return sorted(results)
def _matches_pattern(filename: str, pattern: str) -> bool:
"""Return True if `filename` matches `pattern`.
Pattern forms supported:
- Regex: /pattern/ or /pattern/flags (e.g. /secret/i)
- Glob: contains `*` or `?`
- Substring: case-insensitive containment
"""
# Regex pattern with optional flags: /pattern/flags
if pattern.startswith("/") and pattern.count("/") >= 2:
# find last slash
last = pattern.rfind("/")
pat = pattern[1:last]
flags = pattern[last+1:]
re_flags = 0
if "i" in flags:
re_flags |= re.IGNORECASE
try:
return bool(re.search(pat, filename, flags=re_flags))
except re.error:
return False
# Glob pattern
if "*" in pattern or "?" in pattern:
from fnmatch import fnmatch
return fnmatch(filename, pattern)
# Substring (case-insensitive)
return pattern.lower() in filename.lower()
def print_results(git_files: List[str], fs_files: List[str], pattern: str, *, max_results: int = 20, json_out: bool = False):
"""Pretty print or JSON-serialize results."""
if json_out:
payload = {
"pattern": pattern,
"git": git_files,
"filesystem": fs_files,
}
print(json.dumps(payload, indent=2))
return
print(f"\n🔍 Search Results for: {pattern}\n")
if git_files:
print(f"📦 Found in git history ({len(git_files)}):")
for f in git_files[:max_results]:
print(f" git: {f}")
if len(git_files) > max_results:
print(f" ... and {len(git_files) - max_results} more")
if fs_files:
print(f"\n📁 Found in filesystem ({len(fs_files)}):")
for f in fs_files[:max_results]:
print(f" fs: {f}")
if len(fs_files) > max_results:
print(f" ... and {len(fs_files) - max_results} more")
if not git_files and not fs_files:
print("✅ No matches found")
print()
def main():
parser = argparse.ArgumentParser(
description="Search for files in git history and filesystem",
epilog="""
Examples:
python search.py "*.env" # Find .env files
python search.py "*apikey*" # Find files with 'apikey'
python search.py "secrets" # Find files containing 'secrets'
python search.py "/^\\.env\\..*$/" # Regex: .env.* files
python search.py "*.pem" --git-only
python search.py "*password*" --fs-only /path/to/search
""",
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("pattern", help="Pattern to search (glob, substring, or /regex/)")
parser.add_argument("--git-only", action="store_true", help="Search git history only")
parser.add_argument("--fs-only", action="store_true", help="Search filesystem only")
parser.add_argument("--path", "-p", default=".", help="Root path to search (default: current dir)")
parser.add_argument("--repo", "-r", default=".", help="Git repo path (default: current dir)")
parser.add_argument("--json", action="store_true", help="Output results as JSON")
parser.add_argument("--max-results", type=int, default=20, help="Max results to show per section (default: 20)")
parser.add_argument("--timeout", type=int, default=15, help="Timeout seconds for git operations (default: 15)")
parser.add_argument("--verbose", action="store_true", help="Verbose logging")
args = parser.parse_args()
git_files = []
fs_files = []
if not args.fs_only:
if args.verbose:
print(f"Searching git history in: {args.repo}")
git_files = search_git_history(args.pattern, args.repo, timeout=args.timeout)
if not args.git_only:
if args.verbose:
print(f"Searching filesystem in: {args.path}")
fs_files = search_filesystem(args.pattern, args.path)
print_results(git_files, fs_files, args.pattern, max_results=args.max_results, json_out=args.json)
# Exit codes: 0 = no matches, 2 = matches found (non-zero to signal CI failures)
if git_files or fs_files:
sys.exit(2)
else:
sys.exit(0)
if __name__ == "__main__":
main()