Skip to content

fix a bug with the full build.gradle file being cleared when block to delete is the last one in the file#444

Merged
OdysseusLives merged 1 commit into
mainfrom
fix/full-file-clear
Apr 22, 2026
Merged

fix a bug with the full build.gradle file being cleared when block to delete is the last one in the file#444
OdysseusLives merged 1 commit into
mainfrom
fix/full-file-clear

Conversation

@iuliiasobolevska

Copy link
Copy Markdown
Contributor

Issue #1: The GradleLintPatchAction.groovy generates an invalid patch header for partial end-of-file deletions.

When a lint fix deletes lines with no replacement, afterLineCount is 0 and the @@ hunk header is generated as +0,0:

  // GradleLintPatchAction.groovy line 260                                                                                                                                                                                                                                                                                                                                                                                                  
  +${afterLineCount == 0 ? 0 : firstLineOfContext},$afterLineCount

+0,0 is a valid convention for complete file deletion (@@ -1,N +0,0 @@), where "0 lines at position 0" means the file is empty.
For a partial deletion (@@ -184,3 +0,0 @@), it is semantically wrong — the + position should be firstLineOfContext, not 0.

JGit's ApplyCommand handles +0,0 correctly when context lines are present in the patch (it anchors on them and ignores the +l value). When there are no context lines, JGit falls back to the +l value to locate the hunk; l=0 treats the entire file as the target region, clearing it.

Issue #2. Context lines are dropped when the deleted block is preceded only by blank lines.

GradleLintPatchAction takes 3 lines before the fix as before-context, then strips leading blank ones via dropWhile:

  // line 177                                                                                                                                                                                                                                                                                                                                                                                                                               
  .dropWhile { String line -> j == 0 && StringUtils.isBlank(line) }

When all 3 candidate context lines are blank (e.g. multiple blank lines separating the last block from the rest of the file), dropWhile strips them all. The patch ends up with zero context lines, exposing the +0,0 bug above.

@OdysseusLives OdysseusLives merged commit 5dac7eb into main Apr 22, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants