11503

Read the files at the spesific commit with libgit2sharp

Question:

There is a bare repository, I have a commit id, and want to read all the files at that commit without cloning.

This repository.Lookup<Tree>(repository.Commits.First().Tree.Sha) code give me only the files that are in the commit but I want also other files that exists at that level.

How to do that?

Answer1:

My understanding of your question is that you're willing to access the whole content of a commit, not only the first level of the commit. The code below will work against a bare (or a standard) repository and will allow one to recursively access and examine the content of a commit.

In order to make it easier for you to test drive it, it dumps information (git object meta data along with blob content) in the console output.

RecursivelyDumpTreeContent(repo, "", commit.Tree); [...] private void RecursivelyDumpTreeContent(IRepository repo, string prefix, Tree tree) { foreach (var treeEntry in tree) { var path = prefix + treeEntry.Name; var gitObject = treeEntry.Target; var meta = repo.ObjectDatabase.RetrieveObjectMetadata(gitObject.Id); Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", gitObject.Id, treeEntry.Mode, treeEntry.TargetType, meta.Size, path); if (treeEntry.TargetType == TreeEntryTargetType.Tree) { RecursivelyDumpTreeContent(repo, path + "/", (Tree)gitObject); } if (treeEntry.TargetType == TreeEntryTargetType.Blob) { Console.WriteLine((((Blob)gitObject).GetContentText())); } } }

Would you precisely know the path of a specific file you'd like to access, use the indexer exposed by the Commit type in order to directly access the GitObject you're after.

For instance:

var blob = commit["path/to/my/file.txt"].Target as Blob;

Recommend

  • Can an entity be unloaded from entity framework's ObjectStateManager?
  • jquery ajax bug
  • view SQLite Databases in android listview?
  • How do I see existing classes
  • R Tree With n Branches
  • Watching for file changes within a directory [duplicate]
  • Exception handling as per java coding standards
  • Whats the right place for testhelper-classes? (phpunit/best practise)
  • jinja2 template not found and internal server error
  • How secure are apple APNS push notifications?
  • SQL query to group by maximal sets of a column having inner consecutive distances below a threshold
  • Why does the font in these TD elements render at different sizes?
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • How to use carriage return with multiple line?
  • Jquery UI tool tip close icon
  • D3 nodes and links from JSON with nested arrays of children
  • Breeze - Deleted Items nav properties bug
  • Ajax Loaded meta Tags
  • Control modification in presentation layer
  • Xamarin Forms - UWP Fonts
  • javaw.exe and eclipse startup problems
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Adding custom controls to a full screen movie
  • Apache 2.4 - remove | delete | uninstall
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • R: gsub and capture
  • Confusion with PayPal's monthly billing cycle
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Arrays break string types in Julia
  • Comma separated Values
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Benchmarking RAM performance - UWP and C#
  • Load html files in TinyMce
  • Free memory of cv::Mat loaded using FileStorage API
  • need help with bizarre java.net.HttpURLConnection behavior
  • How to load view controller without button in storyboard?