|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.commons.release.plugin.internal; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.apache.commons.exec.CommandLine; |
| 26 | +import org.apache.commons.exec.DefaultExecutor; |
| 27 | +import org.apache.commons.exec.Executor; |
| 28 | +import org.apache.commons.exec.PumpStreamHandler; |
| 29 | +import org.apache.commons.exec.environment.EnvironmentUtils; |
| 30 | +import org.apache.commons.io.output.NullOutputStream; |
| 31 | + |
| 32 | +/** |
| 33 | + * Builds real Git fixtures on disk by invoking the {@code git} CLI via Commons Exec |
| 34 | + */ |
| 35 | +final class GitFixture { |
| 36 | + |
| 37 | + static final String REPO_BRANCH = "foo"; |
| 38 | + static final String WORKTREE_BRANCH = "bar"; |
| 39 | + static final String SUBDIR = "subdir"; |
| 40 | + /** |
| 41 | + * SHA-1 of the single commit produced by {@link #createRepoAndWorktree}; deterministic thanks to {@link #ENV}. |
| 42 | + */ |
| 43 | + static final String INITIAL_COMMIT_SHA = "a2782b3461d2ed2a81193da1139f65bf9d2befc2"; |
| 44 | + |
| 45 | + /** |
| 46 | + * Process environment with fixed author/committer dates so commit SHAs are stable across runs. |
| 47 | + */ |
| 48 | + private static final Map<String, String> ENV; |
| 49 | + |
| 50 | + static { |
| 51 | + try { |
| 52 | + final Map<String, String> env = EnvironmentUtils.getProcEnvironment(); |
| 53 | + env.put("GIT_AUTHOR_DATE", "2026-01-01T00:00:00Z"); |
| 54 | + env.put("GIT_COMMITTER_DATE", "2026-01-01T00:00:00Z"); |
| 55 | + ENV = env; |
| 56 | + } catch (final IOException e) { |
| 57 | + throw new ExceptionInInitializerError(e); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Creates a Git repo for testing. |
| 63 | + * |
| 64 | + * @param repo Path to the repository to create |
| 65 | + * @param worktree Path to a separate worktree to create |
| 66 | + */ |
| 67 | + static void createRepoAndWorktree(final Path repo, final Path worktree) throws IOException { |
| 68 | + final Path subdir = repo.resolve(SUBDIR); |
| 69 | + Files.createDirectories(subdir); |
| 70 | + git(repo, "init", "-q", "."); |
| 71 | + // Put HEAD on 'foo' before the first commit (portable to older git without --initial-branch). |
| 72 | + git(repo, "symbolic-ref", "HEAD", "refs/heads/" + REPO_BRANCH); |
| 73 | + git(repo, "config", "user.email", "test@example.invalid"); |
| 74 | + git(repo, "config", "user.name", "Test"); |
| 75 | + git(repo, "config", "commit.gpgsign", "false"); |
| 76 | + final Path readme = subdir.resolve("README"); |
| 77 | + Files.write(readme, "hi\n".getBytes(StandardCharsets.UTF_8)); |
| 78 | + git(repo, "add", repo.relativize(readme).toString()); |
| 79 | + git(repo, "commit", "-q", "-m", "init"); |
| 80 | + git(repo, "branch", WORKTREE_BRANCH); |
| 81 | + git(repo, "worktree", "add", "-q", repo.relativize(worktree).toString(), "bar"); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Runs {@code git} with the given args; stdout is discarded, stderr is forwarded to {@link System#err}. |
| 86 | + */ |
| 87 | + static void git(final Path workingDir, final String... args) throws IOException { |
| 88 | + final CommandLine cmd = new CommandLine("git"); |
| 89 | + for (final String a : args) { |
| 90 | + cmd.addArgument(a, false); |
| 91 | + } |
| 92 | + final Executor exec = DefaultExecutor.builder().setWorkingDirectory(workingDir.toFile()).get(); |
| 93 | + exec.setStreamHandler(new PumpStreamHandler(NullOutputStream.INSTANCE, System.err)); |
| 94 | + exec.execute(cmd, ENV); |
| 95 | + } |
| 96 | + |
| 97 | + private GitFixture() { |
| 98 | + } |
| 99 | +} |
0 commit comments