Lost in the Git Orcus (Part 3)

Now with packs!

--amending my last post, I changed the script to search for lost files in the git dungeon.
It now searches all packed objects too (again based on stackoverflow.com from willkil)
The parameters have not changed. See my last post for a short description. You can find the script on GitHub.
There is also an even smaller script, which gets you rid of all the branches the search script may have created.
(Btw.: I changed the hashbang from /opt/local/bin/perl (OS X) to /usr/bin/perl (Linux). You may change it to what fits for you.)
#!/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;
			}
		}
	}
}