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 8241859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:58:18+00:00 2026-06-07T20:58:18+00:00

How do I have to extend the following logCommand, to get the –follow option

  • 0

How do I have to extend the following logCommand, to get the --follow option of the git log command working?

Git git = new Git(myRepository);
Iterable<RevCommit> log = git.log().addPath("com/mycompany/myclass.java").call();

This option is implemented in jGit, but I don’t know how to use it. The logCommand’s methods don’t appear to be useful. Thank you!

  • 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-06-07T20:58:20+00:00Added an answer on June 7, 2026 at 8:58 pm

    During some midnight work I got the following:

    The last commit of a LogCommand will get checked for renames against all older commits until a rename operation is found. This cycle will continue until no rename was found.

    However, that search can take some time, especially if it iterates over all commits until the end and doesn’t find any rename operation anymore. So, I am open for any improvement. I guess git normally uses indexes to perform the follow option in shorter time.

    import org.eclipse.jgit.api.Git;
    import org.eclipse.jgit.api.errors.GitAPIException;
    import org.eclipse.jgit.diff.DiffEntry;
    import org.eclipse.jgit.diff.RenameDetector;
    import org.eclipse.jgit.errors.MissingObjectException;
    import org.eclipse.jgit.lib.Repository;
    import org.eclipse.jgit.revwalk.RevCommit;
    import org.eclipse.jgit.treewalk.TreeWalk;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Create a Log command that enables the follow option: git log --follow -- < path >
     * User: OneWorld
     * Example for usage: ArrayList<RevCommit> commits =  new  LogFollowCommand(repo,"src/com/mycompany/myfile.java").call();
     */
    public class LogFollowCommand {
    
        private final Repository repository;
        private String path;
        private Git git;
    
        /**
         * Create a Log command that enables the follow option: git log --follow -- < path >
         * @param repository
         * @param path
         */
        public LogFollowCommand(Repository repository, String path){
            this.repository = repository;
            this.path = path;
        }
    
        /**
         * Returns the result of a git log --follow -- < path >
         * @return
         * @throws IOException
         * @throws MissingObjectException
         * @throws GitAPIException
         */
        public ArrayList<RevCommit> call() throws IOException, MissingObjectException, GitAPIException {
            ArrayList<RevCommit> commits = new ArrayList<RevCommit>();
            git = new Git(repository);
            RevCommit start = null;
            do {
                Iterable<RevCommit> log = git.log().addPath(path).call();
                for (RevCommit commit : log) {
                    if (commits.contains(commit)) {
                        start = null;
                    } else {
                        start = commit;
                        commits.add(commit);
                    }
                }
                if (start == null) return commits;
            }
            while ((path = getRenamedPath( start)) != null);
    
            return commits;
        }
    
        /**
         * Checks for renames in history of a certain file. Returns null, if no rename was found.
         * Can take some seconds, especially if nothing is found... Here might be some tweaking necessary or the LogFollowCommand must be run in a thread.
         * @param start
         * @return String or null
         * @throws IOException
         * @throws MissingObjectException
         * @throws GitAPIException
         */
        private String getRenamedPath( RevCommit start) throws IOException, MissingObjectException, GitAPIException {
            Iterable<RevCommit> allCommitsLater = git.log().add(start).call();
            for (RevCommit commit : allCommitsLater) {
    
                TreeWalk tw = new TreeWalk(repository);
                tw.addTree(commit.getTree());
                tw.addTree(start.getTree());
                tw.setRecursive(true);
                RenameDetector rd = new RenameDetector(repository);
                rd.addAll(DiffEntry.scan(tw));
                List<DiffEntry> files = rd.compute();
                for (DiffEntry diffEntry : files) {
                    if ((diffEntry.getChangeType() == DiffEntry.ChangeType.RENAME || diffEntry.getChangeType() == DiffEntry.ChangeType.COPY) && diffEntry.getNewPath().contains(path)) {
                        System.out.println("Found: " + diffEntry.toString() + " return " + diffEntry.getOldPath());
                        return diffEntry.getOldPath();
                    }
                }
            }
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: var GoalPanelView = Backbone.View.extend({ // Bind to the goal
I have the following StateManager: Lead.StateManager = Ember.StateManager.extend initialState: 'notParsing' notParsing: Ember.State.create startParsing: (manager,
I have the following situation: var Task = Backbone.Model.extend({ initialize: function() { }, save:
Say I have the following Backbone Router: class App.Routers.ThingsRouter extends Backbone.Router initialize: -> new
Lets have the following code : ( function($) { TeacherModel = Backbone.Model.extend({ defaults :
I have the following program. module C def self.included(base) base.extend(ClassMethods) end module ClassMethods def
I have the following views: window.DmnView = Backbone.View.extend({ template: _.template($(#tmpl_dmnListItem).html()), events: { click .getWhois:
I have the following Backbone.js model and collection: // Model lr.Event = Backbone.Model.extend({}); //
I have the following code to extend my $_SESSION[] variables. They expire after around
I have the following Parent Object: Context = { ContextModel: Backbone.Model.extend({ //model Code }),

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.