Permalink
Browse files

Use NGit as backend library for HTTP services

  • Loading branch information...
1 parent 3679a76 commit 71412b07c48eab70c0404384ec04acc04d5ef0f1 @linquize committed Jul 2, 2012
View
@@ -9,6 +9,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitSharp-VS2010", "lib\GitS
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitSharp.Core-VS2010", "lib\GitSharp\GitSharp.Core\GitSharp.Core-VS2010.csproj", "{C46EDD61-C202-465A-93F1-ADE20A83BB59}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NGit", "lib\NGit\NGit\NGit.csproj", "{E3BFF8B4-189C-496A-A817-7E8B31E22B91}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NSch", "lib\NGit\NSch\NSch.csproj", "{A19E6F3F-A25B-4B01-8922-CF0CC35C781D}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sharpen", "lib\NGit\Sharpen\Sharpen.csproj", "{72944A6C-45FF-4EF8-B349-8C9CABF519D4}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -31,6 +37,18 @@ Global
{C46EDD61-C202-465A-93F1-ADE20A83BB59}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C46EDD61-C202-465A-93F1-ADE20A83BB59}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C46EDD61-C202-465A-93F1-ADE20A83BB59}.Release|Any CPU.Build.0 = Release|Any CPU
+ {E3BFF8B4-189C-496A-A817-7E8B31E22B91}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {E3BFF8B4-189C-496A-A817-7E8B31E22B91}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {E3BFF8B4-189C-496A-A817-7E8B31E22B91}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {E3BFF8B4-189C-496A-A817-7E8B31E22B91}.Release|Any CPU.Build.0 = Release|Any CPU
+ {A19E6F3F-A25B-4B01-8922-CF0CC35C781D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {A19E6F3F-A25B-4B01-8922-CF0CC35C781D}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {A19E6F3F-A25B-4B01-8922-CF0CC35C781D}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {A19E6F3F-A25B-4B01-8922-CF0CC35C781D}.Release|Any CPU.Build.0 = Release|Any CPU
+ {72944A6C-45FF-4EF8-B349-8C9CABF519D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {72944A6C-45FF-4EF8-B349-8C9CABF519D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {72944A6C-45FF-4EF8-B349-8C9CABF519D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {72944A6C-45FF-4EF8-B349-8C9CABF519D4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
View
@@ -89,10 +89,13 @@
<Compile Include="Lib\AppSettings.cs" />
<Compile Include="Lib\ForbiddenResult.cs" />
<Compile Include="Lib\WebBrowsingSettings.cs" />
- <Compile Include="Lib\SimpleRefWriter.cs" />
+ <Compile Include="Lib\GitSharpCoreBasedSimpleRefWriter.cs" />
<Compile Include="Lib\NotFoundResult.cs" />
<Compile Include="Lib\GitRepository.cs" />
<Compile Include="Lib\StructureMapControllerFactory.cs" />
+ <Compile Include="Lib\GitSharpCoreBasedRepository.cs" />
+ <Compile Include="Lib\NGitBasedRepository.cs" />
+ <Compile Include="Lib\NGitBasedSimpleRefWriter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Lib\RepositoryService.cs" />
<Compile Include="PrettyDate.cs" />
@@ -236,6 +239,14 @@
<Project>{7311850F-619A-4241-B09F-157792C75FBA}</Project>
<Name>GitSharp</Name>
</ProjectReference>
+ <ProjectReference Include="..\lib\NGit\NGit\NGit.csproj">
+ <Project>{E3BFF8B4-189C-496A-A817-7E8B31E22B91}</Project>
+ <Name>NGit</Name>
+ </ProjectReference>
+ <ProjectReference Include="..\lib\NGit\Sharpen\Sharpen.csproj">
+ <Project>{72944A6C-45FF-4EF8-B349-8C9CABF519D4}</Project>
+ <Name>Sharpen</Name>
+ </ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="Views\TreeView\EmptyRepository.cshtml" />
@@ -1,97 +1,33 @@
namespace GitAspx.Lib
{
using System;
- using System.Collections.Generic;
using System.IO;
- using System.Linq;
- using System.Text;
- using GitSharp.Core;
- using GitSharp.Core.Transport;
- public class GitRepository
+ public abstract class GitRepository
{
- private DirectoryInfo directory;
- string rootDirectory;
+ protected DirectoryInfo directory;
+ protected string rootDirectory;
public static GitRepository Open(DirectoryInfo directory, string rootDirectory)
{
- if (GitSharp.Repository.IsValid(directory.FullName))
- {
- return new GitRepository(directory, rootDirectory);
- }
-
- return null;
+ return new NGitBasedRepository(directory, rootDirectory);
}
- public GitRepository(DirectoryInfo directory, string rootDirectory)
+ protected GitRepository(DirectoryInfo directory, string rootDirectory)
{
this.directory = directory;
this.rootDirectory = rootDirectory;
}
- public void AdvertiseUploadPack(Stream output)
- {
- using (var repository = GetRepository())
- {
- var pack = new UploadPack(repository);
- pack.sendAdvertisedRefs(new RefAdvertiser.PacketLineOutRefAdvertiser(new PacketLineOut(output)));
- }
- }
+ public abstract void AdvertiseUploadPack(Stream output);
- public void AdvertiseReceivePack(Stream output)
- {
- using (var repository = GetRepository())
- {
- var pack = new ReceivePack(repository);
- pack.SendAdvertisedRefs(new RefAdvertiser.PacketLineOutRefAdvertiser(new PacketLineOut(output)));
- }
- }
+ public abstract void AdvertiseReceivePack(Stream output);
- public void Receive(Stream inputStream, Stream outputStream)
- {
- using (var repository = GetRepository())
- {
- var pack = new ReceivePack(repository);
- pack.setBiDirectionalPipe(false);
- pack.receive(inputStream, outputStream, outputStream);
- }
- }
+ public abstract void Receive(Stream inputStream, Stream outputStream);
- public void Upload(Stream inputStream, Stream outputStream)
- {
- using (var repository = GetRepository())
- {
- using (var pack = new UploadPack(repository))
- {
- pack.setBiDirectionalPipe(false);
- pack.Upload(inputStream, outputStream, outputStream);
- }
- }
- }
+ public abstract void Upload(Stream inputStream, Stream outputStream);
- public CommitInfo GetLatestCommit()
- {
- using (var repository = new GitSharp.Repository(PhysicalPathDotGit))
- {
- var commit = repository.Head.CurrentCommit;
-
- if (commit == null)
- {
- return null;
- }
-
- return new CommitInfo
- {
- Message = commit.Message,
- Date = commit.CommitDate.LocalDateTime
- };
- }
- }
-
- private GitSharp.Core.Repository GetRepository()
- {
- return GitSharp.Core.Repository.Open(directory);
- }
+ public abstract CommitInfo GetLatestCommit();
public string Name
{
@@ -151,55 +87,7 @@ public string GitDirectory()
return Path.Combine(PhysicalPathDotGit, ".git");
}
- public void UpdateServerInfo()
- {
- using (var rep = GetRepository())
- {
- if (rep.ObjectDatabase is ObjectDirectory)
- {
- RefWriter rw = new SimpleRefWriter(rep, rep.getAllRefs().Values);
- rw.writePackedRefs();
- rw.writeInfoRefs();
-
- var packs = GetPackRefs(rep);
- WriteInfoPacks(packs, rep);
- }
- }
- }
-
- private void WriteInfoPacks(IEnumerable<string> packs, GitSharp.Core.Repository repository)
- {
-
- var w = new StringBuilder();
-
- foreach (string pack in packs)
- {
- w.Append("P ");
- w.Append(pack);
- w.Append('\n');
- }
-
- var infoPacksPath = Path.Combine(repository.ObjectsDirectory.FullName, "info/packs");
- var encoded = Encoding.ASCII.GetBytes(w.ToString());
-
-
- using (Stream fs = File.Create(infoPacksPath))
- {
- fs.Write(encoded, 0, encoded.Length);
- }
- }
-
- private IEnumerable<string> GetPackRefs(GitSharp.Core.Repository repository)
- {
- var packDir = repository.ObjectsDirectory.GetDirectories().SingleOrDefault(x => x.Name == "pack");
-
- if (packDir == null)
- {
- return Enumerable.Empty<string>();
- }
-
- return packDir.GetFiles("*.pack").Select(x => x.Name).ToList();
- }
+ public abstract void UpdateServerInfo();
}
public class CommitInfo
@@ -0,0 +1,132 @@
+namespace GitAspx.Lib
+{
+ using System;
+ using System.Collections.Generic;
+ using System.IO;
+ using System.Linq;
+ using System.Text;
+ using GitSharp.Core;
+ using GitSharp.Core.Transport;
+
+ public class GitSharpCoreBasedRepository : GitRepository
+ {
+ public GitSharpCoreBasedRepository(DirectoryInfo directory, string rootDirectory)
+ : base(directory, rootDirectory)
+ {
+ }
+
+ public override void AdvertiseUploadPack(Stream output)
+ {
+ using (var repository = GetRepository())
+ {
+ var pack = new UploadPack(repository);
+ pack.sendAdvertisedRefs(new RefAdvertiser.PacketLineOutRefAdvertiser(new PacketLineOut(output)));
+ }
+ }
+
+ public override void AdvertiseReceivePack(Stream output)
+ {
+ using (var repository = GetRepository())
+ {
+ var pack = new ReceivePack(repository);
+ pack.SendAdvertisedRefs(new RefAdvertiser.PacketLineOutRefAdvertiser(new PacketLineOut(output)));
+ }
+ }
+
+ public override void Receive(Stream inputStream, Stream outputStream)
+ {
+ using (var repository = GetRepository())
+ {
+ var pack = new ReceivePack(repository);
+ pack.setBiDirectionalPipe(false);
+ pack.receive(inputStream, outputStream, outputStream);
+ }
+ }
+
+ public override void Upload(Stream inputStream, Stream outputStream)
+ {
+ using (var repository = GetRepository())
+ {
+ using (var pack = new UploadPack(repository))
+ {
+ pack.setBiDirectionalPipe(false);
+ pack.Upload(inputStream, outputStream, outputStream);
+ }
+ }
+ }
+
+ public override CommitInfo GetLatestCommit()
+ {
+ using (var repository = new GitSharp.Repository(PhysicalPathDotGit))
+ {
+ var commit = repository.Head.CurrentCommit;
+
+ if (commit == null)
+ {
+ return null;
+ }
+
+ return new CommitInfo
+ {
+ Message = commit.Message,
+ Date = commit.CommitDate.LocalDateTime
+ };
+ }
+ }
+
+ private GitSharp.Core.Repository GetRepository()
+ {
+ return GitSharp.Core.Repository.Open(directory);
+ }
+
+ public override void UpdateServerInfo()
+ {
+ using (var rep = GetRepository())
+ {
+ if (rep.ObjectDatabase is ObjectDirectory)
+ {
+ RefWriter rw = new GitSharpCoreBasedSimpleRefWriter(rep, rep.getAllRefs().Values);
+ rw.writePackedRefs();
+ rw.writeInfoRefs();
+
+ var packs = GetPackRefs(rep);
+ WriteInfoPacks(packs, rep);
+ }
+ }
+ }
+
+ private void WriteInfoPacks(IEnumerable<string> packs, GitSharp.Core.Repository repository)
+ {
+
+ var w = new StringBuilder();
+
+ foreach (string pack in packs)
+ {
+ w.Append("P ");
+ w.Append(pack);
+ w.Append('\n');
+ }
+
+ var infoPacksPath = Path.Combine(repository.ObjectsDirectory.FullName, "info/packs");
+ var encoded = Encoding.ASCII.GetBytes(w.ToString());
+
+
+ using (Stream fs = File.Create(infoPacksPath))
+ {
+ fs.Write(encoded, 0, encoded.Length);
+ }
+ }
+
+ private IEnumerable<string> GetPackRefs(GitSharp.Core.Repository repository)
+ {
+ var packDir = repository.ObjectsDirectory.GetDirectories().SingleOrDefault(x => x.Name == "pack");
+
+ if (packDir == null)
+ {
+ return Enumerable.Empty<string>();
+ }
+
+ return packDir.GetFiles("*.pack").Select(x => x.Name).ToList();
+ }
+ }
+}
Oops, something went wrong.

1 comment on commit 71412b0

Owner

linquize commented on 71412b0 Aug 5, 2012

Hardcode this for apache mono
NGit.Util.FS.DETECTED.SetUserHome(new Sharpen.FilePath("/var/www"));

Please sign in to comment.