|
1 | 1 | /* |
2 | | - * Copyright 2020-2024 DiffPlug |
| 2 | + * Copyright 2020-2026 DiffPlug |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
15 | 15 | */ |
16 | 16 | package com.diffplug.spotless.maven; |
17 | 17 |
|
| 18 | +import java.io.File; |
18 | 19 | import java.io.IOException; |
| 20 | +import java.nio.file.Files; |
19 | 21 |
|
20 | 22 | import org.eclipse.jgit.api.Git; |
21 | 23 | import org.eclipse.jgit.api.errors.GitAPIException; |
@@ -150,4 +152,81 @@ private void assertClean() throws Exception { |
150 | 152 | private void assertDirty() throws Exception { |
151 | 153 | mavenRunner().withArguments("spotless:check").runHasError(); |
152 | 154 | } |
| 155 | + |
| 156 | + @Test |
| 157 | + void worktreeSupport() throws Exception { |
| 158 | + // Set up main repository with explicit 'main' branch |
| 159 | + // (Git.init() uses system default which may be 'master' on some systems) |
| 160 | + Git mainGit = Git.init().setDirectory(rootFolder()).call(); |
| 161 | + RefDatabase refDB = mainGit.getRepository().getRefDatabase(); |
| 162 | + refDB.newUpdate(Constants.R_HEADS + "main", false).setNewObjectId(ObjectId.zeroId()); |
| 163 | + refDB.newUpdate(Constants.HEAD, false).link(Constants.R_HEADS + "main"); |
| 164 | + refDB.newUpdate(Constants.R_HEADS + Constants.MASTER, false).delete(); |
| 165 | + |
| 166 | + setFile(TEST_PATH).toContent("HELLO"); |
| 167 | + mainGit.add().addFilepattern(TEST_PATH).call(); |
| 168 | + mainGit.commit().setMessage("Initial commit").call(); |
| 169 | + mainGit.tag().setName("baseline").call(); |
| 170 | + |
| 171 | + // Set up a worktree manually (JGit doesn't support worktrees) |
| 172 | + File worktreeDir = newFolder("worktree"); |
| 173 | + File mainGitDir = new File(rootFolder(), ".git"); |
| 174 | + File worktreeGitDir = new File(rootFolder(), ".git/worktrees/worktree"); |
| 175 | + |
| 176 | + // Create worktree structure |
| 177 | + setFile(".git/worktrees/worktree/gitdir").toContent(worktreeDir.getAbsolutePath() + "/.git"); |
| 178 | + setFile(".git/worktrees/worktree/commondir").toContent("../.."); |
| 179 | + setFile(".git/worktrees/worktree/HEAD").toContent("ref: refs/heads/main"); |
| 180 | + setFile("worktree/.git").toContent("gitdir: " + worktreeGitDir.getAbsolutePath()); |
| 181 | + |
| 182 | + // Copy the test file to worktree (same as baseline so ratchet considers it clean) |
| 183 | + setFile("worktree/" + TEST_PATH).toContent("HELLO"); |
| 184 | + |
| 185 | + // Copy Maven wrapper files to worktree |
| 186 | + setFile("worktree/.gitattributes").toContent("* text eol=lf"); |
| 187 | + Files.copy(new File(rootFolder(), "mvnw").toPath(), new File(worktreeDir, "mvnw").toPath()); |
| 188 | + new File(worktreeDir, "mvnw").setExecutable(true); |
| 189 | + Files.copy(new File(rootFolder(), "mvnw.cmd").toPath(), new File(worktreeDir, "mvnw.cmd").toPath()); |
| 190 | + new File(worktreeDir, ".mvn/wrapper").mkdirs(); |
| 191 | + Files.copy(new File(rootFolder(), ".mvn/wrapper/maven-wrapper.jar").toPath(), |
| 192 | + new File(worktreeDir, ".mvn/wrapper/maven-wrapper.jar").toPath()); |
| 193 | + Files.copy(new File(rootFolder(), ".mvn/wrapper/maven-wrapper.properties").toPath(), |
| 194 | + new File(worktreeDir, ".mvn/wrapper/maven-wrapper.properties").toPath()); |
| 195 | + |
| 196 | + // Create a pom.xml in the worktree |
| 197 | + setFile("worktree/pom.xml").toLines( |
| 198 | + "<project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'>", |
| 199 | + " <modelVersion>4.0.0</modelVersion>", |
| 200 | + " <groupId>test</groupId>", |
| 201 | + " <artifactId>test</artifactId>", |
| 202 | + " <version>1.0.0</version>", |
| 203 | + " <build>", |
| 204 | + " <plugins>", |
| 205 | + " <plugin>", |
| 206 | + " <groupId>com.diffplug.spotless</groupId>", |
| 207 | + " <artifactId>spotless-maven-plugin</artifactId>", |
| 208 | + " <version>" + System.getProperty("spotlessMavenPluginVersion") + "</version>", |
| 209 | + " <configuration>", |
| 210 | + " <formats>", |
| 211 | + " <format>", |
| 212 | + " <ratchetFrom>baseline</ratchetFrom>", |
| 213 | + " <includes>", |
| 214 | + " <include>" + TEST_PATH + "</include>", |
| 215 | + " </includes>", |
| 216 | + " <replace>", |
| 217 | + " <name>Lowercase hello</name>", |
| 218 | + " <search>HELLO</search>", |
| 219 | + " <replacement>hello</replacement>", |
| 220 | + " </replace>", |
| 221 | + " </format>", |
| 222 | + " </formats>", |
| 223 | + " </configuration>", |
| 224 | + " </plugin>", |
| 225 | + " </plugins>", |
| 226 | + " </build>", |
| 227 | + "</project>"); |
| 228 | + |
| 229 | + // Verify that spotless works correctly in a git worktree |
| 230 | + mavenRunner().withProjectDir(worktreeDir).withArguments("spotless:check").runNoError(); |
| 231 | + } |
153 | 232 | } |
0 commit comments