Skip to content

Commit

Permalink
Merge pull request #36 from retronym/topic/ignorance-is-bliss
Browse files Browse the repository at this point in the history
Support ignoring file paths
  • Loading branch information
retronym authored Jun 3, 2019
2 parents 80a2dd3 + 9d90873 commit 1a8af2e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,21 @@ Installing
}
% jardiff -h
usage: jardiff [-c] [-g <dir>] [-h] [-q] [-U <n>] VERSION1 [VERSION2 ...]
usage: jardiff [-c] [-g <dir>] [-h] [-i <arg>] [-p] [-q] [-r] [-U <n>] VERSION1 [VERSION2 ...]
Each VERSION may designate a single file, a directory, JAR file or a
`:`-delimited classpath
-c,--suppress-code Suppress method bodies
-g,--git <dir> Directory to output a git repository containing the diff
-h,--help Display this message
-q,--quiet Don't output diffs to standard out
-r,--raw Disable sorting and filtering of classfile contents
-U,--unified <n> Number of context lines in diff
-c,--suppress-code Suppress method bodies
-g,--git <dir> Directory to output a git repository containing the
diff
-h,--help Display this message
-i,--ignore <arg> File pattern to ignore rendered files in gitignore
format
-p,--suppress-privates Display only non-private members
-q,--quiet Don't output diffs to standard out
-r,--raw Disable sorting and filtering of classfile contents
-U,--unified <n> Number of context lines in diff
% jardiff dir1 dir2
Expand Down
3 changes: 0 additions & 3 deletions core/src/main/scala/scala/tools/jardiff/IOUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,4 @@ object IOUtil {
}
})
}



}
16 changes: 12 additions & 4 deletions core/src/main/scala/scala/tools/jardiff/JarDiff.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ import org.eclipse.jgit.diff.DiffFormatter
import org.eclipse.jgit.revwalk.RevCommit

import scala.tools.jardiff.JGitUtil._
import scala.collection.JavaConverters._

final class JarDiff(files: List[List[Path]], config: JarDiff.Config, renderers: String => List[FileRenderer]) {
private val targetBase = config.gitRepo.getOrElse(Files.createTempDirectory("jardiff-"))

def diff(): Boolean = {
var differenceFound = false
import org.eclipse.jgit.api.Git
val git: Git =
Git.init.setDirectory(targetBase.toFile).call
val git: Git = {
Git.init.setDirectory(targetBase.toFile).call()
}

val excluded = targetBase.resolve(".git").resolve("info").resolve("exclude")
Files.createDirectories(excluded.getParent)
Files.write(excluded, config.ignore.asJava)

def renderAndCommit(fs: List[Path]): RevCommit = {
IOUtil.deleteRecursive(targetBase)
Expand All @@ -33,7 +39,9 @@ final class JarDiff(files: List[List[Path]], config: JarDiff.Config, renderers:
else
renderFile(root, targetBase.resolve(f.getFileName))
}

val status = git.status().call()
val ignored = status.getIgnoredNotInIndex
ignored.forEach(p => IOUtil.deleteRecursive(targetBase.resolve(p)))
git.add().addFilepattern(".").call()
git.commit().setAllowEmpty(true).setAll(true).setMessage("jardiff textified output of: " + fs.mkString(File.pathSeparator)).call()
}
Expand Down Expand Up @@ -112,6 +120,6 @@ object JarDiff {
new JarDiff(files, config, renderers)
}

case class Config(gitRepo: Option[Path], code: Boolean, raw: Boolean, privates: Boolean, contextLines: Option[Int], diffOutputStream: OutputStream)
case class Config(gitRepo: Option[Path], code: Boolean, raw: Boolean, privates: Boolean, contextLines: Option[Int], diffOutputStream: OutputStream, ignore: List[String])

}
9 changes: 7 additions & 2 deletions core/src/main/scala/scala/tools/jardiff/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ object Main {
val NoPrivates = new cli.Option("p", "suppress-privates", false, "Display only non-private members")
val ContextLines = new cli.Option("U", "unified", true, "Number of context lines in diff")
val Quiet = new cli.Option("q", "quiet", false, "Don't output diffs to standard out")
val Ignore = new cli.Option("i", "ignore", true, "File pattern to ignore rendered files in gitignore format")
Ignore.setArgs(cli.Option.UNLIMITED_VALUES)
ContextLines.setArgName("n")
def apply(): Options = {
new cli.Options().addOption(Help).addOption(Git).addOption(ContextLines).addOption(NoCode).addOption(Raw).addOption(NoPrivates).addOption(Quiet)
new cli.Options().addOption(Help).addOption(Git).addOption(ContextLines).addOption(NoCode).addOption(Raw).addOption(NoPrivates).addOption(Quiet).addOption(Ignore)
}
}
private implicit class RichCommandLine(val self: CommandLine) {
Expand Down Expand Up @@ -70,7 +72,10 @@ object Main {
} else {
val gitRepo = if (line.has(Opts.Git)) Some(Paths.get(line.get(Opts.Git))) else None
val diffOutputStream = if (line.has(Opts.Quiet)) NullOutputStream.INSTANCE else System.out
val config = JarDiff.Config(gitRepo, !line.has(Opts.NoCode), line.has(Opts.Raw), !line.has(Opts.NoPrivates), line.getOptInt(Opts.ContextLines), diffOutputStream)
val config = JarDiff.Config(gitRepo, !line.has(Opts.NoCode), line.has(Opts.Raw),
!line.has(Opts.NoPrivates), line.getOptInt(Opts.ContextLines), diffOutputStream,
Option(line.getOptionValues(Opts.Ignore.getOpt)).toList.flatten
)
val paths = trailingArgs.asScala.toList.map(JarDiff.expandClassPath)
paths match {
case Nil => ShowUsage(helpText)
Expand Down

0 comments on commit 1a8af2e

Please sign in to comment.