Created
March 16, 2011 23:01
-
-
Save txdv/873510 to your computer and use it in GitHub Desktop.
manos memory leak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Manos; | |
using System.IO; | |
using LibGit2Sharp.Core; | |
namespace GitWiki | |
{ | |
public class GitWiki : ManosApp | |
{ | |
static string repodir = "testrepo.git/.git/"; | |
// repodir = "blet.git/.git/"; | |
Repository repo = new Repository(repodir); | |
RevisionWalker revwalker; | |
public GitWiki() | |
{ | |
revwalker = new RevisionWalker(repo); | |
Route("/Content/", new StaticContentModule()); | |
} | |
~GitWiki() | |
{ | |
repo.Dispose(); | |
} | |
public class HeaderInfo | |
{ | |
public string Tree { get; set; } | |
public string Parent { get; set; } | |
public string Author { get; set; } | |
public string Committer { get; set; } | |
public string Message { get; set; } | |
public static HeaderInfo Parse(Stream stream) | |
{ | |
StreamReader sr = new StreamReader(stream); | |
HeaderInfo info = new HeaderInfo(); | |
while (!sr.EndOfStream) { | |
string line = sr.ReadLine(); | |
if (line.StartsWith("tree ")) { | |
info.Tree = line.Substring(5); | |
} else if (line.StartsWith("parent ")) { | |
info.Parent = line.Substring(7); | |
} else if (line.StartsWith("author ")) { | |
info.Author = line.Substring(7); | |
} | |
} | |
return info; | |
} | |
} | |
[Route("/")] | |
public void Index(IManosContext ctx) | |
{ | |
var re = ctx.Response; | |
revwalker.Sorting(1); | |
revwalker.Push(repo.Head); | |
re.WriteLine("<pre>"); | |
var db = repo.Database; | |
var rawObj = db.Read(repo.HeadObjectId); | |
var info = HeaderInfo.Parse(rawObj.GetNativeMemoryStream()); | |
int i = 0; | |
while (info.Parent != null) { | |
i++; | |
if (i >= 20) | |
break; | |
rawObj = db.Read(new ObjectId(info.Parent)); | |
info = HeaderInfo.Parse(rawObj.GetNativeMemoryStream()); | |
re.WriteLine(string.Format("<a href=\"{0}/\">{1}</a>", info.Parent, info.Parent)); | |
} | |
re.WriteLine("</pre>"); | |
re.End(); | |
} | |
public void Serve(string file) | |
{ | |
var list = file.Split(new char[] { '/'}); | |
} | |
public GitObject Lookup(Tree tree, string[] path) | |
{ | |
if (path[path.Length - 1] == string.Empty) { | |
string[] newpath = new string[path.Length - 1]; | |
for (int i = 0; i < path.Length - 1; i++) { | |
newpath[i] = path[i]; | |
} | |
path = newpath; | |
} | |
for (int i = 0; i < path.Length; i++) { | |
foreach (var e in tree) { | |
if (e.Filename == path[i]) { | |
if (i + 1 == path.Length) { | |
return e.GetObject(); | |
} else { | |
tree = e.Get<Tree>(); | |
if (tree == null) | |
return null; | |
break; | |
} | |
} | |
} | |
} | |
return null; | |
} | |
public void List(IManosContext ctx, TextWriter o, Tree tree) | |
{ | |
var re = ctx.Response; | |
foreach (var file in tree) { | |
var treeEntry = file.GetObject(); | |
string filename = file.Filename; | |
string ret = null; | |
if (treeEntry is Tree) { | |
ret = string.Format("<a href=\"{0}/\">{0}/</a><br />", filename); | |
} else { | |
ret = string.Format("<a href=\"{0}\">{0}</a><br />", filename); | |
} | |
// using re instead of o will cause a big giant memory leak. | |
// It is called ~300 times | |
o.WriteLine(ret); | |
} | |
re.WriteLine("End"); | |
} | |
public void Lookup(IManosContext ctx, Commit commit, string path) | |
{ | |
var re = ctx.Response; | |
if (path == string.Empty) { | |
List(ctx, System.Console.Out, commit.Tree); | |
return; | |
} | |
var ret = Lookup(commit.Tree, path.Split(new char[] { '/' })); | |
if (ret == null) { | |
re.WriteLine("No such file"); | |
} | |
if (ret is Tree) { | |
List(ctx, System.Console.Out, ret as Tree); | |
} else if (ret is Blob) { | |
var ms = repo.Database.Read(ret.ObjectId).GetNativeMemoryStream(); | |
StreamReader sr = new StreamReader(ms); | |
re.End(sr.ReadToEnd()); | |
} | |
} | |
[Route("/{id}/{file}")] | |
public void Index(IManosContext ctx, string id, string file) | |
{ | |
var re = ctx.Response; | |
Commit comm = null; | |
try { | |
comm = repo.Lookup<Commit>(new ObjectId(id)); | |
} catch { } | |
if (comm == null) { | |
re.End("No such commit"); | |
return; | |
} | |
Lookup(ctx, comm, file); | |
re.End(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment