0

Using JGit, I've checked out a branch (branch1). After some time, I want to fetch() and see which files have been updated (in the origin) since my last fetch.

FetchResult fetchResult = m_git.fetch().setRefSpecs(new RefSpec("refs/heads/branch1")).call();

// For example:
fetchResult.getUpdatedFiles()

How do I do this?

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
paiego
  • 3,619
  • 34
  • 43

2 Answers2

1

The granularity level of the fetch API is commits, not files. Hence fetch itself won't tell you which files were updated.

The FetchResult however holds detailed information about the outcome of the fetch operation.

Iterate over FetchResult::getTrackingRefUpdates to see all refs that were updated (should be just one in your example). For each changed ref you are interested in, the TrackingRefUpdate describes the result of the fetch operation of the respective ref.

First look into the result property to see if the overall outcome is what you asked for. For regular ref update, use getOldObjectId and getNewObjectId to diff the old and new commit.

See here how to diff with JGit: How to show changes between commits with JGit

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • This doesn't work for me. After fetching from origin where there is indeed a change in one file (verified multiple times) fetchResult.getTrackingRefUpdates() still returns an empty collection. Immediately after I merge the fetch_head Ref though, I see 2 merged commits in the MergeResult. – paiego Mar 03 '19 at 19:50
0

I found something that worked for me.

Got most of this from a jgit cookbook: https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/ShowFileDiff.java#start-of-content

The DiffEntry collection below shows me the modified files. (fyi, this can be done before a merge)

fetch();

// todo: isn't there a critical section here? Only within the same repo maybe. We could prevent simultaneous use of the same racfid.
Ref fetchHead;
try {
    fetchHead = m_git.getRepository().findRef("FETCH_HEAD");

    //todo: show the diff bewteen fetch-head and the current head.
    // - get object id of fetch
    // - get oid of head

    ObjectId head = m_git.getRepository().resolve(Constants.HEAD);

    AbstractTreeIterator oldTreeParser = prepareTreeParser(m_git.getRepository(), fetchHead.getObjectId());
    AbstractTreeIterator newTreeParser = prepareTreeParser(m_git.getRepository(), head);

    List<DiffEntry> diff = m_git.diff().
        setOldTree(oldTreeParser).
        setNewTree(newTreeParser).
        // setPathFilter(PathFilter.create("README.md")).
        // to filter on Suffix use the following instead
        //setPathFilter(PathSuffixFilter.create(".java")).
        call();

    for (DiffEntry entry : diff) {
        System.out.println("Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId());
    }

}

private static AbstractTreeIterator prepareTreeParser(Repository repository, ObjectId objectId) throws IOException {
    // from the commit we can build the tree which allows us to construct the TreeParser
    //noinspection Duplicates
    try (RevWalk walk = new RevWalk(repository)) {
        RevCommit commit = walk.parseCommit(objectId);
        RevTree tree = walk.parseTree(commit.getTree().getId());
        CanonicalTreeParser treeParser = new CanonicalTreeParser();
        try (ObjectReader reader = repository.newObjectReader()) {
            treeParser.reset(reader, tree.getId());
        }
        walk.dispose();
        return treeParser;
    }
}
paiego
  • 3,619
  • 34
  • 43