Looping over commits for a file with jGit

前端 未结 4 1481
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-28 10:12

I\'ve managed to get to grips with the basics of jGit file in terms of connecting to a repos and adding, commiting, and even looping of the commit messages for the files.

4条回答
  •  梦谈多话
    2020-12-28 10:52

    araqnid is right, this is how I got a list of Dates, each date relating to commit that included the file in question...

    Then you can retrieve the file from a specific commit as you have the name of the file and the date of the commit, see the two methods below....

    note: this code is in a .groovy class, so you will no doubt have to amend a little for java.

    byte[] getAnyPreviousVersionFileBytes(String relativeFilePath, Date date) {
    
        byte[] bytes = null
        try {
    
            RevWalk revWalk = new RevWalk(repository)
            ObjectId headId = repository.resolve(Constants.HEAD);
            RevCommit root = revWalk.parseCommit(headId);
    
            revWalk.sort(RevSort.COMMIT_TIME_DESC);
            revWalk.markStart(root);
    
            for (RevCommit revCommit: revWalk) {
    
                // if date matches then walk the tree in this commit
                if (new Date(revCommit.commitTime * 1000L) == date) {
    
                    TreeWalk treeWalk = TreeWalk.forPath(repository, relativeFilePath, revCommit.getTree())
    
                    if (treeWalk != null) {
                        treeWalk.setRecursive(true)
                        CanonicalTreeParser canonicalTreeParser = treeWalk.getTree(0, CanonicalTreeParser)
    
                        while (!canonicalTreeParser.eof()) {
    
                            // if the filename matches, we have a match, so set teh byte array to return
                            if (canonicalTreeParser.getEntryPathString() == relativeFilePath) {
                                ObjectLoader objectLoader = repository.open(canonicalTreeParser.getEntryObjectId())
                                bytes = objectLoader.bytes
                            }
                            canonicalTreeParser.next(1)
                        }
                    }
                }
    
            }
    
        }
        catch (Exception e) {
            throw new JgitException(e)
        }
        return bytes
    }
    
    List getFileVersionDateList(String relativeFilePath) {
    
        List versions = new LinkedList()
        try {
    
            RevWalk revWalk = new RevWalk(repository)
            ObjectId headId = repository.resolve(Constants.HEAD);
            RevCommit root = revWalk.parseCommit(headId);
    
            revWalk.sort(RevSort.COMMIT_TIME_DESC);
            revWalk.markStart(root);
    
            for (RevCommit revCommit: revWalk) {
    
                TreeWalk treeWalk = TreeWalk.forPath(repository, relativeFilePath, revCommit.getTree())
    
                if (treeWalk != null) {
                    treeWalk.setRecursive(true)
                    CanonicalTreeParser canonicalTreeParser = treeWalk.getTree(0, CanonicalTreeParser)
    
                    while (!canonicalTreeParser.eof()) {
                        // if the filename matches, we have a match, so add the date of this commit to the list
                        if (canonicalTreeParser.getEntryPathString() == relativeFilePath) {
                            versions.add(new Date(revCommit.commitTime * 1000L))
                        }
                        canonicalTreeParser.next(1)
                    }
                }
            }
        }
        catch (Exception e) {
            throw new JgitException(e)
        }
    
        return versions
    }
    

提交回复
热议问题