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 java.io.File;
23 import java.io.FileWriter;
24 import java.io.IOException;
25
26 import org.apache.maven.scm.PlexusJUnit4TestSupport;
27 import org.junit.Assert;
28
29 import org.codehaus.plexus.util.FileUtils;
30 import org.codehaus.plexus.util.cli.CommandLineException;
31
32
33
34
35
36 public final class GitScmTestUtils
37 {
38
39 public static final String GIT_COMMAND_LINE = "git";
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 = PlexusJUnit4TestSupport.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 return "scm:" + provider + ":" + repositoryRootFile.toPath().toAbsolutePath().toUri().toASCIIString();
83 }
84
85
86 public static void deleteAllDirectories( File startDirectory, String pattern )
87 throws IOException
88 {
89 if ( startDirectory.isDirectory() )
90 {
91 File[] childs = startDirectory.listFiles();
92 for ( int i = 0; i < childs.length; i++ )
93 {
94 File child = childs[i];
95 if ( child.isDirectory() )
96 {
97 if ( child.getName().equals( pattern ) )
98 {
99 FileUtils.deleteDirectory( child );
100 }
101 else
102 {
103 deleteAllDirectories( child, pattern );
104 }
105 }
106 }
107 }
108 }
109
110 public static void setDefaultUser( File repositoryRootFile )
111 {
112 File gitConfigFile = new File( new File( repositoryRootFile, ".git" ), "config" );
113
114 FileWriter fw = null;
115 try
116 {
117 fw = new FileWriter( gitConfigFile , true );
118 fw.append( "[user]\n" );
119 fw.append( "\tname = John Doe\n" );
120 fw.append( "\temail = john.doe@nowhere.com\n" );
121 fw.flush();
122 fw.close();
123 }
124 catch ( IOException e )
125 {
126 System.err.println( "cannot setup a default user for tests purpose inside " + gitConfigFile );
127 e.printStackTrace();
128 }
129 finally
130 {
131 if ( fw != null )
132 {
133 try
134 {
135 fw.close();
136 }
137 catch ( IOException ignore )
138 {
139
140 }
141 }
142 }
143 }
144 }