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.FileWriter;
33 import java.io.IOException;
34
35
36
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
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
85
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
170 }
171 }
172 }
173 }
174 }