001 package org.apache.maven.scm.provider.git;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import junit.framework.Assert;
023 import org.codehaus.plexus.PlexusTestCase;
024 import org.codehaus.plexus.util.FileUtils;
025 import org.codehaus.plexus.util.Os;
026 import org.codehaus.plexus.util.StringUtils;
027 import org.codehaus.plexus.util.cli.CommandLineException;
028 import org.codehaus.plexus.util.cli.CommandLineUtils;
029 import org.codehaus.plexus.util.cli.Commandline;
030
031 import java.io.File;
032 import java.io.IOException;
033
034 /**
035 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a>
036 *
037 */
038 public final class GitScmTestUtils
039 {
040 private GitScmTestUtils()
041 {
042 }
043
044 public static void initRepo( File repository, File workingDirectory, File assertionDirectory )
045 throws IOException
046 {
047 initRepo( "src/test/repository/", repository, workingDirectory );
048
049 FileUtils.deleteDirectory( assertionDirectory );
050
051 Assert.assertTrue( assertionDirectory.mkdirs() );
052 }
053
054 public static void initRepo( String source, File repository, File workingDirectory )
055 throws IOException
056 {
057 // Copy the repository to target
058 File src = PlexusTestCase.getTestFile( source );
059
060 FileUtils.deleteDirectory( repository );
061
062 Assert.assertTrue( repository.mkdirs() );
063
064 FileUtils.copyDirectoryStructure( src, repository );
065
066 File dotGitDirectory = new File( src, "dotgit" );
067
068 if ( dotGitDirectory.exists() )
069 {
070 FileUtils.copyDirectoryStructure( dotGitDirectory, new File( repository, ".git" ) );
071 }
072
073 FileUtils.deleteDirectory( workingDirectory );
074
075 Assert.assertTrue( workingDirectory.mkdirs() );
076 }
077
078 public static String getScmUrl( File repositoryRootFile, String provider )
079 throws CommandLineException
080 {
081 String repositoryRoot = repositoryRootFile.getAbsolutePath();
082
083 // TODO: it'd be great to build this into CommandLineUtils somehow
084 // TODO: some way without a custom cygwin sys property?
085 if ( "true".equals( System.getProperty( "cygwin" ) ) )
086 {
087 Commandline cl = new Commandline();
088
089 cl.setExecutable( "cygpath" );
090
091 cl.createArg().setValue( "--unix" );
092
093 cl.createArg().setValue( repositoryRoot );
094
095 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
096
097 int exitValue = CommandLineUtils.executeCommandLine( cl, stdout, null );
098
099 if ( exitValue != 0 )
100 {
101 throw new CommandLineException( "Unable to convert cygwin path, exit code = " + exitValue );
102 }
103
104 repositoryRoot = stdout.getOutput().trim();
105 }
106 else if ( Os.isFamily( "windows" ) )
107 {
108 repositoryRoot = "/" + StringUtils.replace( repositoryRoot, "\\", "/" );
109 }
110
111 return "scm:" + provider + ":file://" + repositoryRoot;
112 }
113
114
115 public static void deleteAllDirectories( File startDirectory, String pattern )
116 throws IOException
117 {
118 if ( startDirectory.isDirectory() )
119 {
120 File[] childs = startDirectory.listFiles();
121 for ( int i = 0; i < childs.length; i++ )
122 {
123 File child = childs[i];
124 if ( child.isDirectory() )
125 {
126 if ( child.getName().equals( pattern ) )
127 {
128 FileUtils.deleteDirectory( child );
129 }
130 else
131 {
132 deleteAllDirectories( child, pattern );
133 }
134 }
135 }
136 }
137 }
138
139 }