Skip to content

Commit

Permalink
Release 2.1 fix bug with .git folder not in project folder.
Browse files Browse the repository at this point in the history
  • Loading branch information
jawspeak committed Nov 30, 2015
1 parent 2a4ddf8 commit eeab771
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
7 changes: 4 additions & 3 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<idea-plugin version="2">
<id>com.squareup.intellij.plugin.copy-as-github-path</id>
<name>Copy &amp; Browse as Github or Atlassian Stash Path</name>
<version>2.0</version>
<vendor email="[email protected]" url="http://www.squareup.com">Square, Inc</vendor>
<version>2.1</version>
<vendor url="https://github.com/jawspeak/intellij-plugin-copy-and-open-github-url">Jonathan
Wolter
</vendor>

<description><![CDATA[
For projects with git repos in the base, it allows you to copy the current file's path on
Expand All @@ -20,6 +22,7 @@
1.0 Initial release.<br>
1.1 Fix a bug where the line numbers were incorrect if the file had any line folding in it.<br>
2.0 Rename to reflect added support for Stash repos with a second menu option that always pops up.
2.1 Fix bug if the project folder is not the root .git folder.
]]>
</change-notes>

Expand Down
5 changes: 2 additions & 3 deletions copy-as-github-path-plugin.iml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="jdk" jdkName="IntelliJ IDEA Community Edition IC-141.2735.5" jdkType="IDEA JDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
Expand All @@ -20,5 +20,4 @@
</library>
</orderEntry>
</component>
</module>

</module>
13 changes: 5 additions & 8 deletions src/com/squareup/intellij/helper/ActionPerformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.ide.CopyPasteManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;
Expand Down Expand Up @@ -36,22 +35,20 @@ public ActionPerformer(GitRepo repo) {
* 1st).
*/
public void actionPerformed(AnActionEvent event) {
Project project = event.getData(PlatformDataKeys.PROJECT);
final Editor editor = event.getData(PlatformDataKeys.EDITOR);
final VirtualFile file = event.getData(PlatformDataKeys.VIRTUAL_FILE);
Integer line = (editor != null)
// convert the VisualPosition to the LogicalPosition to have the correct line number.
// http://grepcode.com/file/repository.grepcode.com/java/ext/com.jetbrains/intellij-idea/10.0/com/intellij/openapi/editor/LogicalPosition.java#LogicalPosition
? editor.visualToLogicalPosition(editor.getSelectionModel().getSelectionStartPosition()).line + 1 : null;
String url = copyUrl(project, file, line);
? editor.visualToLogicalPosition(
editor.getSelectionModel().getSelectionStartPosition()).line + 1 : null;
String url = copyUrl(file, line);
openBrowser(url);
showStatusBubble(event, file);
}

private String copyUrl(Project project, VirtualFile file, Integer line) {
String basePath = project.getBasePath();
String relativeFilePath = file.getCanonicalPath().replaceFirst(basePath, "");
String url = repo.repoUrlFor(relativeFilePath, line);
private String copyUrl(VirtualFile file, Integer line) {
String url = repo.repoUrlFor(file.getCanonicalPath(), line);
CopyPasteManager.getInstance().setContents(new StringSelection(url));
return url;
}
Expand Down
25 changes: 22 additions & 3 deletions src/com/squareup/intellij/helper/GitRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.annotations.VisibleForTesting;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
Expand All @@ -23,7 +24,8 @@ public GitRepo(String projectRoot) {

@VisibleForTesting
public GitRepo(String projectRoot, String gitconfig) {
gitConfigFile = new File(projectRoot, gitconfig);
String gitRoot = findDotGitFolder(new File(projectRoot));
gitConfigFile = new File(gitRoot, gitconfig);
}

public abstract String brand();
Expand All @@ -37,8 +39,25 @@ public String repoUrlFor(String relativeFilePath) {
return repoUrlFor(relativeFilePath, null);
}

public String repoUrlFor(String relativeFilePath, Integer line) {
return gitBaseUrl() + relativeFilePath + (line != null ? buildLineDomainPrefix() + line : "");
public String repoUrlFor(String filePath, Integer line) {
filePath = filePath.replaceFirst(gitConfigFile.getParentFile().getParent(), "");
return gitBaseUrl() + filePath + (line != null ? buildLineDomainPrefix() + line : "");
}

String findDotGitFolder(File absolutePath) {
if (absolutePath.getParent() == null) {
throw new RuntimeException(
"Could not find parent .git/ folder. Maybe path is not in a git repo? " + absolutePath);
}
FileFilter gitFolderFinder = new FileFilter() {
@Override public boolean accept(File pathname) {
return pathname.getName().equals(".git");
}
};
if (absolutePath.listFiles(gitFolderFinder).length == 1) {
return absolutePath.getAbsolutePath();
}
return findDotGitFolder(absolutePath.getParentFile());
}

private String gitBaseUrl() {
Expand Down

0 comments on commit eeab771

Please sign in to comment.