0

I want to extract signature changes (method parameter changes to be exact) from commits to git repository by a java program. I have used the following code:

for (Ref branch : branches) {
            String branchName = branch.getName();
            for (RevCommit commit : commits) {
                boolean foundInThisBranch = false;

                RevCommit targetCommit = walk.parseCommit(repo.resolve(
                        commit.getName()));
                for (Map.Entry<String, Ref> e : repo.getAllRefs().entrySet()) {
                    if (e.getKey().startsWith(Constants.R_HEADS)) {
                        if (walk.isMergedInto(targetCommit, walk.parseCommit(
                                e.getValue().getObjectId()))) {
                            String foundInBranch = e.getValue().getName();
                            if (branchName.equals(foundInBranch)) {
                                foundInThisBranch = true;
                                break;
                            }
                        }
                    }
                } 

I can extract commit message, commit data and Author name from that, however, I am not able to extract parameter changes from them. I mean it is unable for me to identify parameter changes. I want to know if there is any way to recognize that. I mean it is impossible to recognize them from commit notes that are generated by programmers; I am looking for something like any specific annotation or something else. This is my code to extract differences:

CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
        oldTreeIter.reset(reader, oldId);
        CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
        newTreeIter.reset(reader, headId);
        List<DiffEntry> diffs= git.diff()
                .setNewTree(newTreeIter)
                .setOldTree(oldTreeIter)
                .call();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DiffFormatter df = new DiffFormatter(out);
        df.setRepository(git.getRepository());

The export is really huge and impossible to extract method changes.

AFF
  • 1,515
  • 4
  • 21
  • 35
  • To me it is not clear what you mean with "signature changes (method parameter changes to be exact)". But i don't know java. Perhaps you need to look into file diffs? Perhaps this helps https://stackoverflow.com/q/47307978/5784831? – Christoph Nov 18 '18 at 11:28
  • How do you access Git repository using Java? –  Nov 18 '18 at 11:42
  • You'll need to parse the file diffs. And it won't be easy- lines can be broken, changes could be on one line of a multi line signature, etc. good luck – Erwin Bolwidt Nov 18 '18 at 12:00
  • @ErwinBolwidt I have used the above code for extracting diffs, however, the export is really huge and impossible to extract method changes. Does anyone have any idea? – AFF Nov 18 '18 at 12:10

1 Answers1

1

You show a way you've found to examine the diffs, but say that the output is too large and you can't extract the method signature changes. If by that you mean that you're asking about specific git support for telling you that a method signature changes, then no - no such support exists. This is because git does not "know" anything about the languages you may or may not have used in the files under source control. Everything is just content that is, or is not, different from other content.

Since a method signature could be split across lines in any number of ways, it's not even guaranteed that just because a method's signature changed its name would appear anywhere in the diff. What you would really have to do is perform a sort of "structural diff". That is, you would have to

  • check out the "old" version, and pass it to a java parser
  • check out the "new" version, and pass it to a java parser
  • compare the resulting parse trees, looking for methods that belong to the same object, but have changed

Even that won't be terribly easy, because methods could be renamed, and because method overloading could make it unclear which signature change goes with which version of a method.

From there what you have is a non-trivial coding problem, which is beyond the scope of SO to answer. If you decide to tackle this problem and run into specific programming questions along the way, of course you could post those questions and perhaps someone will be able to help.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52