Skip to content

Issue #363: Include untracked files in patch generation#417

Open
Brijeshthummar02 wants to merge 1 commit into
checkstyle:masterfrom
Brijeshthummar02:untrackedfiles
Open

Issue #363: Include untracked files in patch generation#417
Brijeshthummar02 wants to merge 1 commit into
checkstyle:masterfrom
Brijeshthummar02:untrackedfiles

Conversation

@Brijeshthummar02
Copy link
Copy Markdown
Contributor

Issue #363

The patch generation was missing newly created untracked files, which meant Checkstyle never validated them. This allowed violations in new files to slip through undetected.

The issue occurred because git diff HEAD~1 HEAD only compares two commits and doesn't see untracked files at all.

What i did is added git add -N . (intent-to-add) before generating patches, then changed to git diff HEAD~0 to compare the working directory against HEAD. This will make new files visible to git diff without actually staging them for commit.

Testing

I verified the fix by creating a test git repository with both tracked and untracked files, then compared the patch output before and after the fix. The old approach (git diff HEAD~1 HEAD) missed the new untracked file completely, while the new approach (git add -N . && git diff HEAD~0) successfully included it. I also verified that git add -N doesn't actually stage the content, so there's no risk of accidental commits.

Here's logs:

Setup

mkdir test-scenario-363
cd test-scenario-363
git init

# Create initial commit
echo "public class ExistingFile { }" > ExistingFile.java
git add ExistingFile.java
git commit -m "Initial commit"

# Make second commit with changes
# (modified ExistingFile.java)
git add ExistingFile.java
git commit -m "Second commit"

# Create new untracked file with violations
cat > NewUntrackedFile.java << 'EOF'
public class NewUntrackedFile {
    public void badMethod( )  // violation: space before )
    {  // violation: { should be on same line
        int x=5;  // violation: no spaces around =
    }
}
EOF

Test 1: Old approach (before fix)

$ git diff HEAD~1 HEAD > patch-old.txt
$ cat patch-old.txt

Result:

diff --git a/ExistingFile.java b/ExistingFile.java
index 76dd7f6..f89545a 100644
--- a/ExistingFile.java
+++ b/ExistingFile.java
@@ -2,4 +2,8 @@ public class ExistingFile {
     public void existingMethod() {
         System.out.println("This is an existing file");
     }
+    
+    public void anotherMethod() {
+        System.out.println("Added in second commit");
+    }
 }

Only shows the committed changes. NewUntrackedFile.java is completely missing.

Test 2: New approach (after fix)

$ git add -N .
$ git diff HEAD~0 > patch-new.txt
$ cat patch-new.txt

Result:

diff --git a/NewUntrackedFile.java b/NewUntrackedFile.java
new file mode 100644
index 0000000..c8372ad
--- /dev/null
+++ b/NewUntrackedFile.java
@@ -0,0 +1,8 @@
+public class NewUntrackedFile {
+    // This is a NEW untracked file with Checkstyle violations
+    public void badMethod( )  // violation: space before )
+    {  // violation: { should be on same line
+        int x=5;  // violation: no spaces around =
+        System.out.println("This file should be validated by Checkstyle");
+    }
+}

The new file now appears in the patch with full content. So Checkstyle can validate it.

Test 3: Verify repository safety

$ git status

Result:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        new file:   NewUntrackedFile.java

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

Files remain unstaged after running the command, so they are not included automatically in a normal commit unless explicitly added by the developer.

@Brijeshthummar02
Copy link
Copy Markdown
Contributor Author

@romani As you see git add -N . makes untracked files visible to git diff without fully staging them, and git diff HEAD~0 compares the working directory against HEAD, allowing newly created files to appear in the patch while still remaining unstaged and safe from accidental commits.

@romani
Copy link
Copy Markdown
Member

romani commented May 16, 2026

problem with this apprach is that it changes git state that will be very unexpected by user, as some files created temporary and should not be added actually, but build add them and such files can be leacked as they are not desirable.

we need somthing that does not change git state and just generate pathc file in form we need..

@Brijeshthummar02
Copy link
Copy Markdown
Contributor Author

problem with this apprach is that it changes git state that will be very unexpected by user, as some files created temporary and should not be added actually, but build add them and such files can be leacked as they are not desirable.

we need somthing that does not change git state and just generate pathc file in form we need..

I focused on matching the expected patch output and verified that the approach works functionally, but I agree that modifying the git index during build execution is not ideal and can lead to unexpected side effects for users.

I’ll try to find an approach that includes untracked files in the generated patch without changing git state or touching the index, including temporary/generated files handling.

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