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; 024 025import org.apache.maven.scm.PlexusJUnit4TestCase; 026import org.codehaus.plexus.util.FileUtils; 027import org.codehaus.plexus.util.cli.CommandLineException; 028import org.junit.Assert; 029 030/** 031 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a> 032 * 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 setDefaulGitConfig(File repositoryRootFile) { 091 File gitConfigFile = new File(new File(repositoryRootFile, ".git"), "config"); 092 093 try (FileWriter fw = new FileWriter(gitConfigFile, true)) { 094 fw.append("[user]\n"); 095 fw.append("\tname = John Doe\n"); 096 fw.append("\temail = john.doe@nowhere.com\n"); 097 fw.append("[commit]\n"); 098 fw.append("\tgpgsign = false\n"); 099 fw.flush(); 100 } catch (IOException e) { 101 System.err.println("cannot setup a default user for tests purpose inside " + gitConfigFile); 102 e.printStackTrace(); 103 } 104 } 105}