Is there a way to "script" a git commit - just accepting some of the differences?

I applied some static code analysis - and converted all the fields of the Java package level to secure ones. This is mostly correct, since I forgot to set the access modifier explicitly, and in the general case I protected it as usual.

Unfortunately, the refactoring I used is IntelliJ access modifiers, and it also changes the set of public → protected, on the constructors, etc.

So, one day I have a diff with the following statements in it:

-int myField; +protected int myField; 

and

 -public MyConstructor() +protected MyConstructor() 

Is there a way to write a script that is executed during commit, and allows only the first example above, but not the second? I can define a script, and I'm just not sure how to run it - is this a catch with capture?

If this is a pre-commit hook, how do you consider diff in such a script and accept / reject it based on greping for some string?

+5
source share
1 answer

Yes you can do it using smudge / filters

There are several ways to do this.

git hooks

git hooks are scripts that execute at different stages of the git life cycle.
In the appropriate hook file, check the file / content that you need and do whatever you need.

The hook will run in your files and will check the syntax.

Smudge / clean

Basically, smudge is equivalent to running this code whenever you check something
and the pure equivalent runs this code whenever you check anything in .


Images from this URL: https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes

enter image description here

+2
source

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


All Articles