[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[jgit-dev] question about commits/trees/parents
|
Hi!
I'm writing a short peace of code, that should give me the size of all files touched in the last commit. for this reason i'm using a treewalk with an ANY_DIFF filter (is this the right approach?) and intend to pass the trees of the current and the parent RevCommit.
walk.setFilter(TreeFilter.ANY_DIFF);
walk.reset(commit.getParent(0).getTree(), commit.getTree());
now, in this case i have these evaluations when hitting the reset() line:
commit = commit 4e408bb4f8e84240c19ddfe4ea2d97fdc6996856 1336045390 -----p
commit.getParent(0) = commit 6c88f48e66c953e0c50167378377e8e3273d2ffe 0 ------
calling any getter on the parent (like getTree()) yields null or NPE... what am i doing wrong? i'm using plain jgit as library in a plain java application. the whole code looks like this:
/**
* Investigates files changed by the commit referenced by the patchset of the given event, and
* returns a map of paths to file sizes for files that are too large.
*/
public Map<String, Long> getInvalidSizedFiles(GerritBaseChangeEvent e) {
try {
RevCommit commit = getCommit(e);
if (commit == null) {
throw new IllegalStateException("cannot find commit for event " + e);
}
if (commit.getParentCount() != 1) {
throw new IllegalStateException("commit " + commit + " has not exactly one parent!");
}
// since the actual commit is added as second tree, from now on, index '1' refers to the
// actual commit.
TreeWalk walk = new TreeWalk(repo);
walk.setFilter(TreeFilter.ANY_DIFF);
walk.reset(commit.getParent(0).getTree(), commit.getTree());
walk.setRecursive(true);
ObjectReader reader = walk.getObjectReader();
Map<String, Long> result = new TreeMap<String, Long>();
try {
while (walk.next()) {
String path = walk.getPathString();
FileMode newMode = walk.getFileMode(1);
if (newMode.equals(FileMode.TYPE_FILE)) {
long size = reader.getObjectSize(walk.getObjectId(1), Constants.OBJ_BLOB);
if (size > config.getValidationFszWarn()
|| size > config.getValidationFszError()) {
result.put(path, size);
}
}
}
} finally {
reader.release();
}
return result;
} catch (Exception ex) {
log.error("failed to get file sizes for " + e, ex);
return Collections.emptyMap();
}
}
/**
* Searches for the commit object associated with the given Gerrit event. If no commit can be
* found, <code>null</code> is returned.
*/
public RevCommit getCommit(GerritBaseChangeEvent e) {
GerritPatchset p = e.getPatchset();
if (p == null) {
return null;
}
String rev = p.getRevision();
try {
RevWalk walk = new RevWalk(repo);
return walk.parseCommit(getOidForRev(rev));
} catch (Exception ex) {
log.error("cannot find commit with id " + rev, ex);
return null;
}
}
/**
* Returns the {@link ObjectId} for the given RevString, which may be any valid GIT revision ID.
* If the given revision is not found in the repository, <code>null</code> is returned.
*
* @param rev
* the revision to resolve
* @return the {@link ObjectId} or <code>null</code> if not found.
*/
public ObjectId getOidForRev(String rev) {
try {
return repo.resolve(rev);
} catch (Exception e) {
log.error("cannot resolve rev " + rev, e);
}
return null;
}
Thanks for helping in advance :)
Regards,
Markus