Skip to content

Commit

Permalink
refactored git source control provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
craiggwilson committed Sep 30, 2010
1 parent f49715e commit b71c9d5
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 76 deletions.
1 change: 1 addition & 0 deletions src/Foundry.Core/Foundry.Core.csproj
Expand Up @@ -57,6 +57,7 @@
<Compile Include="Security\UserAuthorization.cs" />
<Compile Include="SourceControl\IBranch.cs" />
<Compile Include="SourceControl\ICommit.cs" />
<Compile Include="SourceControl\ICommitInfo.cs" />
<Compile Include="SourceControl\IHasParents.cs" />
<Compile Include="SourceControl\ISourceFile.cs" />
<Compile Include="SourceControl\ISourceObject.cs" />
Expand Down
12 changes: 3 additions & 9 deletions src/Foundry.Core/SourceControl/ICommit.cs
Expand Up @@ -5,14 +5,8 @@

namespace Foundry.SourceControl
{
public interface ICommit
public interface ICommit : ICommitInfo
{
string Username { get; }

string Message { get; }

DateTime DateTime { get; }

string Version { get; }
IEnumerable<ISourceFile> Files { get; }
}
}
}
18 changes: 18 additions & 0 deletions src/Foundry.Core/SourceControl/ICommitInfo.cs
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Foundry.SourceControl
{
public interface ICommitInfo
{
string Username { get; }

string Message { get; }

DateTime DateTime { get; }

string Version { get; }
}
}
4 changes: 3 additions & 1 deletion src/Foundry.Core/SourceControl/ISourceControlManager.cs
Expand Up @@ -14,7 +14,9 @@ public interface ISourceControlManager

IEnumerable<IBranch> GetBranches(Project project);

IEnumerable<ICommit> GetHistory(Project project, string path);
ICommit GetCommit(Project project, string path);

IEnumerable<ICommitInfo> GetHistory(Project project, string path);

ISourceControlProviderMetadata GetProviderMetadata(Project project);

Expand Down
4 changes: 3 additions & 1 deletion src/Foundry.Core/SourceControl/ISourceControlProvider.cs
Expand Up @@ -12,7 +12,9 @@ public interface ISourceControlProvider

IEnumerable<IBranch> GetBranches(Project project);

IEnumerable<ICommit> GetHistory(Project project, string path);
IEnumerable<ICommitInfo> GetHistory(Project project, string path);

ICommit GetCommit(Project project, string path);

ISourceObject GetSourceObject(Project project, string path);
}
Expand Down
8 changes: 7 additions & 1 deletion src/Foundry.Services/SourceControl/SourceControlManager.cs
Expand Up @@ -43,7 +43,13 @@ public IEnumerable<IBranch> GetBranches(Project project)
return provider.Value.GetBranches(project);
}

public IEnumerable<ICommit> GetHistory(Project project, string path)
public ICommit GetCommit(Project project, string path)
{
var provider = _sourceControlProviders.Single(x => x.Metadata.Name == project.SourceControlProvider);
return provider.Value.GetCommit(project, path);
}

public IEnumerable<ICommitInfo> GetHistory(Project project, string path)
{
var provider = _sourceControlProviders.Single(x => x.Metadata.Name == project.SourceControlProvider);
return provider.Value.GetHistory(project, path);
Expand Down
Expand Up @@ -55,10 +55,11 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="GitCommit.cs" />
<Compile Include="GitSourceFile.cs" />
<Compile Include="GitSourceTree.cs" />
<Compile Include="GitBranch.cs" />
<Compile Include="GitCommit.cs" />
<Compile Include="GitCommitInfo.cs" />
<Compile Include="GitRouteHandler.cs" />
<Compile Include="GitHttpHandler.cs" />
<Compile Include="GitSettings.cs" />
Expand Down
12 changes: 2 additions & 10 deletions src/Foundry.SourceControl.GitIntegration/GitCommit.cs
Expand Up @@ -5,16 +5,8 @@

namespace Foundry.SourceControl.GitIntegration
{
public class GitCommit : ICommit, IHasParents
public class GitCommit : GitCommitInfo, ICommit
{
public string Username { get; set; }

public string Message { get; set; }

public DateTime DateTime { get; set; }

public string Version { get; set; }

public IEnumerable<string> ParentVersions { get; set; }
public IEnumerable<ISourceFile> Files { get; set; }
}
}
20 changes: 20 additions & 0 deletions src/Foundry.SourceControl.GitIntegration/GitCommitInfo.cs
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Foundry.SourceControl.GitIntegration
{
public class GitCommitInfo : ICommitInfo, IHasParents
{
public string Username { get; set; }

public string Message { get; set; }

public DateTime DateTime { get; set; }

public string Version { get; set; }

public IEnumerable<string> ParentVersions { get; set; }
}
}
126 changes: 76 additions & 50 deletions src/Foundry.SourceControl.GitIntegration/GitSourceControlProvider.cs
Expand Up @@ -36,14 +36,34 @@ public IEnumerable<IBranch> GetBranches(Project project)
});
}

public IEnumerable<ICommit> GetHistory(Project project, string path)
public ICommit GetCommit(Project project, string path)
{
var repo = GetRepository(project);

AbstractObject obj;
if (!TryGetObject(repo, path, out obj) || obj.IsCommit)
return null;

var commit = (Commit)obj;

return new GitCommit
{
Username = commit.Committer.Name,
DateTime = commit.CommitDate.DateTime,
Message = commit.Message,
Version = commit.ShortHash,
ParentVersions = commit.HasParents ? commit.Parents.Select(p => p.ShortHash) : Enumerable.Empty<string>()
};
}

public IEnumerable<ICommitInfo> GetHistory(Project project, string path)
{
var repo = GetRepository(project);

var branch = repo.Branches[path];
yield return new GitCommit
yield return new GitCommitInfo
{
Username = branch.CurrentCommit.Author.Name,
Username = branch.CurrentCommit.Committer.Name,
DateTime = branch.CurrentCommit.CommitDate.DateTime,
Message = branch.CurrentCommit.Message,
Version = branch.CurrentCommit.ShortHash,
Expand All @@ -52,9 +72,9 @@ public IEnumerable<ICommit> GetHistory(Project project, string path)

foreach (var ancestor in branch.CurrentCommit.Ancestors)
{
yield return new GitCommit
yield return new GitCommitInfo
{
Username = ancestor.Author.Name,
Username = ancestor.Committer.Name,
DateTime = ancestor.CommitDate.DateTime,
Message = ancestor.Message,
Version = ancestor.Tree.ShortHash,
Expand All @@ -69,70 +89,76 @@ public ISourceObject GetSourceObject(Project project, string path)

var parts = path.Split('/');

AbstractTreeNode node;
if (!TryGetTreeNode(repo, parts[0], out node))
AbstractObject obj;
if (!TryGetObject(repo, parts[0], out obj) || !obj.IsTree)
return null;

return GetSourceObject(repo, parts[0], node, parts.Skip(1));
var node = GetNode((AbstractTreeNode)obj, parts.Skip(1));
return CreateGitSourceObject(parts[0], node);
}

public static bool TryGetTreeNode(Repository repo, string id, out AbstractTreeNode node)
public static bool TryGetObject(Repository repo, string id, out AbstractObject obj)
{
node = null;
obj = null;
if (repo.Branches.ContainsKey(id))
node = repo.Branches[id].CurrentCommit.Tree;

return node != null;
}
obj = repo.Branches[id].CurrentCommit.Tree;

private static ISourceObject GetSourceObject(Repository repo, string branchName, AbstractTreeNode node, IEnumerable<string> path)
{
if (node.IsTree)
return GetSourceObjectFromTree(repo, branchName, (Tree)node, path);

return null;
return obj != null;
}

private static ISourceObject GetSourceObjectFromTree(Repository repo, string branchName, Tree tree, IEnumerable<string> path)
private static AbstractTreeNode GetNode(AbstractTreeNode parentNode, IEnumerable<string> path)
{
if (path.Any())
{
AbstractTreeNode node = tree.Trees.SingleOrDefault(x => x.Name == path.ElementAt(0));
if (node != null)
return GetSourceObjectFromTree(repo, branchName, (Tree)node, path.Skip(1));

node = tree.Leaves.SingleOrDefault(x => x.Name == path.ElementAt(0));
if (node != null)
if (parentNode.IsTree)
{
return new GitSourceFile
{
Name = node.Name,
IsTree = false,
Path = GetPath(branchName, node.Path),
LastModified = node.GetLastCommit().CommitDate.DateTime,
Message = node.GetLastCommit().Message,
Content = ((Leaf)node).RawData
};
var name = path.First();
var tree = (Tree)parentNode;
parentNode = tree.Trees.SingleOrDefault(x => x.Name == name);
if(parentNode == null)
parentNode = tree.Leaves.SingleOrDefault(x => x.Name == name);
if(parentNode == null)
throw new InvalidOperationException("Unable to determine path.");

return GetNode(parentNode, path.Skip(1));
}
else
{
throw new InvalidOperationException("Unable to determine path.");
}

return null;
}

return new GitSourceTree
{
Name = tree.Name,
IsTree = true,
Path = GetPath(branchName, tree.Path),
LastModified = tree.GetLastCommit().CommitDate.DateTime,
Message = tree.GetLastCommit().Message,
Children = tree.Trees.Select(x => CreateGitSourceObject(branchName, x))
.Union(tree.Leaves.Select(x => CreateGitSourceObject(branchName, x)))
};
return parentNode;
}

private static GitSourceObject CreateGitSourceObject(string branchName, AbstractTreeNode node)
private static GitSourceObject CreateGitSourceObject(string prefix, AbstractTreeNode node)
{
return new GitSourceObject { Name = node.Name, Path = GetPath(branchName, node.Path), IsTree = node.IsTree, LastModified = node.GetLastCommit().CommitDate.DateTime, Message = node.GetLastCommit().Message };
if (node.IsTree)
{
var tree = (Tree)node;
return new GitSourceTree
{
Name = tree.Name,
IsTree = true,
Path = GetPath(prefix, tree.Path),
LastModified = tree.GetLastCommit().CommitDate.DateTime,
Message = tree.GetLastCommit().Message,
Children = tree.Trees.Select(x => CreateGitSourceObject(prefix, x))
.Union(tree.Leaves.Select(x => CreateGitSourceObject(prefix, x)))
};
}
else
{
return new GitSourceFile
{
Name = node.Name,
IsTree = false,
Path = GetPath(prefix, node.Path),
LastModified = node.GetLastCommit().CommitDate.DateTime,
Message = node.GetLastCommit().Message,
Content = ((Leaf)node).RawData
};
}
}

private static string GetPath(string branchName, string path)
Expand Down
2 changes: 1 addition & 1 deletion src/Website/Controllers/ProjectController.cs
Expand Up @@ -55,7 +55,7 @@ public virtual ActionResult Index(string account, string repository)
PopulateCommon(model, account, repository);

if (model.DefaultBranch == null)
model.Commits = Enumerable.Empty<ICommit>();
model.Commits = Enumerable.Empty<ICommitInfo>();
else
model.Commits = _sourceControlManager.GetHistory(model.Project, model.DefaultBranch.Name).Take(20);

Expand Down
2 changes: 1 addition & 1 deletion src/Website/Models/Project/IndexViewModel.cs
Expand Up @@ -9,6 +9,6 @@ namespace Foundry.Website.Models.Project
{
public class IndexViewModel : ProjectViewModel
{
public IEnumerable<ICommit> Commits { get; set; }
public IEnumerable<ICommitInfo> Commits { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Website/Models/Project/ProjectViewModel.cs
Expand Up @@ -12,7 +12,7 @@ public class ProjectViewModel : ViewModel

public IBranch DefaultBranch { get; set; }

public ICommit LastCommit { get; set; }
public ICommitInfo LastCommit { get; set; }

public bool IsEmpty { get; set; }

Expand Down

0 comments on commit b71c9d5

Please sign in to comment.