View Javadoc
1   package org.apache.maven.scm.provider.git;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.Assert;
23  import org.codehaus.plexus.PlexusTestCase;
24  import org.codehaus.plexus.util.FileUtils;
25  import org.codehaus.plexus.util.Os;
26  import org.codehaus.plexus.util.StringUtils;
27  import org.codehaus.plexus.util.cli.CommandLineException;
28  import org.codehaus.plexus.util.cli.CommandLineUtils;
29  import org.codehaus.plexus.util.cli.Commandline;
30  
31  import java.io.File;
32  import java.io.FileWriter;
33  import java.io.IOException;
34  
35  /**
36   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
37   *
38   */
39  public final class GitScmTestUtils
40  {
41      private GitScmTestUtils()
42      {
43      }
44  
45      public static void initRepo( File repository, File workingDirectory, File assertionDirectory )
46          throws IOException
47      {
48          initRepo( "src/test/repository/", repository, workingDirectory );
49  
50          FileUtils.deleteDirectory( assertionDirectory );
51  
52          Assert.assertTrue( assertionDirectory.mkdirs() );
53      }
54  
55      public static void initRepo( String source, File repository, File workingDirectory )
56          throws IOException
57      {
58          // Copy the repository to target
59          File src = PlexusTestCase.getTestFile( source );
60  
61          FileUtils.deleteDirectory( repository );
62  
63          Assert.assertTrue( repository.mkdirs() );
64  
65          FileUtils.copyDirectoryStructure( src, repository );
66  
67          File dotGitDirectory = new File( src, "dotgit" );
68  
69          if ( dotGitDirectory.exists() )
70          {
71              FileUtils.copyDirectoryStructure( dotGitDirectory, new File( repository, ".git" ) );
72          }
73  
74          FileUtils.deleteDirectory( workingDirectory );
75  
76          Assert.assertTrue( workingDirectory.mkdirs() );
77      }
78  
79      public static String getScmUrl( File repositoryRootFile, String provider )
80          throws CommandLineException
81      {
82          String repositoryRoot = repositoryRootFile.getAbsolutePath();
83  
84          // TODO: it'd be great to build this into CommandLineUtils somehow
85          // TODO: some way without a custom cygwin sys property?
86          if ( "true".equals( System.getProperty( "cygwin" ) ) )
87          {
88              Commandline cl = new Commandline();
89  
90              cl.setExecutable( "cygpath" );
91  
92              cl.createArg().setValue( "--unix" );
93  
94              cl.createArg().setValue( repositoryRoot );
95  
96              CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
97  
98              int exitValue = CommandLineUtils.executeCommandLine( cl, stdout, null );
99  
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 }