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&oslash;l</a>
036     * @version $Id: GitScmTestUtils.java 823147 2009-10-08 12:39:23Z struberg $
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            // now let's get rid of all .svn directories in the copied folder
067            deleteAllDirectories( repository, ".svn" );
068    
069            FileUtils.deleteDirectory( workingDirectory );
070    
071            Assert.assertTrue( workingDirectory.mkdirs() );
072        }
073    
074        public static String getScmUrl( File repositoryRootFile, String provider )
075            throws CommandLineException
076        {
077            String repositoryRoot = repositoryRootFile.getAbsolutePath();
078    
079            // TODO: it'd be great to build this into CommandLineUtils somehow
080            // TODO: some way without a custom cygwin sys property?
081            if ( "true".equals( System.getProperty( "cygwin" ) ) )
082            {
083                Commandline cl = new Commandline();
084    
085                cl.setExecutable( "cygpath" );
086    
087                cl.createArg().setValue( "--unix" );
088    
089                cl.createArg().setValue( repositoryRoot );
090    
091                CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
092    
093                int exitValue = CommandLineUtils.executeCommandLine( cl, stdout, null );
094    
095                if ( exitValue != 0 )
096                {
097                    throw new CommandLineException( "Unable to convert cygwin path, exit code = " + exitValue );
098                }
099    
100                repositoryRoot = stdout.getOutput().trim();
101            }
102            else if ( Os.isFamily( "windows" ) )
103            {
104                repositoryRoot = "/" + StringUtils.replace( repositoryRoot, "\\", "/" );
105            }
106    
107            return "scm:" + provider + ":file://" + repositoryRoot;
108        }
109        
110        
111        public static void deleteAllDirectories( File startDirectory, String pattern ) 
112        throws IOException
113        {
114            if ( startDirectory.isDirectory() ) 
115            {
116                File[] childs = startDirectory.listFiles();
117                for ( int i = 0; i < childs.length; i++ )
118                {
119                    File child = childs[ i ];
120                    if ( child.isDirectory() )
121                    {
122                        if ( child.getName().equals( pattern ) )
123                        {
124                            FileUtils.deleteDirectory( child );
125                        }
126                        else
127                        {
128                            deleteAllDirectories( child, pattern );
129                        }
130                    }
131                }
132            }
133        }
134    
135    }