mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-98735 github: try to open file in browser by hash
This commit is contained in:
@@ -23,12 +23,17 @@ import com.intellij.openapi.editor.SelectionModel;
|
||||
import com.intellij.openapi.project.DumbAwareAction;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
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.history.VcsRevisionNumber;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.vcsUtil.VcsUtil;
|
||||
import git4idea.GitLocalBranch;
|
||||
import git4idea.GitRemoteBranch;
|
||||
import git4idea.GitRevisionNumber;
|
||||
import git4idea.GitUtil;
|
||||
import git4idea.history.GitHistoryUtils;
|
||||
import git4idea.repo.GitRepository;
|
||||
import git4idea.repo.GitRepositoryManager;
|
||||
import icons.GithubIcons;
|
||||
@@ -38,6 +43,7 @@ import org.jetbrains.plugins.github.util.GithubNotifications;
|
||||
import org.jetbrains.plugins.github.util.GithubUrlUtil;
|
||||
import org.jetbrains.plugins.github.util.GithubUtil;
|
||||
|
||||
import static org.jetbrains.plugins.github.util.GithubUtil.LOG;
|
||||
import static org.jetbrains.plugins.github.util.GithubUtil.setVisibleEnabled;
|
||||
|
||||
/**
|
||||
@@ -105,8 +111,7 @@ public class GithubOpenInBrowserAction extends DumbAwareAction {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getGithubUrl(@NotNull Project project, @NotNull VirtualFile virtualFile, @Nullable Editor editor) {
|
||||
|
||||
private static String getGithubUrl(@NotNull Project project, @NotNull VirtualFile virtualFile, @Nullable Editor editor) {
|
||||
GitRepositoryManager manager = GitUtil.getRepositoryManager(project);
|
||||
final GitRepository repository = manager.getRepositoryForFile(virtualFile);
|
||||
if (repository == null) {
|
||||
@@ -132,19 +137,20 @@ public class GithubOpenInBrowserAction extends DumbAwareAction {
|
||||
return null;
|
||||
}
|
||||
|
||||
String branch = getBranchNameOnRemote(project, repository);
|
||||
if (branch == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String relativePath = path.substring(rootPath.length());
|
||||
String urlToOpen = makeUrlToOpen(editor, relativePath, branch, githubRemoteUrl);
|
||||
if (urlToOpen == null) {
|
||||
GithubNotifications.showError(project, CANNOT_OPEN_IN_BROWSER, "Can't create properly url", githubRemoteUrl);
|
||||
return null;
|
||||
|
||||
String branch = getCurrentBranchNameOnRemote(repository);
|
||||
if (branch != null) {
|
||||
return makeUrlToOpen(editor, relativePath, branch, githubRemoteUrl);
|
||||
}
|
||||
|
||||
return urlToOpen;
|
||||
String hash = getCurrentFileRevisionHash(project, virtualFile);
|
||||
if (hash != null) {
|
||||
return makeUrlToOpen(editor, relativePath, hash, githubRemoteUrl);
|
||||
}
|
||||
|
||||
GithubNotifications.showError(project, CANNOT_OPEN_IN_BROWSER, "Can't find related tracked branch or hash.");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -180,23 +186,32 @@ public class GithubOpenInBrowserAction extends DumbAwareAction {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getBranchNameOnRemote(@NotNull Project project, @NotNull GitRepository repository) {
|
||||
private static String getCurrentBranchNameOnRemote(@NotNull GitRepository repository) {
|
||||
GitLocalBranch currentBranch = repository.getCurrentBranch();
|
||||
if (currentBranch == null) {
|
||||
GithubNotifications.showError(project, CANNOT_OPEN_IN_BROWSER,
|
||||
"Can't open the file on GitHub when repository is on detached HEAD. Please checkout a branch.");
|
||||
return null;
|
||||
}
|
||||
|
||||
GitRemoteBranch tracked = currentBranch.findTrackedBranch(repository);
|
||||
if (tracked == null) {
|
||||
GithubNotifications
|
||||
.showError(project, CANNOT_OPEN_IN_BROWSER, "Can't open the file on GitHub when current branch doesn't have a tracked branch.",
|
||||
"Current branch: " + currentBranch + ", tracked info: " + repository.getBranchTrackInfos());
|
||||
return null;
|
||||
}
|
||||
|
||||
return tracked.getNameForRemoteOperations();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String getCurrentFileRevisionHash(@NotNull Project project, @NotNull VirtualFile file) {
|
||||
try {
|
||||
VcsRevisionNumber revision = GitHistoryUtils.getCurrentRevision(project, VcsUtil.getFilePath(file), null);
|
||||
if (revision instanceof GitRevisionNumber) {
|
||||
return ((GitRevisionNumber)revision).getRev();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
catch (VcsException e) {
|
||||
LOG.warn(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user