diff --git a/CHANGELOG.md b/CHANGELOG.md index b4de37c6..7535644e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Try to detect pull request id automatically, if `PULL_REQUEST_ID` is not specified. Inspired by @willnet/prid. * [#40](https://github.com/mmozuras/pronto/issues/40): Add '--index' option for 'pronto run'. Pronto analyzes changes before committing. * [#50](https://github.com/mmozuras/pronto/pull/50): Adds GitLab formatter +* [#52](https://github.com/mmozuras/pronto/pull/52): Allow specifying a path for 'pronto run'. ### Changes * Github and Github pull request formatters now filter out duplicate offenses on the same line to avoid spamming with redundant comments. diff --git a/lib/pronto.rb b/lib/pronto.rb index c7f37f62..d2b921a3 100644 --- a/lib/pronto.rb +++ b/lib/pronto.rb @@ -23,15 +23,16 @@ require 'pronto/formatter/formatter' module Pronto - def self.run(commit = 'master', repo_path = '.', formatter = nil) + def self.run(commit = 'master', repo_path = '.', + formatter = Formatter::TextFormatter.new, file = nil) commit ||= 'master' repo = Git::Repository.new(repo_path) - patches = repo.diff(commit) + options = { paths: [file] } if file + patches = repo.diff(commit, options) result = run_all_runners(patches) - formatter ||= default_formatter puts formatter.format(result, repo) result @@ -59,8 +60,4 @@ def self.run_all_runners(patches) runner.new.run(patches, patches.commit) end.flatten.compact end - - def default_formatter - Formatter::TextFormatter.new - end end diff --git a/lib/pronto/cli.rb b/lib/pronto/cli.rb index 135f4f43..5d092340 100644 --- a/lib/pronto/cli.rb +++ b/lib/pronto/cli.rb @@ -41,7 +41,7 @@ def is_thor_reserved_word?(word, type) aliases: '-f', banner: "Pick output formatter. Available: #{::Pronto::Formatter.names.join(', ')}" - def run + def run(path = nil) gem_names = options[:runner].any? ? options[:runner] : ::Pronto.gem_names gem_names.each do |gem_name| require "pronto/#{gem_name}" @@ -49,7 +49,7 @@ def run formatter = ::Pronto::Formatter.get(options[:formatter]) commit = options[:index] ? :index : options[:commit] - messages = ::Pronto.run(commit, '.', formatter) + messages = ::Pronto.run(commit, '.', formatter, path) exit(messages.count) if options[:'exit-code'] rescue Rugged::RepositoryError puts '"pronto" should be run from a git repository' diff --git a/lib/pronto/git/repository.rb b/lib/pronto/git/repository.rb index 1bdc5666..618e6627 100644 --- a/lib/pronto/git/repository.rb +++ b/lib/pronto/git/repository.rb @@ -7,13 +7,13 @@ def initialize(path) @repo = Rugged::Repository.new(path) end - def diff(commit) + def diff(commit, options = nil) if commit == :index - patches = @repo.index.diff + patches = @repo.index.diff(options) Patches.new(self, head, patches) else merge_base = merge_base(commit) - patches = @repo.diff(merge_base, head) + patches = @repo.diff(merge_base, head, options) Patches.new(self, merge_base, patches) end end