-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
GitLink.java
157 lines (131 loc) · 6.06 KB
/
GitLink.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package uk.co.ben_gibson.git.link;
import com.intellij.ide.browsers.BrowserLauncher;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import uk.co.ben_gibson.git.link.Exception.Codes;
import uk.co.ben_gibson.git.link.Exception.GitLinkException;
import uk.co.ben_gibson.git.link.Git.Branch;
import uk.co.ben_gibson.git.link.Git.Commit;
import uk.co.ben_gibson.git.link.Git.Repository;
import uk.co.ben_gibson.git.link.Git.RepositoryFactory;
import uk.co.ben_gibson.git.link.UI.ExceptionRenderer;
import uk.co.ben_gibson.git.link.UI.LineSelection;
import uk.co.ben_gibson.git.link.Url.Factory.*;
import uk.co.ben_gibson.git.link.Url.Modifier.UrlModifier;
import uk.co.ben_gibson.git.link.Url.Modifier.UrlModifierProvider;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Objects;
public class GitLink
{
private Logger logger = Logger.getInstance(ServiceManager.getService(Plugin.class).displayName());
private RepositoryFactory repositoryFactory;
private ExceptionRenderer exceptionRenderer;
private UrlModifierProvider urlModifierProvider;
GitLink(
RepositoryFactory repositoryFactory,
ExceptionRenderer exceptionRenderer,
UrlModifierProvider urlModifierProvider
)
{
this.repositoryFactory = repositoryFactory;
this.exceptionRenderer = exceptionRenderer;
this.urlModifierProvider = urlModifierProvider;
}
public void openFile(
@NotNull Project project,
@NotNull VirtualFile file,
@Nullable Commit commit,
@Nullable LineSelection lineSelection
)
{
try {
Preferences preferences = Preferences.getInstance(project);
Repository repository = this.repositoryFactory.create(project, file, preferences.getDefaultBranch(), preferences.remoteName);
UrlFactory urlFactory = UrlFactoryProvider.fromPreferences(preferences).urlFactoryForHost(preferences.getRemoteHost());
Task.Backgroundable task = new Task.Backgroundable(project, "GitLink - Opening file")
{
@Override
public void run(@NotNull ProgressIndicator indicator)
{
try {
URL url;
// If we have explicitly been given a commit or the current commit exists on the remote
// repository then open the file at that commit otherwise use the branch.
if (commit != null || repository.isCurrentCommitOnRemote()) {
url = urlFactory.createUrlToFileAtCommit(
repository.remote(),
repository.repositoryFileFromVirtualFile(file),
Objects.requireNonNull((commit != null) ? commit : repository.currentCommit()),
lineSelection
);
} else {
url = urlFactory.createUrlToFileOnBranch(
repository.remote(),
repository.repositoryFileFromVirtualFile(file),
repository.currentBranch(),
lineSelection
);
}
GitLink.this.openUrlInBrowser(preferences, url);
} catch (GitLinkException e) {
GitLink.this.exceptionRenderer.render(e);
}
}
};
task.queue();
} catch (GitLinkException e) {
this.exceptionRenderer.render(e);
}
}
public void openCommit(@NotNull Project project, @NotNull Commit commit, @NotNull VirtualFile file)
{
try {
Preferences preferences = Preferences.getInstance(project);
Branch defaultBranch = new Branch(preferences.defaultBranchName);
Repository repository = this.repositoryFactory.create(project, file, defaultBranch, preferences.remoteName);
UrlFactory urlFactory = UrlFactoryProvider.fromPreferences(preferences).urlFactoryForHost(preferences.getRemoteHost());
Task.Backgroundable task = new Task.Backgroundable(project, "GitLink - Opening commit")
{
@Override
public void run(@NotNull ProgressIndicator indicator)
{
try {
URL url = urlFactory.createUrlToCommit(repository.remote(), commit);
GitLink.this.openUrlInBrowser(preferences, url);
} catch (GitLinkException e) {
GitLink.this.exceptionRenderer.render(e);
}
}
};
task.queue();
} catch (GitLinkException e) {
this.exceptionRenderer.render(e);
}
}
private void openUrlInBrowser(@NotNull Preferences preferences, @NotNull URL url) throws GitLinkException
{
GitLink.this.logger.info(String.format("Generated URL '%s'", url.toString()));
for (UrlModifier urlModifier : GitLink.this.urlModifierProvider.modifiers()) {
if (preferences.isModifierEnabled(urlModifier)) {
url = urlModifier.modify(url);
GitLink.this.logger.info(String.format(
"Applied modifier '%s' - '%s'",
urlModifier.name(),
url.toString()
));
}
}
try {
BrowserLauncher.getInstance().open(url.toURI().toASCIIString());
} catch (URISyntaxException e) {
throw new GitLinkException(e.getMessage(), Codes.COULD_NOT_OPEN_URL_IN_BROWSER);
}
}
}