Skip to content

Commit

Permalink
Improved repository performance and refactored changeset history grid.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisellgren committed May 13, 2012
1 parent f062b2b commit 9b80fec
Show file tree
Hide file tree
Showing 13 changed files with 307 additions and 129 deletions.
1 change: 1 addition & 0 deletions App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<ResourceDictionary Source="Styles/GridSplitter.xaml"/>
<ResourceDictionary Source="Styles/DisplayTags.xaml"/>
<ResourceDictionary Source="Components/StatusGridContextMenu.xaml"/>
<ResourceDictionary Source="Styles/ChangesetHistoryDataGrid.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Expand Down
4 changes: 2 additions & 2 deletions DesignData/Branches.xaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<local:RepositoryViewModel xmlns:local="clr-namespace:GG"
xmlns:models="clr-namespace:GG.Models"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<local:RepositoryViewModel.LocalBranches>
<local:RepositoryViewModel.Branches>
<models:Branch Name="master" Tip="0123asd" IsRemote="False" IsTracking="True" />
<models:Branch Name="foo" Tip="0123asd" IsRemote="False" IsTracking="False" />
<models:Branch Name="bar" Tip="0123asd" IsRemote="False" IsTracking="False" />
<models:Branch Name="test" Tip="0123asd" IsRemote="True" IsTracking="True" />
</local:RepositoryViewModel.LocalBranches>
</local:RepositoryViewModel.Branches>
</local:RepositoryViewModel>
5 changes: 5 additions & 0 deletions Git-GUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
<Compile Include="Libraries\FileUtil.cs" />
<Compile Include="Libraries\GenericUtil.cs" />
<Compile Include="Libraries\IconUtil.cs" />
<Compile Include="Libraries\Extensions\ObservableCollection.cs" />
<Compile Include="Libraries\RepoUtil.cs" />
<Compile Include="Libraries\UIHelper.cs" />
<Compile Include="Models\Branch.cs" />
Expand Down Expand Up @@ -193,6 +194,10 @@
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Page Include="Styles\ChangesetHistoryDataGrid.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Styles\DisplayTags.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
84 changes: 48 additions & 36 deletions Libraries/ChangesetGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ private void SetBranchColors(LibGit2Sharp.BranchCollection branches)
/// </summary>
public void Draw(ItemCollection commitList)
{
if (graph == null)
return;

Console.WriteLine("Drawing graph.");

LibGit2Sharp.Repository repo = new LibGit2Sharp.Repository(repositoryViewModel.FullPath);
Expand All @@ -76,6 +79,9 @@ public void Draw(ItemCollection commitList)
Commit commit = commitList.CurrentItem as Commit;
int rowNumber = commitList.CurrentPosition;

if (commit == null)
break;

// Get a list of branches around this commit.
List<Branch> branchesAroundCommit = commit.BranchesAround;

Expand Down Expand Up @@ -137,44 +143,49 @@ public void Draw(ItemCollection commitList)
foreach (string hash in commit.ParentHashes)
{
// Retrieve the parent commit dot position.
int[] parentPosition = commitDotPositions.Where(o => o.Key == hash).First().Value;
Brush lineColor = BranchColors[commit.Branches.ElementAt(0).Name];

// Calculate line positions.
float startLineX1 = dotX + dotSize / 2;
float startLineY1 = dotY + dotSize / 2;
float endLineX2 = parentPosition[0] + dotSize / 2;
float endLineY2 = parentPosition[1] + dotSize / 2;
float startLineX2;
float startLineY2;
float endLineX1;
float endLineY1;

if (commit.IsMergeCommit())
{
startLineX2 = endLineX2;
startLineY2 = startLineY1;
var positions = commitDotPositions.Where(o => o.Key == hash);

endLineX1 = endLineX2;
endLineY1 = startLineY1;
}
else
if (positions.Count() > 0)
{
startLineX2 = startLineX1;
startLineY2 = parentPosition[1] - cellHeight / 2 + dotSize / 2 + 6;
int[] parentPosition = commitDotPositions.Where(o => o.Key == hash).First().Value;

endLineX1 = startLineX1;
endLineY1 = parentPosition[1] - cellHeight / 2 + dotSize / 2 + 12;
}
Brush lineColor = BranchColors[commit.Branches.ElementAt(0).Name];

// Construct and draw the line path.
Path path = new Path
{
Stroke = lineColor,
StrokeThickness = 3,
Data = new PathGeometry
// Calculate line positions.
float startLineX1 = dotX + dotSize / 2;
float startLineY1 = dotY + dotSize / 2;
float endLineX2 = parentPosition[0] + dotSize / 2;
float endLineY2 = parentPosition[1] + dotSize / 2;
float startLineX2;
float startLineY2;
float endLineX1;
float endLineY1;

if (commit.IsMergeCommit())
{
startLineX2 = endLineX2;
startLineY2 = startLineY1;

endLineX1 = endLineX2;
endLineY1 = startLineY1;
}
else
{
startLineX2 = startLineX1;
startLineY2 = parentPosition[1] - cellHeight / 2 + dotSize / 2 + 6;

endLineX1 = startLineX1;
endLineY1 = parentPosition[1] - cellHeight / 2 + dotSize / 2 + 12;
}

// Construct and draw the line path.
Path path = new Path
{
Figures = new PathFigureCollection
Stroke = lineColor,
StrokeThickness = 3,
Data = new PathGeometry
{
Figures = new PathFigureCollection
{
new PathFigure
{
Expand All @@ -193,10 +204,11 @@ public void Draw(ItemCollection commitList)
}
}
}
}
};
}
};

graph.Children.Add(path);
graph.Children.Add(path);
}
}

commitList.MoveCurrentToPrevious();
Expand Down
71 changes: 71 additions & 0 deletions Libraries/Extensions/ObservableCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Linq;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.ComponentModel;

namespace GG.Libraries.Extensions
{

/// <summary>
/// Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.
/// </summary>
/// <typeparam name="T"></typeparam>
public class ObservableCollection<T> : System.Collections.ObjectModel.ObservableCollection<T>
{

/// <summary>
/// Adds the elements of the specified collection to the end of the ObservableCollection(Of T).
/// </summary>
public void AddRange(IEnumerable<T> collection)
{
foreach (var i in collection) Items.Add(i);
//OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, collection.ToList()));
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
}

/// <summary>
/// Removes the first occurence of each item in the specified collection from ObservableCollection(Of T).
/// </summary>
public void RemoveRange(IEnumerable<T> collection)
{
foreach (var i in collection) Items.Remove(i);
//OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, collection.ToList()));
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
}

/// <summary>
/// Clears the current collection and replaces it with the specified item.
/// </summary>
public void Replace(T item)
{
ReplaceRange(new T[] { item });
}
/// <summary>
/// Clears the current collection and replaces it with the specified collection.
/// </summary>
public void ReplaceRange(IEnumerable<T> collection)
{
List<T> old = new List<T>(Items);
Items.Clear();
foreach (var i in collection) Items.Add(i);
//OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, collection.ToList()));
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
}

/// <summary>
/// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class.
/// </summary>
public ObservableCollection() : base() { }

/// <summary>
/// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class that contains elements copied from the specified collection.
/// </summary>
/// <param name="collection">collection: The collection from which the elements are copied.</param>
/// <exception cref="System.ArgumentNullException">The collection parameter cannot be null.</exception>
public ObservableCollection(IEnumerable<T> collection) : base(collection) { }
}
}
3 changes: 3 additions & 0 deletions Libraries/RepoUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public static List<Branch> GetBranchesAroundCommit(Commit commit, ObservableColl
// Loop through all branches and determine if they are around the specified commit.
foreach (Branch branch in branches)
{
if (branch.Tip == null)
continue;

// The branch's tip must be newer/same than the commit.
if (branch.Tip.Date >= commit.Date) // TODO: && first commit-ever must be older? We might not need to do that... ... ?
{
Expand Down
22 changes: 20 additions & 2 deletions Models/Branch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Branch
/// <param name="repo"></param>
/// <param name="branch"></param>
/// <returns></returns>
public static Branch Create(LibGit2Sharp.Repository repo, LibGit2Sharp.Branch branch)
public static Branch Create(LibGit2Sharp.Repository repo, LibGit2Sharp.Branch branch, ObservableCollection<Commit> commits, int commitsPerPage)
{
Branch newBranch = new Branch
{
Expand All @@ -48,6 +48,24 @@ public static Branch Create(LibGit2Sharp.Repository repo, LibGit2Sharp.Branch br
TrackedBranchName = branch.TrackedBranch != null ? branch.TrackedBranch.Name : null
};

// Loop through the first N commits and let them know about me.
foreach (LibGit2Sharp.Commit branchCommit in branch.Commits.Take(commitsPerPage))
{
Commit commit = commits.Where(c => c.Hash == branchCommit.Sha.ToString()).FirstOrDefault();

if (commit != null)
{
commit.Branches.Add(newBranch); // Let the commit know that I am one of her branches.

// Process commit DisplayTags (tags to display next to the commit description, in this case = branch Tips).
if (newBranch.TipHash == commit.Hash)
{
commit.DisplayTags.Add(branch.Name);
newBranch.Tip = commit;
}
}
}

return newBranch;
}

Expand All @@ -61,7 +79,7 @@ public void PostProcess(ObservableCollection<Branch> branches, ObservableCollect
TrackedBranch = branches.Where(b => b.Name == TrackedBranchName).FirstOrDefault();

// Set the Tip to be an actual Commit model.
Tip = commits.Where(c => c.Hash == TipHash).First();
Tip = commits.Where(c => c.Hash == TipHash).FirstOrDefault();
}
}
}
26 changes: 2 additions & 24 deletions Models/Commit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,10 @@ public bool IsMergeCommit()
/// <returns></returns>
public static Commit Create(LibGit2Sharp.Repository repo,
LibGit2Sharp.Commit commit,
ObservableCollection<Branch> branches,
ObservableCollection<Tag> tags)
{
Commit c = new Commit();

// Process DisplayTags (tags to display next to the commit description).
List<string> displayTags = new List<string>();
foreach (Branch branch in branches)
{
if (branch.TipHash == commit.Sha.ToString())
{
displayTags.Add(branch.Name);
branch.Tip = c;
}
}

// Process Tags (Git tags to display next to the commit description).
List<string> commitTags = new List<string>();
foreach (Tag tag in tags)
Expand All @@ -108,23 +96,13 @@ public static Commit Create(LibGit2Sharp.Repository repo,
parentHashes.Add(parentCommit.Sha.ToString());
}

// Process branches.
List<Branch> commitBranches = new List<Branch>();
foreach (LibGit2Sharp.Branch branch in RepoUtil.GetBranchesContaininingCommit(repo, commit.Sha))
{
Branch commitBranch = branches.Where(b => b.Name == branch.Name).FirstOrDefault();

if (commitBranch != null)
commitBranches.Add(commitBranch);
}

// Set properties.
c.AuthorEmail = commit.Author.Email;
c.AuthorName = commit.Author.Name;
c.Branches = commitBranches;
c.Date = commit.Author.When.DateTime;
c.Description = commit.MessageShort;
c.DisplayTags = displayTags;
c.DisplayTags = new List<string>();
c.Branches = new List<Branch>();
c.Tags = commitTags;
c.Hash = commit.Sha.ToString();
c.ParentHashes = parentHashes;
Expand Down
Loading

0 comments on commit 9b80fec

Please sign in to comment.