Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CommandRunException extends RuntimeException {
public CommandRunException(int exitCode, String output, String... command) {
super(
String.format(
"'%s' failed with code %s: \n\n %s",
"'%s' failed with code %s: %n%n %s",
StringUtils.join(command, StringUtils.SPACE), exitCode, output));
this.exitCode = exitCode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;

import com.cosium.code.format.MavenGitCodeFormatException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -47,7 +48,7 @@ public long getSize() {
}
cachedSize = countingInputStream.getByteCount();
} catch (IOException e) {
throw new RuntimeException(e);
throw new MavenGitCodeFormatException(e);
}
return cachedSize;
}
Expand All @@ -73,7 +74,7 @@ private byte[] convertBytes(byte[] bytes) {
return IOUtils.toByteArray(
EolStreamTypeUtil.wrapInputStream(new ByteArrayInputStream(bytes), eolStreamType));
} catch (IOException e) {
throw new RuntimeException(e);
throw new MavenGitCodeFormatException(e);
}
}

Expand Down
74 changes: 38 additions & 36 deletions core/src/main/java/com/cosium/code/format/git/GitIndexEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,48 +132,50 @@ private void logObjectContent(ObjectLoader objectLoader, String virtualName)
}

private LineRanges computeLineRanges(DirCacheEntry dirCacheEntry) {
Git git = new Git(repository);
boolean partiallyStaged;
try {
partiallyStaged =
!git.status().addPath(dirCacheEntry.getPathString()).call().getModified().isEmpty();
} catch (GitAPIException e) {
throw new MavenGitCodeFormatException(e);
}
try (Git git = new Git(repository)) {
boolean partiallyStaged;
try {
partiallyStaged =
!git.status().addPath(dirCacheEntry.getPathString()).call().getModified().isEmpty();
} catch (GitAPIException e) {
throw new MavenGitCodeFormatException(e);
}

if (!partiallyStaged) {
return LineRanges.all();
}
if (!partiallyStaged) {
return LineRanges.all();
}

try {
return git
.diff()
.setPathFilter(PathFilter.create(dirCacheEntry.getPathString()))
.setCached(true)
.call()
.stream()
.map(this::computeLineRanges)
.reduce(LineRanges::concat)
.orElse(LineRanges.all());

} catch (GitAPIException e) {
throw new MavenGitCodeFormatException(e);
try {
return git
.diff()
.setPathFilter(PathFilter.create(dirCacheEntry.getPathString()))
.setCached(true)
.call()
.stream()
.map(this::computeLineRanges)
.reduce(LineRanges::concat)
.orElse(LineRanges.all());

} catch (GitAPIException e) {
throw new MavenGitCodeFormatException(e);
}
}
}

private LineRanges computeLineRanges(DiffEntry diffEntry) {
DiffFormatter diffFormatter = new DiffFormatter(NullOutputStream.INSTANCE);
diffFormatter.setRepository(repository);

try {
FileHeader fileHeader = diffFormatter.toFileHeader(diffEntry);
return fileHeader.getHunks().stream()
.map(HunkHeader.class::cast)
.map(this::computeLineRanges)
.reduce(LineRanges::concat)
.orElse(LineRanges.all());
} catch (IOException e) {
throw new MavenGitCodeFormatException(e);
try (DiffFormatter diffFormatter = new DiffFormatter(NullOutputStream.INSTANCE)) {
diffFormatter.setRepository(repository);

try {
FileHeader fileHeader = diffFormatter.toFileHeader(diffEntry);
return fileHeader.getHunks().stream()
.map(HunkHeader.class::cast)
.map(this::computeLineRanges)
.reduce(LineRanges::concat)
.orElse(LineRanges.all());
} catch (IOException e) {
throw new MavenGitCodeFormatException(e);
}
}
}

Expand Down
26 changes: 14 additions & 12 deletions core/src/main/java/com/cosium/code/format/git/GitStagedFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,21 @@ private GitStagedFiles(Log log, Repository repository, Set<String> filePaths) {

public static GitStagedFiles read(Log log, Repository repository, Predicate<Path> fileFilter)
throws GitAPIException {
Status gitStatus = new Git(repository).status().call();
Path workTree = repository.getWorkTree().toPath();
Set<String> filePaths =
Stream.concat(gitStatus.getChanged().stream(), gitStatus.getAdded().stream())
.filter(relativePath -> fileFilter.test(workTree.resolve(relativePath)))
.collect(Collectors.toSet());
log.debug("Staged files: " + filePaths.toString());
return new GitStagedFiles(log, repository, filePaths);
try (Git git = new Git(repository)) {
Status gitStatus = git.status().call();
Path workTree = repository.getWorkTree().toPath();
Set<String> filePaths =
Stream.concat(gitStatus.getChanged().stream(), gitStatus.getAdded().stream())
.filter(relativePath -> fileFilter.test(workTree.resolve(relativePath)))
.collect(Collectors.toSet());
log.debug("Staged files: " + filePaths.toString());
return new GitStagedFiles(log, repository, filePaths);
}
}

public void format(CodeFormatters formatters) throws IOException {
Git git = new Git(repository);

try (Index index = Index.lock(repository);
try (Git git = new Git(repository);
Index index = Index.lock(repository);
TemporaryFile temporaryDiffFile =
TemporaryFile.create(log, "diff-between-unformatted-and-formatted-files")) {
DirCacheEditor dirCacheEditor = index.editor();
Expand All @@ -80,8 +81,9 @@ public void format(CodeFormatters formatters) throws IOException {

try (Repository autoCRLFRepository =
new AutoCRLFRepository(git.getRepository().getDirectory(), eolStreamType);
Git gitForAutoCRLFRepository = new Git(autoCRLFRepository);
OutputStream diffOutput = temporaryDiffFile.newOutputStream()) {
new Git(autoCRLFRepository)
gitForAutoCRLFRepository
.diff()
.setOutputStream(diffOutput)
.setOldTree(treeIterator(repository.readDirCache()))
Expand Down
8 changes: 4 additions & 4 deletions core/src/test/java/com/cosium/code/format/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public final void before() throws Exception {
try {
Files.deleteIfExists(path);
} catch (IOException e) {
throw new RuntimeException(e);
throw new MavenGitCodeFormatException(e);
}
});
Files.deleteIfExists(projectDestination);
Expand Down Expand Up @@ -117,9 +117,9 @@ protected Path resolveRelativelyToProjectRoot(String sourceName) {
protected String sha1(String sourceName) {
try (InputStream inputStream =
Files.newInputStream(resolveRelativelyToProjectRoot(sourceName))) {
return DigestUtils.shaHex(inputStream);
return DigestUtils.sha1Hex(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
throw new MavenGitCodeFormatException(e);
}
}

Expand All @@ -131,7 +131,7 @@ protected void assertMatchExpected(String sourceName) {
assertThat(IOUtils.toString(actual, StandardCharsets.UTF_8))
.isEqualTo(IOUtils.toString(expected, StandardCharsets.UTF_8));
} catch (IOException e) {
throw new RuntimeException(e);
throw new MavenGitCodeFormatException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private String fixImports(final String unformattedContent) throws FormatterExcep
formattedContent = RemoveUnusedImports.removeUnusedImports(formattedContent);
}
if (!options.isSkipSortingImports()) {
formattedContent = ImportOrderer.reorderImports(formattedContent);
formattedContent = ImportOrderer.reorderImports(formattedContent, options.javaFormatterOptions().style());
}
return formattedContent;
}
Expand Down