Compare commits

..

23 commits
v0.1.0 ... main

Author SHA1 Message Date
a8afb17e70
Merge pull request #11 from 5sw/dependabot/bundler/rexml-3.3.9
Bump rexml from 3.3.6 to 3.3.9
2024-12-01 12:49:36 +01:00
dependabot[bot]
04c2430096
Bump rexml from 3.3.6 to 3.3.9
Bumps [rexml](https://github.com/ruby/rexml) from 3.3.6 to 3.3.9.
- [Release notes](https://github.com/ruby/rexml/releases)
- [Changelog](https://github.com/ruby/rexml/blob/master/NEWS.md)
- [Commits](https://github.com/ruby/rexml/compare/v3.3.6...v3.3.9)

---
updated-dependencies:
- dependency-name: rexml
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-10-28 18:52:26 +00:00
8bfdecec21
Merge pull request #10 from 5sw/dependabot/bundler/rexml-3.3.6
Bump rexml from 3.3.4 to 3.3.6
2024-09-23 20:10:04 +02:00
dependabot[bot]
0af937edac
Bump rexml from 3.3.4 to 3.3.6
Bumps [rexml](https://github.com/ruby/rexml) from 3.3.4 to 3.3.6.
- [Release notes](https://github.com/ruby/rexml/releases)
- [Changelog](https://github.com/ruby/rexml/blob/master/NEWS.md)
- [Commits](https://github.com/ruby/rexml/compare/v3.3.4...v3.3.6)

---
updated-dependencies:
- dependency-name: rexml
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-08-22 20:55:01 +00:00
e894f65f30 Remove rubocop-rspec for now 2024-08-03 19:43:09 +02:00
567198c182 add dev gems to Gemfile instead of gemspec 2024-08-03 19:28:01 +02:00
604fe35be6 autocorrect 2024-08-03 19:23:03 +02:00
120bb4b79a Update dependencies 2024-08-03 19:21:16 +02:00
dependabot[bot]
8fa719df7a
Bump rexml from 3.3.0 to 3.3.3
Bumps [rexml](https://github.com/ruby/rexml) from 3.3.0 to 3.3.3.
- [Release notes](https://github.com/ruby/rexml/releases)
- [Changelog](https://github.com/ruby/rexml/blob/master/NEWS.md)
- [Commits](https://github.com/ruby/rexml/compare/v3.3.0...v3.3.3)

---
updated-dependencies:
- dependency-name: rexml
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-08-02 16:42:11 +00:00
24f5c56ce7
Merge pull request #8 from 5sw/dependabot/bundler/rexml-3.2.8
Bump rexml from 3.2.5 to 3.2.8
2024-06-16 17:56:47 +02:00
aa72af8c5a
Merge branch 'main' into dependabot/bundler/rexml-3.2.8 2024-06-16 17:54:52 +02:00
40fe55854a Update dependencies 2024-06-16 17:51:45 +02:00
dependabot[bot]
672ad20150
Bump rexml from 3.2.5 to 3.2.8
Bumps [rexml](https://github.com/ruby/rexml) from 3.2.5 to 3.2.8.
- [Release notes](https://github.com/ruby/rexml/releases)
- [Changelog](https://github.com/ruby/rexml/blob/master/NEWS.md)
- [Commits](https://github.com/ruby/rexml/compare/v3.2.5...v3.2.8)

---
updated-dependencies:
- dependency-name: rexml
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-05-16 19:55:42 +00:00
d092c3650f
Remove schedule from Rubocop workflow 2022-08-07 11:18:22 +02:00
6171a51720 Add explanation why Ruby 2022-06-07 10:34:12 +02:00
bfc0eb7e45 Bump version 2022-06-07 10:16:36 +02:00
f5ce138392 [#5] Load version file 2022-06-07 10:15:40 +02:00
32dc629db3 Update readme file.
Content stolen from the original git-format-staged repo
2022-06-07 10:14:43 +02:00
496cb08324 Bump version 2022-06-07 09:48:45 +02:00
477cc57f80 Pin commit for action-junit-report 2022-06-07 09:33:41 +02:00
667931bfc2 Strings for ruby versions 2022-06-07 09:31:58 +02:00
2dd04cac7b Run tests with ruby 2.7 and 3.0 2022-06-07 09:29:46 +02:00
7254cc338c Allow ruby >= 2.7 2022-06-07 09:25:41 +02:00
12 changed files with 166 additions and 49 deletions

View file

@ -11,6 +11,10 @@ jobs:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
ruby:
- '2.7'
- '3.0'
steps:
- name: Checkout repository
@ -19,7 +23,7 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@f20f1eae726df008313d2e0d78c5e602562a1bcf
with:
ruby-version: 2.7
ruby-version: ${{ matrix.ruby }}
- name: Install dependencies
run: bundle install --with=ci
@ -28,7 +32,9 @@ jobs:
run: bundle exec rake spec_github
- name: Publish Test Report
uses: mikepenz/action-junit-report@v3
uses: mikepenz/action-junit-report@41a3188dde10229782fd78cd72fc574884dd7686
if: always() # always run even if the previous step fails
with:
report_paths: 'rspec.xml'
report_paths: rspec.xml
check_name: Rspec for Ruby ${{ matrix.ruby }}
fail_on_failure: true

View file

@ -12,8 +12,6 @@ on:
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
schedule:
- cron: '20 4 * * 5'
jobs:
rubocop:

View file

@ -1,3 +1,6 @@
require:
- rubocop-rake
AllCops:
NewCops: enable
TargetRubyVersion: 2.7

View file

@ -8,3 +8,9 @@ group :ci do
gem 'code-scanning-rubocop'
gem 'rspec_junit_formatter'
end
gem 'rake', '~> 13.0'
gem 'rspec'
gem 'rubocop', '~> 1.29'
gem 'rubocop-rake', '~> 0.6'

View file

@ -1,7 +1,7 @@
PATH
remote: .
specs:
format-staged (0.1.0)
format-staged (0.1.2)
colorize
GEM
@ -10,47 +10,51 @@ GEM
ast (2.4.2)
code-scanning-rubocop (0.6.1)
rubocop (~> 1.0)
colorize (0.8.1)
diff-lcs (1.5.0)
parallel (1.22.1)
parser (3.1.2.0)
colorize (1.1.0)
diff-lcs (1.5.1)
json (2.7.2)
language_server-protocol (3.17.0.3)
parallel (1.25.1)
parser (3.3.4.0)
ast (~> 2.4.1)
racc
racc (1.8.1)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.4.0)
rexml (3.2.5)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
rake (13.2.1)
regexp_parser (2.9.2)
rexml (3.3.9)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-support (3.11.0)
rspec_junit_formatter (0.5.1)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
rspec_junit_formatter (0.6.0)
rspec-core (>= 2, < 4, != 2.12.0)
rubocop (1.29.1)
rubocop (1.65.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.1.0.0)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
regexp_parser (>= 2.4, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.17.0, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.18.0)
parser (>= 3.1.1.0)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.3)
parser (>= 3.3.1.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
rubocop-rspec (2.11.1)
rubocop (~> 1.19)
ruby-progressbar (1.11.0)
unicode-display_width (2.1.0)
ruby-progressbar (1.13.0)
unicode-display_width (2.5.0)
PLATFORMS
ruby
@ -63,7 +67,6 @@ DEPENDENCIES
rspec_junit_formatter
rubocop (~> 1.29)
rubocop-rake (~> 0.6)
rubocop-rspec
BUNDLED WITH
2.1.4

108
README.md
View file

@ -1,3 +1,109 @@
# git-format-staged
Port of [hallettj/git-format-staged](https://github.com/hallettj/git-format-staged) to Ruby.
Port of [hallettj/git-format-staged](https://github.com/hallettj/git-format-staged)
to Ruby.
Consider a project where you want all code formatted consistently. So you use
a formatter and/or linter. (For example [SwiftFormat][]) You want to make sure
that everyone working on the project runs the formatter, add a git pre-commit
hook to run it. The naive way to write that hook would be to:
- get a list of staged files
- run the formatter on those files
- run `git add` to stage the results of formatting
The problem with that solution is it forces you to commit entire files. At
worst this will lead to contributors to unwittingly committing changes. At
best it disrupts workflow for contributors who use `git add -p`.
git-format-staged tackles this problem by running the formatter on the staged
version of the file. Staging changes to a file actually produces a new file
that exists in the git object database. git-format-staged uses some git
plumbing commands to send content from that file to your formatter. The command
replaces file content in the git index. The process bypasses the working tree,
so any unstaged changes are ignored by the formatter, and remain unstaged.
After formatting a staged file git-format-staged computes a patch which it
attempts to apply to the working tree file to keep the working tree in sync
with staged changes. If patching fails you will see a warning message. The
version of the file that is committed will be formatted properly - the warning
just means that working tree copy of the file has been left unformatted. The
patch step can be disabled with the `--no-update-working-tree` option.
[SwiftFormat]: https://github.com/nicklockwood/SwiftFormat
## How to install
Requires Ruby 2.7 or newer. Tests run on 2.7 and 3.0.
Install as a development dependency in a project that uses bundle to manage
Ruby dependencies:
$ bundle add format-staged
Or install globally:
$ gem install format-staged
## How to use
For detailed information run:
$ [bundle exec] git-format-staged --help
The command expects a shell command to run a formatter, and one or more file
patterns to identify which files should be formatted. For example:
$ git-format-staged --formatter 'prettier --stdin-filepath "{}"' '*.js'
That will format all `.js` files using `prettier`.
The formatter command must read file content from `stdin`, and output formatted
content to `stdout`.
Patterns are evaluated from left-to-right: if a file matches multiple patterns
the right-most pattern determines whether the file is included or excluded.
git-format-staged never operates on files that are excluded from version
control. So it is not necessary to explicitly exclude stuff like
`vendor/`.
The formatter command may include a placeholder, `{}`, which will be replaced
with the path of the file that is being formatted. This is useful if your
formatter needs to know the file extension to determine how to format or to
lint each file. For example:
$ git-format-staged -f 'prettier --stdin-filepath "{}"' '*.js' '*.css'
Do not attempt to read or write to `{}` in your formatter command! The
placeholder exists only for referencing the file name and path.
### Check staged changes with a linter without formatting
Perhaps you do not want to reformat files automatically; but you do want to
prevent files from being committed if they do not conform to style rules. You
can use git-format-staged with the `--no-write` option, and supply a lint
command instead of a format command. Here is an example using ESLint:
$ git-format-staged --no-write -f 'eslint --stdin --stdin-filename "{}" >&2' 'src/*.js'
If this command is run in a pre-commit hook, and the lint command fails the
commit will be aborted and error messages will be displayed. The lint command
must read file content via `stdin`. Anything that the lint command outputs to
`stdout` will be ignored. In the example above `eslint` is given the `--stdin`
option to tell it to read content from `stdin` instead of reading files from
disk, and messages from `eslint` are redirected to `stderr` (using the `>&2`
notation) so that you can see them.
### Why the Ruby port if there already is a fine Python implementation?
I dont like Python ;)
But jokes aside, I am already setting up a Ruby environment (using [rbenv][]) for my
projects to run [cocoapods][] and [fastlane][] and our git hooks. By using this port
we dont need to ensure to have python available as well.
[rbenv]: https://github.com/rbenv/rbenv/
[cocoapods]: https://cocoapods.org/
[fastlane]: https://fastlane.tools/

View file

@ -17,7 +17,7 @@ parser = OptionParser.new do |opt|
<<~DOC
Shell command to format files, will run once per file. Occurrences of the placeholder `{}` will \
be replaced with a path to the file being formatted. \
(Example: "prettier --stdin-filepath \'{}\'")
(Example: "prettier --stdin-filepath '{}'")
DOC
) do |o|
parameters[:formatter] = o

View file

@ -15,16 +15,10 @@ Gem::Specification.new do |s|
s.executables << 'git-format-staged'
s.homepage = 'https://github.com/5sw/format-staged'
s.license = 'MIT'
s.required_ruby_version = '~> 2.7'
s.required_ruby_version = '>= 2.7'
s.add_dependency 'colorize'
s.add_development_dependency 'rake', '~> 13.0'
s.add_development_dependency 'rspec'
s.add_development_dependency 'rubocop', '~> 1.29'
s.add_development_dependency 'rubocop-rake', '~> 0.6'
s.add_development_dependency 'rubocop-rspec'
s.metadata = {
'rubygems_mfa_required' => 'true'
}

View file

@ -39,7 +39,7 @@ module FormatStaged
return false unless formatted.size == files.size
quiet = @verbose ? [] : ['--quiet']
return !write || get_status('git', 'diff-index', '--cached', '--exit-code', *quiet, 'HEAD') != 0
!write || get_status('git', 'diff-index', '--cached', '--exit-code', *quiet, 'HEAD') != 0
end
def repo_root

View file

@ -1,5 +1,5 @@
# frozen_string_literal: true
module FormatStaged
VERSION = '0.1.0'
VERSION = '0.1.2'
end

View file

@ -1,6 +1,7 @@
# frozen_string_literal: true
require_relative 'format-staged/job'
require_relative 'format-staged/version'
##
# FormatStaged module

View file

@ -132,7 +132,7 @@ describe FormatStaged do
repo.set_content 'test.test', 'a=b'
repo.stage 'test.test'
expect(repo.run_formatter write: false).to be_truthy
expect(repo.run_formatter(write: false)).to be_truthy
expect(repo.get_content('test.test')).to eq 'a=b'
expect(repo.get_staged('test.test')).to eq 'a=b'
end