Skip to content
This repository has been archived by the owner on Jan 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from jamesdbaker/bare_repository_fix
Browse files Browse the repository at this point in the history
Bare Repository Fix
  • Loading branch information
jonnyelliot authored Apr 16, 2020
2 parents 5b2f12f + 296410f commit 5209fde
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 43 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Additionally the `install-hooks` goal may be used to install a pre-commit Git ho
<plugin>
<groupId>io.committed</groupId>
<artifactId>speedy-spotless-maven-plugin</artifactId>
<version>0.0.2</version>
<version>0.1.1</version>
<executions>
<execution>
<id>install-formatter-hook</id>
Expand Down
17 changes: 11 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.committed</groupId>
<artifactId>speedy-spotless-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>0.1.0</version>
<version>0.1.1</version>
<name>speedy-spotless-maven-plugin</name>
<description>For easy formatting of staged changes.</description>
<url>http://github.com/commitd/speedy-spotless</url>
Expand Down Expand Up @@ -71,7 +71,7 @@
<plugin>
<groupId>io.committed</groupId>
<artifactId>speedy-spotless-maven-plugin</artifactId>
<version>0.0.2</version>
<version>0.1.0</version>
<executions>
<execution>
<?m2e ignore?>
Expand All @@ -97,12 +97,17 @@
<dependency>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>1.26.1</version>
<version>1.30.0</version>
</dependency>
<dependency>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-lib</artifactId>
<version>1.28.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>5.4.3.201909031940-r</version>
<version>5.7.0.202003110725-r</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
Expand Down
51 changes: 17 additions & 34 deletions src/main/java/io/committed/speedy/format/StagedMojo.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package io.committed.speedy.format;

import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;

import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.maven.SpotlessApplyMojo;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
import org.eclipse.jgit.treewalk.filter.TreeFilter;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;

@Mojo(name = "staged", threadSafe = true)
public class StagedMojo extends SpotlessApplyMojo {

Expand All @@ -34,11 +34,11 @@ protected void process(List<File> files, Formatter formatter) throws MojoExecuti
if (files.isEmpty()) {
return;
}
Repository repository = getRepo();

Path workTreePath = repository.getWorkTree().toPath();
try (Git git = Git.open(new File("."))) {

try (Git git = new Git(repository)) {
Repository repository = git.getRepository();
Path workTreePath = repository.getWorkTree().toPath();

TreeFilter treeFilter =
PathFilterGroup.createFromStrings(
Expand All @@ -65,7 +65,7 @@ protected void process(List<File> files, Formatter formatter) throws MojoExecuti

List<File> stagedFiles =
stagedChangedFiles.stream()
.map(filePath -> gitBaseDir().resolve(filePath).toFile())
.map(filePath -> repository.getDirectory().getParentFile().toPath().resolve(filePath).toFile())
.collect(toList());
super.process(stagedFiles, formatter);
getLog().info("Formatted " + stagedFiles.size() + " staged files");
Expand All @@ -76,6 +76,8 @@ protected void process(List<File> files, Formatter formatter) throws MojoExecuti
if (!partiallyStagedFiles.isEmpty()) {
throwPartialUnstaged(partiallyStagedFiles);
}
}catch (IOException e){
throw new MojoExecutionException("Could not open Git repository", e);
}
}

Expand Down Expand Up @@ -116,23 +118,4 @@ private List<String> getChangedFiles(Git git, boolean staged, TreeFilter pathFil
throw new MojoExecutionException("Failed to list changed files", e);
}
}

protected Repository getRepo() {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository;
try {
repository =
builder
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build();
} catch (IOException e) {
throw new RuntimeException("Failed to find Git repository", e);
}
return repository;
}

protected final Path gitBaseDir() {
return getRepo().getDirectory().getParentFile().toPath();
}
}
4 changes: 2 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ echo '<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w
<plugin>
<groupId>io.committed</groupId>
<artifactId>speedy-spotless-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.1.1</version>
<configuration>
<java>
<googleJavaFormat>
Expand All @@ -53,7 +53,7 @@ git commit -m initial
sed -i "" 's:\/\/ modline1:String s="";:g' "src/main/java/io/committed/speedy/Example.java"
git add .

mvn -X io.committed:speedy-spotless-maven-plugin:0.0.1-SNAPSHOT:staged
mvn -X io.committed:speedy-spotless-maven-plugin:0.1.1:staged
cat src/main/java/io/committed/speedy/Example.java
cat pom.xml

Expand Down

0 comments on commit 5209fde

Please sign in to comment.