|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 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 | + * https://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.spotless.changelog.gradle; |
| 17 | + |
| 18 | + |
| 19 | +import com.diffplug.common.base.Preconditions; |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.List; |
| 23 | +import org.eclipse.jgit.api.Git; |
| 24 | +import org.eclipse.jgit.api.errors.GitAPIException; |
| 25 | +import org.eclipse.jgit.diff.DiffEntry; |
| 26 | +import org.eclipse.jgit.lib.ObjectId; |
| 27 | +import org.eclipse.jgit.lib.ObjectReader; |
| 28 | +import org.eclipse.jgit.lib.Repository; |
| 29 | +import org.eclipse.jgit.revwalk.RevWalk; |
| 30 | +import org.eclipse.jgit.storage.file.FileRepositoryBuilder; |
| 31 | +import org.eclipse.jgit.treewalk.CanonicalTreeParser; |
| 32 | +import org.eclipse.jgit.treewalk.filter.PathFilter; |
| 33 | +import org.eclipse.jgit.treewalk.filter.TreeFilter; |
| 34 | +import org.gradle.api.Action; |
| 35 | +import org.gradle.api.GradleException; |
| 36 | +import org.gradle.api.Project; |
| 37 | +import org.gradle.api.initialization.Settings; |
| 38 | + |
| 39 | +public abstract class IfGitDiffExtension<T> { |
| 40 | + static final String NAME = "ifGitDiff"; |
| 41 | + |
| 42 | + public static class ForProject extends IfGitDiffExtension<Project> { |
| 43 | + public ForProject(Project owner) { |
| 44 | + super(owner); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + protected File file(Object fileArg) { |
| 49 | + return owner.file(fileArg); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static class ForSettings extends IfGitDiffExtension<Settings> { |
| 54 | + public ForSettings(Settings owner) { |
| 55 | + super(owner); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + protected File file(Object fileArg) { |
| 60 | + if (fileArg instanceof File) { |
| 61 | + return (File) fileArg; |
| 62 | + } else if (fileArg instanceof String) { |
| 63 | + return new File(owner.getRootDir(), (String) fileArg); |
| 64 | + } else { |
| 65 | + throw new IllegalArgumentException("We only support String or File, this was " + fileArg.getClass()); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + final T owner; |
| 71 | + |
| 72 | + IfGitDiffExtension(T owner) { |
| 73 | + this.owner = owner; |
| 74 | + } |
| 75 | + |
| 76 | + private String baseline = "origin/main"; |
| 77 | + |
| 78 | + public void setBaseline(String baseline) { |
| 79 | + this.baseline = baseline; |
| 80 | + } |
| 81 | + |
| 82 | + public String getBaseline() { |
| 83 | + return baseline; |
| 84 | + } |
| 85 | + |
| 86 | + protected abstract File file(Object fileArg); |
| 87 | + |
| 88 | + private TreeFilter filterTo(Repository repo, File child) { |
| 89 | + String rootAbs = repo.getWorkTree().getAbsolutePath(); |
| 90 | + String childAbs = child.getAbsolutePath(); |
| 91 | + if (rootAbs.equals(childAbs)) { |
| 92 | + return TreeFilter.ALL; |
| 93 | + } else if (childAbs.startsWith(rootAbs)) { |
| 94 | + String filter = childAbs.substring(rootAbs.length()).replace('\\', '/'); |
| 95 | + Preconditions.checkState(filter.charAt(0) == '/'); |
| 96 | + return PathFilter.create(filter.substring(1)); |
| 97 | + } else { |
| 98 | + throw new GradleException(childAbs + " is not contained within the git repo " + rootAbs); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public void inFolder(Object folder, Action<T> onChanged) { |
| 103 | + try (Repository repo = new FileRepositoryBuilder() |
| 104 | + .findGitDir(file("")) |
| 105 | + .build()) { |
| 106 | + ObjectId baselineSha = repo.resolve(baseline); |
| 107 | + if (baselineSha == null) { |
| 108 | + throw new GradleException("Unable to resolve " + baseline); |
| 109 | + } |
| 110 | + |
| 111 | + CanonicalTreeParser baselineTree = new CanonicalTreeParser(); |
| 112 | + try (ObjectReader reader = repo.newObjectReader()) { |
| 113 | + RevWalk walk = new RevWalk(reader); |
| 114 | + baselineTree.reset(reader, walk.parseCommit(baselineSha).getTree()); |
| 115 | + } |
| 116 | + Git git = new Git(repo); |
| 117 | + List<DiffEntry> changes = git.diff() |
| 118 | + .setOldTree(baselineTree) |
| 119 | + .setShowNameAndStatusOnly(true) |
| 120 | + .setPathFilter(filterTo(repo, file(folder))) |
| 121 | + .call(); |
| 122 | + if (!changes.isEmpty()) { |
| 123 | + onChanged.execute(owner); |
| 124 | + } |
| 125 | + } catch (IOException e) { |
| 126 | + throw new GradleException("Unable to find git repository", e); |
| 127 | + } catch (GitAPIException e) { |
| 128 | + throw new RuntimeException(e); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments