|
| 1 | +/* |
| 2 | + * This file is part of git-commit-id-plugin-core by Konrad 'ktoso' Malawski <konrad.malawski@java.pl> |
| 3 | + * |
| 4 | + * git-commit-id-plugin-core is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Lesser General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * git-commit-id-plugin-core is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public License |
| 15 | + * along with git-commit-id-plugin-core. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package pl.project13.core.util; |
| 19 | + |
| 20 | +import java.io.BufferedReader; |
| 21 | +import java.io.File; |
| 22 | +import java.io.FileReader; |
| 23 | +import java.io.IOException; |
| 24 | +import java.nio.file.Path; |
| 25 | +import javax.annotation.Nonnull; |
| 26 | +import javax.annotation.Nullable; |
| 27 | +import org.eclipse.jgit.lib.Constants; |
| 28 | + |
| 29 | +/** |
| 30 | + * This class encapsulates logic to locate a valid .git directory of the currently used project. If |
| 31 | + * it's not already specified, this logic will try to find it. |
| 32 | + */ |
| 33 | +public class GitDirLocator { |
| 34 | + final File projectBasedir; |
| 35 | + |
| 36 | + /** |
| 37 | + * Constructor to encapsulates all references required to locate a valid .git directory |
| 38 | + * |
| 39 | + * @param projectBasedir The project basedir that will be used as last resort to search |
| 40 | + * the parent project hierarchy until a .git directory is found. |
| 41 | + */ |
| 42 | + public GitDirLocator(File projectBasedir) { |
| 43 | + this.projectBasedir = projectBasedir; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Attempts to lookup a valid .git directory of the currently used project. |
| 48 | + * |
| 49 | + * @param manuallyConfiguredDir A user has the ability to configure a git-directory with the |
| 50 | + * {@code dotGitDirectory} configuration setting. By default it should be simply {@code |
| 51 | + * ${project.basedir}/.git} |
| 52 | + * @return A valid .git directory, or {@code null} if none could be found under the user specified |
| 53 | + * location or within the project or it's reactor projects. |
| 54 | + */ |
| 55 | + @Nullable |
| 56 | + public File lookupGitDirectory(@Nonnull File manuallyConfiguredDir) { |
| 57 | + if (manuallyConfiguredDir.exists()) { |
| 58 | + |
| 59 | + // If manuallyConfiguredDir is a directory then we can use it as the git path. |
| 60 | + if (manuallyConfiguredDir.isDirectory()) { |
| 61 | + return manuallyConfiguredDir; |
| 62 | + } |
| 63 | + |
| 64 | + // If the path exists but is not a directory it might be a git submodule "gitdir" link. |
| 65 | + File gitDirLinkPath = processGitDirFile(manuallyConfiguredDir); |
| 66 | + |
| 67 | + // If the linkPath was found from the file and it exists then use it. |
| 68 | + if (isExistingDirectory(gitDirLinkPath)) { |
| 69 | + return gitDirLinkPath; |
| 70 | + } |
| 71 | + |
| 72 | + /* |
| 73 | + * FIXME: I think we should fail here because a manual path was set and it was not found |
| 74 | + * but I'm leaving it falling back to searching for the git path because that is the current |
| 75 | + * behaviour - Unluckypixie. |
| 76 | + */ |
| 77 | + } |
| 78 | + |
| 79 | + return findProjectGitDirectory(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Search up all the parent project hierarchy until a .git directory is found. |
| 84 | + * |
| 85 | + * @return File which represents the location of the .git directory or NULL if none found. |
| 86 | + */ |
| 87 | + @Nullable |
| 88 | + private File findProjectGitDirectory() { |
| 89 | + File basedir = this.projectBasedir; |
| 90 | + while (basedir != null) { |
| 91 | + File gitdir = new File(basedir, Constants.DOT_GIT); |
| 92 | + if (gitdir.exists()) { |
| 93 | + if (gitdir.isDirectory()) { |
| 94 | + return gitdir; |
| 95 | + } else if (gitdir.isFile()) { |
| 96 | + return processGitDirFile(gitdir); |
| 97 | + } else { |
| 98 | + return null; |
| 99 | + } |
| 100 | + } |
| 101 | + basedir = basedir.getParentFile(); |
| 102 | + } |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Load a ".git" git submodule file and read the gitdir path from it. |
| 108 | + * |
| 109 | + * @return File object with path loaded or null |
| 110 | + */ |
| 111 | + private File processGitDirFile(@Nonnull File file) { |
| 112 | + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { |
| 113 | + // There should be just one line in the file, e.g. |
| 114 | + // "gitdir: /usr/local/src/parentproject/.git/modules/submodule" |
| 115 | + String line = reader.readLine(); |
| 116 | + if (line == null) { |
| 117 | + return null; |
| 118 | + } |
| 119 | + // Separate the key and the value in the string. |
| 120 | + String[] parts = line.split(": "); |
| 121 | + |
| 122 | + // If we don't have 2 parts or if the key is not gitdir then give up. |
| 123 | + if (parts.length != 2 || !parts[0].equals("gitdir")) { |
| 124 | + return null; |
| 125 | + } |
| 126 | + |
| 127 | + // All seems ok so return the "gitdir" value read from the file. |
| 128 | + String extractFromConfig = parts[1]; |
| 129 | + File gitDir = resolveWorktree(new File(extractFromConfig)); |
| 130 | + if (gitDir.isAbsolute()) { |
| 131 | + // gitdir value is an absolute path. Return as-is |
| 132 | + return gitDir; |
| 133 | + } else { |
| 134 | + // gitdir value is relative. |
| 135 | + return new File(file.getParentFile(), extractFromConfig); |
| 136 | + } |
| 137 | + } catch (IOException e) { |
| 138 | + return null; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Attempts to resolve the actual location of the .git folder for a given |
| 144 | + * worktree. |
| 145 | + * For example for a worktree like {@code a/.git/worktrees/X} structure would |
| 146 | + * return {@code a/.git}. |
| 147 | + * |
| 148 | + * If the conditions for a git worktree like file structure are met simply return the provided |
| 149 | + * argument as is. |
| 150 | + */ |
| 151 | + static File resolveWorktree(File fileLocation) { |
| 152 | + Path parent = fileLocation.toPath().getParent(); |
| 153 | + if (parent == null) { |
| 154 | + return fileLocation; |
| 155 | + } |
| 156 | + if (parent.endsWith(Path.of(".git", "worktrees"))) { |
| 157 | + return parent.getParent().toFile(); |
| 158 | + } |
| 159 | + return fileLocation; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Helper method to validate that the specified {@code File} is an existing directory. |
| 164 | + * |
| 165 | + * @param fileLocation The {@code File} that should be checked if it's actually an existing |
| 166 | + * directory. |
| 167 | + * @return {@code true} if the specified {@code File} is an existing directory, {@false} |
| 168 | + * otherwise. |
| 169 | + */ |
| 170 | + private static boolean isExistingDirectory(@Nullable File fileLocation) { |
| 171 | + return fileLocation != null && fileLocation.exists() && fileLocation.isDirectory(); |
| 172 | + } |
| 173 | +} |
0 commit comments