0

I want to hook pre-commit. And in pre-commit, I want to check code style such as ./gradlew check.

However it checks all files include staged files and non-staged files. After I format my code, ./gradlew check passed however the modified files have not been staged. In another word, now I can commit successfully but the code in fact is not formatted.

How can I do this? Yes I can use Travis-CI but I can not.

CoXier
  • 2,523
  • 8
  • 33
  • 60
  • 1
    You'd have to extract the staged files somewhere, since you're running the check on the working directory. In the worst case, this means that if you stage some files, update them without staging, the pre-commit hook will run on the modified files in the working directory, which have nothing at all to do with the staged files. – Mad Physicist Jun 07 '18 at 03:36
  • So how to extract the staged files? – CoXier Jun 07 '18 at 03:46
  • Beats me :) https://stackoverflow.com/q/5153199/2988730 – Mad Physicist Jun 07 '18 at 04:55
  • 1
    @CoXier Suppose the repo is `/foo/bar/`. To extract the staged files into another path, for example `/foo/tmp`, `mkdir -p /foo/tmp; git --git-dir=/foo/bar/.git --work-tree=/foo/tmp checkout-index -a -f`. – ElpieKay Jun 07 '18 at 04:59
  • @ElpieKay However there are many files in tmp dir include some unmodified files. – CoXier Jun 07 '18 at 05:30
  • @CoXier Then try `git --git-dir=/foo/bar/.git diff --cached --name-only | git --git-dir=/foo/bar/.git --work-tree=/foo/tmp checkout-index -f -q --stdin`. If you are now under `/foo/bar`, `--git-dir=/foo/bar/.git` can be omitted. You could also specify the path at the end of `git checkout-index`. – ElpieKay Jun 07 '18 at 05:48
  • Very nice job. I will appreciate it if you can explain this command. – CoXier Jun 07 '18 at 05:59

0 Answers0