Permalink
Browse files

using digraph toposorter for revlist now

  • Loading branch information...
1 parent 33c6260 commit 04013c2a8f229b9ded991674e64289732f3801b6 @schacon committed Dec 3, 2009
Showing with 27 additions and 14 deletions.
  1. +22 −10 src/git.erl
  2. +2 −1 src/git.hrl
  3. +3 −3 src/git_object.erl
View
32 src/git.erl
@@ -8,11 +8,7 @@
-include("git.hrl").
%%-define(cassandra_ZERO, 0).
-
%-record(git_dir, {path}).
-%-record(commit, {commit_sha, tree_sha, parents, author, committer, encoding, message}).
-
-% Cp = #commit{sha=Sha, tree=Tree},
open(Path) ->
% normalize the path (look for .git, etc)
@@ -31,18 +27,34 @@ open(Path) ->
%io:fwrite("Log:~n").
rev_list(Git, Shas) ->
- rev_list(Git, Shas, []).
+ Graph = digraph:new(),
+ rev_list(Git, Graph, Shas),
+ digraph_utils:topsort(Graph).
-rev_list(Git, [Sha|Shas], Gathered) ->
+rev_list(Git, Graph, [Sha|Shas]) ->
{ok, Commit} = commit(Git, Sha),
+ digraph:add_vertex(Graph, Sha),
Parents = Commit#commit.parents,
- rev_list(Git, Parents ++ Shas, [Sha|Gathered]);
-rev_list(_Git, [], Gathered) ->
- Gathered.
+ AddParents = rev_list_add_edges(Graph, Sha, Parents),
+ rev_list(Git, Graph, AddParents ++ Shas);
+rev_list(_Git, Graph, []) ->
+ ok.
+
+rev_list_add_edges(Graph, Sha, [Parent|Rest]) ->
+ Vertex = case digraph:vertex(Graph, Parent) of
+ false ->
+ digraph:add_vertex(Graph, Parent);
+ _Else ->
+ []
+ end,
+ digraph:add_edge(Graph, Sha, Parent),
+ [Vertex|rev_list_add_edges(Graph, Sha, Rest)];
+rev_list_add_edges(Graph, Commit, []) ->
+ [].
commit(Git, Sha) ->
{_Type, _Size, Data} = read_object(Git, Sha),
- git_object:parse_commit(Data).
+ git_object:parse_commit(Sha, Data).
git_dir(Git) ->
{Path} = Git,
View
3 src/git.hrl
@@ -8,7 +8,8 @@
offsets,
packcs}).
--record(commit, {parents,
+-record(commit, {sha,
+ parents,
tree,
author,
committer,
View
6 src/git_object.erl
@@ -5,11 +5,11 @@
%% TODO: replace regexp:first_match with re
-module(git_object).
--export([parse_commit/1]).
+-export([parse_commit/2]).
-include("git.hrl").
-parse_commit(Data) ->
+parse_commit(Sha, Data) ->
CommitString = binary_to_list(Data),
{match, Offset, Len} = regexp:first_match(CommitString, "\n\n"),
{Meta, Message} = lists:split(Offset + Len - 1, CommitString),
@@ -19,7 +19,7 @@ parse_commit(Data) ->
Committer = extract_one(Meta, "committer (.*)"),
Encoding = extract_one(Meta, "encoding (.*)"),
%io:format("Parents:~p~nTree:~p~nAuthor:~p~nMessage:~p~n~n", [Parents, Tree, Author, Message]),
- Commit = #commit{tree=Tree, parents=Parents,
+ Commit = #commit{sha=Sha, tree=Tree, parents=Parents,
author=Author, committer=Committer,
encoding=Encoding, message=Message},
{ok, Commit}.

0 comments on commit 04013c2

Please sign in to comment.