Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jgit-dev] Getting the status of a single file or all files in one directory (non-recursive)

One option would be to use a IndexDiff with a PathFilter. That's quite fast if you filter on a single file. Of course you can also try to find on your own by setting up your own TreeWalk. But look at what IndexDiff returns as status and you see that, even for a single file, the status is quite complicated. Tracked, Added, Deleted, Conflicting, Changed, Modified (yes, both!)... Here is some example code:

import java.io.File;
import java.io.IOException;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.IndexDiff;
import org.eclipse.jgit.treewalk.FileTreeIterator;
import org.eclipse.jgit.treewalk.filter.PathFilterGroup;

public class CheckStatusOfSingleFile {
	/**
	 * @param args
	 *            args[0] is the path to working tree directory. args[1] is the
	 *            path (relative to root of repo) of the file to be checked
	 */
	public static void main(String args[]) throws IOException, GitAPIException {
		Git git = Git.open(new File(args[0]));
		IndexDiff diff = new IndexDiff(git.getRepository(), "HEAD",
				new FileTreeIterator(git.getRepository()));
		diff.setFilter(new PathFilterGroup().createFromStrings(args[1]));
		diff.diff();
		if (!diff.getAdded().isEmpty())
			System.out.println("file was added");
		if (!diff.getChanged().isEmpty())
			System.out.println("file was changed");
		// ...
	}
}

Back to the top