|
| 1 | +/* |
| 2 | + * Copyright 2020 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.spotless.extra; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.eclipse.jgit.api.Git; |
| 24 | +import org.eclipse.jgit.api.errors.GitAPIException; |
| 25 | +import org.eclipse.jgit.lib.Constants; |
| 26 | +import org.eclipse.jgit.lib.ObjectId; |
| 27 | +import org.eclipse.jgit.lib.RefDatabase; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +import com.diffplug.spotless.ResourceHarness; |
| 31 | + |
| 32 | +public class GitRachetMergeBaseTest extends ResourceHarness { |
| 33 | + @Test |
| 34 | + public void test() throws IllegalStateException, GitAPIException, IOException { |
| 35 | + try (Git git = initRepo()) { |
| 36 | + setFile("mine.txt").toContent("init"); |
| 37 | + setFile("untouched.txt").toContent("init"); |
| 38 | + addAndCommit(git, "init"); |
| 39 | + |
| 40 | + git.checkout().setCreateBranch(true).setName("rem-branch").call(); |
| 41 | + // so at the point where we start work, we are clean relative to the remote |
| 42 | + ratchetFrom("main", "rem-branch").allClean(); |
| 43 | + |
| 44 | + // but when the remote changes |
| 45 | + setFile("untouched.txt").toContent("changed"); |
| 46 | + addAndCommit(git, "remote"); |
| 47 | + |
| 48 | + // it shouldn't affect files that we haven't changed |
| 49 | + git.checkout().setName("main").call(); |
| 50 | + ratchetFrom("main", "rem-branch").allClean(); |
| 51 | + |
| 52 | + // and when we work, it should continue to only affect our own work |
| 53 | + setFile("mine.txt").toContent("changed"); |
| 54 | + ratchetFrom("main", "rem-branch").onlyDirty("mine.txt"); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + static class GitRatchetSimple extends GitRatchet<File> { |
| 59 | + @Override |
| 60 | + protected File getDir(File project) { |
| 61 | + return project; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected File getParent(File project) { |
| 66 | + return project.getParentFile(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + Asserter ratchetFrom(String... ratchetFroms) { |
| 71 | + return new Asserter(ratchetFroms); |
| 72 | + } |
| 73 | + |
| 74 | + class Asserter { |
| 75 | + final GitRatchetSimple ratchet = new GitRatchetSimple(); |
| 76 | + final String[] ratchetFroms; |
| 77 | + final ObjectId[] shas; |
| 78 | + |
| 79 | + Asserter(String... ratchetFrom) { |
| 80 | + this.ratchetFroms = ratchetFrom; |
| 81 | + this.shas = Arrays.stream(ratchetFrom) |
| 82 | + .map(from -> ratchet.rootTreeShaOf(rootFolder(), from)) |
| 83 | + .toArray(ObjectId[]::new); |
| 84 | + } |
| 85 | + |
| 86 | + private void assertClean(int i, String filename, boolean expected) throws IOException { |
| 87 | + boolean actual = ratchet.isClean(rootFolder(), shas[i], newFile(filename)); |
| 88 | + if (actual != expected) { |
| 89 | + throw new AssertionError("Expected " + filename + " to be " + (expected ? "clean" : "dirty") + " relative to " + ratchetFroms[i]); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public void allClean() throws IOException { |
| 94 | + onlyDirty(); |
| 95 | + } |
| 96 | + |
| 97 | + public void allDirty() throws IOException { |
| 98 | + String[] filenames = Arrays.stream(rootFolder().listFiles()) |
| 99 | + .filter(File::isFile) |
| 100 | + .map(File::getName) |
| 101 | + .toArray(String[]::new); |
| 102 | + onlyDirty(filenames); |
| 103 | + } |
| 104 | + |
| 105 | + public void onlyDirty(String... filenames) throws IOException { |
| 106 | + List<String> dirtyFiles = Arrays.asList(filenames); |
| 107 | + for (File file : rootFolder().listFiles()) { |
| 108 | + if (!file.isFile()) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + boolean expectedClean = !dirtyFiles.contains(file.getName()); |
| 112 | + for (int i = 0; i < shas.length; ++i) { |
| 113 | + assertClean(i, file.getName(), expectedClean); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private Git initRepo() throws IllegalStateException, GitAPIException, IOException { |
| 120 | + Git git = Git.init().setDirectory(rootFolder()).call(); |
| 121 | + RefDatabase refDB = git.getRepository().getRefDatabase(); |
| 122 | + refDB.newUpdate(Constants.R_HEADS + "main", false).setNewObjectId(ObjectId.zeroId()); |
| 123 | + refDB.newUpdate(Constants.HEAD, false).link(Constants.R_HEADS + "main"); |
| 124 | + refDB.newUpdate(Constants.R_HEADS + Constants.MASTER, false).delete(); |
| 125 | + return git; |
| 126 | + } |
| 127 | + |
| 128 | + private void addAndCommit(Git git, String message) throws GitAPIException { |
| 129 | + git.add().addFilepattern(".").call(); |
| 130 | + git.commit().setMessage(message).call(); |
| 131 | + } |
| 132 | +} |
0 commit comments