Skip to content

Commit

Permalink
dev: inital version 1 commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Gibson committed Feb 2, 2016
1 parent 5a81ddd commit 9cda48c
Show file tree
Hide file tree
Showing 19 changed files with 982 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea
*.jar
*.class
out
.DS_Store
build/
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Changelog

All Notable changes to command-bus will be documented in this file

## NEXT - YYYY-MM-DD

### Added
- Nothing

### Deprecated
- Nothing

### Fixed
- Nothing

### Removed
- Nothing

### Security
- Nothing
22 changes: 22 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

We accept contributions via Pull Requests on [Github](https://github.com/ben-gibson/remote-repository-mapper).

## Pull Requests

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.

- **Create feature branches** - Don't ask us to pull from your master branch.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.


## Running Tests

**Happy coding**!
53 changes: 53 additions & 0 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<idea-plugin version="2">
<id>uk.co.ben-gibson.remote.repository.mapper</id>
<name>Remote Repository Mapper</name>
<version>1.0</version>
<vendor email="ben.gibson.2011@gmail.com" url="h">https://github.com/ben-gibson/remote-repository-mapper</vendor>

<description><![CDATA[
Opens a local file under git version control in its remote origin repository.
After installing select your remote repository provider in Settings -> Other Settings -> Remote Repository Mapper (Defaults to GitHub).
The current checked out branch is used unless it does not track a remote branch, in which case it defaults to using master.
To use, open a file that is under git version control in the editor and select File->Open in remote repository.
The resulting link can be copied to the clipboard depending on your preference in the settings.
]]></description>

<change-notes><![CDATA[
<ul>
<li>Fixed clipboard preference not persisting</li>
<li>Updated default shortcut</li>
<li>Moved action to file menu</li>
<li>Action appears as disabled when unusable in the current context instead of being hidden</li>
</ul>
]]>
</change-notes>

<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
<idea-version since-build="141.0"/>

<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->

<depends>com.intellij.modules.vcs</depends>
<depends>com.intellij.modules.lang</depends>
<depends>com.intellij.modules.platform</depends>
<depends>Git4Idea</depends>

<extensions defaultExtensionNs="com.intellij">
<applicationService serviceImplementation="uk.co.ben_gibson.repositorymapper.Context.ContextProvider"/>
<applicationService serviceImplementation="uk.co.ben_gibson.repositorymapper.UrlFactory.UrlFactoryProvider"/>
<projectService serviceImplementation="uk.co.ben_gibson.repositorymapper.Settings.Settings"/>
<projectConfigurable instance="uk.co.ben_gibson.repositorymapper.Settings.Configuration"/>
</extensions>

<actions>
<action id="RemoteRepositoryMapper.uk.co.ben_gibson.repositorymapper.OpenContextAction" class="uk.co.ben_gibson.repositorymapper.OpenContextAction" text="Open in remote repository"
description="Open the current context in the remote repository">
<add-to-group group-id="FileMenu" anchor="last"/>
<keyboard-shortcut keymap="$default" first-keystroke="shift ctrl M"/>
</action>
</actions>

</idea-plugin>
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Remote Repository Mapper

A Jetbrains plugin that opens a local file in a remote repository.

## Install

## Usage

After installing select your remote repository provider in Settings -> Other Settings -> Remote Repository Mapper (Defaults to GitHub).
Open a file that is under git version control in the editor and select File->Open in remote repository.
The current checked out branch is used unless it does not track a remote branch, in which case it defaults to using master.
The resulting link can be copied to the clipboard depending on your preference in the settings.

## Change log

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## Credits

## License

Please see [CONTRIBUTING](LICENSE) for details.
14 changes: 14 additions & 0 deletions remote-repository-mapper.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PLUGIN_MODULE" version="4">
<component name="DevKit.ModuleBuildProperties" url="file://$MODULE_DIR$/META-INF/plugin.xml" />
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" scope="PROVIDED" name="git4idea-rt" level="project" />
</component>
</module>
42 changes: 42 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<idea-plugin version="2">
<id>uk.co.ben-gibson.remote.repository.mapper</id>
<name>Remote Repository Mapper</name>
<version>1.0</version>
<vendor email="ben.gibson.2011@gmail.com" url="h">https://github.com/ben-gibson/remote-repository-mapper</vendor>

<description><![CDATA[
Opens a file in a remove repository.
]]></description>

<change-notes><![CDATA[
Nothing to see here.
]]>
</change-notes>

<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
<idea-version since-build="141.0"/>

<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->

<depends>com.intellij.modules.vcs</depends>
<depends>com.intellij.modules.lang</depends>
<depends>com.intellij.modules.platform</depends>
<depends>Git4Idea</depends>

<extensions defaultExtensionNs="com.intellij">
<applicationService serviceImplementation="uk.co.ben_gibson.repositorymapper.Context.ContextProvider"/>
<applicationService serviceImplementation="uk.co.ben_gibson.repositorymapper.UrlFactory.UrlFactoryProvider"/>
<projectService serviceImplementation="uk.co.ben_gibson.repositorymapper.Settings.Settings"/>
<projectConfigurable instance="uk.co.ben_gibson.repositorymapper.Settings.Configuration"/>
</extensions>

<actions>
<action id="RemoteRepositoryMapper.uk.co.ben_gibson.repositorymapper.OpenContextAction" class="uk.co.ben_gibson.repositorymapper.OpenContextAction" text="Open in remote repository"
description="Open the current context in the remote repository">
<add-to-group group-id="ViewMenu" anchor="last"/>
<keyboard-shortcut keymap="$default" first-keystroke="shift meta alt I"/>
</action>
</actions>

</idea-plugin>
90 changes: 90 additions & 0 deletions src/uk/co/ben_gibson/repositorymapper/Context/Context.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package uk.co.ben_gibson.repositorymapper.Context;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.net.URL;

/**
* Represents some context that can be opened in a remote repository.
*/
public class Context
{
private static final String DEFAULT_BRANCH = "master";

@NotNull
private URL remoteHost;
@NotNull
private String path;
@NotNull
private String branch;
@Nullable
private Integer caretLinePosition;


/**
* Constructor.
*
* @param remoteHost The remote host.
* @param path The path of the file we want to view.
* @param branch The branch if we have one.
* @param caretLinePosition The line position of the caret.
*/
public Context(
@NotNull URL remoteHost,
@NotNull String path,
@Nullable String branch,
@Nullable Integer caretLinePosition
)
{
this.remoteHost = remoteHost;
this.path = path;
this.branch = (branch != null) ? branch : DEFAULT_BRANCH;
this.caretLinePosition = caretLinePosition;
}


/**
* Get the path.
*
* @return String
*/
@NotNull
public String getPath()
{
return this.path;
}


/**
* Get the caret line position.
*
* @return Integer
*/
@Nullable
public Integer getCaretLinePosition()
{
return this.caretLinePosition;
}


/**
* Get the remote host.
*
* @return URL
*/
@NotNull
public URL getRemoteHost() {
return remoteHost;
}


/**
* Get the branch.
*
* @return String
*/
@NotNull
public String getBranch() {
return this.branch;
}
}
108 changes: 108 additions & 0 deletions src/uk/co/ben_gibson/repositorymapper/Context/ContextProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package uk.co.ben_gibson.repositorymapper.Context;

import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import git4idea.GitLocalBranch;
import git4idea.GitUtil;
import git4idea.repo.GitRemote;
import git4idea.repo.GitRepository;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.net.MalformedURLException;
import java.net.URL;

/**
* Context Provider.
*/
public class ContextProvider
{

/**
* Provides a context based on the current environment.
*
* @param project The active project.
*
* @return Context
*/
@Nullable
public Context getContext(@NotNull Project project) throws MalformedURLException, ContextProviderException {

Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();

if (editor == null) {
return null;
}

VirtualFile file = FileDocumentManager.getInstance().getFile(editor.getDocument());

if (file == null) {
return null;
}

GitRepository repository = GitUtil.getRepositoryManager(project).getRepositoryForFile(file);

if (repository == null) {
return null;
}

URL remoteHost = this.getRemoteHostFromRepository(repository);

GitLocalBranch branch = null;

if (repository.getCurrentBranch() != null && repository.getCurrentBranch().findTrackedBranch(repository) != null) {
branch = repository.getCurrentBranch();
}

String path = file.getPath().substring(repository.getRoot().getPath().length());
String branchName = branch != null ? branch.getName() : null;

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

return new Context(remoteHost, path, branchName, caretPosition);
}


/**
* Get a clean url from the repositories remote origin.
*
* @return URL
*/
@NotNull
private URL getRemoteHostFromRepository(@NotNull GitRepository repository) throws MalformedURLException, ContextProviderException
{
GitRemote origin = null;

for (GitRemote remote : repository.getRemotes()) {
if (remote.getName().equals("origin")) {
origin = remote;
}
}

if (origin == null) {
throw ContextProviderException.originRemoteNotFound(repository);
}

if (origin.getFirstUrl() == null) {
throw ContextProviderException.urlNotFoundForRemote(origin);
}

String url = StringUtil.trimEnd(origin.getFirstUrl(), ".git");

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

if (url.startsWith("http")) {
return new URL(url);
}

url = StringUtil.replace(url, "git@", "");
url = StringUtil.replace(url, "ssh://", "");

url = "https://" + StringUtil.replace(url, ":", "/");

return new URL(url);
}
}
Loading

0 comments on commit 9cda48c

Please sign in to comment.