Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 61 lines (52 sloc) 1.69 KB
#!/usr/bin/perl
my $type = $ARGV[0]; # 'tree' or 'blob'?
my $pattern = $ARGV[1]; # filename to search for as regex
my $createBranches = $ARGV[2]; # 'y' if you want to create branches (only when search for 'tree')
##################################################################
# find all objects (blobs, trees, commits, tags) in .git/objects
##################################################################
my %AllSha1;
# scan the packed and non-packed files
my @AllFiles = `find .git/objects/`;
for my $f (@AllFiles) {
if($f =~ /([0-9a-f][0-9a-f])\/([0-9a-f]{38})/) {
# non-packed
my $sha1 = $1 . $2;
$AllSha1{$sha1} = 1;
}
elsif($f =~ /idx$/) {
# packed
my @IndexContent = `git show-index < $f`;
for my $indexLine (@IndexContent) {
my @IndexLineParts = split(/ /, $indexLine);
my $sha1 = $IndexLineParts[1];
$AllSha1{$sha1} = 1;
}
}
}
##################################################################
# find all trees or blobs containing the pattern
##################################################################
my $ctr = 0;
for my $foundSha1 (keys %AllSha1) {
chomp $foundSha1;
next if length($foundSha1) == 0;
my $t = `git cat-file -t $foundSha1`;
chomp $t;
if($t eq $type) {
my @Lines = `git cat-file -p $foundSha1`;
for my $line (@Lines) {
if($line =~ /$pattern/) {
$ctr++;
printf "found $type: %s\n", $foundSha1;
if($type eq 'tree' && $createBranches eq 'y') {
my $branchName = sprintf "found/%03s", $ctr;
printf "created branch: %s\n", $branchName;
my $commitSha1 = `git commit-tree $foundSha1 -m "found $pattern"`;
`git branch $branchName $commitSha1`;
}
last;
}
}
}
}