Using the JGIT, how can I retrieve the line numbers of added/deleted lines

前端 未结 3 1213
梦谈多话
梦谈多话 2021-02-20 16:56

Assuming the following piece of code is committed to a Git repository:

int test(){
   int a = 3;
   int b = 4;
   int c = a + b;
   return c;
}

3条回答
  •  孤独总比滥情好
    2021-02-20 17:57

    I do it this way but I don't know if it is correct

    public void linesChangeInFile(Git git, List commits, String fileName, String pathRepository) {
            try {
                List commitsComparer = new ArrayList<>();
                List linesChange = new ArrayList<>();
    
                for (int i = 0; i < commits.size() - 1; i++) {
    
                    ObjectId commitIDOld = commits.get(i).getId();
    
                    if (Validador.isFileExistInCommit(commits.get(i), getRepository(), fileName)) {
    
                        if (i != commits.size() - 1 && !commitsComparer.contains(commits.get(i))) {
                            ObjectId commitIDNew = commits.get(i + 1);
                            commitsComparer.add(commits.get(i));
                            linesChange.add(diff(git, commitIDOld.getName(), commitIDNew.getName(), fileName));
                        }
    
                        try (final FileInputStream input = new FileInputStream(pathRepository + "\\" + fileName)) {
                            currentLines = IOUtils.readLines(input, "UTF-8").size();
                        }
                    }
    
                }
    
                Integer sumLinesAdd = 0;
                Integer sumLinesDel = 0;
                for (String lineChange : linesChange) {
                    String[] lChange = lineChange.split(";");
                    sumLinesAdd += Integer.parseInt(lChange[0]);
                    sumLinesDel += Integer.parseInt(lChange[1]);
                }
    
                System.out.println("Lines Add total:" + sumLinesAdd);
                System.out.println("Lines Del total:" + sumLinesDel);
                System.out.println("Total lines change:" + (sumLinesAdd + sumLinesDel));
    
            } catch (RevisionSyntaxException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    private String diff(Git git, String commitIDOld, String commitIDNew, String fileName) {
            int linesAdded = 0;
            int linesDeleted = 0;
            DiffFormatter df = null;
            try {
                AbstractTreeIterator oldTreeParser = prepareTreeParser(getRepository(), commitIDOld);
                AbstractTreeIterator newTreeParser = prepareTreeParser(getRepository(), commitIDNew);
    
                List diffs = git.diff().setOldTree(oldTreeParser).setNewTree(newTreeParser)
                        .setPathFilter(PathFilter.create(fileName)).call();
    
                df = new DiffFormatter(DisabledOutputStream.INSTANCE);
                df.setRepository(getRepository());
                df.setDiffComparator(RawTextComparator.DEFAULT);
                df.setDetectRenames(true);
    
                for (DiffEntry entry : diffs) {
                    // System.out.println("Entry: " + entry + ", from: " + entry.getOldId() + ", to:
                    // " + entry.getNewId());
                    // try (DiffFormatter formatter = new DiffFormatter(System.out)) {
                    // formatter.setContext(0);
                    // formatter.setRepository(repository);
                    // formatter.format(entry);
                    // }
                    for (Edit edit : df.toFileHeader(entry).toEditList()) {
                        linesDeleted += edit.getEndA() - edit.getBeginA();
                        linesAdded += edit.getEndB() - edit.getBeginB();
                    }
                }
            } catch (IOException | GitAPIException e) {
                System.err.println("Error:" + e.getMessage());
            }
    
            return linesAdded + ";" + linesDeleted;
    
        }
    

提交回复
热议问题