Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

cat file internals for loose objects basically working

  • Loading branch information...
commit 14732d8f3f348367be2c4240e0798892e4c031a4 1 parent c68b42c
@schacon authored
Showing with 54 additions and 11 deletions.
  1. +4 −0 Rakefile
  2. +47 −9 src/git.erl
  3. +3 −2 t/001-cat-file.t
View
4 Rakefile
@@ -7,3 +7,7 @@ task :default do
cd "src"
sh "erlc #{ERLC_FLAGS} *.erl"
end
+
+task :console do
+ sh "erl +Bc +K true -smp enable -pz ./ebin/ -sname local_console_#{$$} -kernel start_boot_server true"
+end
View
56 src/git.erl
@@ -13,16 +13,54 @@
% Cp = #commit{sha=Sha, tree=Tree},
open(Path) ->
- %% normalize the path (look for .git, etc)
+ % normalize the path (look for .git, etc)
{Path}.
+references(Git) ->
+ % read all the refs from disk/packed-refs and return an array
+ {Git}.
+
+print_branches(Git) ->
+ % print branches out to stdout
+ io:fwrite("Branches:~n").
+
+print_log(Git, Ref) ->
+ % traverse the reference, printing out all the log information to stdout
+ io:fwrite("Log:~n").
+
read_object(Git, ObjectSha) ->
- %% find object path (loose)
- %% read object data
- %% de-header it
- %% de-zlib it
- %% return object data
- Type = commit,
- Size = 355,
- Data = <<"hello">>,
+ RawData = get_object_data(Git, ObjectSha),
+ {Type, Size, Data} = extract_object_data(RawData),
+ % de-zlib the data
+ % return object data
{Type, Size, Data}.
+
+%% TODO: make this more efficient - this is ridiculous
+%% should be able to do this as a binary
+extract_object_data(CompData) ->
+ RawData = binary_to_list(zlib:uncompress(CompData)),
+ Split = string:chr(RawData, 0),
+ {Header, Data} = lists:split(Split, RawData),
+ Split2 = string:chr(Header, 32),
+ Header2 = lists:sublist(Header, length(Header) - 1),
+ {Type, Size} = lists:split(Split2, Header2),
+ Type2 = lists:sublist(Type, length(Type) - 1),
+ {binary_to_atom(list_to_binary(Type2), latin1), list_to_integer(Size), list_to_binary(Data)}.
+
+git_dir(Git) ->
+ {Path} = Git,
+ Path.
+
+% get the raw object data out of loose or packed formats
+get_object_data(Git, ObjectSha) ->
+ % see if the object is loose, read the data
+ % else check the packfile indexes and get the object out of a packfile
+ First = string:substr(ObjectSha, 1, 2),
+ Second = string:substr(ObjectSha, 3, 38),
+ FileName = git_dir(Git) ++ "/objects/" ++ First ++ "/" ++ Second,
+ case file:read_file(FileName) of
+ {ok, Data} ->
+ Data;
+ _Else ->
+ invalid
+ end.
View
5 t/001-cat-file.t
@@ -4,5 +4,6 @@
main(_) ->
Git = git:open("/Users/schacon/projects/subsucka/.git"),
- {Type, Size, _} = git:read_object(Git, "019b03b2b5f184a9bbec13bfb4be53b80561f213"), %% blob
- io:fwrite("Data: ~p ~p~n", [Type, Size]).
+ {Type, Size, Data} = git:read_object(Git, "019b03b2b5f184a9bbec13bfb4be53b80561f213"), %% blob
+ io:fwrite("Data: ~p ~p~n", [Type, Size]),
+ io:fwrite("Data: ~p~n", [Data]).
Please sign in to comment.
Something went wrong with that request. Please try again.