Diff of two git files

I need to write a plugin which can show the diff of a branch and master given the repository link in diff window. How shall i proceed?

0
18 comments

Hi sreehari,

It seems that this functionality is already available in IntelliJ IDEA: select a file and invoke Git | Compare with Branch. Doesn't it suit well for you?

0

Is it possible to bind exactly this functionality to a self-written action? "Compare with branch" is a nice feature, but in my case i know the branch in every case and i dont want to select them first. i tried macros for example, but Macros arent suitable thatfore... macros are the badest feature in intellij by the way, you can not really do much with it.... however... is there a way to do this programatically?

0

It is possible to write a simple plugin which would provide your custom action with the specific logic you need. If you want to try, I can give you some directions on where to start from.

0

Yes, please!

I`m writing currently a plugin to do code reviews within phpstorm based on bitbucket pullrequests. Some code snippets could help :-)

0

Here are some useful links (in case you didn't see them yet):

* Root SDK documentation page: http://www.jetbrains.org/intellij/sdk/docs/

* How to create AnAction: http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/creating_an_action.html

 

Once you've got your AnAction, take a look at `GitCompareWithBranchAction` and its parent `DvcsCompareWithBranchAction`. Since you know the branch you want to compare, I guess, you don't need the popup there, so you can take the code from `showDiffWithBranchUnderModalProgress` directly (btw, the name method is incorrect: it is not modal anymore, I'll rename it).

0

Do i need a Action explicit? I Have a Toolwindow. Sorry I`m Java beginner!It looks like the following is what i need:

 

VcsDiffUtil.showDiffFor(project, changes, VcsDiffUtil.getRevisionTitle(compare, false), VcsDiffUtil.getRevisionTitle(head, true),
VcsUtil.getFilePath(file));

but how to get cahnges and all the other ones...

Im also a little bit confused about "file" in this call...
At the end i want to comapre the current branch with another with ALL FILEs and not just a single file
0

If you need to compare whole branches, not just the given file, the situation is even simpler: just use GitBrancher.compare(). It will compare branches in the background and show the dialog once ready.

GitBrancher is a Service, so you can call ServiceManager.getService(myProject, GitBrancher.class) to the the instance of it.

0

That sounds simple. But i dont have GitBrancher. Its part of git4idea, right? And thatfor i need to include as described by vikash kumar here https://intellij-support.jetbrains.com/hc/en-us/community/posts/207100489-How-to-get-current-git-branch-inside-StartActivity ... sorry again, its maybe too late :-)

0

Yes, vikash kumar has described everything correctly.

0

Hmmm i dont get it :-(

  • Add as dependency

...but GitBrancher and GitCompareWithBranchAction is not available (https://github.com/JetBrains/intellij-community/blob/master/plugins/git4idea/src/git4idea/branch/GitBrancher.java)

 

 

So, where is my mistake :-/ ? Thank you

0

Your mistake is that you use some 3-party git4idea library which has nothing to do with the official git4idea plugin by JetBrains which is included in every IDEA installation (<IDEA install dir>/plugins/)

0

Aah i can take it directly from the sources of Intellij... i have assumed if i include the SDK sources that i include all in once. But there are plugin-sources which i can include seperatly.. OK...

 Now it works basically. Thanks

0

Ah youre the author of that stuff :-) nice that you have time to help me :-)

OK.. back to the topic. I do not quite understand the call .compare() .

Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
Document document = editor.getDocument();
VirtualFile file = FileDocumentManager.getInstance().getFile(document);
List<GitRepository> repos = GitUtil.getRepositoryManager(project).getRepositories();
GitRepository gitRepository = GitUtil.getRepositoryManager(project).getRepositoryForFile(file);

GitBrancher gitBrancher = ServiceManager.getService(project, GitBrancher.class);
gitBrancher.compare("master", repos, gitRepository);

With this code i get the following result:

but i want the more simple version of it (the same as "compare with branch"):

...thanks for help again :-)

0

Then you can either look inside the implementation of "compare" and take the stuff you need, either look back at the GitCompareWithBranchAction and use the Git repository root folder as the FilePath.

0

sorry i dont get it! at least i tried 

VcsHistoryUtil.showDiff(...)

but how can i get the Revisions?

Or can you post a code snippet, please?
0

It depends on where do you want to invoke this action from, and which items do you want to compare.

At any case you can create a `GitFileRevision` object manually.

See also `GitCompareWithBranchAction`.

0

Thank you. Here is my Solution:

 

public class Vcs extends GitCompareWithBranchAction {

Project project;
VirtualFile thisFile;
VirtualFile rootFile;
FilePath thisPath;
FilePath rootPath;
String currentBranchName;
String presentableRevisionName;

java.util.List<GitRepository> repos;
GitRepository repository;

public Vcs(Project project) {
this.project = project;

com.intellij.openapi.editor.Editor editor = FileEditorManager.getInstance(this.project).getSelectedTextEditor();
Document document = editor.getDocument();

try {
this.thisFile = FileDocumentManager.getInstance().getFile(document);
this.thisPath = new FilePathImpl(this.thisFile);
this.rootFile = GitUtil.getGitRoot(this.thisFile);
this.rootPath = new FilePathImpl(this.rootFile);
} catch (VcsException e) {
e.printStackTrace();
}

this. repos = GitUtil.getRepositoryManager(this.project).getRepositories();
this.repository = GitUtil.getRepositoryManager(this.project).getRepositoryForFile(this.rootFile);

this.currentBranchName = this.repository.getCurrentBranchName();

String currentRevision = ObjectUtils.assertNotNull(this.repository.getCurrentRevision());
this.presentableRevisionName = DvcsUtil.getShortHash(currentRevision);

}

public void compareWithBranch(String compareBranchName) {

showDiffWithBranchUnderModalProgress(
this.project,
this.rootFile,
this.presentableRevisionName,
compareBranchName
);
}

private void showDiffWithBranchUnderModalProgress(@NotNull final Project project,
@NotNull final VirtualFile file,
@NotNull final String head,
@NotNull final String compare) {
new Task.Backgroundable(project, "Collecting Changes...", true) {
private Collection<Change> changes;

@Override
public void run(@NotNull ProgressIndicator indicator) {
try {
changes = getDiffChanges(project, file, compare);
}
catch (VcsException e) {
VcsNotifier.getInstance(project).notifyImportantWarning("Couldn't compare with branch", String
.format("Couldn't compare " + DvcsUtil.fileOrFolder(file) + " [%s] with branch [%s];\n %s", file, compare, e.getMessage()));
}
}

@Override
public void onSuccess() {
//if changes null -> then exception occurred before
if (changes != null) {
VcsDiffUtil.showDiffFor(
project,
changes,
VcsDiffUtil.getRevisionTitle(compare, false),
VcsDiffUtil.getRevisionTitle(head, true),
VcsUtil.getFilePath(file)
);
}
}
}.queue();
}
}




Usage:
Vcs vcs = new Vcs(this.project);
vcs.compareWithBranch("master");



0

Please sign in to leave a comment.