diff options
author | Brian Cully <bjc@kublai.com> | 2009-03-29 02:06:19 -0400 |
---|---|---|
committer | Brian Cully <bjc@kublai.com> | 2009-03-29 02:06:19 -0400 |
commit | 8d43f720444142fe34f5e866c7805d0cfc7a5f71 (patch) | |
tree | 4fa2d006f50fa800704dc23d1248e37dee773cfb | |
parent | db757eb82c812336a9cce01225c14e61cffd050e (diff) | |
download | dvcs-git-slides-8d43f720444142fe34f5e866c7805d0cfc7a5f71.tar.gz dvcs-git-slides-8d43f720444142fe34f5e866c7805d0cfc7a5f71.zip |
Add hexdump of contents.
-rwxr-xr-x | read_object.pl | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/read_object.pl b/read_object.pl index 8360882..6968a5d 100755 --- a/read_object.pl +++ b/read_object.pl @@ -37,17 +37,36 @@ sub format_object { $obj; } elsif ($type eq 'tag') { warn 'TODO - unimplemented'; - join '', map { sprintf('%02x ', $_) } unpack('C*', $obj); + hexdump($obj); } elsif ($type eq 'tree') { warn 'TODO - unimplemented'; - join '', map { sprintf('%02x ', $_) } unpack('C*', $obj); + hexdump($obj); } elsif ($type eq 'blob') { - warn 'TODO - unimplemented'; - join '', map { sprintf('%02x ', $_) } unpack('C*', $obj); + hexdump($obj); } else { warn "Unknown object type, showing hex representation: $type.\n"; - join '', map { sprintf('%02x', $_) } unpack('C*', $obj); + hexdump($obj); + } +} + +sub hexdump { + my ($str) = @_; + + my ($i, $len, @chunks) = (0, length($str), ()); + while ($i < $len) { + my $rem = $len - $i; + $rem = 16 unless $rem < 16; + push @chunks, substr($str, $i, $rem); + $i += $rem; } + + join "\n", map { + my @chars = unpack('C*', $_); + my @hex = map { sprintf('%02x ', $_) } @chars; + my @filtered = map { ($_ >= 040 && $_ <= 0176) ? pack('C', $_) : '.' } @chars; + my $spaces = (16 - @hex) * 3 + 4; + join('', @hex) . (' ' x $spaces) . join('', @filtered) + } @chunks; } 1; |