Skip to content
This repository has been archived by the owner on Apr 14, 2020. It is now read-only.

Commit

Permalink
Bug fix for IDEA 13 EAP
Browse files Browse the repository at this point in the history
  • Loading branch information
bsideup committed Oct 26, 2013
1 parent 2fb0574 commit a7390f9
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 187 deletions.
8 changes: 4 additions & 4 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<idea-plugin version="2">
<id>ru.trylogic.idea.gitlab.integration</id>
<name>GitLab integration</name>
<version>1.0</version>
<vendor email="bsideup@gmail.com" url="https://github.com/bsideup">Sergei Egorov</vendor>
<version>1.0.1</version>
<vendor email="bsideup@gmail.com" url="https://github.com/bsideup">Sergei BSiDeUp Egorov</vendor>

<description><![CDATA[
<a href="http://gitlab.org/">GitLab</a> integration plugin.
Support "Open file in browser" command.
]]></description>

<change-notes><![CDATA[
Initial release. "Open file in browser" command.
Bug fix for IDEA 13 EAP. Tested on 132.719.
]]>
</change-notes>

<idea-version since-build="107.105"/>
<idea-version since-build="132.719"/>

<depends>com.intellij.modules.vcs</depends>
<depends>com.intellij.modules.lang</depends>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,36 +1,96 @@
package ru.trylogic.idea.gitlab.integration.actions;

import com.intellij.ide.BrowserUtil;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.changes.ChangeListManager;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import git4idea.GitLocalBranch;
import git4idea.GitRemoteBranch;
import git4idea.GitUtil;
import git4idea.repo.GitRemote;
import git4idea.repo.GitRepository;
import git4idea.repo.GitRepositoryManager;
import ru.trylogic.idea.gitlab.integration.utils.GitlabUrlUtil;
import icons.GitlabIcons;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import ru.trylogic.idea.gitlab.integration.utils.GitlabUrlUtil;

public class GitLabOpenInBrowserAction extends DumbAwareAction {

public static final String CANNOT_OPEN_IN_BROWSER = "Cannot open in browser";

protected GitLabOpenInBrowserAction() {
super("Open on GitLab", "Open corresponding link in browser", GitlabIcons.Gitlab_icon);
}

static void setVisibleEnabled(AnActionEvent e, boolean visible, boolean enabled) {
e.getPresentation().setVisible(visible);
e.getPresentation().setEnabled(enabled);
}

protected GitLabOpenInBrowserAction() {
super("Open on GitLab", "Open corresponding link in browser", GitlabIcons.Gitlab_icon);
static void showError(Project project, String cannotOpenInBrowser) {
showError(project, cannotOpenInBrowser, null);
}

static void showError(Project project, String cannotOpenInBrowser, String s) {
showError(project, cannotOpenInBrowser, s, null);
}

static void showError(Project project, String cannotOpenInBrowser, String s, String s1) {
System.out.println(cannotOpenInBrowser + ";" + s + ";" + s1);
}

@Nullable
static String makeUrlToOpen(@Nullable Editor editor,
@NotNull String relativePath,
@NotNull String branch,
@NotNull String remoteUrl) {
final StringBuilder builder = new StringBuilder();
final String repoUrl = GitlabUrlUtil.makeRepoUrlFromRemoteUrl(remoteUrl);
if (repoUrl == null) {
return null;
}
builder.append(repoUrl).append("/blob/").append(branch).append(relativePath);

if (editor != null && editor.getDocument().getLineCount() >= 1) {
// lines are counted internally from 0, but from 1 on gitlab
SelectionModel selectionModel = editor.getSelectionModel();
final int begin = editor.getDocument().getLineNumber(selectionModel.getSelectionStart()) + 1;
final int selectionEnd = selectionModel.getSelectionEnd();
int end = editor.getDocument().getLineNumber(selectionEnd) + 1;
if (editor.getDocument().getLineStartOffset(end - 1) == selectionEnd) {
end -= 1;
}
builder.append("#L").append(begin).append('-').append(end);
}

return builder.toString();
}

@Nullable
public static String getBranchNameOnRemote(@NotNull Project project, @NotNull GitRepository repository) {
GitLocalBranch currentBranch = repository.getCurrentBranch();
if (currentBranch == null) {
showError(project, CANNOT_OPEN_IN_BROWSER,
"Can't open the file on GitLab when repository is on detached HEAD. Please checkout a branch.");
return null;
}

GitRemoteBranch tracked = currentBranch.findTrackedBranch(repository);
if (tracked == null) {
showError(project, CANNOT_OPEN_IN_BROWSER, "Can't open the file on GitLab when current branch doesn't have a tracked branch.",
"Current branch: " + currentBranch + ", tracked info: " + repository.getBranchTrackInfos());
return null;
}

return tracked.getNameForRemoteOperations();
}

@Override
Expand All @@ -41,9 +101,8 @@ public void update(final AnActionEvent e) {
setVisibleEnabled(e, false, false);
return;
}
GitRepositoryManager manager = GitUtil.getRepositoryManager(project);

final GitRepository gitRepository = manager.getRepositoryForFile(virtualFile);
final GitRepository gitRepository = GitUtil.getRepositoryManager(project).getRepositoryForFile(virtualFile);
if (gitRepository == null) {
setVisibleEnabled(e, false, false);
return;
Expand Down Expand Up @@ -73,15 +132,6 @@ public void actionPerformed(final AnActionEvent e) {
return;
}

String urlToOpen = getUrl(project, virtualFile, editor);
if (urlToOpen != null) {
BrowserUtil.launchBrowser(urlToOpen);
}
}

@Nullable
public static String getUrl(@NotNull Project project, @NotNull VirtualFile virtualFile, @Nullable Editor editor) {

GitRepositoryManager manager = GitUtil.getRepositoryManager(project);
final GitRepository repository = manager.getRepositoryForFile(virtualFile);
if (repository == null) {
Expand All @@ -90,94 +140,96 @@ public static String getUrl(@NotNull Project project, @NotNull VirtualFile virtu
details.append(repo.getPresentableUrl()).append("; ");
}
showError(project, CANNOT_OPEN_IN_BROWSER, "Can't find git repository", details.toString());
return null;
return;
}

final String remoteUrl = GitlabUrlUtil.findRemoteUrl(repository);
if (remoteUrl == null) {
if (repository.getRemotes().size() == 0) {
showError(project, CANNOT_OPEN_IN_BROWSER, "Can't find gitlab remote");
return null;
return;
}

final String rootPath = repository.getRoot().getPath();
final String path = virtualFile.getPath();
if (!path.startsWith(rootPath)) {
showError(project, CANNOT_OPEN_IN_BROWSER, "File is not under repository root", "Root: " + rootPath + ", file: " + path);
return null;
}
DefaultActionGroup remotesActionGroup = new DefaultActionGroup();
remotesActionGroup.add(new RemoteSelectedAction(project, repository, editor, repository.getRemotes().iterator().next(), rootPath, path));

String branch = getBranchNameOnRemote(project, repository);
if (branch == null) {
return null;
}

String relativePath = path.substring(rootPath.length());
String urlToOpen = makeUrlToOpen(editor, relativePath, branch, remoteUrl);
if (urlToOpen == null) {
showError(project, CANNOT_OPEN_IN_BROWSER, "Can't create properly url", remoteUrl);
return null;
}
DataContext dataContext = e.getDataContext();
final ListPopup popup =
JBPopupFactory.getInstance().createActionGroupPopup(
"Select remote",
remotesActionGroup,
dataContext,
JBPopupFactory.ActionSelectionAid.SPEEDSEARCH,
true);

return urlToOpen;
popup.showInBestPositionFor(dataContext);
}

private static void showError(Project project, String cannotOpenInBrowser) {
showError(project, cannotOpenInBrowser, null);
}

private static void showError(Project project, String cannotOpenInBrowser, String s) {
showError(project, cannotOpenInBrowser, s, null);
}

class RemoteSelectedAction extends AnAction {

private final Editor editor;

private final GitRemote remote;

private final String rootPath;

private final String path;

private final Project project;

private final GitRepository repository;

public RemoteSelectedAction(@NotNull Project project, @NotNull GitRepository repository, @Nullable Editor editor,
@NotNull GitRemote remote, @NotNull String rootPath, @NotNull String path) {
super(remote.getName());
this.project = project;
this.repository = repository;
this.editor = editor;
this.remote = remote;
this.rootPath = rootPath;
this.path = path;
}

private static void showError(Project project, String cannotOpenInBrowser, String s, String s1) {
System.out.println(cannotOpenInBrowser + ";" + s + ";" + s1);
@Override
public void update(AnActionEvent e) {
super.update(e);
setEnabledInModalContext(true);
e.getPresentation().setEnabled(true);
}

@Nullable
private static String makeUrlToOpen(@Nullable Editor editor,
@NotNull String relativePath,
@NotNull String branch,
@NotNull String remoteUrl) {
final StringBuilder builder = new StringBuilder();
final String repoUrl = GitlabUrlUtil.makeRepoUrlFromRemoteUrl(remoteUrl);
if (repoUrl == null) {
return null;
@Override
public void actionPerformed(AnActionEvent anActionEvent) {
if (!path.startsWith(rootPath)) {
GitLabOpenInBrowserAction.showError(project, GitLabOpenInBrowserAction.CANNOT_OPEN_IN_BROWSER,
"File is not under repository root", "Root: " + rootPath + ", file: " + path);
return;
}
builder.append(repoUrl).append("/blob/").append(branch).append(relativePath);

if (editor != null && editor.getDocument().getLineCount() >= 1) {
// lines are counted internally from 0, but from 1 on gitlab
SelectionModel selectionModel = editor.getSelectionModel();
final int begin = editor.getDocument().getLineNumber(selectionModel.getSelectionStart()) + 1;
final int selectionEnd = selectionModel.getSelectionEnd();
int end = editor.getDocument().getLineNumber(selectionEnd) + 1;
if (editor.getDocument().getLineStartOffset(end - 1) == selectionEnd) {
end -= 1;
}
builder.append("#L").append(begin).append('-').append(end);
String branch = GitLabOpenInBrowserAction.getBranchNameOnRemote(project, repository);
if (branch == null) {
return;
}

String remoteUrl = remote.getFirstUrl();

return builder.toString();
}

@Nullable
public static String getBranchNameOnRemote(@NotNull Project project, @NotNull GitRepository repository) {
GitLocalBranch currentBranch = repository.getCurrentBranch();
if (currentBranch == null) {
showError(project, CANNOT_OPEN_IN_BROWSER,
"Can't open the file on GitLab when repository is on detached HEAD. Please checkout a branch.");
return null;
if (remoteUrl == null) {
GitLabOpenInBrowserAction.showError(project, GitLabOpenInBrowserAction.CANNOT_OPEN_IN_BROWSER,
"Can't obtain url for remote", remote.getName());
return;
}

GitRemoteBranch tracked = currentBranch.findTrackedBranch(repository);
if (tracked == null) {
showError(project, CANNOT_OPEN_IN_BROWSER, "Can't open the file on GitLab when current branch doesn't have a tracked branch.",
"Current branch: " + currentBranch + ", tracked info: " + repository.getBranchTrackInfos());
return null;
String relativePath = path.substring(rootPath.length());
String urlToOpen = GitLabOpenInBrowserAction.makeUrlToOpen(editor, relativePath, branch, remoteUrl);
if (urlToOpen == null) {
GitLabOpenInBrowserAction.showError(project, GitLabOpenInBrowserAction.CANNOT_OPEN_IN_BROWSER,
"Can't create properly url", remote.getFirstUrl());
return;
}

return tracked.getNameForRemoteOperations();
BrowserUtil.launchBrowser(urlToOpen);
}


}
}
Loading

0 comments on commit a7390f9

Please sign in to comment.