Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

converting the shas to hex

  • Loading branch information...
commit 96c8fc72a658e7acf6f80d331457d85043926a89 1 parent b038a65
@schacon authored
Showing with 38 additions and 4 deletions.
  1. +9 −1 src/git.erl
  2. +18 −0 src/hex.erl
  3. +11 −3 src/packindex.erl
View
10 src/git.erl
@@ -85,11 +85,19 @@ get_packfile_object_data(Git, ObjectSha) ->
get_packfile_with_object(Git, [Index|Rest], ObjectSha) ->
PackIndex = git_dir(Git) ++ "/objects/pack/" ++ Index,
+ io:fwrite("Looking for ~p in ~p~n", [ObjectSha, PackIndex]),
case file:read_file(PackIndex) of
{ok, Data} ->
case packindex:extract_packfile_index(Data) of
{ok, IndexData} ->
- io:fwrite("PackIndex Size:~p~n", [IndexData#index.size]);
+ io:fwrite("PackIndex Size:~p~n", [IndexData#index.size]),
+ io:fwrite("IndexData:~p~n", [IndexData]),
+ case packindex:object_offset(IndexData, ObjectSha) of
+ {ok, Offset} ->
+ io:fwrite("Object Offset:~p~n", [Offset]);
+ not_found ->
+ get_packfile_with_object(Git, Rest, ObjectSha)
+ end;
Else ->
io:fwrite("Invalid, Biatch~p~n", [Else]),
invalid
View
18 src/hex.erl
@@ -0,0 +1,18 @@
+%
+% via Steve Vinoski
+% http://necrobious.blogspot.com/2008/03/binary-to-hex-string-back-to-binary-in.html
+%
+-module(hex).
+-export([bin_to_hexstr/1,hexstr_to_bin/1]).
+
+bin_to_hexstr(Bin) ->
+ lists:flatten([io_lib:format("~2.16.0b", [X]) ||
+ X <- binary_to_list(Bin)]).
+
+hexstr_to_bin(S) ->
+ hexstr_to_bin(S, []).
+hexstr_to_bin([], Acc) ->
+ list_to_binary(lists:reverse(Acc));
+hexstr_to_bin([X,Y|T], Acc) ->
+ {ok, [V], []} = io_lib:fread("~16u", [X,Y]),
+ hexstr_to_bin(T, [V | Acc]).
View
14 src/packindex.erl
@@ -3,10 +3,17 @@
%%
-module(packindex).
--export([extract_packfile_index/1]).
+-export([extract_packfile_index/1, object_offset/2]).
-include("packindex.hrl").
+
+%%%
+% get an object offset from an index record
+%%%
+object_offset(Index, ObjectSha) ->
+ not_found.
+
%%%
% extract a sha offset from packfile index data
% TODO: check to see if this is version 1 or version 2 (currently only v2)
@@ -22,7 +29,7 @@ extract_packfile_index(Data) ->
{PackCs, Data8} = split_binary(Data7, 20),
{_IdxCs, _Empty} = split_binary(Data8, 20),
Index = #index{header=Header, version=Version, size=Size, fanout=FanoutTable,
- shalist=ShaList, crclist=CrcList, offsets=OffsetList, packcs=PackCs},
+ shalist=ShaList, crclist=CrcList, offsets=OffsetList, packcs=hex:bin_to_hexstr(PackCs)},
{ok, Index}.
%%%
@@ -57,7 +64,8 @@ extract_sha_list(IndexData, Size, Size, Listing) ->
{lists:reverse(Listing), IndexData};
extract_sha_list(IndexData, Size, Total, Listing) ->
{Sha, IndexDataRem} = split_binary(IndexData, 20), % SHA
- extract_sha_list(IndexDataRem, Size, Total + 1, [Sha|Listing]).
+ HexSha = hex:bin_to_hexstr(Sha),
+ extract_sha_list(IndexDataRem, Size, Total + 1, [HexSha|Listing]).
%%%
% extract fanout table from packfile index
Please sign in to comment.
Something went wrong with that request. Please try again.