Skip to content

Commit

Permalink
add BlobContent class
Browse files Browse the repository at this point in the history
  • Loading branch information
yysun committed Jun 30, 2010
1 parent 1bc2729 commit 8903aab
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 13 deletions.
1 change: 1 addition & 0 deletions GitTools.WebApp/GitTools.WebApp.csproj
Expand Up @@ -154,6 +154,7 @@
<DependentUpon>Security.aspx</DependentUpon>
</Compile>
<Compile Include="Services\Blob.cs" />
<Compile Include="Services\BlobContent.cs" />
<Compile Include="Services\Branch.cs" />
<Compile Include="Services\Commit.cs" />
<Compile Include="Services\Tree.cs" />
Expand Down
12 changes: 1 addition & 11 deletions GitTools.WebApp/Services/Blob.cs
Expand Up @@ -12,16 +12,6 @@ public class Blob
{
public string Id { get; set; }
public string Name { get; set; }
public string RepoFolder { get; set; }

public string Content
{
get
{
var output = Git.Run("cat-file -p " + this.Id, this.RepoFolder);

return output;
}
}
public BlobContent Content { get; set; }
}
}
38 changes: 38 additions & 0 deletions GitTools.WebApp/Services/BlobContent.cs
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Services.Common;
using System.IO;
using System.Text;

namespace GitTools.WebApp.Services
{
[DataServiceKey("Id")]
public class BlobContent
{
public string Id { get; set; }
public string RepoFolder { get; set; }

private byte[] bytes;
public byte[] Bytes
{
get
{
if (bytes == null)
{
var fileName = Path.GetTempFileName();

Git.RunCmd("cat-file -p " + this.Id + " > " + fileName,
this.RepoFolder);

bytes = File.ReadAllBytes(fileName);

if (File.Exists(fileName)) File.Delete(fileName);
}
return bytes;
}
}

}
}
2 changes: 2 additions & 0 deletions GitTools.WebApp/Services/GitDataSource.cs
Expand Up @@ -32,6 +32,8 @@ where Repository.IsValid(dir.FullName)

public IQueryable<Blob> Blobs { get { return null; } }

public IQueryable<BlobContent> BlobContents { get { return null; } }

public IQueryable<Tag> Tags { get { return null; } }

}
Expand Down
8 changes: 6 additions & 2 deletions GitTools.WebApp/Services/Tree.cs
Expand Up @@ -34,13 +34,17 @@ public IEnumerable<Blob> Blobs
get
{
return from c in Git.Run("ls-tree " + this.Id, this.RepoFolder).Split('\n')
where !string.IsNullOrWhiteSpace(c) &&
where !string.IsNullOrWhiteSpace(c) &&
c.Substring(7, 4) == "blob"
select new Blob
{
Id = c.Substring(12, 40),
RepoFolder = this.RepoFolder,
Name = c.Substring(52),
Content = new BlobContent
{
Id = c.Substring(12, 40),
RepoFolder = this.RepoFolder,
}
};
}
}
Expand Down
25 changes: 25 additions & 0 deletions GitTools/Git.cs
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Diagnostics;
using System.Configuration;
using System.IO;

namespace GitTools
{
Expand Down Expand Up @@ -35,5 +36,29 @@ public static string Run(string args, string workingDirectory)
return output;
}
}


public static void RunCmd(string args, string workingDirectory)
{
var gitExePath = ConfigurationManager.AppSettings["GitExePath"];

var pinfo = new ProcessStartInfo("cmd.exe")
{
Arguments = "/C \"\"" + gitExePath + "\"\" " + args,
CreateNoWindow = true,
RedirectStandardError = true,
UseShellExecute = false,
WorkingDirectory = workingDirectory,
};

using (var process = Process.Start(pinfo))
{
string error = process.StandardError.ReadToEnd();
process.WaitForExit();

if (!string.IsNullOrEmpty(error))
throw new Exception(error);
}
}
}
}

0 comments on commit 8903aab

Please sign in to comment.