Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » redundand file revisions when iterating over branches
redundand file revisions when iterating over branches [message #575237] Tue, 20 October 2009 14:47
Eclipse UserFriend
Originally posted by:


while iterating over all branches of a repository and extracting all the
revisions of one file I get redundand versions of the file in different
These branches may have cloned the file and updated it from the master
branch or another but I need each version of a file only once (in the branch
where the file was created/modified).
See my code below.

(I'm checking out each branch and loop over all RevCommits of the RevWalk.
From the RevCommit I get the ObjectId of the file revision and then I just
get the string out of the blob.)
How can I filter the revisions for a result like that which you get using
the command "gitk --all filepath"?
There each file revision is displayed only once at the graph.

public class GitFileHistory {
private Repository repo;
private RevWalk walk;
private String fileRelativePath;
private Vector<Tupel<ObjectId,String>> fileRevisionIds = new
private String currentBranch;

public GitFileHistory(File originalFile, Repository repo) throws
IOException {
this.repo = repo;
fileRelativePath =
this.repo.getWorkDir()).replace(File.separator, "/");
for (String refName : getAllBranchNames()) {
walk = buildWalk();

private RevWalk buildWalk() {
RevWalk walk = new RevWalk(repo);
walk.sort(RevSort.COMMIT_TIME_DESC, true);
walk.sort(RevSort.BOUNDARY, true);
walk.setTreeFilter(AndTreeFilter.create(PathFilterGroup.crea teFromStrings(Collections.singleton(fileRelativePath)),
return walk;

private Vector<Tupel<ObjectId,String>> buildRevisions() throws IOException
Vector<Tupel<ObjectId,String>> result = new
AnyObjectId headID = repo.resolve(Constants.HEAD);
if(headID == null) {
System.err.println("NULL - headID");
return null;


for (RevCommit revCommit : walk) {
TreeWalk fileWalker = TreeWalk.forPath(repo, fileRelativePath,
if(fileWalker != null) {
result.add(new Tupel<ObjectId,

return result;

public Vector<Tupel<String,String>> getFileRevisions() throws IOException {
Vector<Tupel<String,String>> result = new Vector<Tupel<String,String>>();
ObjectDatabase db = repo.getObjectDatabase();
for (Tupel<ObjectId,String> t : fileRevisionIds) {
byte[] buffer = db.openObject(new WindowCursor(), t.getO1()).getBytes();
RawCharSequence seq = new RawCharSequence(buffer, 0, buffer.length);
result.add(new Tupel<String, String>(seq.toString(), t.getO2()));
return result;

private List<String> getAllBranchNames() {
return new ArrayList<String>(repo.getAllRefs().keySet());

private void loadBranch(String refName) throws IOException {
Commit commit = repo.mapCommit(refName);
Tree tree = commit.getTree();
GitIndex index = repo.getIndex();
WorkDirCheckout co = new WorkDirCheckout(repo, repo.getWorkDir(), index,
repo.writeSymref(Constants.HEAD, refName);
currentBranch = refName;

public int countBranches() {
HashSet<String> branches = new HashSet<String>();
for (Tupel<ObjectId,String> t : fileRevisionIds) {
return branches.size();
Previous Topic:Operation not supported - Internal Error
Next Topic:Cloning Public Repository in Github
Goto Forum:

Current Time: Thu Aug 18 18:02:25 GMT 2022

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

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

Back to the top