Skip to content

Instantly share code, notes, and snippets.

@shonnly
Created September 27, 2016 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shonnly/1784e78bbfbcf44944970b7f47e17b89 to your computer and use it in GitHub Desktop.
Save shonnly/1784e78bbfbcf44944970b7f47e17b89 to your computer and use it in GitHub Desktop.
namespace StashFileFetcher.Controllers
{
using System.Diagnostics;
using System.IO;
using System.Text;
using LibGit2Sharp;
class RepoDigger : IDisposable
{
public static RepoDigger GetRepositoryInformationForPath(string path)
{
if (LibGit2Sharp.Repository.IsValid(path))
{
return new RepoDigger(path);
}
return null;
}
public string GetFileAtRevision(string filePath, string revision)
{
var commit = _repo.Lookup<Commit>(revision);
var treeEntry = commit[filePath];
Debug.Assert(treeEntry.TargetType == TreeEntryTargetType.Blob);
var blob = (Blob)treeEntry.Target;
var contentStream = blob.GetContentStream();
string content;
using (var tr = new StreamReader(contentStream, Encoding.UTF8))
{
content = tr.ReadToEnd();
}
return content;
}
public string CommitHash
{
get
{
return _repo.Head.Tip.Sha;
}
}
public bool HasUnpushedCommits
{
get
{
return _repo.Head.TrackingDetails.AheadBy > 0;
}
}
public bool HasUncommittedChanges
{
get
{
return _repo.RetrieveStatus().Any(s => s.State != FileStatus.Ignored);
}
}
public IEnumerable<Commit> Log
{
get
{
return _repo.Head.Commits;
}
}
public void Dispose()
{
if (!_disposed)
{
_disposed = true;
_repo.Dispose();
}
}
private RepoDigger(string path)
{
_repo = new Repository(path);
}
private bool _disposed;
private readonly Repository _repo;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment