001package 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
022import junit.framework.Assert;
023import org.codehaus.plexus.PlexusTestCase;
024import org.codehaus.plexus.util.FileUtils;
025import org.codehaus.plexus.util.Os;
026import org.codehaus.plexus.util.StringUtils;
027import org.codehaus.plexus.util.cli.CommandLineException;
028import org.codehaus.plexus.util.cli.CommandLineUtils;
029import org.codehaus.plexus.util.cli.Commandline;
030
031import java.io.File;
032import java.io.FileWriter;
033import java.io.IOException;
034
035/**
036 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
037 *
038 */
039public final class GitScmTestUtils
040{
041    private GitScmTestUtils()
042    {
043    }
044
045    public static void initRepo( File repository, File workingDirectory, File assertionDirectory )
046        throws IOException
047    {
048        initRepo( "src/test/repository/", repository, workingDirectory );
049
050        FileUtils.deleteDirectory( assertionDirectory );
051
052        Assert.assertTrue( assertionDirectory.mkdirs() );
053    }
054
055    public static void initRepo( String source, File repository, File workingDirectory )
056        throws IOException
057    {
058        // Copy the repository to target
059        File src = PlexusTestCase.getTestFile( source );
060
061        FileUtils.deleteDirectory( repository );
062
063        Assert.assertTrue( repository.mkdirs() );
064
065        FileUtils.copyDirectoryStructure( src, repository );
066
067        File dotGitDirectory = new File( src, "dotgit" );
068
069        if ( dotGitDirectory.exists() )
070        {
071            FileUtils.copyDirectoryStructure( dotGitDirectory, new File( repository, ".git" ) );
072        }
073
074        FileUtils.deleteDirectory( workingDirectory );
075
076        Assert.assertTrue( workingDirectory.mkdirs() );
077    }
078
079    public static String getScmUrl( File repositoryRootFile, String provider )
080        throws CommandLineException
081    {
082        String repositoryRoot = repositoryRootFile.getAbsolutePath();
083
084        // TODO: it'd be great to build this into CommandLineUtils somehow
085        // TODO: some way without a custom cygwin sys property?
086        if ( "true".equals( System.getProperty( "cygwin" ) ) )
087        {
088            Commandline cl = new Commandline();
089
090            cl.setExecutable( "cygpath" );
091
092            cl.createArg().setValue( "--unix" );
093
094            cl.createArg().setValue( repositoryRoot );
095
096            CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
097
098            int exitValue = CommandLineUtils.executeCommandLine( cl, stdout, null );
099
100            if ( exitValue != 0 )
101            {
102                throw new CommandLineException( "Unable to convert cygwin path, exit code = " + exitValue );
103            }
104
105            repositoryRoot = stdout.getOutput().trim();
106        }
107        else if ( Os.isFamily( "windows" ) )
108        {
109            repositoryRoot = "/" + StringUtils.replace( repositoryRoot, "\\", "/" );
110        }
111
112        return "scm:" + provider + ":file://" + repositoryRoot;
113    }
114
115
116    public static void deleteAllDirectories( File startDirectory, String pattern )
117        throws IOException
118    {
119        if ( startDirectory.isDirectory() )
120        {
121            File[] childs = startDirectory.listFiles();
122            for ( int i = 0; i < childs.length; i++ )
123            {
124                File child = childs[i];
125                if ( child.isDirectory() )
126                {
127                    if ( child.getName().equals( pattern ) )
128                    {
129                        FileUtils.deleteDirectory( child );
130                    }
131                    else
132                    {
133                        deleteAllDirectories( child, pattern );
134                    }
135                }
136            }
137        }
138    }
139
140    public static void setDefaultUser( File repositoryRootFile )
141    {
142        File gitConfigFile = new File( new File( repositoryRootFile, ".git" ), "config" );
143
144        FileWriter fw = null;
145        try
146        {
147            fw = new FileWriter( gitConfigFile , true );
148            fw.append( "[user]\n" );
149            fw.append( "\tname = John Doe\n" );
150            fw.append( "\temail = john.doe@nowhere.com\n" );
151            fw.flush();
152            fw.close();
153        }
154        catch ( IOException e )
155        {
156            System.err.println( "cannot setup a default user for tests purpose inside " + gitConfigFile );
157            e.printStackTrace();
158        }
159        finally
160        {
161            if ( fw != null )
162            {
163                try
164                {
165                    fw.close();
166                }
167                catch ( IOException ignore )
168                {
169                    // ignored
170                }
171            }
172        }
173    }
174}