1

Here is what I'm doing. add a pre-commit hook using npm scripts, an example repo

"is-commitable": "git status | grep 'Changes to be committed:'",
"stash-pop": "git stash pop >> /dev/null",
"stash-unstaged": "git stash save -k --include-untracked 'unstaged-stash' >> /dev/null",
"check-commitable": "npm run is-commitable || (npm run stash-pop && exit 1)",
"lint-staged": "(eslint . --fix && git add .) || (npm run stash-pop && exit 1)",
"recheck-commitable": "npm run check-commitable",
"stash-linted": "git stash save 'linted-stash' >> /dev/null",
"stash-pop-unstaged": "git stash pop stash@{1}",
"stash-pop-linted": "git read-tree stash && git stash drop",
"clean-lint-staged": "(lint-staged >> /dev/null) || exit 0"

Example

  1. have two lines of commited code:

    line1
    line2
    
  2. make some changes:

    * line1-changed
    * line2-changed
    
  3. partially add one of them:

    - line1
    + line1-changed
    * line2-changed
    
  4. run commit and pre-commit hook is triggered

    • stash-unstaged

      - line1
      + line1-changed
      line2(changed part been stashed in unstaged-stash)
      
    • lint-staged

      - line1
      + line1-fixed
      line2(changed part been stashed in unstaged-stash)
      
    • stash-linted

      line1(changed and fixed part been stashed in linted-stash)
      line2(changed part been stashed in unstaged-stash)
      
    • stash-pop-unstaged

      line1(changed and fixed part been stashed in linted-stash)
      * line2-changed
      
    • stash-pop-linted

      - line1
      + line1-fixed
      * line1-changed
      * line2-changed
      
    • clean-lint-staged

      - line1
      + line1-fixed
      * line2-fixed
      

Problem

The last 2 steps are not perfect.

Expected result

I wanted to get

- line1
+ line1-fixed
* line2-changed

Actual results

But now I can only get

- line1
+ line1-fixed
* line1-changed
* line2-changed

or

- line1
+ line1-fixed
* line2-fixed

Need help

So can I find a way to programmatically git checkout a file only the staged lines?

Or drop the last step find a way to fix the last 2nd step

Stupidism
  • 113
  • 1
  • 2
  • 7
  • See, e.g., http://stackoverflow.com/q/20479794/1256452 - but note that I wrote this before `git stash create` and `git stash store` existed. However, the bug I wrote about is still present. Using `git stash --keep-index` to do all this is generally a bad idea all around. – torek May 20 '17 at 03:09
  • I didn't get it. What should I use then? `git stash -q -k`? – Stupidism May 23 '17 at 18:49
  • Don't use `git stash` at all. Just commit things: use ordinary commits. If you want to test what *will be* committed, use `git checkout-index` to extract files from the index into a separate area (a temporary work-tree, so that you don't clobber files in the regular work-tree). – torek May 23 '17 at 18:52
  • But what I want to do is to auto-fix lint errors during pre-commit without affecting unstaged partials and files. – Stupidism May 24 '17 at 17:20
  • *Finding* them is reasonable, albeit a bit problematic ("warning: your staged version of foo probably has a bug on line 72 ... by the way that line appears nowhere in your current work-tree, and is not in your HEAD commit, it's only in the staged file"). *Fixing* them is more difficult: where will you *store* the fixed version? What if the automated fix is *wrong?* – torek May 24 '17 at 18:24

0 Answers0