|
| 1 | +/* |
| 2 | + * Copyright 2016 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.gradle.spotless; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.Objects; |
| 21 | +import java.util.TreeMap; |
| 22 | + |
| 23 | +import javax.annotation.Nullable; |
| 24 | + |
| 25 | +import org.eclipse.jgit.dircache.DirCache; |
| 26 | +import org.eclipse.jgit.dircache.DirCacheIterator; |
| 27 | +import org.eclipse.jgit.lib.Constants; |
| 28 | +import org.eclipse.jgit.lib.ObjectId; |
| 29 | +import org.eclipse.jgit.lib.Repository; |
| 30 | +import org.eclipse.jgit.lib.RepositoryCache; |
| 31 | +import org.eclipse.jgit.revwalk.RevCommit; |
| 32 | +import org.eclipse.jgit.revwalk.RevWalk; |
| 33 | +import org.eclipse.jgit.storage.file.FileRepositoryBuilder; |
| 34 | +import org.eclipse.jgit.treewalk.AbstractTreeIterator; |
| 35 | +import org.eclipse.jgit.treewalk.FileTreeIterator; |
| 36 | +import org.eclipse.jgit.treewalk.TreeWalk; |
| 37 | +import org.eclipse.jgit.treewalk.WorkingTreeIterator; |
| 38 | +import org.eclipse.jgit.treewalk.filter.AndTreeFilter; |
| 39 | +import org.eclipse.jgit.treewalk.filter.IndexDiffFilter; |
| 40 | +import org.eclipse.jgit.treewalk.filter.PathFilter; |
| 41 | +import org.eclipse.jgit.util.FS; |
| 42 | +import org.gradle.api.Project; |
| 43 | + |
| 44 | +import com.diffplug.common.base.Errors; |
| 45 | +import com.diffplug.common.collect.HashBasedTable; |
| 46 | +import com.diffplug.common.collect.Table; |
| 47 | + |
| 48 | +class GitRatchet implements AutoCloseable { |
| 49 | + /** There is a single GitRatchet instance shared across the entire Gradle build, this method helps you get it. */ |
| 50 | + private static GitRatchet instance(Project project) { |
| 51 | + return project.getPlugins().getPlugin(SpotlessPlugin.class).spotlessExtension.registerDependenciesTask.gitRatchet; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * This is the highest-level method, which all the others serve. Given the sha |
| 56 | + * of a git tree (not a commit!), and the file in question, this method returns |
| 57 | + * true if that file is clean relative to that tree. A naive implementation of this |
| 58 | + * could be verrrry slow, so the rest of this is about speeding this up. |
| 59 | + */ |
| 60 | + public static boolean isClean(Project project, ObjectId treeSha, File file) throws IOException { |
| 61 | + GitRatchet instance = instance(project); |
| 62 | + Repository repo = instance.repositoryFor(project); |
| 63 | + String path = repo.getWorkTree().toPath().relativize(file.toPath()).toString(); |
| 64 | + |
| 65 | + // TODO: should be cached-per-repo if it is thread-safe, or per-repo-per-thread if it is not |
| 66 | + DirCache dirCache = repo.readDirCache(); |
| 67 | + |
| 68 | + try (TreeWalk treeWalk = new TreeWalk(repo)) { |
| 69 | + treeWalk.addTree(treeSha); |
| 70 | + treeWalk.addTree(new DirCacheIterator(dirCache)); |
| 71 | + treeWalk.addTree(new FileTreeIterator(repo)); |
| 72 | + treeWalk.setFilter(AndTreeFilter.create( |
| 73 | + PathFilter.create(path), |
| 74 | + new IndexDiffFilter(INDEX, WORKDIR))); |
| 75 | + |
| 76 | + if (!treeWalk.next()) { |
| 77 | + // the file we care about is git clean |
| 78 | + return true; |
| 79 | + } else { |
| 80 | + AbstractTreeIterator treeIterator = treeWalk.getTree(TREE, AbstractTreeIterator.class); |
| 81 | + DirCacheIterator dirCacheIterator = treeWalk.getTree(INDEX, DirCacheIterator.class); |
| 82 | + WorkingTreeIterator workingTreeIterator = treeWalk.getTree(WORKDIR, WorkingTreeIterator.class); |
| 83 | + |
| 84 | + boolean hasTree = treeIterator != null; |
| 85 | + boolean hasDirCache = dirCacheIterator != null; |
| 86 | + |
| 87 | + if (!hasTree) { |
| 88 | + // it's not in the tree, so it was added |
| 89 | + return false; |
| 90 | + } else { |
| 91 | + if (hasDirCache) { |
| 92 | + boolean treeEqualsIndex = treeIterator.idEqual(dirCacheIterator) && treeIterator.getEntryRawMode() == dirCacheIterator.getEntryRawMode(); |
| 93 | + boolean indexEqualsWC = !workingTreeIterator.isModified(dirCacheIterator.getDirCacheEntry(), true, treeWalk.getObjectReader()); |
| 94 | + if (treeEqualsIndex != indexEqualsWC) { |
| 95 | + // if one is equal and the other isn't, then it has definitely changed |
| 96 | + return false; |
| 97 | + } else if (treeEqualsIndex) { |
| 98 | + // this means they are all equal to each other, which should never happen |
| 99 | + // the IndexDiffFilter should keep those out of the TreeWalk entirely |
| 100 | + throw new IllegalStateException("Index status for " + file + " against treeSha " + treeSha + " is invalid."); |
| 101 | + } else { |
| 102 | + // they are all unique |
| 103 | + // we have to check manually |
| 104 | + return worktreeIsCleanCheckout(treeWalk); |
| 105 | + } |
| 106 | + } else { |
| 107 | + // no dirCache, so we will compare the tree to the workdir manually |
| 108 | + return worktreeIsCleanCheckout(treeWalk); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** Returns true if the worktree file is a clean checkout of head (possibly smudged). */ |
| 116 | + private static boolean worktreeIsCleanCheckout(TreeWalk treeWalk) { |
| 117 | + return treeWalk.idEqual(TREE, WORKDIR); |
| 118 | + } |
| 119 | + |
| 120 | + private final static int TREE = 0; |
| 121 | + private final static int INDEX = 1; |
| 122 | + private final static int WORKDIR = 2; |
| 123 | + |
| 124 | + TreeMap<Project, Repository> gitRoots = new TreeMap<>(); |
| 125 | + Table<Repository, String, ObjectId> shaCache = HashBasedTable.create(); |
| 126 | + |
| 127 | + /** |
| 128 | + * The first part of making this fast is finding the appropriate git repository quickly. Because of composite |
| 129 | + * builds and submodules, it's quite possible that a single Gradle project will span across multiple git repositories. |
| 130 | + * We cache the Repository for every Project in `gitRoots`, and use dynamic programming to populate it. |
| 131 | + */ |
| 132 | + private Repository repositoryFor(Project project) throws IOException { |
| 133 | + Repository repo = gitRoots.get(project); |
| 134 | + if (repo == null) { |
| 135 | + if (isGitRoot(project.getProjectDir())) { |
| 136 | + repo = createRepo(project.getProjectDir()); |
| 137 | + } else { |
| 138 | + Project parentProj = project.getParent(); |
| 139 | + if (parentProj == null) { |
| 140 | + repo = traverseParentsUntil(project.getProjectDir().getParentFile(), null); |
| 141 | + if (repo == null) { |
| 142 | + throw new IllegalArgumentException("Cannot find git repository in any parent directory"); |
| 143 | + } |
| 144 | + } else { |
| 145 | + repo = traverseParentsUntil(project.getProjectDir().getParentFile(), parentProj.getProjectDir()); |
| 146 | + if (repo == null) { |
| 147 | + repo = repositoryFor(parentProj); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + gitRoots.put(project, repo); |
| 152 | + } |
| 153 | + return repo; |
| 154 | + } |
| 155 | + |
| 156 | + private static @Nullable Repository traverseParentsUntil(File startWith, File file) throws IOException { |
| 157 | + do { |
| 158 | + if (isGitRoot(startWith)) { |
| 159 | + return createRepo(startWith); |
| 160 | + } else { |
| 161 | + startWith = startWith.getParentFile(); |
| 162 | + } |
| 163 | + } while (!Objects.equals(startWith, file)); |
| 164 | + return null; |
| 165 | + } |
| 166 | + |
| 167 | + private static boolean isGitRoot(File dir) { |
| 168 | + File dotGit = new File(dir, Constants.DOT_GIT); |
| 169 | + return dotGit.isDirectory() && RepositoryCache.FileKey.isGitRepository(dotGit, FS.DETECTED); |
| 170 | + } |
| 171 | + |
| 172 | + static Repository createRepo(File dir) throws IOException { |
| 173 | + return FileRepositoryBuilder.create(new File(dir, Constants.DOT_GIT)); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Fast way to return treeSha of the given ref against the git repository which stores the given project. |
| 178 | + * Because of parallel project evaluation, there may be races here, so we synchronize on ourselves. However, this method |
| 179 | + * is the only method which can trigger any changes, and it is only called during project evaluation. That means our state |
| 180 | + * is final/read-only during task execution, so we don't need any locks during the heavy lifting. |
| 181 | + */ |
| 182 | + public static ObjectId treeShaOf(Project project, String reference) { |
| 183 | + GitRatchet instance = instance(project); |
| 184 | + synchronized (instance) { |
| 185 | + try { |
| 186 | + Repository repo = instance.repositoryFor(project); |
| 187 | + ObjectId treeSha = instance.shaCache.get(repo, reference); |
| 188 | + if (treeSha == null) { |
| 189 | + ObjectId commitSha = repo.resolve(reference); |
| 190 | + try (RevWalk revWalk = new RevWalk(repo)) { |
| 191 | + RevCommit revCommit = revWalk.parseCommit(commitSha); |
| 192 | + treeSha = revCommit.getTree(); |
| 193 | + } |
| 194 | + instance.shaCache.put(repo, reference, treeSha); |
| 195 | + } |
| 196 | + return treeSha; |
| 197 | + } catch (Exception e) { |
| 198 | + throw Errors.asRuntime(e); |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public void close() { |
| 205 | + gitRoots.values().stream() |
| 206 | + .distinct() |
| 207 | + .forEach(Repository::close); |
| 208 | + } |
| 209 | +} |
0 commit comments