jgit如何跟踪项目所有版本中的所有文件

f87krz0w  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(255)

我需要跟踪项目所有版本的所有文件。
我的问题是,我甚至检测不到文件的重命名或副本。
在第一个版本之前,我还与各自的父级进行了提交的差异,我看到一些在第一个版本中不存在的文件发生了一些变化!
我正在使用jgit库,我所做的是:
我拿到了一个项目的所有版本

public ArrayList<GitRelease> getReleases() {

    ArrayList<GitRelease> releases = new ArrayList<GitRelease>();

    try {

        List<Ref> tagList = git.tagList().call();

        //ottiene un walk sul grafo dei commit
        RevWalk walk = new RevWalk(this.git.getRepository());

        for (Ref tag: tagList) {

            String tagName = tag.getName();
            String releaseName = tagName.substring("/ref/tags/".length());
            RevCommit commit = null;

            try {
                commit = walk.parseCommit(tag.getObjectId());
            } catch (MissingObjectException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IncorrectObjectTypeException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Date date = new Date(commit.getCommitTime() *1000L);

            GitRelease release = new GitRelease();
            release.setReleaseID(commit.getId());
            release.setName(releaseName);
            release.setDate(date);
            releases.add(release);
        }
        walk.close();

        return releases;

    } catch (GitAPIException e) {
        e.printStackTrace();
    }

    return releases;

}

对于每个版本,我都进行相对提交,从这个版本开始,我获取提交时存在的所有文件

public void setClassesRelease(GitRelease release){

    try {
        RevWalk revWalk = new RevWalk(this.git.getRepository()); 
        TreeWalk treeWalk = new TreeWalk(this.git.getRepository()); 

        ObjectId commitId = ObjectId.fromString(release.getReleaseID().getName());
        RevCommit commit = revWalk.parseCommit(commitId);
        ObjectId treeId = commit.getTree();

        treeWalk.reset(treeId);
        treeWalk.setRecursive(true);
        while (treeWalk.next()) {
            String path = treeWalk.getPathString();
            if (path.contains(".java")) {
                ClassProject classProject = new ClassProject(path, release.getName());

               release.addClassToClassList(classProject);
            }

        }
        revWalk.dispose();
    } catch (IOException e ) {
        e.printStackTrace();
    }

    Collections.sort(release.getClasses(), new Comparator<ClassProject>() {
        // @Override
        public int compare(ClassProject c1, ClassProject c2) {
            return c1.getName().compareTo(c2.getName());
        }
    });

}

对于每个版本,我都执行了将该版本与上一版本分开的提交

public void setRevisionsForRelease(ArrayList<GitRelease> releases) {

    for(int i = 0; i < releases.size(); i ++) {
        if (i==0) {
            releases.get(i).setRevisions(this.getRevisionsBetweenTwoRelease(null, releases.get(i).getReleaseID()));
        } else {
            releases.get(i).setRevisions(this.getRevisionsBetweenTwoRelease(releases.get(i-1).getReleaseID(), releases.get(i).getReleaseID()));
        }
    }

}

public ArrayList<GitCommit> getRevisionsBetweenTwoRelease(ObjectId startRelease, ObjectId endRelease) {

    LogCommand logCommand = this.git.log();

    Iterable<RevCommit> commits = null;
    try {
        if(startRelease == null)
            logCommand = logCommand.add(endRelease);
        else 
            logCommand = logCommand.addRange(startRelease, endRelease);
        commits = logCommand.call();

    } catch (NoHeadException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (GitAPIException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MissingObjectException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IncorrectObjectTypeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ArrayList<GitCommit> commitList = new ArrayList<GitCommit>();
    for (RevCommit commit : commits) {

        ObjectId commitID = commit.getId();
        Date date = new Date(commit.getCommitTime() *1000L);
        String message = commit.getFullMessage();
        ObjectId parentID;

        if (commit.getParentCount() == 0)
            parentID = null;
        else {
            parentID = commit.getParent(0).getId();
        }
        GitCommit gitCommit = new  GitCommit(commitID, date, parentID, message);
        commitList.add(0,gitCommit);
    }

    return commitList;

}

对于这些提交中的每一个,我在提交和父提交之间进行了区分,以便跟踪对文件所做的更改。此时,如果添加了一个文件,我会将其添加到正在考虑的发布文件列表中

public TreeSet<String> setClassPerReleaseMeasures(GitRelease release) {

    int copy = 0, modify= 0, rename = 0, delete = 0,add = 0;

    TreeSet<String> setNotFound = new TreeSet<String>();
    TreeSet<String> setFound = new TreeSet<String>();

    DiffCommand diffCommand = git.diff();
    ArrayList<GitCommit> revisions = release.getRevisions();

    for (GitCommit revision: revisions) {

        ObjectId revisionId = revision.getCommitID();
        ObjectId prevRevisionId = revision.getParentID();

        try {
            diffCommand = diffCommand.setNewTree(getCanonicalTreeParser(revisionId));

            if(prevRevisionId == null) {
                diffCommand = diffCommand;
            } else {
                diffCommand = diffCommand.setOldTree(getCanonicalTreeParser(prevRevisionId));
            }

            diffCommand = diffCommand.setShowNameAndStatusOnly(false);
            List<DiffEntry> diffEntries = diffCommand.call();

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            DiffFormatter formatter = new DiffFormatter(outputStream);
            formatter.setRepository(git.getRepository());

            for (DiffEntry diffEntry:diffEntries) {
                switch(diffEntry.getChangeType()) {
                case RENAME: 
                    rename = rename +1;
                    System.out.print("RENAME");
                    if(diffEntry.getNewPath().contains(".java")) {
                        System.out.println("RENAME: " + diffEntry.getOldPath() + "\n"+diffEntry.getNewPath()+"\nFineRENAME");
                        }
                    break;
                case COPY:
                    copy = copy +1;
                    System.out.print("COPY");
                    if(diffEntry.getNewPath().contains(".java")) {
                        System.out.println("COPY: " + diffEntry.getOldPath() + "\n"+diffEntry.getNewPath()+"\nFineCOPY");
                    }
                    break;
                case MODIFY:
                    modify = modify +1;
                    formatter.format(diffEntry);
                    String diffText = outputStream.toString();

                    if( diffEntry.getOldPath().contains(".java") && diffEntry.getNewPath().contains(".java")) {
                        if(!diffEntry.getOldPath().equalsIgnoreCase(diffEntry.getNewPath())) {
                            System.out.println("DIOMADONNA");
                        }

                        Diff diff = parseDiffEntry(diffEntry, diffText);

                        ClassProject classProject = release.getClassByName(diffEntry.getNewPath());
                        if(classProject == null) {
                            setNotFound.add(diffEntry.getNewPath());

                        }else {
                            setFound.add(diffEntry.getNewPath());
                            Measure classProjectMeasure = classProject.getMeasure();
                            classProjectMeasure.setMeasuresPerRelease(diff);
                        }
                    }
                    break;

                case ADD:
                    add = add +1;
                    if(diffEntry.getNewPath().contains(".java")) {
                        ClassProject classProject = release.getClassByName(diffEntry.getNewPath());
                        if(classProject == null) {
                            classProject = new ClassProject(diffEntry.getNewPath(), release.getName());
                            release.addClassToClassList(classProject);
                        //  setNotFound.add(diffEntry.getNewPath());
                        } else {
                            classProject.setDateCreation(revision.getDate());
                            setFound.add(diffEntry.getNewPath());
                        }
                    }
                    break;
                case DELETE:
                    delete = delete +1;
    //              System.out.print("DELETE");

                    if(diffEntry.getOldPath().contains(".java")) {
                        ClassProject classProject = release.getClassByName(diffEntry.getOldPath());
                        if(classProject == null) {
                            setNotFound.add(diffEntry.getOldPath());
                        } else {
                            setFound.add(diffEntry.getOldPath());
                        }
                    }
                    break;
                default:

                }
                outputStream.reset();
            }

            formatter.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (GitAPIException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    System.out.println("rename copy modify add delete = " + rename + " "+ copy + " "+ modify+ " "+ add+ " "+ delete);
    System.out.println("file  not found " + setNotFound.size()); 
    System.out.println("file  found " + setFound.size());
    return setNotFound;
}

有人能告诉我为什么我找不到一个重命名或复制的案例吗?我做错什么了?
有一件事发生了,但我不确定要保持跟踪是当整个文件夹被删除或移动。。。如果整个文件夹被移动,changetype是重命名还是删除并添加文件?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题