0

Basically I need to get older version of a file in the repository without changing the current working file. I thought of different ways:

  • Temporary branch and do the thing
  • Copy the file, checkout the file do my thing and return it

But those ways are really slow and complex. Isn't there something simpler than that in ngit?

Ilian Iliev
  • 1,866
  • 1
  • 13
  • 13
  • Does it have to be ngit? `git show :path/to/file > file.old-version` (or I suppose maybe ngit has a git-show analog?) – Cascabel Feb 21 '12 at 17:50
  • [git-cat-file](http://schacon.github.com/git/git-cat-file.html) is also a choice. [git show answered here](http://stackoverflow.com/questions/610208/how-to-retrieve-a-single-file-from-specific-revision-in-git) fully – Lazy Badger Feb 21 '12 at 18:48
  • thanks guys, but our product is using already ngit and it will be quite an effort to change everything. Well, I can try and wrap the console for this sole reason, but I am still hoping for a ngit solution :) – Ilian Iliev Feb 21 '12 at 19:13

1 Answers1

0

Using NGit: for details of the method usage, please see jgit documentation http://jgit.info/javadoc/

ObjectId commitId = git.GetRepository().Resolve(ref);
RevWalk revWalk = new RevWalk(git.GetRepository());
RevTree revTree = loRevWalk.ParseTree(commitId);
TreeWalk treeWalk = new TreeWalk(git.GetRepository());
treeWalk.AddTree(revTree);

while (treeWalk.Next())
{
    //compare treeWalk.NameString yourself
    byte[] bytes = treeWalk.ObjectReader.Open(treeWalk.GetObjectId(0)).GetCachedBytes()
    ...
}
linquize
  • 19,828
  • 10
  • 59
  • 83