#!/usr/bin/perl -w # Copyright 2018 The Linux Foundation # License: AGPL-3.0+ use strict; use warnings; use Email::MIME; use Digest::MD5 qw(md5_hex); $| = 0; my $h = '[0-9a-f]'; my $state = ''; my $blob; my $suff; # 16 bytes for git hashing while () { if ($_ eq "blob\n") { $state = 'blob'; } elsif (/^commit /) { $state = 'commit'; } elsif ($state eq 'commit') { if (m{^(M 100644 :\d+) (${h}{2})/(${h}{2})(${h}{2})(${h}{34})}o) { my ($pfx, $x2, $x4, $x6, $x34) = ($1, $2, $3, $4, $5); print "$pfx $x2/$x4/$x6/$x34.$suff\n"; next; } if (/^data (\d+)/) { print $_; my $len = $1; if ($len) { my $tmp; read(STDIN, $tmp, $len) or die "read: $!\n"; print $tmp; } next; } } elsif ($state eq 'blob') { if (/^data (\d+)/) { my $len = $1; print $_; next unless $len; read(STDIN, $blob, $len) or die "read: $!\n"; print $blob; my $mime = Email::MIME->new($blob); $suff = $mime->header('Subject'); utf8::encode($suff); # git uses the last 16 bytes for deltas $suff = substr(md5_hex(substr($suff, -16)), -16); next; } } print $_; }