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: hillner.informatik.uni-leipzig.de

Hello,

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
branches.
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
Vector<Tupel<ObjectId,String>>();
private String currentBranch;


public GitFileHistory(File originalFile, Repository repo) throws
IOException {
this.repo = repo;
fileRelativePath =
FileUtility.getRelativePath(originalFile.getAbsoluteFile(),
this.repo.getWorkDir()).replace(File.separator, "/");
loadBranch("refs/heads/master");
for (String refName : getAllBranchNames()) {
if(refName.equals("HEAD"))
continue;
loadBranch(refName);
walk = buildWalk();
fileRevisionIds.addAll(buildRevisions());
}
loadBranch("refs/heads/master");
this.repo.close();
}

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)),
TreeFilter.ANY_DIFF));
return walk;
}

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

walk.markStart(walk.parseCommit(headID));

for (RevCommit revCommit : walk) {
TreeWalk fileWalker = TreeWalk.forPath(repo, fileRelativePath,
revCommit.asCommit(walk).getTreeId());
if(fileWalker != null) {
result.add(new Tupel<ObjectId,
String>(fileWalker.getObjectId(0),currentBranch));
}
}

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,
tree);
index.write();
repo.writeSymref(Constants.HEAD, refName);
currentBranch = refName;
}

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


Current Time: Thu Aug 22 18:51:54 GMT 2019

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

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

Back to the top