|
4 | 4 | import java.util.Properties; |
5 | 5 |
|
6 | 6 | 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"; |
13 | 8 |
|
14 | 9 | // gitRemoteURL returns the remote URL for the given remote name. |
15 | 10 | // 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; |
18 | 17 | } |
19 | 18 |
|
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 | + } |
29 | 31 | } |
30 | | - return gitRemoteURL(repoDir, remotes[0]); |
31 | 32 | } |
32 | 33 |
|
33 | 34 | // gitRootDir returns the repository root directory for any directory |
@@ -88,7 +89,7 @@ public static RepoInfo repoInfo(String fileName) { |
88 | 89 |
|
89 | 90 | // Determine file path, relative to repository root. |
90 | 91 | fileRel = fileName.substring(repoRoot.length()+1); |
91 | | - remoteURL = gitDefaultRemoteURL(repoRoot); |
| 92 | + remoteURL = configuredGitRemoteURL(repoRoot); |
92 | 93 | branch = gitBranch(repoRoot); |
93 | 94 | } catch (Exception err) { |
94 | 95 | Logger.getInstance(Util.class).info(err); |
|
0 commit comments