From 86e5c9dcc06162e090d3c353d279a091c03c0a45 Mon Sep 17 00:00:00 2001 From: Haacked Date: Tue, 6 Mar 2012 01:34:28 -0800 Subject: [PATCH] Stopped double adding edges --- SeeGitApp/Models/CommitEdge.cs | 47 +++++++++++++++++++++ SeeGitApp/Models/RepositoryGraph.cs | 1 + SeeGitApp/Models/RepositoryGraphBuilder.cs | 23 +++++++++- SeeGitApp/ViewModels/MainWindowViewModel.cs | 16 +++++-- 4 files changed, 82 insertions(+), 5 deletions(-) diff --git a/SeeGitApp/Models/CommitEdge.cs b/SeeGitApp/Models/CommitEdge.cs index 1ee1850..7dc2038 100644 --- a/SeeGitApp/Models/CommitEdge.cs +++ b/SeeGitApp/Models/CommitEdge.cs @@ -6,9 +6,56 @@ namespace SeeGit [DebuggerDisplay("{Source.Sha}..{Target.Sha}")] public class CommitEdge : Edge { + 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; + } + } } } \ No newline at end of file diff --git a/SeeGitApp/Models/RepositoryGraph.cs b/SeeGitApp/Models/RepositoryGraph.cs index b02c8ca..d0be324 100644 --- a/SeeGitApp/Models/RepositoryGraph.cs +++ b/SeeGitApp/Models/RepositoryGraph.cs @@ -4,5 +4,6 @@ namespace SeeGit { public class RepositoryGraph : BidirectionalGraph { + public string LayoutAlgorithmType { get; set; } } } \ No newline at end of file diff --git a/SeeGitApp/Models/RepositoryGraphBuilder.cs b/SeeGitApp/Models/RepositoryGraphBuilder.cs index de5aa9e..920a2f6 100644 --- a/SeeGitApp/Models/RepositoryGraphBuilder.cs +++ b/SeeGitApp/Models/RepositoryGraphBuilder.cs @@ -9,6 +9,7 @@ public class RepositoryGraphBuilder : IRepositoryGraphBuilder private readonly RepositoryGraph _graph = new RepositoryGraph(); private readonly Repository _repository; private readonly Dictionary _vertices = new Dictionary(); + private readonly Dictionary _edges = new Dictionary(); public RepositoryGraphBuilder(string gitRepositoryPath) { @@ -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())) @@ -45,6 +55,10 @@ public RepositoryGraph Graph() commit.Branches.Add(branch.Name); AddCommitsToGraph(firstCommit, null); } + if (_vertices.Count > 1) + { + _graph.LayoutAlgorithmType = "EfficientSugiyama"; + } return _graph; } @@ -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) @@ -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}; diff --git a/SeeGitApp/ViewModels/MainWindowViewModel.cs b/SeeGitApp/ViewModels/MainWindowViewModel.cs index 2a50891..1a64aed 100644 --- a/SeeGitApp/ViewModels/MainWindowViewModel.cs +++ b/SeeGitApp/ViewModels/MainWindowViewModel.cs @@ -7,6 +7,7 @@ public class MainWindowViewModel : INotifyPropertyChanged { private RepositoryGraph _graph; private readonly IRepositoryGraphBuilder _graphBuilder; + private string layoutAlgorithmType = "Tree"; public MainWindowViewModel(IRepositoryGraphBuilder graphBuilder, string repositoryPath) { @@ -14,10 +15,18 @@ public MainWindowViewModel(IRepositoryGraphBuilder graphBuilder, string reposito 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 { @@ -25,11 +34,12 @@ public RepositoryGraph Graph set { _graph = value; + LayoutAlgorithmType = _graph.LayoutAlgorithmType; NotifyPropertyChanged("Graph"); } } - public string RepositoryPath { get; private set;} + public string RepositoryPath { get; private set; } public void Refresh() {