From 0d17e0627e7fc7a1a90d3eab27f03658fe337eb8 Mon Sep 17 00:00:00 2001 From: TheSnoozer Date: Sat, 2 Mar 2024 18:08:10 +0100 Subject: [PATCH] remove logic that was moved to the git-commit-id-plugin-core --- .../project13/maven/git/GitCommitIdMojo.java | 28 --- .../pl/project13/maven/git/GitDirLocator.java | 174 ------------------ .../maven/git/GitDirLocatorTest.java | 120 ------------ 3 files changed, 322 deletions(-) delete mode 100644 src/main/java/pl/project13/maven/git/GitDirLocator.java delete mode 100644 src/test/java/pl/project13/maven/git/GitDirLocatorTest.java diff --git a/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java b/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java index 6a5c9ed2..f956da32 100644 --- a/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java +++ b/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java @@ -1207,24 +1207,10 @@ public void error(String msg, Throwable t) { return; } - dotGitDirectory = lookupGitDirectory(); - if (failOnNoGitDirectory && !directoryExists(dotGitDirectory)) { - throw new GitCommitIdExecutionException( - ".git directory is not found! Please specify a valid [dotGitDirectory] in your" - + " pom.xml"); - } - if (gitDescribe == null) { gitDescribe = new GitDescribeConfig(); } - if (dotGitDirectory != null) { - log.info("dotGitDirectory '" + dotGitDirectory.getAbsolutePath() + "'"); - } else { - log.info("dotGitDirectory is null, aborting execution!"); - return; - } - try { commitIdGenerationModeEnum = CommitIdGenerationMode.valueOf(commitIdGenerationMode.toUpperCase()); @@ -1503,16 +1489,6 @@ private void appendPropertiesToReactorProjects(LogInterface log, Properties prop log.info("Added properties to '" + reactorProjects.size() + "' projects"); } - /** - * Find the git directory of the currently used project. If it's not already specified, this - * method will try to find it. - * - * @return the File representation of the .git directory - */ - private File lookupGitDirectory() throws GitCommitIdExecutionException { - return new GitDirLocator(project.getBasedir()).lookupGitDirectory(dotGitDirectory); - } - private void logProperties(LogInterface log, Properties propertiesToPublish) { for (String propertyName : propertiesToPublish.stringPropertyNames()) { log.info("including property '" + propertyName + "' in results"); @@ -1522,8 +1498,4 @@ private void logProperties(LogInterface log, Properties propertiesToPublish) { private boolean isPomProject(@Nonnull MavenProject project) { return project.getPackaging().equalsIgnoreCase("pom"); } - - private boolean directoryExists(@Nullable File fileLocation) { - return fileLocation != null && fileLocation.exists() && fileLocation.isDirectory(); - } } diff --git a/src/main/java/pl/project13/maven/git/GitDirLocator.java b/src/main/java/pl/project13/maven/git/GitDirLocator.java deleted file mode 100644 index 59d3bd23..00000000 --- a/src/main/java/pl/project13/maven/git/GitDirLocator.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * This file is part of git-commit-id-maven-plugin - * Originally invented by Konrad 'ktoso' Malawski - * - * git-commit-id-maven-plugin is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * git-commit-id-maven-plugin is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with git-commit-id-maven-plugin. If not, see . - */ - -package pl.project13.maven.git; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.nio.file.Path; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import org.eclipse.jgit.lib.Constants; - -/** - * This class encapsulates logic to locate a valid .git directory of the currently used project. If - * it's not already specified, this logic will try to find it. - */ -public class GitDirLocator { - final File projectBasedir; - - /** - * Constructor to encapsulates all references required to locate a valid .git directory - * - * @param projectBasedir The project basedir that will be used as last resort to search - * the parent project hierarchy until a .git directory is found. - */ - public GitDirLocator(File projectBasedir) { - this.projectBasedir = projectBasedir; - } - - /** - * Attempts to lookup a valid .git directory of the currently used project. - * - * @param manuallyConfiguredDir A user has the ability to configure a git-directory with the - * {@code dotGitDirectory} configuration setting. By default it should be simply {@code - * ${project.basedir}/.git} - * @return A valid .git directory, or {@code null} if none could be found under the user specified - * location or within the project or it's reactor projects. - */ - @Nullable - public File lookupGitDirectory(@Nonnull File manuallyConfiguredDir) { - if (manuallyConfiguredDir.exists()) { - - // If manuallyConfiguredDir is a directory then we can use it as the git path. - if (manuallyConfiguredDir.isDirectory()) { - return manuallyConfiguredDir; - } - - // If the path exists but is not a directory it might be a git submodule "gitdir" link. - File gitDirLinkPath = processGitDirFile(manuallyConfiguredDir); - - // If the linkPath was found from the file and it exists then use it. - if (isExistingDirectory(gitDirLinkPath)) { - return gitDirLinkPath; - } - - /* - * FIXME: I think we should fail here because a manual path was set and it was not found - * but I'm leaving it falling back to searching for the git path because that is the current - * behaviour - Unluckypixie. - */ - } - - return findProjectGitDirectory(); - } - - /** - * Search up all the parent project hierarchy until a .git directory is found. - * - * @return File which represents the location of the .git directory or NULL if none found. - */ - @Nullable - private File findProjectGitDirectory() { - File basedir = this.projectBasedir; - while (basedir != null) { - File gitdir = new File(basedir, Constants.DOT_GIT); - if (gitdir.exists()) { - if (gitdir.isDirectory()) { - return gitdir; - } else if (gitdir.isFile()) { - return processGitDirFile(gitdir); - } else { - return null; - } - } - basedir = basedir.getParentFile(); - } - return null; - } - - /** - * Load a ".git" git submodule file and read the gitdir path from it. - * - * @return File object with path loaded or null - */ - private File processGitDirFile(@Nonnull File file) { - try (BufferedReader reader = new BufferedReader(new FileReader(file))) { - // There should be just one line in the file, e.g. - // "gitdir: /usr/local/src/parentproject/.git/modules/submodule" - String line = reader.readLine(); - if (line == null) { - return null; - } - // Separate the key and the value in the string. - String[] parts = line.split(": "); - - // If we don't have 2 parts or if the key is not gitdir then give up. - if (parts.length != 2 || !parts[0].equals("gitdir")) { - return null; - } - - // All seems ok so return the "gitdir" value read from the file. - String extractFromConfig = parts[1]; - File gitDir = resolveWorktree(new File(extractFromConfig)); - if (gitDir.isAbsolute()) { - // gitdir value is an absolute path. Return as-is - return gitDir; - } else { - // gitdir value is relative. - return new File(file.getParentFile(), extractFromConfig); - } - } catch (IOException e) { - return null; - } - } - - /** - * Attempts to resolve the actual location of the .git folder for a given - * worktree. - * For example for a worktree like {@code a/.git/worktrees/X} structure would - * return {@code a/.git}. - * - * If the conditions for a git worktree like file structure are met simply return the provided - * argument as is. - */ - static File resolveWorktree(File fileLocation) { - Path parent = fileLocation.toPath().getParent(); - if (parent == null) { - return fileLocation; - } - if (parent.endsWith(Path.of(".git", "worktrees"))) { - return parent.getParent().toFile(); - } - return fileLocation; - } - - /** - * Helper method to validate that the specified {@code File} is an existing directory. - * - * @param fileLocation The {@code File} that should be checked if it's actually an existing - * directory. - * @return {@code true} if the specified {@code File} is an existing directory, {@false} - * otherwise. - */ - private static boolean isExistingDirectory(@Nullable File fileLocation) { - return fileLocation != null && fileLocation.exists() && fileLocation.isDirectory(); - } -} diff --git a/src/test/java/pl/project13/maven/git/GitDirLocatorTest.java b/src/test/java/pl/project13/maven/git/GitDirLocatorTest.java deleted file mode 100644 index 63761043..00000000 --- a/src/test/java/pl/project13/maven/git/GitDirLocatorTest.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * This file is part of git-commit-id-maven-plugin - * Originally invented by Konrad 'ktoso' Malawski - * - * git-commit-id-maven-plugin is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * git-commit-id-maven-plugin is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with git-commit-id-maven-plugin. If not, see . - */ - -package pl.project13.maven.git; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.File; -import java.nio.file.Files; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; - -@RunWith(MockitoJUnitRunner.class) -public class GitDirLocatorTest { - @Rule - public TemporaryFolder folder = new TemporaryFolder(); - - @Test - public void shouldUseTheManuallySpecifiedDirectory() throws Exception { - // given - File dotGitDir = folder.newFolder("temp"); - try { - // when - GitDirLocator locator = new GitDirLocator(dotGitDir); - File foundDirectory = locator.lookupGitDirectory(dotGitDir); - - // then - assertThat(foundDirectory).isNotNull(); - assertThat(foundDirectory.getAbsolutePath()).isEqualTo(dotGitDir.getAbsolutePath()); - } finally { - if (!dotGitDir.delete()) { - dotGitDir.deleteOnExit(); - } - } - } - - @Test - public void shouldResolveRelativeSubmodule() throws Exception { - // given - folder.newFolder("main-project"); - folder.newFolder("main-project", ".git", "modules", "sub-module"); - folder.newFolder("main-project", "sub-module"); - - // and a .git dir in submodule that points to the main's project .git/modules/submodule - File dotGitDir = folder.getRoot().toPath() - .resolve("main-project") - .resolve("sub-module") - .resolve(".git") - .toFile(); - Files.write( - dotGitDir.toPath(), - "gitdir: ../.git/modules/sub-module".getBytes() - ); - - try { - // when - GitDirLocator locator = new GitDirLocator(dotGitDir); - File foundDirectory = locator.lookupGitDirectory(dotGitDir); - - // then - assertThat(foundDirectory).isNotNull(); - assertThat( - foundDirectory.getCanonicalFile() - ).isEqualTo( - folder.getRoot().toPath() - .resolve("main-project") - .resolve(".git") - .resolve("modules") - .resolve("sub-module") - .toFile() - ); - } finally { - if (!dotGitDir.delete()) { - dotGitDir.deleteOnExit(); - } - } - } - - @Test - public void testWorktreeResolution() { - // tests to ensure we do not try to modify things that should not be modified - String[] noopCases = { - "", - "a", - "a/b", - ".git/worktrees", - ".git/worktrees/", - "a.git/worktrees/b", - ".git/modules", - ".git/modules/", - "a.git/modules/b", - }; - for (String path : noopCases) { - assertThat(GitDirLocator.resolveWorktree(new File(path))).isEqualTo(new File(path)); - } - // tests that worktree resolution works - assertThat(GitDirLocator.resolveWorktree(new File("a/.git/worktrees/b"))) - .isEqualTo(new File("a/.git")); - assertThat(GitDirLocator.resolveWorktree(new File("/a/.git/worktrees/b"))) - .isEqualTo(new File("/a/.git")); - } -}