Cosium / git-code-format-maven-plugin

A maven plugin that automatically deploys code formatters as pre-commit git hook

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot apply HunkHeader on Windows

msharathrajms opened this issue · comments

I am getting the below error.

Failed to execute goal com.cosium.code:git-code-format-maven-plugin:2.4:on-pre-commit (default-cli) on project qe-jira-soap: org.eclipse.jgit.api.errors.PatchApplyException: Cannot ap
ply: HunkHeader[1,9->1,5] -

I have added -X to the pre-commit script and here is the error log.

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.cosium.code:git-code-format-maven-plugin:2.4:on-pre-commit (default-cli) on project qe-jira-soap: org.eclips
e.jgit.api.errors.PatchApplyException: Cannot apply: HunkHeader[1,9->1,5]
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: org.eclipse.jgit.api.errors.PatchApplyException: Cannot apply: HunkHeader[1,9->1,5]
at com.cosium.code.format.OnPreCommitMojo.doExecute(OnPreCommitMojo.java:25)
at com.cosium.code.format.AbstractModuleMavenGitCodeFormatMojo.execute(AbstractModuleMavenGitCodeFormatMojo.java:66)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
... 20 more
Caused by: com.cosium.code.format.MavenGitCodeFormatException: org.eclipse.jgit.api.errors.PatchApplyException: Cannot apply: HunkHeader[1,9->1,5]
at com.cosium.code.format.git.GitStagedFiles.format(GitStagedFiles.java:97)
at com.cosium.code.format.OnPreCommitMojo.onPreCommit(OnPreCommitMojo.java:30)
at com.cosium.code.format.OnPreCommitMojo.doExecute(OnPreCommitMojo.java:22)
... 23 more
Caused by: org.eclipse.jgit.api.errors.PatchApplyException: Cannot apply: HunkHeader[1,9->1,5]
at org.eclipse.jgit.api.ApplyCommand.apply(ApplyCommand.java:224)
at org.eclipse.jgit.api.ApplyCommand.call(ApplyCommand.java:138)
at com.cosium.code.format.git.GitStagedFiles.format(GitStagedFiles.java:92)
... 25 more

I am using CLI, Git version 2.23.0, Windows, Plugin version 2.4. I have a parent pom and added it to that, i am using again in a sub project and this is a multi module project.

Failed to execute goal com.cosium.code:git-code-format-maven-plugin:2.1:on-pre-commit (default-cli) on project scaffold: org.eclipse.jgit.api.errors.PatchApplyException: Cannot apply: HunkHeader[11,7->11,6] -> [Help 1]

plugin version: 2.4
maven: 3.6
OS: "windows 10"
arch: "amd64"

I can run the manual command successful, but when i try to commit and the hook trigger the automatic formatting, it brakes with the above error.

the same look like another issue #42
@reda-alaoui

This is a long standing and hard to debug Windows specific issue.
I currently don't have the time and the motivation (me and almost all my coworkers are on Linux/Mac OS) to deal with it.

So I hope one of the impacted people will give it a shot. After all, the plugin is written in java and we are all java developers here :)

I try to fix it, but it doesn't seem easy.

As the author said, this is a problem caused by the combination of lf and CRLF , so change the IDE's line separator to lf. and set the git config below:

git config --global core.autocrlf true
git config --global core.safecrlf true

add * text=auto eol=lf to the repo's .gitattributes file. then run the commond:

git rm --cached -r .
git reset --hard

it will update the repositroy files line separator to lf

I found prettier to be more useful. It is so powerful that it can customize many configurations, and it runs faster in tests. Also it can automatically converts CRLF to lf.

Prettier requires nodejs.
I would rather die than make a java project depend on nodejs :D

Here is a maven plugin ,you don't have to install nodejs, it will fetch a nodejs executable file into the local maven repository.
So, just add the plugin into pom.xml, it works.

This is not an issue with the installation part.

Node/npm is incredibly unstable compared to java/maven. This is why we use node/npm only when we are forced to. And we are only forced to do that inside the frontend build.

Node/npm will never put feet on our company java perimeter :)

I don't know why you think Node/npm is unstable.

I use Java and JS in my daily work, according to my knowledge, javascript is not a static language, so it has many potential type safety issues. But prettier uses typescript, which strikes a balance between flexibility and type safety, i believe it's stable.

Prettier was created by Facebook, it's widely used.I think it has been proven in practice.

My twitter handle is in my github profile. If you want to discuss, use this medium please ;)

I managed to resolve this in windows using this methods. Not sure why it works though.

git stash push

git stash pop

Somehow the HunkHeader issue is gone for my case.

Personnally I'm still struggle with this issue:

Using Git Bash / unix2dos, I made sure everything was included CRLF so that my git configuration could handle it correctly. I was only dealing with Java files, so I filtered my matching based on .java file extensions.

shopt -s globstar
unix2dos -v **/*.java