Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jgit-dev] question about commits/trees/parents

On 05/03/2012 02:00 PM, Markus Duft wrote:
> 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 ------

oh, and i forgot:

 new RevWalk(repo).parseCommit(commit.getParent(0).getId())

gives a "valid" looking result:

 commit 1b6c2d82292ddd3d55747146ebe5d82e47d3f3a4 1336044951 -----p

Regards,
Markus

> 
> 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
> _______________________________________________
> jgit-dev mailing list
> jgit-dev@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/jgit-dev


Back to the top