Skip to content

Commit ea2b821

Browse files
committed
Merge pull request #9 from ben-gibson/add-tests
dev: Refactoring and added tests
2 parents 973faff + 41ec867 commit ea2b821

23 files changed

+246
-214
lines changed

build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ apply plugin: 'java'
77

88
repositories {
99
jcenter()
10+
mavenCentral()
1011
}
1112

1213
dependencies {
1314
testCompile "org.mockito:mockito-core:1.+"
15+
testCompile group: 'junit', name: 'junit', version: '4.12'
16+
testCompile group: 'com.tngtech.java', name: 'junit-dataprovider', version: '1.10.0'
1417
}
1518

1619
sourceSets {
@@ -36,4 +39,4 @@ intellij {
3639
pluginName 'RemoteRepositoryMapper'
3740
updateSinceUntilBuild false
3841
sameSinceUntilBuild false
39-
}
42+
}

remote-repository-mapper.iml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,11 @@
121121
<orderEntry type="library" exported="" name="Gradle: com.jetbrains:remote-servers-git:14.1.4" level="project" />
122122
<orderEntry type="library" exported="" name="Gradle: com.jetbrains:trilead-ssh2:14.1.4" level="project" />
123123
<orderEntry type="library" exported="" scope="RUNTIME" name="Gradle: com.jetbrains:tools:14.1.4" level="project" />
124-
<orderEntry type="library" exported="" name="Gradle: com.jetbrains:resources_en:14.1.4" level="project" />
125124
<orderEntry type="library" scope="TEST" name="Gradle: org.mockito:mockito-core:1.10.19" level="project" />
126-
<orderEntry type="library" scope="TEST" name="Gradle: org.hamcrest:hamcrest-core:1.1" level="project" />
127125
<orderEntry type="library" scope="TEST" name="Gradle: org.objenesis:objenesis:2.1" level="project" />
126+
<orderEntry type="library" exported="" name="Gradle: com.jetbrains:resources_en:14.1.4" level="project" />
127+
<orderEntry type="library" scope="TEST" name="Gradle: junit:junit:4.12" level="project" />
128+
<orderEntry type="library" scope="TEST" name="Gradle: com.tngtech.java:junit-dataprovider:1.10.0" level="project" />
129+
<orderEntry type="library" scope="TEST" name="Gradle: org.hamcrest:hamcrest-core:1.3" level="project" />
128130
</component>
129131
</module>

resources/META-INF/plugin.xml

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
<idea-plugin version="2">
22
<id>uk.co.ben-gibson.remote.repository.mapper</id>
33
<name>Remote Repository Mapper</name>
4-
<version>1.1</version>
4+
<version>1.2</version>
55
<vendor url="h">https://github.com/ben-gibson/remote-repository-mapper</vendor>
66

77
<description><![CDATA[
8-
Opens a local file under git version control in its remote origin repository.
9-
<br />
10-
<br />
11-
This is a really simple plugin that provides a way to quickly share some source code via its remote repository.
8+
This is a really simple plugin that provides a convenient shortcut to open a git version controlled file from
9+
PhpStorm in its origin repository. This can be useful, for example, if you need to share the code with someone or
10+
just get quick access to it in the remote repository.
1211
<br />
1312
<br />
1413
After installing select your remote repository provider in Settings &rarr; Other Settings &rarr; Remote Repository Mapper
1514
(currently supports GitHub, Stash, BitBucket, GitLab).
1615
<br />
1716
<br />
1817
To use, open a file that is under git version control in the editor and select File &rarr; Open in remote repository
19-
(the current checked out branch is used unless it does not track a remote branch, in which case it defaults to using master).
20-
<br />
21-
<br />
22-
The resulting link can be copied to the clipboard depending on your preference in the settings.
18+
(the active branch is used unless it does not track a remote branch in which case it defaults to using master).
2319
]]></description>
2420

2521
<change-notes><![CDATA[
2622
<ul>
27-
<li>Added BitBucket as a provider</li>
28-
<li>Added GitLab as a provider</li>
23+
<li>Added support for BitBucket</li>
24+
<li>Added support for GitLab</li>
2925
</ul>
3026
]]>
3127
</change-notes>

src/uk/co/ben_gibson/repositorymapper/Context/Context.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public String getBranch()
9696
* @return String
9797
*/
9898
@NotNull
99-
public String getRepositoryRelativeFilePath()
99+
public String getFilePathRelativeToRepository()
100100
{
101101
return this.file.getPath().substring(this.repository.getRoot().getPath().length());
102102
}

src/uk/co/ben_gibson/repositorymapper/OpenContextAction.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.intellij.openapi.project.Project;
2020
import uk.co.ben_gibson.repositorymapper.UrlFactory.UrlFactoryProvider;
2121
import java.awt.*;
22-
import java.awt.datatransfer.Clipboard;
2322
import java.awt.datatransfer.StringSelection;
2423
import java.net.URL;
2524

@@ -57,8 +56,7 @@ public void actionPerformed(AnActionEvent event)
5756
URL url = urlFactoryProvider.getUrlFactoryForProvider(settings.getRepositoryProvider()).getUrlFromContext(context);
5857

5958
if (settings.getCopyToClipboard()) {
60-
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
61-
clipboard.setContents(new StringSelection(url.toString()), null);
59+
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(url.toString()), null);
6260
}
6361

6462
BrowserLauncher.getInstance().browse(url.toURI());
@@ -111,6 +109,8 @@ public Context getContext(@NotNull Project project)
111109

112110
Integer caretPosition = editor.getCaretModel().getLogicalPosition().line + 1;
113111

114-
return new Context(new Repository(repository, "master"), file, caretPosition);
112+
Repository repositoryWrapper = new Repository(repository, "master");
113+
114+
return new Context(repositoryWrapper, file, caretPosition);
115115
}
116116
}

src/uk/co/ben_gibson/repositorymapper/Repository/Exception/BranchNotFoundException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public BranchNotFoundException(String message)
2222

2323

2424
/**
25-
* Could not find active branch with a remote.
25+
* Could not find active branch that tracked a remote branch.
2626
*
2727
* @return BranchNotFoundException
2828
*/
@@ -31,4 +31,4 @@ public static BranchNotFoundException activeBranchWithRemoteTrackingNotFound()
3131
{
3232
return new BranchNotFoundException("Could not find an active branch that tracked a remote branch");
3333
}
34-
}
34+
}

src/uk/co/ben_gibson/repositorymapper/Repository/Exception/RemoteNotFoundException.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,21 @@ public RemoteNotFoundException(String message)
2222

2323

2424
/**
25-
* Could not find the remote 'origin'
25+
* Could not find 'origin'
2626
*
2727
* @return RemoteNotFoundException
2828
*/
2929
@NotNull
30-
public static RemoteNotFoundException remoteOriginNotFound()
30+
public static RemoteNotFoundException originNotFound()
3131
{
3232
return new RemoteNotFoundException("Could not find the remote 'origin'");
3333
}
3434

3535

3636
/**
37-
* Could not find the url associated with the remote
37+
* Could not find a url associated with the remote
38+
*
39+
* @param remote The remote that had no url.
3840
*
3941
* @return RemoteNotFoundException
4042
*/

src/uk/co/ben_gibson/repositorymapper/Repository/Remote.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22

33
import git4idea.repo.GitRemote;
44
import org.jetbrains.annotations.NotNull;
5-
import org.jetbrains.annotations.Nullable;
5+
import uk.co.ben_gibson.repositorymapper.Repository.Exception.RemoteNotFoundException;
66

77
/**
8-
* Wrapper for a git remote.
8+
* Decorates the git remote.
99
*/
1010
public class Remote
1111
{
1212

13+
@NotNull
1314
GitRemote remote;
1415

1516
/**
1617
* Constructor.
1718
*
18-
* @param remote the git remote to wrap
19+
* @param remote The git remote to decorate
1920
*/
20-
public Remote(GitRemote remote)
21+
public Remote(@NotNull GitRemote remote)
2122
{
2223
this.remote = remote;
2324
}
@@ -36,9 +37,15 @@ public String getName()
3637
/**
3738
* {@inheritDoc}
3839
*/
39-
@Nullable
40-
public String getFirstUrl()
40+
@NotNull
41+
public String getFirstUrl() throws RemoteNotFoundException
4142
{
42-
return remote.getFirstUrl();
43+
String url = this.remote.getFirstUrl();
44+
45+
if (url == null) {
46+
throw RemoteNotFoundException.urlNotFoundForRemote(this);
47+
}
48+
49+
return url;
4350
}
4451
}

src/uk/co/ben_gibson/repositorymapper/Repository/Repository.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@
1010
import java.net.URL;
1111

1212
/**
13-
* Wraps up a GitRepository to add some behaviour.
13+
* Decorates the git repository.
1414
*/
1515
public class Repository
1616
{
1717

18+
@NotNull
1819
private GitRepository repository;
20+
21+
@NotNull
1922
private String defaultBranch;
2023

2124

2225
/**
2326
* Constructor.
2427
*
25-
* @param repository The repository.
28+
* @param repository The git repository.
2629
* @param defaultBranch The default branch.
2730
*/
2831
public Repository(@NotNull GitRepository repository, @NotNull String defaultBranch)
@@ -33,7 +36,7 @@ public Repository(@NotNull GitRepository repository, @NotNull String defaultBran
3336

3437

3538
/**
36-
* Get default branch.
39+
* Get the default branch.
3740
*
3841
* @return String
3942
*/
@@ -46,35 +49,40 @@ public String getDefaultBranch()
4649

4750

4851
/**
49-
* Get the active branch which tracks a remote or default to master.
52+
* Get the active branch if it tracks a remote branch.
5053
*
5154
* @return String
5255
*/
5356
@NotNull
5457
public String getActiveBranchWithRemote() throws BranchNotFoundException
5558
{
5659
if (this.repository.getCurrentBranch() != null && this.repository.getCurrentBranch().findTrackedBranch(this.repository) != null) {
57-
return this.repository.getCurrentBranch().getName();
60+
return this.repository.getCurrentBranch().getName();
5861
}
5962

6063
throw BranchNotFoundException.activeBranchWithRemoteTrackingNotFound();
6164
}
6265

6366

6467
/**
65-
* Get a url from a remote.
68+
* Get the canonical origin url
6669
*
6770
* @return URL
6871
*/
6972
public URL getOriginUrl() throws RemoteNotFoundException, MalformedURLException
7073
{
71-
Remote origin = this.getOrigin();
74+
return this.getRemoteUrl(this.getOrigin());
75+
}
7276

73-
if (origin.getFirstUrl() == null) {
74-
throw RemoteNotFoundException.urlNotFoundForRemote(origin);
75-
}
7677

77-
String url = StringUtil.trimEnd(origin.getFirstUrl(), ".git");
78+
/**
79+
* Get the canonical url from a remote.
80+
*
81+
* @return URL
82+
*/
83+
public URL getRemoteUrl(Remote remote) throws MalformedURLException, RemoteNotFoundException
84+
{
85+
String url = StringUtil.trimEnd(remote.getFirstUrl(), ".git");
7886

7987
url = url.replaceAll(":\\d{1,4}", ""); // remove port
8088

@@ -92,21 +100,20 @@ public URL getOriginUrl() throws RemoteNotFoundException, MalformedURLException
92100

93101

94102
/**
95-
* Fetch the remote origin
103+
* Get origin.
96104
*
97105
* @return Remote
98106
*/
99107
@NotNull
100-
public Remote getOrigin() throws RemoteNotFoundException
108+
private Remote getOrigin() throws RemoteNotFoundException
101109
{
102-
103110
for (GitRemote remote : this.repository.getRemotes()) {
104111
if (remote.getName().equals("origin")) {
105112
return new Remote(remote);
106113
}
107114
}
108115

109-
throw RemoteNotFoundException.remoteOriginNotFound();
116+
throw RemoteNotFoundException.originNotFound();
110117
}
111118

112119

src/uk/co/ben_gibson/repositorymapper/RepositoryProvider/RepositoryProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package uk.co.ben_gibson.repositorymapper.RepositoryProvider;
22

33
/**
4-
* Represents different remote repository providers that we support.
4+
* Represents different remote repository providers that are supported.
55
*/
66
public enum RepositoryProvider
77
{
@@ -16,7 +16,7 @@ public enum RepositoryProvider
1616
/**
1717
* Constructor.
1818
*
19-
* @param name The name.
19+
* @param name The repository provider name.
2020
*/
2121
RepositoryProvider(String name)
2222
{

src/uk/co/ben_gibson/repositorymapper/Settings/Configuration.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.intellij.openapi.ui.ComboBox;
55
import com.intellij.openapi.util.Comparing;
66
import com.intellij.ui.EnumComboBoxModel;
7-
import com.intellij.ui.IdeBorderFactory;
87
import com.intellij.ui.components.JBCheckBox;
98
import com.intellij.ui.components.JBLabel;
109
import com.intellij.openapi.components.ServiceManager;
@@ -31,7 +30,7 @@ public class Configuration implements Configurable
3130
/**
3231
* Constructor.
3332
*
34-
* @param project The project.
33+
* @param project The project.
3534
*/
3635
public Configuration(Project project)
3736
{
@@ -47,6 +46,7 @@ public Configuration(Project project)
4746
*
4847
* @return JPanel
4948
*/
49+
@Override
5050
public JComponent createComponent()
5151
{
5252
this.reset();
@@ -81,8 +81,9 @@ public JComponent createComponent()
8181
/**
8282
* {@inheritDoc}
8383
*
84-
* This determines if the 'apply' button should be disabled.
84+
* Determines if the 'apply' button should be disabled.
8585
*/
86+
@Override
8687
public boolean isModified()
8788
{
8889
return !Comparing.equal(this.copyToClipboardCheckBox.isSelected(), this.settings.getCopyToClipboard()) ||
@@ -95,6 +96,7 @@ public boolean isModified()
9596
*
9697
* Saves the changes.
9798
*/
99+
@Override
98100
public void apply() throws ConfigurationException
99101
{
100102
this.settings.setCopyToClipboard(this.copyToClipboardCheckBox.isSelected());

src/uk/co/ben_gibson/repositorymapper/Settings/Settings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public RepositoryProvider getRepositoryProvider()
7373
/**
7474
* Set the repository provider.
7575
*
76-
* @param repositoryProvider The repository provider.
76+
* @param repositoryProvider The repository provider.
7777
*/
7878
public void setRepositoryProvider(@NotNull RepositoryProvider repositoryProvider)
7979
{

0 commit comments

Comments
 (0)