Understanding git commit - only preliminary fixes

I am working on binding pre-commit to code reformatting, and overall it works; it reformatts git addany staged files, and the resulting commit contains the reformatted code as desired.

However, it does not play well with git commit --only(which is an option used by the JetBrains IDE), and I'm trying to understand why. Combining git commit --onlyand binding pre-commit results in an unwanted index / work tree, as described in the following sequence of events:

If I make a small change with a formatting error in a file and then run it git status, this is what I see:

On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   file.php

no changes added to commit (use "git add" and/or "git commit -a")

If I then transfer usage git commit --only -- file.php, pre-commit capture is performed, and the modified and formatted file.phpcommit is committed.

However, if I run git statusit again , this is the result (my annotation arrows):

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   file.php <-- contains original change, improperly formatted

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   file.php <-- contains original change, properly formatted (per the most recent commit)

Where is the new step change and change the working tree?

Can someone explain how it git commit --onlyinteracts with the index to get the result shown above, and even better, is there a way for my pre-commit hook to play well with it?

, git commit --only , git add , , , , ( git commit, , git commit --only).

clean , , , , , .

: Phpstorm pre commit hooks, , git commit --only. , , , JetBrains, .

+4
2

Git , - , JetBrains , , Git , , Git -. Git :

  • commit-to-make
  • .

git commit, git commit, --only, --include, Git , . , GIT_INDEX_FILE, . 1 Git , pre-commit , git write-tree .

, , , , , --include vs --only, - .

- , , . , -let, test, - "headvers" (HEAD) . , "indexvers" git add test. , test "indexvers". , "", git commit --only test, git commit --include test.

, : , workvers, Git . ? , --include vs --only? , "" ! , , , , Git , , , workvers ( , ). , , .

( Git , / , " , " " temp -index, temp-index".)


1 , , , . , Git "" , , GIT_INDEX_FILE . , , --include vs --only.

, git commit -a . , Git 1.7 Git 2.10, git status , git commit -a.

+5

. , Jetbrains .


git commit --only :

.

(ruby):

`git status --porcelain`.lines do |line|
    changed_file = line.split(' ', 2)[1].strip()
    if (File.extname(changed_file).downcase() == '.java')
        system "java -jar bin/google-java-format-1.4-all-deps.jar --aosp --replace #{changed_file}"
        system "git add #{changed_file}"
    end
end

post-commit:

git update-index -g

https://youtrack.jetbrains.com/issue/IDEA-81139#comment=27-295117

+3

Source: https://habr.com/ru/post/1665360/


All Articles