Created
September 27, 2016 08:10
-
-
Save shonnly/1784e78bbfbcf44944970b7f47e17b89 to your computer and use it in GitHub Desktop.
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
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