New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Git workflow #75
Merged
Merged
Git workflow #75
Changes from all commits
Commits
Show all changes
139 commits
Select commit
Hold shift + click to select a range
1cbd6dd
Use `git stash --keep-index` to enable linting and committing only st…
okonet fe2a5d2
Added gitWorkflow and basic tests
okonet c801b04
More work on tests + some refactoring
okonet 23f300b
Added more tests + fixed some bugs
okonet 763bc9a
Work with absolute and relative paths. Added tests.
okonet 4bb557b
Switched to gitWorkflow
okonet 479e5d1
Added test user name and email to try to fix Travis
okonet 72596b3
Bumped version to 4.0.0-beta-1
okonet fba5ccd
Bumped version
okonet eff0daa
Extracted expect extension to a separate file. Added a failing test f…
okonet 9fbdbc8
Switched to async/await for tests
okonet ce16b5d
Removed no-console eslint error
okonet b38618e
Some progress on git workflow.
okonet 8b15f70
Merge branch 'master' into git-worflow
okonet d4bde33
tests: Migrate git workflow tests to Jest
okonet 6bf42cd
chore: Switch to husky and fix dependencies
okonet bb6d983
chore: Add *.json to .lintstagedrc config
okonet 91375fa
tests: Remove dependency to expect
okonet f6cd4d6
tests: Update tests
okonet 27362ec
Use gitRestore in post command
okonet 7d99e40
tests: Update tests and add gitRestore
okonet de605bc
test: Skip failing test for now
okonet 1de7c6c
Trying a proposed approach and restoring from stash before the commit
okonet 76a0932
tests: Removed unnecessary code, test, and snapshot
okonet 5442438
Merge branch 'master' into git-worflow
646cdd4
refactor: Fix ESLint errors, run prettier, remove yarn.lock
e7307c2
refactor: Use functions and normal modules.export instead of class
b9f031d
feat: Add git stash and restore workflow
dd1228c
Move git workflow to runAll and use Listr for running it
31ded6e
Only stash/unstash files if there are unstaged files found
9b10e1c
Use base options for root listr tasks
383bed5
Better tests for git workflow
b9b693a
WIP on better partially staged patch application and conflict resolution
1b58716
Reduce boilerplate in test
fc9a2ae
Add a failing test for partially staged files
6611bd3
refactor: Remove unused jest-stdout-serializer
de57b1a
Create more git utilities for staged / unsteaged files
b8d54f7
tests: Add a failing test for deletion
bfe9c65
tests: More tests for git workflow
a2c4d72
Fix execution order problem
07b5fe8
Run in verbose mode
5c9a505
tests: Update test case and comment the conent check for now
d0777c1
Generate path if there are unstaged files
778180c
tests: Skip the failing test
ad48268
tests: Update snapshots
fbb27c7
Merge branch 'master' into git-worflow
627b241
chore: Use debug instead of console.log
f76f011
Run lint-staged in debug mode
3f7b7b0
Remove unused code that fails tests on Node 4
2e3765c
fixup! chore: Use debug instead of console.log
b9697f3
Remove unused post.js and pre.js files
490d6ff
Add stashUnstaged flag to the config
b163fb9
Fix tests
987724e
Update test title
cb5e547
Check if there are staged files before stashing
b90cda1
Add logging for git commands
7a84caf
chore: Use Node >=8 to supoort async / await
27de35c
chore: Disable prettier eslint plugin in .eslintrc
0ca3bfb
chore: Use prettier for *.js, *.json and *.md files
c1bda2b
test: Better test for partially-staged files
effd855
Use task context and set hasErrors if linters fail
69101d6
Cleaner test name
1bfb8e1
Add update index step to runAll which should only be executed if all …
3ef13f3
chore: Update yarn.lock
a0b4d8c
WIP on better gitWorkflow
5eb6fe8
Use async/await for patch strategy
844e57a
Better test names
ede22f7
fix test
6b8c4ab
Upgrade Jest
9948b6e
Use async/await for git workflow. Return stdout from execGit
bcbe2cc
Remove console.log
c0e4803
fixup! Use async/await for git workflow. Return stdout from execGit
c4f8246
tests: Fix tests for gitWorkflow.
6c069c7
Implement the thing!
3932dc2
Remove patch file after applying it. Add debug message
1b96d41
When formatted changes could not be applied, we still want to format …
d920f44
fixup! tests: Fix tests for gitWorkflow.
4870675
fix
ac92f94
fix(git): Fix hasUnstagedFiles check + add test
f4010cb
Make the partially staged test more realistic
414856c
Fix the partially staged case by adding a EOF to patch file
21b629c
Merge branch 'master' into git-worflow
bdc6926
v8.0.0-beta.0
4c1a8d9
fix: only run stash/unstash sequence when partially staged files dete…
30fa156
fix: Add cleanup function and use named variables for refs
44ccac1
refactor: Remove unneded tree check
1e25925
fix: Restore working copy when terminated
02eb5d1
refactor: remove unused `stashUnstaged` option
c836cf3
fix: check if `git status` returns a string
4832b4c
v8.0.0-beta.1
1d20445
test: mock gitWorkflow module in runAll tests
32f155a
fix: Do not display skip message for stashing step if stashed
99379a7
tests: Add more tests for stashing and restoring
36cd1f5
tests: Mock gitWorkflow in index.spec. Test non-zero exitCode
7be76dd
feat: Add whitespace between errors and make error messages stand out
1562d46
feat: Better task name and message for update stash step
9a034db
fix: use the forked version of listr-update-renderer until the fix is…
79eda6b
fix: upgrade execa to v1.0.0
52382c6
refactor: Cleanup gitWorkflow module
9b17afe
feat: detect SIGINT termination for linter tasks and exit appropriately
15266ca
fix: do not exit main Listr process on SIGINT
f65ac02
refactor: add a return from the main process
55c6a6c
v8.0.0-beta.2
6c490e8
tests: Move mock file to /test
9b4b4ea
fixup! chore: Use Node >=8 to supoort async / await
60da8bb
tests: Remove redundant test
4c0d6bb
refactor(gitWorkflow): Use g-status and remove custom partially stage…
e9fe671
refactor(gitWorkflow): Remove redundant gitDir option
fa492ab
fix(gitWorflow): Make the copy for partially staged case more clear
63d464f
tests(windows): Use mock of log-symbols to match snapshots on Mac/Win…
60b9895
Merge branch 'snapshots-win' into git-worflow
032c49c
tests: Update snapshots
adcd368
tests: Replace CLRF with LF in tests to normalize on Windows
7f5bd99
fix: Fix fail message when termination signal is detected
a2cb6b9
fix: Fix the case where failure message could reach impossible state
d21cd62
fixup! chore: Use Node >=8 to supoort async / await
8506945
refactor: Fix typo
e89ea5d
refactor: Use `skip` instead of `enabled` for stashing task
c7bd02d
chore: Remove fs-promise in favor of pify and native fs
3bc1526
chore: Do not use debug flag for lint-staged
a8cc52d
tests: Restore execa mock and remove redundant code
160987f
tests: Remove unnecessary stash checks since we don't rely on git stash
7192141
Merge remote-tracking branch 'origin/master' into git-worflow
815a2ab
tests: Switch some tests to inline snapshots
da2adf9
tests: Add more tests for renamed and untracked files
e78c034
v8.0.0-beta.3
083a201
refactor: Use stdin for patch instead of writing a the file
95dd571
fixup! chore: Use Node >=8 to supoort async / await
fb4e7ba
chore: Remove unused dependency
53fac77
fix: Delete *.rej files
499d7d7
fix(git): Always add a newline to the end of the patch.
3a74c22
v8.0.0-beta.4
6da31f0
fix(git): Fix hasPartiallyStagedFiles return true for untracked files
454b978
chore: Add a TODO
e95cdd2
refactor: Use Object spread
2bd723a
test: Add jest-snapshot-serializer-ansi
ed94335
refactor: Remove impossible case handling from makeErr
e3af19c
refactor: Rename function to match exports
66a3e9f
chore: Revert version in package.json
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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
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,178 @@ | ||
'use strict' | ||
|
||
const path = require('path') | ||
const execa = require('execa') | ||
const gStatus = require('g-status') | ||
const del = require('del') | ||
const debug = require('debug')('lint-staged:git') | ||
|
||
let workingCopyTree = null | ||
okonet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let indexTree = null | ||
let formattedIndexTree = null | ||
|
||
function getAbsolutePath(dir) { | ||
return path.isAbsolute(dir) ? dir : path.resolve(dir) | ||
} | ||
|
||
async function execGit(cmd, options) { | ||
okonet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const cwd = options && options.cwd ? options.cwd : process.cwd() | ||
debug('Running git command', cmd) | ||
try { | ||
const { stdout } = await execa('git', [].concat(cmd), { | ||
...options, | ||
cwd: getAbsolutePath(cwd) | ||
}) | ||
return stdout | ||
} catch (err) { | ||
throw new Error(err) | ||
okonet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
async function writeTree(options) { | ||
return execGit(['write-tree'], options) | ||
} | ||
|
||
async function getDiffForTrees(tree1, tree2, options) { | ||
debug(`Generating diff between trees ${tree1} and ${tree2}...`) | ||
return execGit( | ||
[ | ||
'diff-tree', | ||
'--ignore-submodules', | ||
'--binary', | ||
'--no-color', | ||
'--no-ext-diff', | ||
'--unified=0', | ||
tree1, | ||
tree2 | ||
], | ||
options | ||
) | ||
} | ||
|
||
async function hasPartiallyStagedFiles(options) { | ||
const files = await gStatus(options) | ||
const partiallyStaged = files.filter( | ||
file => | ||
file.index !== ' ' && | ||
file.workingTree !== ' ' && | ||
file.index !== '?' && | ||
file.workingTree !== '?' | ||
) | ||
return partiallyStaged.length > 0 | ||
} | ||
|
||
// eslint-disable-next-line | ||
okonet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
async function gitStashSave(options) { | ||
debug('Stashing files...') | ||
// Save ref to the current index | ||
indexTree = await writeTree(options) | ||
// Add working copy changes to index | ||
await execGit(['add', '.'], options) | ||
okonet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Save ref to the working copy index | ||
workingCopyTree = await writeTree(options) | ||
// Restore the current index | ||
await execGit(['read-tree', indexTree], options) | ||
// Remove all modifications | ||
await execGit(['checkout-index', '-af'], options) | ||
// await execGit(['clean', '-dfx'], options) | ||
debug('Done stashing files!') | ||
return [workingCopyTree, indexTree] | ||
} | ||
|
||
async function updateStash(options) { | ||
formattedIndexTree = await writeTree(options) | ||
return formattedIndexTree | ||
} | ||
|
||
async function applyPatchFor(tree1, tree2, options) { | ||
const diff = await getDiffForTrees(tree1, tree2, options) | ||
/** | ||
* This is crucial for patch to work | ||
* For some reason, git-apply requires that the patch ends with the newline symbol | ||
* See http://git.661346.n2.nabble.com/Bug-in-Git-Gui-Creates-corrupt-patch-td2384251.html | ||
* and https://stackoverflow.com/questions/13223868/how-to-stage-line-by-line-in-git-gui-although-no-newline-at-end-of-file-warnin | ||
*/ | ||
// TODO: Figure out how to test this. For some reason tests were working but in the real env it was failing | ||
const patch = `${diff}\n` // TODO: This should also work on Windows but test would be good | ||
if (patch) { | ||
try { | ||
/** | ||
* Apply patch to index. We will apply it with --reject so it it will try apply hunk by hunk | ||
* We're not interested in failied hunks since this mean that formatting conflicts with user changes | ||
* and we prioritize user changes over formatter's | ||
*/ | ||
await execGit( | ||
['apply', '-v', '--whitespace=nowarn', '--reject', '--recount', '--unidiff-zero'], | ||
{ | ||
...options, | ||
input: patch | ||
} | ||
) | ||
} catch (err) { | ||
debug('Could not apply patch to the stashed files cleanly') | ||
debug(err) | ||
debug('Patch content:') | ||
debug(patch) | ||
throw new Error('Could not apply patch to the stashed files cleanly.', err) | ||
} | ||
} | ||
} | ||
|
||
async function gitStashPop(options) { | ||
if (workingCopyTree === null) { | ||
throw new Error('Trying to restore from stash but could not find working copy stash.') | ||
} | ||
|
||
debug('Restoring working copy') | ||
// Restore the stashed files in the index | ||
await execGit(['read-tree', workingCopyTree], options) | ||
// and sync it to the working copy (i.e. update files on fs) | ||
await execGit(['checkout-index', '-af'], options) | ||
|
||
// Then, restore the index after working copy is restored | ||
if (indexTree !== null && formattedIndexTree === null) { | ||
// Restore changes that were in index if there are no formatting changes | ||
debug('Restoring index') | ||
await execGit(['read-tree', indexTree], options) | ||
} else { | ||
/** | ||
* There are formatting changes we want to restore in the index | ||
* and in the working copy. So we start by restoring the index | ||
* and after that we'll try to carry as many as possible changes | ||
* to the working copy by applying the patch with --reject option. | ||
*/ | ||
debug('Restoring index with formatting changes') | ||
await execGit(['read-tree', formattedIndexTree], options) | ||
try { | ||
await applyPatchFor(indexTree, formattedIndexTree, options) | ||
} catch (err) { | ||
debug( | ||
'Found conflicts between formatters and local changes. Formatters changes will be ignored for conflicted hunks.' | ||
) | ||
/** | ||
* Clean up working directory from *.rej files that contain conflicted hanks. | ||
* These hunks are coming from formatters so we'll just delete them since they are irrelevant. | ||
*/ | ||
try { | ||
const rejFiles = await del(['*.rej'], options) | ||
debug('Deleted files and folders:\n', rejFiles.join('\n')) | ||
} catch (delErr) { | ||
debug('Error deleting *.rej files', delErr) | ||
} | ||
} | ||
} | ||
// Clean up references | ||
workingCopyTree = null | ||
indexTree = null | ||
formattedIndexTree = null | ||
|
||
return null | ||
} | ||
|
||
module.exports = { | ||
execGit, | ||
gitStashSave, | ||
gitStashPop, | ||
hasPartiallyStagedFiles, | ||
updateStash | ||
} |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, would it be possible to get this released as a version on npm instead of depending on it through a loose git branch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having issues to install anything that depends on lint-staged now. We are behind a corporate proxy here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use a npm published version!