0

Background:

I want to retrieve a file from a specific revision in Git. Previously I used the git show command using the method described in this thread: How to retrieve a single file from specific revision in Git? meaning:

git show HEAD2:file.txt

However, based on the problem explained in my other post: Read both binary and text from process output I couldn't read binary data from git show's output. Following the suggestion of SO users, I moved to JGit as a native implementation of Java. I found this thread on SO which describes how to retireve a file from a specific revision using JGit API. How to "cat" a file in JGit?

Question:

Performance is very much crucial for my project. Thus, I'm looking for the best way to retrieve the file. The API method described above seems to navigate through tree and get the right revision. Considering I could use a single command in Git to retrieve the file, I'm not sure which one is faster.

  • Should I use the method described for JGit or should I go with the git show command?
  • Is there a better solution for JGit?
Community
  • 1
  • 1
Alireza Noori
  • 14,961
  • 30
  • 95
  • 179

2 Answers2

1

You can use

git checkout version -- path/to/file

further, if you want it to go somewhere else, you can

git --git-dir=.git --work-tree=somewhere/else checkout version -- path/to/file
Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
  • Thanks. Since my system is constantly running on multithread environment, I don't think checking out specific versions of files is preferable (which is the reason I used `git show` in the first place). So you think jGit is not good enough? – Alireza Noori Jan 18 '13 at 16:00
  • git is best used off the command line. It's what it was designed for and hence would always be my recommendation as the first choice. – Adam Dymitruk Jan 18 '13 at 23:28
1

Well, the only way to know which one is faster is to measure both in the situation in which they will be used. Anything else is speculation.

As to the work that is needed to get this information out of a Git repository in general, it's the same between git show and JGit. It goes like this:

  1. Parse the specified ref
  2. Retrieve the underlying commit object ID
  3. Parse it to get the tree object ID
  4. Retrieve the tree object and parse it (once for each directory level) to find the blob object ID
  5. Retrieve the blob object contents

See the chapter about Git Internals of the Pro Git book for more details.

robinst
  • 30,027
  • 10
  • 102
  • 108