Skip to content

Commit

Permalink
Stopped double adding edges
Browse files Browse the repository at this point in the history
  • Loading branch information
haacked committed Mar 6, 2012
1 parent ae42a7a commit 86e5c9d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
47 changes: 47 additions & 0 deletions SeeGitApp/Models/CommitEdge.cs
Expand Up @@ -6,9 +6,56 @@ namespace SeeGit
[DebuggerDisplay("{Source.Sha}..{Target.Sha}")]
public class CommitEdge : Edge<CommitVertex>
{
private string _id;

public CommitEdge(CommitVertex child, CommitVertex parent)
: base(child, parent)
{
}

public string Id
{
get
{
if (_id == null)
{
if (Source != null)
{
_id = Source.Sha;
}
_id += "..";
if (Target != null)
{
_id += Target.Sha;
}
}
return _id;
}
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (CommitEdge)) return false;
return Equals((CommitEdge)obj);
}

public bool Equals(CommitEdge other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(other.Source, other.Target);
}

public override int GetHashCode()
{
unchecked
{
int result = (Target != null ? Target.GetHashCode() : 0);
result = (result*397) ^ (Source != null ? Source.GetHashCode() : 0);
return result;
}
}
}
}
1 change: 1 addition & 0 deletions SeeGitApp/Models/RepositoryGraph.cs
Expand Up @@ -4,5 +4,6 @@ namespace SeeGit
{
public class RepositoryGraph : BidirectionalGraph<CommitVertex, CommitEdge>
{
public string LayoutAlgorithmType { get; set; }
}
}
23 changes: 21 additions & 2 deletions SeeGitApp/Models/RepositoryGraphBuilder.cs
Expand Up @@ -9,6 +9,7 @@ public class RepositoryGraphBuilder : IRepositoryGraphBuilder
private readonly RepositoryGraph _graph = new RepositoryGraph();
private readonly Repository _repository;
private readonly Dictionary<string, CommitVertex> _vertices = new Dictionary<string, CommitVertex>();
private readonly Dictionary<string, CommitEdge> _edges = new Dictionary<string, CommitEdge>();

public RepositoryGraphBuilder(string gitRepositoryPath)
{
Expand Down Expand Up @@ -36,6 +37,15 @@ public RepositoryGraph Graph()
_graph.Clear();
return _graph;
}

foreach (var vertice in _vertices.Values)
{
if (vertice.Branches.Any())
{
vertice.Branches.Clear();
}
}

AddCommitsToGraph(commits.First(), null);

foreach (var branch in _repository.Branches.Where(branch => branch.Commits.Any()))
Expand All @@ -45,6 +55,10 @@ public RepositoryGraph Graph()
commit.Branches.Add(branch.Name);
AddCommitsToGraph(firstCommit, null);
}
if (_vertices.Count > 1)
{
_graph.LayoutAlgorithmType = "EfficientSugiyama";
}

return _graph;
}
Expand All @@ -55,7 +69,12 @@ private void AddCommitsToGraph(Commit commit, CommitVertex childVertex)
_graph.AddVertex(commitVertex);
if (childVertex != null)
{
_graph.AddEdge(new CommitEdge(childVertex, commitVertex));
var edge = new CommitEdge(childVertex, commitVertex);
if (!_edges.ContainsKey(edge.Id))
{
_graph.AddEdge(edge);
_edges.Add(edge.Id, edge);
}
}

foreach (var parent in commit.Parents)
Expand All @@ -66,7 +85,7 @@ private void AddCommitsToGraph(Commit commit, CommitVertex childVertex)

private CommitVertex GetCommitVertex(Commit commit)
{
CommitVertex commitVertex = null;
CommitVertex commitVertex;
if (!_vertices.TryGetValue(commit.Sha, out commitVertex))
{
commitVertex = new CommitVertex(commit.Sha, commit.MessageShort) {Description = commit.Message};
Expand Down
16 changes: 13 additions & 3 deletions SeeGitApp/ViewModels/MainWindowViewModel.cs
Expand Up @@ -7,29 +7,39 @@ public class MainWindowViewModel : INotifyPropertyChanged
{
private RepositoryGraph _graph;
private readonly IRepositoryGraphBuilder _graphBuilder;
private string layoutAlgorithmType = "Tree";

public MainWindowViewModel(IRepositoryGraphBuilder graphBuilder, string repositoryPath)
{
_graphBuilder = graphBuilder;
RepositoryPath = repositoryPath;
Graph = _graphBuilder.Graph();
LayoutAlgorithmType = "Tree";
// Tree, LinLog, KK, ISOM, EfficientSugiyama, FR, CompoundFDP, BoundedFR, Circular
// Tree, LinLog, KK, ISOM, EfficientSugiyama, FR, CompoundFDP, BoundedFR, Circular
}

public string LayoutAlgorithmType { get; private set; }
public string LayoutAlgorithmType
{
get { return layoutAlgorithmType; }
set
{
layoutAlgorithmType = value;
NotifyPropertyChanged("LayoutAlgorithmType");
}
}

public RepositoryGraph Graph
{
get { return _graph; }
set
{
_graph = value;
LayoutAlgorithmType = _graph.LayoutAlgorithmType;
NotifyPropertyChanged("Graph");
}
}

public string RepositoryPath { get; private set;}
public string RepositoryPath { get; private set; }

public void Refresh()
{
Expand Down

0 comments on commit 86e5c9d

Please sign in to comment.