Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » Getting revisions for a specific folder/file
Getting revisions for a specific folder/file [message #636850] Wed, 03 November 2010 05:54 Go to next message
Adar is currently offline AdarFriend
Messages: 4
Registered: November 2010
Junior Member
I'm trying to get the revisions for a specific folder and for a specific folder, and for a specific folder (two separate use cases). For this purpose I tried to define a tree filter, for a folder named 'f1'. When I defined the filter with a full or relative path ("c:/temp/f1" or "/f1"), I got no revisions at all. When I only gave the folder name ("f1"), I got all the revisions, not just the ones belonging to the specific folder. Code attached below.

	private static void walkRevisions(final Repository repository){

		RevWalk rw = new RevWalk(repository);
		ObjectId objHead;
		try {
			objHead = repository.resolve("HEAD");
			Collection<String> strCol = new ArrayList<String>();
			rw.setTreeFilter(AndTreeFilter.create(PathFilterGroup.createFromStrings(strCol), TreeFilter.ALL));//, TreeFilter.ANY_DIFF));
			RevCommit prevC = null;
			for (RevCommit c : rw) {
				if (prevC == null){
					prevC = c;
				//RevTree rt = c.getTree();
				TreeWalk tw = 
					new TreeWalk(repository);
					System.out.println(c.getCommitTime() + " " + c.getFullMessage());
					FileDiff[] diffs = FileDiff.compute(tw, c, prevC, true);
					for (FileDiff fd: diffs){
						System.out.println(fd.getPath() + " " + fd.getChange() + fd.getModes());
					prevC = c;
		} catch (AmbiguousObjectException e2) {
		} catch (IOException e2) {
Re: Getting revisions for a specific folder/file [message #636977 is a reply to message #636850] Wed, 03 November 2010 15:58 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 269
Registered: July 2009
Senior Member

I have not tried your code, but there are some comments I can
give already now:

When you are searching through the history to find commits which touched a certain file you use TreeFilters. The method createFromStrings() which you use expect that your specify git pathes (pathes which are relative to the git repo working directory root). Specifying absolute file pathes like "c:/temp/.." will not work. Make sure you specify appropiate pathes.

After you found the commit you are inspecting the commit with a treewalk. But this treewalk you don't limit to the path which you are interested in. Your code may find only those commits which touch the folder, but for each of those commits you give out everything this commit touches. I would suggest you use also a filter for your treewalk.

Re: Getting revisions for a specific folder/file [message #637179 is a reply to message #636977] Thu, 04 November 2010 13:31 Go to previous messageGo to next message
Adar is currently offline AdarFriend
Messages: 4
Registered: November 2010
Junior Member
Thanks for the quick response.
Putting a filter on the treewalk inspecting the commit helped to get rid of files not in the path, but it did not solve the main problem. I'm still getting all the revisions in the repository, whether there were changes to the folder/contained files or not. Is a folder / file considered 'touched' by a commit to it's parent even if there were no changes?

Is there a better approach to obtaining this information?

Another question: given a specific revision, how do I perform a 'rollback' of a file / batch?


Re: Getting revisions for a specific folder/file [message #637406 is a reply to message #637179] Fri, 05 November 2010 12:13 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Adar wrote:
> Is there a better approach to obtaining this information?

I've been using code very similar to yours and I have the same problem:

String repoRelativePath = "bundles/org.eclipse.e4.deeplink";

final RevWalk walk = new RevWalk(repo);
// these 2 lines are copied from somewhere else, but didn't help
walk.sort(RevSort.TOPO, true);
walk.sort(RevSort.COMMIT_TIME_DESC, true);

final ObjectId start = repo.resolve(Constants.HEAD);
// I should only be able to see commits that contain files
// where project is a path prefix
final RevCommit commit =;

"commit" should be the last commit that touched a file in
repoRelativePath. Instead, I get the last commit to the repo and if I
keep walking, I get all commits.


Paul Webster .platform.doc.isv/guide/workbench.htm

Re: Getting revisions for a specific folder/file [message #655278 is a reply to message #637406] Fri, 18 February 2011 22:49 Go to previous message
Alex Redington is currently offline Alex RedingtonFriend
Messages: 1
Registered: February 2011
Junior Member
I was having the same problem of getting either nothing, or everything. In my explorations I found:

1) Using the TreeFilter.ANY_DIFF is important. Try changing this line:
rw.setTreeFilter(AndTreeFilter.create(PathFilterGroup.createFromStrings(strCol), TreeFilter.ALL));//, TreeFilter.ANY_DIFF));

to this:
rw.setTreeFilter(AndTreeFilter.create(PathFilterGroup.createFromStrings(strCol), TreeFilter.ANY_DIFF));

2) I had luck using a single file pathspec. (e.g. src/test/org/mongodb/misc/ instead of src/test/org/mongodb/misc)
3) Your pathspec cannot start with a root /. "/src/test/org/mongodb/misc/" matches nothing, "src/test/org/mongodb/misc/" worked for me.
4) I started off with a PathFilter, as opposed to PathFilterGroup and met with success. See if changing to PathFilter helps you!
Previous Topic:How can I get content of a file from git index?
Next Topic:Hooks and supported script languages?
Goto Forum:

Current Time: Sat Apr 04 19:37:01 GMT 2020

Powered by FUDForum. Page generated in 0.02123 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top