Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6563971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:54:35+00:00 2026-05-25T13:54:35+00:00

A while back I was looking for an embeddable distributed version control system in

  • 0

A while back I was looking for an embeddable distributed version control system in Java,
and I think I have found it in JGit, which is a pure Java implementation of git.
However, there is not much in the way of sample code or tutorials.

How can I use JGit to retrieve the HEAD version of a certain file (just like svn cat or hg cat whould do)?

I suppose this involves some rev-tree-walking and am looking for a code sample.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:54:36+00:00Added an answer on May 25, 2026 at 1:54 pm

    Unfortunately Thilo’s answer does not work with the latest JGit API. Here is the solution I found:

    File repoDir = new File("test-git");
    // open the repository
    Repository repository = new Repository(repoDir);
    // find the HEAD
    ObjectId lastCommitId = repository.resolve(Constants.HEAD);
    // now we have to get the commit
    RevWalk revWalk = new RevWalk(repository);
    RevCommit commit = revWalk.parseCommit(lastCommitId);
    // and using commit's tree find the path
    RevTree tree = commit.getTree();
    TreeWalk treeWalk = new TreeWalk(repository);
    treeWalk.addTree(tree);
    treeWalk.setRecursive(true);
    treeWalk.setFilter(PathFilter.create(path));
    if (!treeWalk.next()) {
      return null;
    }
    ObjectId objectId = treeWalk.getObjectId(0);
    ObjectLoader loader = repository.open(objectId);
    
    // and then one can use either
    InputStream in = loader.openStream()
    // or
    loader.copyTo(out)
    

    I wish it was simpler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A while back I was trying to bruteforce a remote control which sent a
A while back a found a great-looking framework that allowed .net developers to implement
I asked this question a while back but now I'm looking to implement an
A problem I ran into a while back I never found a good solution
I remember looking at a couple information visualizations tool a while back. One was
I'm new to iPhone development, I bought the certificate a while back and have
I am looking to alert the user if they hit the back button while
A while back I was reading the W3C article on ' Re-using Strings in
a while back I ran across a situation where we needed to display message-boxes
I noticed a good while back that Wikipedia links to a Javascript implementation of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.