-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4df8a27
Showing
16 changed files
with
4,920 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"env", | ||
{ | ||
"useBuiltIns": true, | ||
"targets": { | ||
"node": 6.9 | ||
} | ||
} | ||
], | ||
"stage-0" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# top-most EditorConfig file | ||
root = true | ||
|
||
# Unix-style newlines with a newline ending every file | ||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 2 | ||
|
||
[{*.js,.{eslintrc,editorconfig,babelrc}}] | ||
max_line_length = 80 | ||
|
||
[*.md] | ||
indent_size = unset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dist | ||
node_modules | ||
*.log | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
language: node_js | ||
cache: | ||
directories: | ||
- node_modules | ||
notifications: | ||
email: false | ||
node_js: | ||
- 6 | ||
- 7 | ||
- node | ||
before_script: | ||
- npm prune | ||
after_success: | ||
- npm run build && npm run semantic-release | ||
branches: | ||
except: | ||
- /^v\d+\.\d+\.\d+$/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Place your settings in this file to overwrite default and user settings. | ||
{ | ||
"editor.formatOnSave": true, | ||
"files.exclude": { | ||
"**/.git": true, | ||
"**/.svn": true, | ||
"**/.hg": true, | ||
"**/CVS": true, | ||
"**/.DS_Store": true, | ||
"dist": true, | ||
"coverage": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 Moti Zilberman | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# git-exec-and-restage | ||
|
||
[![npm](https://img.shields.io/npm/v/npm.svg)](https://www.npmjs.com/package/git-exec-and-restage) | ||
[![Travis branch](https://img.shields.io/travis/motiz88/git-exec-and-restage/master.svg)](https://travis-ci.org/motiz88/git-exec-and-restage) | ||
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) | ||
|
||
> Safely amend Git commits after applying auto-fixing tools | ||
`git-exec-and-restage` executes a command for you on a set of files. This | ||
command may modify the files (imagine a linter in auto-fix mode, like `prettier | ||
--write` or `eslint --fix`). If the files were fully staged before the command | ||
ran, the changes will be automatically added to the Git index; if they were | ||
_partially_ changed the Git index will remain untouched. | ||
|
||
## Table of Contents | ||
|
||
- [Install](#install) | ||
- [Usage](#usage) | ||
- [Contribute](#contribute) | ||
- [License](#license) | ||
|
||
## Install | ||
|
||
```sh | ||
npm install git-exec-and-restage | ||
``` | ||
|
||
## Usage | ||
|
||
Manually e.g. with `prettier`: | ||
|
||
```sh | ||
git-exec-and-restage prettier --write -- file1.js file2.js | ||
``` | ||
|
||
Automatically e.g. with `lint-staged`: | ||
|
||
### `package.json` | ||
|
||
```json | ||
{ | ||
"scripts": { | ||
"precommit": "lint-staged" | ||
}, | ||
"lint-staged": { | ||
"*.js": ["git-exec-and-restage eslint --fix --"] | ||
} | ||
} | ||
``` | ||
|
||
## Contribute | ||
|
||
PRs accepted. | ||
|
||
## License | ||
|
||
MIT © Moti Zilberman |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"name": "git-exec-and-restage", | ||
"description": "Safely amend Git commits after applying auto-fixing tools", | ||
"version": "0.0.0-development", | ||
"main": "dist", | ||
"bin": "dist/index.js", | ||
"scripts": { | ||
"build": "babel src --copy-files --out-dir dist", | ||
"watch": "npm run build -- --watch", | ||
"prepublish": "npm run build", | ||
"dev": "babel-node src", | ||
"test": "if-env CI=true && npm run ci || jest --watchAll", | ||
"ci": "jest --ci && codecov", | ||
"semantic-release": "semantic-release pre && npm publish && semantic-release post" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"author": "Moti Zilberman <[email protected]>", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/motiz88/git-exec-and-restage.git" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1", | ||
"babel-core": "^6.24.1", | ||
"babel-preset-env": "1", | ||
"babel-preset-stage-0": "^6.24.1", | ||
"codecov": "^2.2.0", | ||
"if-env": "^1.0.0", | ||
"jest": "^20.0.0", | ||
"mock-bin": "^1.0.1", | ||
"mock-git": "^1.0.3", | ||
"mz": "^2.6.0", | ||
"tmp-promise": "^1.0.3", | ||
"semantic-release": "^6.3.6" | ||
}, | ||
"engines": { | ||
"node": ">=6", | ||
"npm": ">=3" | ||
}, | ||
"dependencies": { | ||
"babel-polyfill": "^6.23.0", | ||
"cross-spawn-promise": "^0.10.1", | ||
"semantic-release-cli": "^3.0.3" | ||
}, | ||
"jest": { | ||
"testPathIgnorePatterns": [ | ||
"<rootDir>/node_modules/", | ||
"<rootDir>/dist/" | ||
], | ||
"collectCoverage": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default class CliError extends Error {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import mockGit from "mock-git"; | ||
import mockBin from "mock-bin"; | ||
import tmp from "tmp-promise"; | ||
import path from "path"; | ||
import fs from "mz/fs"; | ||
|
||
export default class TestEnvironment { | ||
_unmock = []; | ||
_spyLogFile = undefined; | ||
__makeSpyJs = undefined; | ||
|
||
async setAllStagedFiles(files = []) { | ||
this._unmock.push( | ||
await mockGit( | ||
`${this._makeSpyJs("git")} for (const file of ${JSON.stringify(files)}) console.log(file);`, | ||
"diff-index" | ||
) | ||
); | ||
} | ||
|
||
async setPartialFiles(files = []) { | ||
this._unmock.push( | ||
await mockGit( | ||
`${this._makeSpyJs("git")} for (const file of ${JSON.stringify(files)}) console.log(file);`, | ||
"diff" | ||
) | ||
); | ||
} | ||
|
||
get log() { | ||
return (async () => { | ||
if (await fs.exists(this._spyLogFile)) { | ||
const logContent = await fs.readFile(this._spyLogFile, "utf8"); | ||
return logContent | ||
.split("\n") | ||
.filter(Boolean) | ||
.map(line => JSON.parse(line).join(" ")); | ||
} | ||
return []; | ||
})(); | ||
} | ||
|
||
async mockBin(name) { | ||
this._unmock.push(await mockBin(name, "node", this._makeSpyJs(name))); | ||
} | ||
|
||
_makeSpyJs(...prefix) { | ||
return ` | ||
require("fs").appendFileSync(${JSON.stringify(this._spyLogFile)}, JSON.stringify(${JSON.stringify(prefix)}.concat(process.argv.slice(2))) + "\\n", "utf8"); | ||
`; | ||
} | ||
|
||
async setup() { | ||
this._spyLogFile = await tmp.tmpName(); | ||
this._unmock.push(await mockGit(this._makeSpyJs("git"), "add")); | ||
await this.setAllStagedFiles(); | ||
await this.setPartialFiles(); | ||
} | ||
cleanup() { | ||
for (const unmockOne of this._unmock) { | ||
unmockOne(); | ||
} | ||
this._unmock = []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`git-exec-and-restage no command, no args and no files staged 1`] = `[Error: Usage: git-exec-and-restage <command> [...command-args --] [...files]]`; | ||
exports[`git-exec-and-restage with command and arguments, no files 1`] = ` | ||
Array [ | ||
"git diff-index --cached --name-only --diff-filter=ACDMRTUXB", | ||
"git diff --name-only --diff-filter=ACDMRTUXB", | ||
"prettier --write", | ||
] | ||
`; | ||
exports[`git-exec-and-restage with command+args, files implicit 1`] = ` | ||
Array [ | ||
"git diff-index --cached --name-only --diff-filter=ACDMRTUXB", | ||
"git diff --name-only --diff-filter=ACDMRTUXB fullystaged.js partiallystaged.js", | ||
"prettier --write", | ||
"git add fullystaged.js", | ||
] | ||
`; | ||
exports[`git-exec-and-restage with command+args, mix of fully and partially staged files 1`] = ` | ||
Array [ | ||
"git diff --name-only --diff-filter=ACDMRTUXB fullystaged.js partiallystaged.js", | ||
"prettier --write fullystaged.js partiallystaged.js", | ||
"git add fullystaged.js", | ||
] | ||
`; | ||
exports[`git-exec-and-restage with command+args, pick one of multiple files 1`] = ` | ||
Array [ | ||
"git diff --name-only --diff-filter=ACDMRTUXB fullystaged1.js", | ||
"prettier --write fullystaged1.js", | ||
"git add fullystaged1.js", | ||
] | ||
`; | ||
exports[`git-exec-and-restage with command, arguments, fully staged file, no partials 1`] = ` | ||
Array [ | ||
"git diff --name-only --diff-filter=ACDMRTUXB fullystaged.js", | ||
"prettier --write fullystaged.js", | ||
"git add fullystaged.js", | ||
] | ||
`; | ||
exports[`git-exec-and-restage with command, no args and no files 1`] = ` | ||
Array [ | ||
"git diff-index --cached --name-only --diff-filter=ACDMRTUXB", | ||
"git diff --name-only --diff-filter=ACDMRTUXB", | ||
"prettier", | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import gitExecAndRestage from "../"; | ||
import TestEnvironment from "TestEnvironment"; | ||
|
||
describe("git-exec-and-restage", () => { | ||
const env = new TestEnvironment(); | ||
beforeEach(() => env.setup()); | ||
afterEach(() => env.cleanup()); | ||
it("no command, no args and no files staged", async () => { | ||
await expect(gitExecAndRestage()).rejects.toMatchSnapshot(); | ||
}); | ||
it("with command, no args and no files", async () => { | ||
await env.mockBin("prettier"); | ||
await gitExecAndRestage(["prettier"]); | ||
expect(await env.log).toMatchSnapshot(); | ||
}); | ||
it("with command and arguments, no files", async () => { | ||
await env.mockBin("prettier"); | ||
await gitExecAndRestage(["prettier", "--write", "--"]); | ||
expect(await env.log).toMatchSnapshot(); | ||
}); | ||
it("with command, arguments, fully staged file, no partials", async () => { | ||
await env.mockBin("prettier"); | ||
await gitExecAndRestage(["prettier", "--write", "--", "fullystaged.js"]); | ||
expect(await env.log).toMatchSnapshot(); | ||
}); | ||
it("with command+args, mix of fully and partially staged files", async () => { | ||
await env.mockBin("prettier"); | ||
await env.setPartialFiles(["partiallystaged.js"]); | ||
await gitExecAndRestage([ | ||
"prettier", | ||
"--write", | ||
"--", | ||
"fullystaged.js", | ||
"partiallystaged.js" | ||
]); | ||
expect(await env.log).toMatchSnapshot(); | ||
}); | ||
it("with command+args, files implicit", async () => { | ||
await env.mockBin("prettier"); | ||
await env.setAllStagedFiles(["fullystaged.js", "partiallystaged.js"]); | ||
await env.setPartialFiles(["partiallystaged.js"]); | ||
await gitExecAndRestage(["prettier", "--write", "--"]); | ||
expect(await env.log).toMatchSnapshot(); | ||
}); | ||
it("with command+args, pick one of multiple files", async () => { | ||
await env.mockBin("prettier"); | ||
await env.setAllStagedFiles([ | ||
"fullystaged1.js", | ||
"fullystaged2.js", | ||
"partiallystaged1.js", | ||
"partiallystaged2.js" | ||
]); | ||
await env.setPartialFiles(["partiallystaged1.js", "partiallystaged2.js"]); | ||
await gitExecAndRestage(["prettier", "--write", "--", "fullystaged1.js"]); | ||
expect(await env.log).toMatchSnapshot(); | ||
}); | ||
}); |
Oops, something went wrong.