Skip to content
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

feat: use git stashes for gitWorkflow #663

Merged
merged 37 commits into from Oct 31, 2019
Merged

feat: use git stashes for gitWorkflow #663

merged 37 commits into from Oct 31, 2019

Conversation

iiroj
Copy link
Member

@iiroj iiroj commented Jul 20, 2019

This PR rewrites the gitWorkflow to use git stashes instead of cache-busting read-tree and write-tree.

Some benefits:

  1. faster on very large repositories, since the index doesn't get cleared during operations
  2. Since the first task is to create a stash of the original HEAD state, it makes lint-staged more robust to errors, when on failure the stash will either get restored, or in the case lint-staged crashes, left behind for easy manual restore.

This currently passes all current tests (albeit with some modifications since the logic is a bit different), but I'm not sure about situations where there would be merge conflicts between modified and staged, and unstaged changes.

Consider this an evolution of #633, which did not pass all intergration scenarios.

TODO

  • make a test for a merge conflict situation, where restoring unstaged changes might be impossible because of linter modifications to staged changes.
  • Handle merge conflicts
  • Handle untracked files

Fixes

@iiroj iiroj force-pushed the git-stashes branch 2 times, most recently from c873e14 to 1382586 Compare July 21, 2019 08:23
@codecov
Copy link

codecov bot commented Jul 21, 2019

Codecov Report

Merging #663 into master will increase coverage by 0.32%.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff            @@
##           master   #663      +/-   ##
========================================
+ Coverage   99.67%   100%   +0.32%     
========================================
  Files          11     11              
  Lines         306    278      -28     
  Branches       57     53       -4     
========================================
- Hits          305    278      -27     
+ Misses          1      0       -1
Impacted Files Coverage Δ
src/gitWorkflow.js 100% <100%> (+1.53%) ⬆️
src/runAll.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7432469...1382586. Read the comment docs.

@codecov
Copy link

codecov bot commented Jul 21, 2019

Codecov Report

Merging #663 into beta will decrease coverage by 0.54%.
The diff coverage is 98.08%.

Impacted file tree graph

@@            Coverage Diff             @@
##             beta     #663      +/-   ##
==========================================
- Coverage   99.67%   99.13%   -0.55%     
==========================================
  Files          11       12       +1     
  Lines         309      347      +38     
  Branches       64       64              
==========================================
+ Hits          308      344      +36     
- Misses          1        3       +2
Impacted Files Coverage Δ
lib/resolveGitDir.js 100% <ø> (ø)
lib/generateTasks.js 100% <ø> (ø)
lib/getStagedFiles.js 100% <ø> (ø)
lib/printErrors.js 100% <ø> (ø)
lib/validateConfig.js 100% <ø> (ø)
lib/index.js 100% <ø> (ø)
lib/file.js 100% <100%> (ø)
lib/resolveTaskFn.js 100% <100%> (ø)
lib/runAll.js 100% <100%> (ø)
lib/execGit.js 100% <100%> (ø)
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3f27bc7...2b57db0. Read the comment docs.

@iiroj iiroj force-pushed the git-stashes branch 5 times, most recently from 4b04d15 to dfb013a Compare July 21, 2019 12:30
@iiroj
Copy link
Member Author

iiroj commented Jul 21, 2019

Regarding the TODO, it seems this branch doesn't pass the new test introduced in #664, so there's still something to be done about that.

EDIT: this was solved by retrying with a 3-way merge

@iiroj
Copy link
Member Author

iiroj commented Jul 31, 2019

@okonet Do you have thoughts on this?

@okonet
Copy link
Collaborator

okonet commented Jul 31, 2019

Didn’t have time to lock yet. Vacation :)

@egeriis
Copy link

egeriis commented Aug 15, 2019

Been craving a solution like this for a long time, good work @iiroj 🙂

@okonet
Copy link
Collaborator

okonet commented Aug 17, 2019

I’m wondering where all the tests for the git stash are gone. They look deleted in the branch.

@iiroj
Copy link
Member Author

iiroj commented Aug 17, 2019

@okonet If you mean tests for the partial staging logic, they have been removed as that is no longer done, but rather stashing is done every time. Since the workflow is different, the old tests no longer apply.

@okonet
Copy link
Collaborator

okonet commented Aug 17, 2019

Yes, but old tests could be modified since they test merging behavior. Right now there is no test asserting that the merge succeed and the right state being committed.

@okonet
Copy link
Collaborator

okonet commented Aug 17, 2019

And why is the logic would not apply? It’s still the multi step process so simulating it should give you similar results for git status. What am I missing?

@iiroj
Copy link
Member Author

iiroj commented Aug 17, 2019

Alright, so the tests for calling of the separate methods have been removed, because they tested only the old logic. The full integration tests are still there and remain the same, as they test commits and git status.

I guess I could rewrite those tests, but they do not increase coverage because runAll.unmocked.spec already covers it.

Looking at runAll, the logic is like this:

  1. Create a backup stash and hide unstaged changes
  2. Run tasks on staged changes
  3. If there are errors, reset and restore everything from backup stash
  4. if there are no errors, apply unstaged changes back
  5. Remove the backup stash

So there could still be tests for 3 and 4, since they are conditional.

Additionally, if there are errors during 4, we should probably revert to the original status and fail committing, because in that case there's something messed up between staged and unstaged changes, caused by the tasks, and manual review would be the best. This is not currently the behaviour in the branch, since 3-way merging seems to handle them.

@iiroj
Copy link
Member Author

iiroj commented Aug 17, 2019

I rebased this branch on top of unix-paths (#678), because in any case that should be merged first. With this branch, I'm more interested in releasing it under the next tag, so that it can be tested before merging.

@okonet
Copy link
Collaborator

okonet commented Aug 17, 2019

with 4 that’s not really how it suppose to work since you almost always going to get conflicts with stash and reformatted files. The original version would apply as many changes as possible and ignore the conflicted hunks. I guess your version will just bail out.

That said, this behavior also causes #565 since it ignores all errors which it shouldn’t probably so we can improve that.

@iiroj
Copy link
Member Author

iiroj commented Aug 17, 2019

Are you open to considering this as a change in the way lint-staged should work (regarding bailing in case of unresolvable merge)? Currently, lint-staged is approaching being unusable in our large repo because of performance issues with it clearing the git index cache while running. This makes git actions take over a minute, and lint-staged itself like two minutes.

I'm also open to trying different approaches to solve this issue.

I think it might be possible to create a diff and apply in the same way as currently (linter modifications over the original state), but it would take multiple stashes.

@okonet
Copy link
Collaborator

okonet commented Oct 31, 2019

@iiroj I have to force push in order to change the branch. Please be aware and do git reset --hard origin/git-stashes next time instead of git pull

@okonet
Copy link
Collaborator

okonet commented Oct 31, 2019

Ah seems I misunderstood the idea. We have to merge to beta from this one in order to release. Let me try one more time.

@okonet okonet merged commit 2b57db0 into beta Oct 31, 2019
@okonet
Copy link
Collaborator

okonet commented Oct 31, 2019

Ah fuck I screwed it didn't I?

@okonet
Copy link
Collaborator

okonet commented Oct 31, 2019

🎉 This PR is included in version 9.5.0-beta.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@okonet
Copy link
Collaborator

okonet commented Oct 31, 2019

@iiroj apparently it works as expected now I should have not rebased from master onto beta I guess. Not sure if we should try to recover from that or proceed with smaller PRs that we'll merge into beta manually so it's released.

Another thing I noticed, it won't release as major since there are no commits with BREAKING CHANGE marker inside. See https://github.com/okonet/lint-staged/releases/tag/v9.5.0-beta.1%40beta

@iiroj
Copy link
Member Author

iiroj commented Oct 31, 2019

@okonet I’ll add a breaking change to the commit when I update the readme about git add.

@okonet
Copy link
Collaborator

okonet commented Oct 31, 2019

@iiroj what should we do about this branch? Should we try restoring it somehow?

@okonet
Copy link
Collaborator

okonet commented Oct 31, 2019

Ah I see, branch is in place the the PR is closed. We won't be able to re-open it I guess so you'd have to create a new one and refer to this one for the discussion. Okay?

@okonet
Copy link
Collaborator

okonet commented Oct 31, 2019

Alternatively we could rebase the branch back on master and see if that would help... Do you want me to try?

@iiroj
Copy link
Member Author

iiroj commented Oct 31, 2019

@okonet nah it’s ok, I’ll open a new PR for the beta branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

7 participants