1 package org.apache.maven.scm.provider.git;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
84
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 }