{"id":17041279,"url":"https://github.com/5sw/format-staged","last_synced_at":"2025-04-12T14:33:11.699Z","repository":{"id":39654422,"uuid":"495011068","full_name":"5sw/format-staged","owner":"5sw","description":null,"archived":false,"fork":false,"pushed_at":"2024-12-01T11:49:37.000Z","size":75,"stargazers_count":0,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-25T05:03:32.280Z","etag":null,"topics":["git","git-hooks","hacktoberfest","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/5sw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-22T09:13:23.000Z","updated_at":"2024-12-01T11:49:41.000Z","dependencies_parsed_at":"2024-06-16T17:54:06.434Z","dependency_job_id":null,"html_url":"https://github.com/5sw/format-staged","commit_stats":{"total_commits":65,"total_committers":1,"mean_commits":65.0,"dds":0.0,"last_synced_commit":"d092c3650fc53fbb8c3556822eaa3ee190693def"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5sw%2Fformat-staged","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5sw%2Fformat-staged/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5sw%2Fformat-staged/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5sw%2Fformat-staged/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/5sw","download_url":"https://codeload.github.com/5sw/format-staged/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248581358,"owners_count":21128155,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["git","git-hooks","hacktoberfest","ruby"],"created_at":"2024-10-14T09:11:57.862Z","updated_at":"2025-04-12T14:33:11.673Z","avatar_url":"https://github.com/5sw.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# git-format-staged\n\nPort of [hallettj/git-format-staged](https://github.com/hallettj/git-format-staged) \nto Ruby.\n\nConsider a project where you want all code formatted consistently. So you use\na formatter and/or linter. (For example [SwiftFormat][]) You want to make sure \nthat everyone working on the project runs the formatter, add a git pre-commit\nhook to run it. The naive way to write that hook would be to:\n\n- get a list of staged files\n- run the formatter on those files\n- run `git add` to stage the results of formatting\n\nThe problem with that solution is it forces you to commit entire files. At\nworst this will lead to contributors to unwittingly committing changes. At\nbest it disrupts workflow for contributors who use `git add -p`.\n\ngit-format-staged tackles this problem by running the formatter on the staged\nversion of the file. Staging changes to a file actually produces a new file\nthat exists in the git object database. git-format-staged uses some git\nplumbing commands to send content from that file to your formatter. The command\nreplaces file content in the git index. The process bypasses the working tree,\nso any unstaged changes are ignored by the formatter, and remain unstaged.\n\nAfter formatting a staged file git-format-staged computes a patch which it\nattempts to apply to the working tree file to keep the working tree in sync\nwith staged changes. If patching fails you will see a warning message. The\nversion of the file that is committed will be formatted properly - the warning\njust means that working tree copy of the file has been left unformatted. The\npatch step can be disabled with the `--no-update-working-tree` option.\n\n[SwiftFormat]: https://github.com/nicklockwood/SwiftFormat\n\n## How to install\n\nRequires Ruby 2.7 or newer. Tests run on 2.7 and 3.0.\n\nInstall as a development dependency in a project that uses bundle to manage\nRuby dependencies:\n\n    $ bundle add format-staged\n\nOr install globally:\n\n    $ gem install format-staged\n\n## How to use\n\nFor detailed information run:\n\n    $ [bundle exec] git-format-staged --help\n\nThe command expects a shell command to run a formatter, and one or more file\npatterns to identify which files should be formatted. For example:\n\n    $ git-format-staged --formatter 'prettier --stdin-filepath \"{}\"' '*.js'\n\nThat will format all `.js` files using `prettier`.\n\nThe formatter command must read file content from `stdin`, and output formatted\ncontent to `stdout`.\n\nPatterns are evaluated from left-to-right: if a file matches multiple patterns\nthe right-most pattern determines whether the file is included or excluded.\n\ngit-format-staged never operates on files that are excluded from version\ncontrol. So it is not necessary to explicitly exclude stuff like\n`vendor/`.\n\nThe formatter command may include a placeholder, `{}`, which will be replaced\nwith the path of the file that is being formatted. This is useful if your\nformatter needs to know the file extension to determine how to format or to\nlint each file. For example:\n\n    $ git-format-staged -f 'prettier --stdin-filepath \"{}\"' '*.js' '*.css'\n\nDo not attempt to read or write to `{}` in your formatter command! The\nplaceholder exists only for referencing the file name and path.\n\n### Check staged changes with a linter without formatting\n\nPerhaps you do not want to reformat files automatically; but you do want to\nprevent files from being committed if they do not conform to style rules. You\ncan use git-format-staged with the `--no-write` option, and supply a lint\ncommand instead of a format command. Here is an example using ESLint:\n\n    $ git-format-staged --no-write -f 'eslint --stdin --stdin-filename \"{}\" \u003e\u00262' 'src/*.js'\n\nIf this command is run in a pre-commit hook, and the lint command fails the\ncommit will be aborted and error messages will be displayed. The lint command\nmust read file content via `stdin`. Anything that the lint command outputs to\n`stdout` will be ignored. In the example above `eslint` is given the `--stdin`\noption to tell it to read content from `stdin` instead of reading files from\ndisk, and messages from `eslint` are redirected to `stderr` (using the `\u003e\u00262`\nnotation) so that you can see them.\n\n### Why the Ruby port if there already is a fine Python implementation?\n\nI don’t like Python ;)\n\nBut jokes aside, I am already setting up a Ruby environment (using [rbenv][]) for my \nprojects to run [cocoapods][] and [fastlane][] and our git hooks. By using this port\nwe don’t need to ensure to have python available as well.\n\n\n[rbenv]: https://github.com/rbenv/rbenv/\n[cocoapods]: https://cocoapods.org/\n[fastlane]: https://fastlane.tools/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F5sw%2Fformat-staged","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F5sw%2Fformat-staged","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F5sw%2Fformat-staged/lists"}