Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
manushakaru committed Nov 7, 2020
1 parent 48555ba commit b0fb01b
Show file tree
Hide file tree
Showing 14 changed files with 288 additions and 463 deletions.
9 changes: 0 additions & 9 deletions src/main/java/Analyse.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Computable;
import com.intellij.psi.PsiClass;
import com.intellij.psi.impl.search.MethodUsagesSearcher;
import org.jetbrains.annotations.NotNull;

public class Analyse extends com.intellij.openapi.actionSystem.AnAction {

AnalyseReport analyseReport;
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
/*analyseReport=new AnalyseReport(true);
String depth=Messages.showInputDialog("Insert Depth","Analyse",Messages.getInformationIcon());
analyseReport.showAndGet();*/
GitVcs.getAffectedFiles(e.getProject());

}
}
24 changes: 7 additions & 17 deletions src/main/java/AnalyseReport.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import org.jetbrains.annotations.Nullable;

import com.intellij.openapi.ui.DialogWrapper;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTextField;

import javax.swing.*;
import java.awt.*;

public class AnalyseReport extends DialogWrapper {

private JPanel mainPanel = new JPanel(new GridBagLayout());
//private JScrollPane ChangesScrollPane;
//private GridBag gridBag;
private JTextField textField=new JTextField();
private final JPanel mainPanel = new JPanel(new GridBagLayout());
private final JTextField textField = new JTextField();

protected AnalyseReport(boolean canBeParent) {
super(canBeParent);
Expand All @@ -22,16 +16,12 @@ protected AnalyseReport(boolean canBeParent) {
protected void init() {
super.init();
setTitle("Analyse Report");
setSize(600,400);
setSize(600, 400);
}

@Override
protected JComponent createCenterPanel() {
mainPanel.setPreferredSize(new Dimension(600,400));
/*gridBag = new GridBag().
setDefaultInsets(new Insets(0,0, AbstractLayout.DEFAULT_VGAP,AbstractLayout.DEFAULT_HGAP))
.setDefaultWeightX(1.0)
.setDefaultFill(GridBagConstraints.HORIZONTAL);*/
mainPanel.setPreferredSize(new Dimension(600, 400));
mainPanel.add(textField);
return mainPanel;
}
Expand Down
81 changes: 25 additions & 56 deletions src/main/java/GitVcs.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.AbstractVcs;
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.changes.ChangeListManager;
import com.intellij.openapi.vcs.changes.ContentRevision;
import com.intellij.openapi.vcs.changes.LocalChangeList;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.search.FilenameIndex;
import model.MethodEntity;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.intellij.psi.util.MethodSignatureUtil.areParametersErasureEqual;
import static com.intellij.psi.util.MethodSignatureUtil.areSignaturesEqual;

public class GitVcs {

public static List<MethodEntity> getAffectedFiles(Project project) {

List<MethodEntity> methodList = new ArrayList<>();
final ChangeListManager changeListManager = ChangeListManager.getInstance(project);
final List<VirtualFile> modifiedFiles = ChangeListManager.getInstance(project).getAffectedFiles(); // ??
//modifiedFiles.get(0);
//now it only gets first changeList--> change it to get all
final List<VirtualFile> modifiedFiles = ChangeListManager.getInstance(project).getAffectedFiles();
final LocalChangeList changeList = changeListManager.getChangeLists().get(0);

if (changeList != null) {

System.out.println("ChangeListSize : "+changeList.getChanges().size());
System.out.println("ChangeListSize : " + changeList.getChanges().size());
for (Change change : changeList.getChanges()) {
final ContentRevision afterRevision = change.getAfterRevision();
final ContentRevision beforeRevision = change.getBeforeRevision();
Expand All @@ -43,13 +35,12 @@ public static List<MethodEntity> getAffectedFiles(Project project) {
final PsiFileFactory factory = PsiFileFactory.getInstance(project);

PsiFile before = factory.createFileFromText(JavaLanguage.INSTANCE, beforeRevision.getContent());
//PsiFile after = factory.createFileFromText(JavaLanguage.INSTANCE, beforeRevision.getContent());
PsiFile after = PsiManager.getInstance(project).findFile(afterRevision.getFile().getVirtualFile());

if (before instanceof PsiJavaFile && after instanceof PsiJavaFile) {

PsiJavaFile jBefore=(PsiJavaFile) before;
PsiJavaFile jAfter=(PsiJavaFile) after;
PsiJavaFile jBefore = (PsiJavaFile) before;
PsiJavaFile jAfter = (PsiJavaFile) after;

PsiClass[] psiClassesBefore = jBefore.getClasses();
PsiClass[] psiClassesAfter = jAfter.getClasses();
Expand All @@ -59,92 +50,71 @@ public static List<MethodEntity> getAffectedFiles(Project project) {
afterMethods.add(method);
}
}
//System.out.println("Checking equal");
for (PsiMethod psiMethodAfter:afterMethods) {

for (PsiMethod psiMethodAfter : afterMethods) {

beforeMethods.clear();
for (PsiClass psiClass : psiClassesBefore) {
if(psiClass.getName().equals(psiMethodAfter.getContainingClass().getName())) {
if (psiClass.getName().equals(psiMethodAfter.getContainingClass().getName())) {
for (PsiMethod method : psiClass.getMethods()) {
if(psiMethodAfter.getName().equals(method.getName())) {
if (psiMethodAfter.getName().equals(method.getName())) {
beforeMethods.add(method);
}
}
}
}

if(beforeMethods.size()==0){
if (beforeMethods.size() == 0) {
//method has changed
methodList.add(new MethodEntity(psiMethodAfter));
continue;
}

boolean hasSimilar=false;
for (PsiMethod psiMethodBefore:beforeMethods) {
if(psiMethodAfter.getName().equals(psiMethodBefore.getName()) && areSignaturesEqual(psiMethodAfter,psiMethodBefore)){
//System.out.println(psiMethodAfter.getName()+" equal "+psiMethodBefore.getName());
// System.out.println(psiMethodAfter.getModifierList().getText()); --> Modifier list equality is not checking

boolean hasSimilar = false;
for (PsiMethod psiMethodBefore : beforeMethods) {
if (psiMethodAfter.getName().equals(psiMethodBefore.getName()) && areSignaturesEqual(psiMethodAfter, psiMethodBefore)) {
for (int i = 0; i < psiMethodAfter.getModifierList().getChildren().length; i++) {
System.out.println(psiMethodAfter.getName()+" "+psiMethodAfter.getModifierList().getChildren()[i]+" CHILD "+compare(psiMethodAfter.getModifierList().getChildren()[i],psiMethodBefore.getModifierList().getChildren()[i]));
System.out.println(psiMethodAfter.getName() + " " + psiMethodAfter.getModifierList().getChildren()[i] + " CHILD " + compare(psiMethodAfter.getModifierList().getChildren()[i], psiMethodBefore.getModifierList().getChildren()[i]));
}

String af=psiMethodAfter.getText().trim();
String bef=psiMethodBefore.getText().trim();
if(compare(psiMethodBefore.getBody(),psiMethodAfter.getBody())==1) {
hasSimilar=true;
String af = psiMethodAfter.getText().trim();
String bef = psiMethodBefore.getText().trim();
if (compare(psiMethodBefore.getBody(), psiMethodAfter.getBody()) == 1) {
hasSimilar = true;
break;
}
}else{
//System.out.println(psiMethodBefore.getName()+" & "+psiMethodAfter.getName()+" "+0);
}
}

if(!hasSimilar){
if (!hasSimilar) {
methodList.add(new MethodEntity(psiMethodAfter));
}
}
}
//-->Important-->PsiFile psiFile = PsiManager.getInstance(project).findFile(change.getVirtualFile());
}catch (Exception e){

} catch (Exception e) {
System.out.println(e.getMessage());
}

//PsiFile[] psiFiles= FilenameIndex.getFilesByName(project,afterRevision.getFile().getName(), GlobalSearchScope.projectScope(project));

/*for (PsiFile psiFile:psiFiles) {
System.out.println("Path-"+psiFile.getVirtualFile().getPath());
if (psiFile instanceof PsiJavaFile) {
PsiClass[] psiClasses = ((PsiJavaFile) psiFile).getClasses();
for (PsiClass psiClass : psiClasses) {
for (PsiMethod method : psiClass.getMethods()) {
//System.out.println(method.getName());
System.out.println(method);
}
}
}
}*/
}
}
return methodList;
}

//return 0 if not equal
public static int compare(PsiElement before, PsiElement after) {
if (!before.toString().trim().equals(after.toString().trim())) {
System.out.println(before+"="+after);
System.out.println(before + "=" + after);
return 0;
} else {
if (before.getChildren().length == 0 || after.getChildren().length == 0) {
return 1;
} else if (after.getChildren().length != before.getChildren().length) {
//System.out.println(before.getChildren().length+"="+after.getChildren().length);
return 0;
} else {
int comp = 1;
for (int i = 0; i < before.getChildren().length; i++) {
if(compare(before.getChildren()[i],after.getChildren()[i])==0){
comp=0;
if (compare(before.getChildren()[i], after.getChildren()[i]) == 0) {
comp = 0;
break;
}
}
Expand All @@ -153,4 +123,3 @@ public static int compare(PsiElement before, PsiElement after) {
}
}
}
//areSignaturesEqual(psiMethodAfter,psiMethodBefore)
71 changes: 33 additions & 38 deletions src/main/java/HighlightAction.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import model.ClassEntity;
import model.MethodEntity;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
Expand All @@ -19,44 +16,57 @@
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
import org.eclipse.jgit.treewalk.FileTreeIterator;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class HighlightAction extends AnAction {

private static AbstractTreeIterator prepareTreeParser(Repository repository, RevCommit commit) throws Exception {

RevWalk walk = new RevWalk(repository);
RevTree tree = walk.parseTree(commit.getTree().getId());
CanonicalTreeParser oldTreeParser = new CanonicalTreeParser();
ObjectReader oldReader = repository.newObjectReader();

try {
oldTreeParser.reset(oldReader, tree.getId());
} finally {

}
return oldTreeParser;
}

@Override
public void actionPerformed(AnActionEvent anActionEvent) {
System.out.println(anActionEvent.getProject().getBasePath()+"/.git");
System.out.println(anActionEvent.getProject().getBasePath() + "/.git");
try {
Repository existingRepo = new FileRepositoryBuilder()
.setGitDir(new File(anActionEvent.getProject().getBasePath()+"/.git"))
.setGitDir(new File(anActionEvent.getProject().getBasePath() + "/.git"))
.build();

Git git = new Git(existingRepo);

List<Ref> branches = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call();
RevCommit youngestCommit=null;
RevWalk walk = new RevWalk(git.getRepository());
for(Ref branch : branches) {
RevCommit commit = walk.parseCommit(branch.getObjectId());
if (youngestCommit == null || commit.getAuthorIdent().getWhen().compareTo(
youngestCommit.getAuthorIdent().getWhen()) > 0)
youngestCommit = commit;
}
RevCommit youngestCommit = null;
RevWalk walk = new RevWalk(git.getRepository());
for (Ref branch : branches) {
RevCommit commit = walk.parseCommit(branch.getObjectId());
if (youngestCommit == null || commit.getAuthorIdent().getWhen().compareTo(
youngestCommit.getAuthorIdent().getWhen()) > 0)
youngestCommit = commit;
}

DiffFormatter formatter = new DiffFormatter( System.out );
formatter.setRepository( git.getRepository() );
AbstractTreeIterator commitTreeIterator = prepareTreeParser( git.getRepository(), youngestCommit );
FileTreeIterator workTreeIterator = new FileTreeIterator( git.getRepository() );
List<DiffEntry> diffEntries = formatter.scan( commitTreeIterator, workTreeIterator );
DiffFormatter formatter = new DiffFormatter(System.out);
formatter.setRepository(git.getRepository());
AbstractTreeIterator commitTreeIterator = prepareTreeParser(git.getRepository(), youngestCommit);
FileTreeIterator workTreeIterator = new FileTreeIterator(git.getRepository());
List<DiffEntry> diffEntries = formatter.scan(commitTreeIterator, workTreeIterator);

for( DiffEntry entry : diffEntries ) {
System.out.println( "Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId() );
formatter.format( entry );
for (DiffEntry entry : diffEntries) {
System.out.println("Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId());
formatter.format(entry);
}
} catch (IOException | GitAPIException e) {
e.printStackTrace();
Expand All @@ -65,19 +75,4 @@ public void actionPerformed(AnActionEvent anActionEvent) {
}
}

private static AbstractTreeIterator prepareTreeParser(Repository repository, RevCommit commit) throws Exception {
RevWalk walk = new RevWalk(repository);
RevTree tree = walk.parseTree(commit.getTree().getId());

CanonicalTreeParser oldTreeParser = new CanonicalTreeParser();
ObjectReader oldReader = repository.newObjectReader();
try {
oldTreeParser.reset(oldReader, tree.getId());
} finally {

}
return oldTreeParser;
}


}
Loading

0 comments on commit b0fb01b

Please sign in to comment.