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.IOException;
33  
34  /**
35   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
36   *
37   */
38  public final class GitScmTestUtils
39  {
40      private GitScmTestUtils()
41      {
42      }
43  
44      public static void initRepo( File repository, File workingDirectory, File assertionDirectory )
45          throws IOException
46      {
47          initRepo( "src/test/repository/", repository, workingDirectory );
48  
49          FileUtils.deleteDirectory( assertionDirectory );
50  
51          Assert.assertTrue( assertionDirectory.mkdirs() );
52      }
53  
54      public static void initRepo( String source, File repository, File workingDirectory )
55          throws IOException
56      {
57          // Copy the repository to target
58          File src = PlexusTestCase.getTestFile( source );
59  
60          FileUtils.deleteDirectory( repository );
61  
62          Assert.assertTrue( repository.mkdirs() );
63  
64          FileUtils.copyDirectoryStructure( src, repository );
65  
66          File dotGitDirectory = new File( src, "dotgit" );
67  
68          if ( dotGitDirectory.exists() )
69          {
70              FileUtils.copyDirectoryStructure( dotGitDirectory, new File( repository, ".git" ) );
71          }
72  
73          FileUtils.deleteDirectory( workingDirectory );
74  
75          Assert.assertTrue( workingDirectory.mkdirs() );
76      }
77  
78      public static String getScmUrl( File repositoryRootFile, String provider )
79          throws CommandLineException
80      {
81          String repositoryRoot = repositoryRootFile.getAbsolutePath();
82  
83          // TODO: it'd be great to build this into CommandLineUtils somehow
84          // TODO: some way without a custom cygwin sys property?
85          if ( "true".equals( System.getProperty( "cygwin" ) ) )
86          {
87              Commandline cl = new Commandline();
88  
89              cl.setExecutable( "cygpath" );
90  
91              cl.createArg().setValue( "--unix" );
92  
93              cl.createArg().setValue( repositoryRoot );
94  
95              CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
96  
97              int exitValue = CommandLineUtils.executeCommandLine( cl, stdout, null );
98  
99              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 }