001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.scm.provider.git; 020 021import java.io.File; 022import java.io.FileWriter; 023import java.io.IOException; 024import java.util.function.Consumer; 025 026import org.apache.maven.scm.PlexusJUnit4TestCase; 027import org.codehaus.plexus.util.FileUtils; 028import org.codehaus.plexus.util.cli.CommandLineException; 029import org.junit.Assert; 030 031/** 032 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a> 033 */ 034public final class GitScmTestUtils { 035 /** 'git' command line. */ 036 public static final String GIT_COMMAND_LINE = "git"; 037 038 private GitScmTestUtils() {} 039 040 public static void initRepo(File repository, File workingDirectory, File assertionDirectory) throws IOException { 041 initRepo("src/test/repository/", repository, workingDirectory); 042 043 FileUtils.deleteDirectory(assertionDirectory); 044 045 Assert.assertTrue(assertionDirectory.mkdirs()); 046 } 047 048 public static void initRepo(String source, File repository, File workingDirectory) throws IOException { 049 // Copy the repository to target 050 File src = PlexusJUnit4TestCase.getTestFile(source); 051 052 FileUtils.deleteDirectory(repository); 053 054 Assert.assertTrue(repository.mkdirs()); 055 056 FileUtils.copyDirectoryStructure(src, repository); 057 058 File dotGitDirectory = new File(src, "dotgit"); 059 060 if (dotGitDirectory.exists()) { 061 FileUtils.copyDirectoryStructure(dotGitDirectory, new File(repository, ".git")); 062 } 063 064 FileUtils.deleteDirectory(workingDirectory); 065 066 Assert.assertTrue(workingDirectory.mkdirs()); 067 } 068 069 public static String getScmUrl(File repositoryRootFile, String provider) throws CommandLineException { 070 return "scm:" + provider + ":" 071 + repositoryRootFile.toPath().toAbsolutePath().toUri().toASCIIString(); 072 } 073 074 public static void deleteAllDirectories(File startDirectory, String pattern) throws IOException { 075 if (startDirectory.isDirectory()) { 076 File[] childs = startDirectory.listFiles(); 077 for (int i = 0; i < childs.length; i++) { 078 File child = childs[i]; 079 if (child.isDirectory()) { 080 if (child.getName().equals(pattern)) { 081 FileUtils.deleteDirectory(child); 082 } else { 083 deleteAllDirectories(child, pattern); 084 } 085 } 086 } 087 } 088 } 089 090 public static void setDefaultGitConfig(File repositoryRootFile) throws IOException { 091 setDefaultGitConfig(repositoryRootFile, null); 092 } 093 094 public static void setDefaultGitConfig(File repositoryRootFile, Consumer<FileWriter> configWriterCustomizer) 095 throws IOException { 096 File gitConfigFile = new File(new File(repositoryRootFile, ".git"), "config"); 097 098 try (FileWriter fw = new FileWriter(gitConfigFile, true)) { 099 fw.append("[user]\n"); 100 fw.append("\tname = John Doe\n"); 101 fw.append("\temail = john.doe@nowhere.com\n"); 102 fw.append("[commit]\n"); 103 // disable gpg signing for commits and tags by default 104 fw.append("\tgpgsign = false\n"); 105 fw.append("[tag]\n"); 106 fw.append("\tgpgsign = false\n"); 107 if (configWriterCustomizer != null) { 108 configWriterCustomizer.accept(fw); 109 } 110 } 111 } 112 113 /** 114 * Sets up a pre-receive git hook that rejects all commits for the given repository (server-side). 115 * This is useful for testing scenarios where you want to simulate a repository 116 * that doesn't allow any new commits. 117 * This hook is <a href="https://github.com/eclipse-jgit/jgit/issues/192">not supported by JGit</a>. 118 * 119 * @param repositoryRootFile the root directory of the git repository 120 * @throws IOException if there's an error creating or writing the hook file 121 */ 122 public static void setupRejectAllCommitsPreReceiveHook(File repositoryRootFile) throws IOException { 123 setupRejectAllCommitsHook(repositoryRootFile, true, "pre-receive"); 124 } 125 126 /** 127 * Sets up a pre-push git hook that rejects all commits for the given repository (client-side). 128 * This is useful for testing scenarios where you want to simulate a repository 129 * that doesn't allow any new commits. 130 * 131 * @param workspaceRoot the root directory of the git working copy 132 * @throws IOException if there's an error creating or writing the hook file 133 */ 134 public static void setupRejectAllCommitsPrePushHook(File workspaceRoot) throws IOException { 135 setupRejectAllCommitsHook(workspaceRoot, false, "pre-push"); 136 } 137 138 public static void setupRejectAllCommitsPreCommitHook(File workspaceRoot) throws IOException { 139 setupRejectAllCommitsHook(workspaceRoot, false, "pre-commit"); 140 } 141 142 private static void setupRejectAllCommitsHook( 143 File repositoryOrWorkspaceRootFile, boolean isServerSide, String hookName) throws IOException { 144 File hooksDir; 145 if (!isServerSide) { 146 // For client-side hooks, we use the .git/hooks directory 147 hooksDir = new File(repositoryOrWorkspaceRootFile, ".git/hooks"); 148 } else { 149 // For server-side hooks, we use the hooks directory directly 150 hooksDir = new File(repositoryOrWorkspaceRootFile, "hooks"); 151 } 152 if (!hooksDir.exists() && !hooksDir.mkdirs()) { 153 throw new IOException("Failed to create hooks directory: " + hooksDir.getAbsolutePath()); 154 } 155 156 File preReceiveHook = new File(hooksDir, hookName); 157 try (FileWriter fw = new FileWriter(preReceiveHook)) { 158 fw.write("#!/bin/sh\n"); 159 fw.write("# Pre-receive hook that rejects all commits\n"); 160 fw.write("echo \"Error: This repository is configured to reject all commits\"\n"); 161 fw.write("exit 1\n"); 162 } 163 164 // Make the hook executable (on Unix-like systems) 165 if (!preReceiveHook.setExecutable(true)) { 166 throw new IOException("Could not make pre-receive hook executable at: " + preReceiveHook.getAbsolutePath()); 167 } 168 } 169}