|
| 1 | +/* |
| 2 | + * Copyright 2024 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; |
| 17 | + |
| 18 | +import java.lang.annotation.ElementType; |
| 19 | +import java.lang.annotation.Retention; |
| 20 | +import java.lang.annotation.RetentionPolicy; |
| 21 | +import java.lang.annotation.Target; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.eclipse.jgit.lib.StoredConfig; |
| 25 | +import org.eclipse.jgit.util.SystemReader; |
| 26 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
| 27 | +import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 28 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 29 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 30 | +import org.junit.jupiter.api.parallel.ResourceAccessMode; |
| 31 | +import org.junit.jupiter.api.parallel.ResourceLock; |
| 32 | + |
| 33 | +@Retention(RetentionPolicy.RUNTIME) |
| 34 | +@Target({ElementType.TYPE, ElementType.METHOD}) |
| 35 | +@ExtendWith(ClearGitConfig.GitConfigExtension.class) |
| 36 | +@ResourceLock(value = "GIT", mode = ResourceAccessMode.READ_WRITE) |
| 37 | +public @interface ClearGitConfig { |
| 38 | + |
| 39 | + class GitConfigExtension implements BeforeEachCallback, AfterEachCallback { |
| 40 | + |
| 41 | + @Override |
| 42 | + public void beforeEach(ExtensionContext extensionContext) throws Exception { |
| 43 | + for (var config : getConfigs()) { |
| 44 | + config.clear(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void afterEach(ExtensionContext extensionContext) throws Exception { |
| 50 | + for (var config : getConfigs()) { |
| 51 | + config.load(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private static List<StoredConfig> getConfigs() throws Exception { |
| 56 | + var reader = SystemReader.getInstance(); |
| 57 | + return List.of(reader.getUserConfig(), reader.getSystemConfig()); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments