Skip to content
This repository was archived by the owner on Nov 14, 2023. It is now read-only.

Commit 5686b54

Browse files
author
Stephen Gutekanst
committed
Better remote heuristic + make remote configurable
Fixes #9
1 parent 1169d41 commit 5686b54

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

README.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Sourcegraph for JetBrains IDEs [![JetBrains Plugin](https://img.shields.io/badge/JetBrains-Sourcegraph-green.svg)](https://plugins.jetbrains.com/plugin/9682-sourcegraph)
22

3-
The Sourcegraph plugin for JetBrains IDEs enables you to quickly open and search code on Sourcegraph.com easily and efficiently in JetBrains IDEs such as IntelliJ. This plugin works with most JetBrains IDEs:
3+
The Sourcegraph plugin for JetBrains IDEs enables you to quickly open and search code on Sourcegraph easily and efficiently in JetBrains IDEs such as IntelliJ. This plugin works with most JetBrains IDEs:
44

55
- IntelliJ IDEA
66
- IntelliJ IDEA Community Edition
@@ -11,7 +11,7 @@ The Sourcegraph plugin for JetBrains IDEs enables you to quickly open and search
1111
- RubyMine
1212
- AppCode
1313
- CLion
14-
- Gogland
14+
- GoLand
1515
- DataGrip
1616
- Rider
1717
- Android Studio
@@ -45,6 +45,7 @@ The plugin is configurable by creating a `sourcegraph-jetbrains.properties` in y
4545
url = https://sourcegraph.com
4646
```
4747

48+
By default, the plugin will use the `origin` git remote to determine which repository on Sourcegraph corresponds to the local repository. You may configure this by adding a `sourcegraph` remote which will take priority.
4849

4950
## Questions & Feedback
5051

@@ -73,7 +74,19 @@ Please file an issue: https://github.com/sourcegraph/sourcegraph-jetbrains/issue
7374

7475
## Version History
7576

76-
- v1.1.2 - Fixed an error that occurred when trying to search with no selection.
77-
- v1.1.1 - Fixed search shortcut; Updated the search URL to reflect a recent Sourcegraph.com change.
78-
- v1.1.0 - Added support for using the plugin with on-premises Sourcegraph instances.
79-
- v1.0.0 - Initial Release; basic Open File & Search functionality.
77+
#### v1.1.2
78+
79+
- Fixed an error that occurred when trying to search with no selection.
80+
- The git remote used for repository detection is now `sourcegraph` and then `origin`, instead of the previously poor choice of just the first git remote.
81+
82+
#### v1.1.1
83+
84+
- Fixed search shortcut; Updated the search URL to reflect a recent Sourcegraph.com change.
85+
86+
#### v1.1.0
87+
88+
- Added support for using the plugin with on-premises Sourcegraph instances.
89+
90+
#### v1.0.0
91+
92+
- Initial Release; basic Open File & Search functionality.

resources/META-INF/plugin.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<vendor email="hi@sourcegraph.com" url="https://sourcegraph.com">Sourcegraph</vendor>
66

77
<description><![CDATA[
8-
Sourcegraph for JetBrains IDEs (IntelliJ, PyCharm, Goglang, etc.)<br>
8+
Sourcegraph for JetBrains IDEs (IntelliJ, PyCharm, GoLand, etc.)<br>
99
]]></description>
1010

1111
<change-notes><![CDATA[
@@ -14,6 +14,9 @@
1414
<ul>
1515
<li>Fixed an error that occurred when trying to search with no selection.</li>
1616
</ul>
17+
<ul>
18+
<li>The git remote used for repository detection is now `sourcegraph` and then `origin`, instead of the previously poor choice of just the first git remote.</li>
19+
</ul>
1720
</li>
1821
<li>v1.1.1 - Fixed search shortcut.
1922
<ul>

src/Util.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,31 @@
44
import java.util.Properties;
55

66
public class Util {
7-
public static String VERSION = "v1.1.1";
8-
9-
// gitRemotes returns the names of all git remotes, e.g. ["origin", "foobar"]
10-
public static String[] gitRemotes(String repoDir) throws IOException {
11-
return exec("git remote", repoDir).split("[\\r\\n]+");
12-
}
7+
public static String VERSION = "v1.1.2";
138

149
// gitRemoteURL returns the remote URL for the given remote name.
1510
// e.g. "origin" -> "git@github.com:foo/bar"
16-
public static String gitRemoteURL(String repoDir, String remoteName) throws IOException {
17-
return exec("git remote get-url " + remoteName, repoDir).trim();
11+
public static String gitRemoteURL(String repoDir, String remoteName) throws Exception {
12+
String s = exec("git remote get-url " + remoteName, repoDir).trim();
13+
if (s.isEmpty()) {
14+
throw new Exception("no such remote");
15+
}
16+
return s;
1817
}
1918

20-
// gitDefaultRemoteURL returns the remote URL of the first Git remote
21-
// found. An exception is thrown if there is not one.
22-
public static String gitDefaultRemoteURL(String repoDir) throws Exception {
23-
String[] remotes = gitRemotes(repoDir);
24-
if (remotes.length == 0) {
25-
throw new Exception("no configured git remotes");
26-
}
27-
if (remotes.length > 1) {
28-
Logger.getInstance(Util.class).info("using first git remote: " + remotes[0]);
19+
// configuredGitRemoteURL returns the URL of the "sourcegraph" remote, if
20+
// configured, or else the URL of the "origin" remote. An exception is
21+
// thrown if neither exists.
22+
public static String configuredGitRemoteURL(String repoDir) throws Exception {
23+
try {
24+
return gitRemoteURL(repoDir, "sourcegraph");
25+
} catch (Exception err) {
26+
try {
27+
return gitRemoteURL(repoDir, "origin");
28+
} catch (Exception err2) {
29+
throw new Exception("no configured git remote \"sourcegraph\" or \"origin\"");
30+
}
2931
}
30-
return gitRemoteURL(repoDir, remotes[0]);
3132
}
3233

3334
// gitRootDir returns the repository root directory for any directory
@@ -88,7 +89,7 @@ public static RepoInfo repoInfo(String fileName) {
8889

8990
// Determine file path, relative to repository root.
9091
fileRel = fileName.substring(repoRoot.length()+1);
91-
remoteURL = gitDefaultRemoteURL(repoRoot);
92+
remoteURL = configuredGitRemoteURL(repoRoot);
9293
branch = gitBranch(repoRoot);
9394
} catch (Exception err) {
9495
Logger.getInstance(Util.class).info(err);

0 commit comments

Comments
 (0)