View Javadoc
1   package org.apache.maven.scm.provider.git.gitexe.command.checkin;
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 org.apache.maven.scm.ScmFileSet;
23  import org.apache.maven.scm.ScmTestCase;
24  import org.apache.maven.scm.ScmVersion;
25  import org.apache.maven.scm.command.add.AddScmResult;
26  import org.apache.maven.scm.command.checkin.CheckInScmResult;
27  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
28  import org.apache.maven.scm.command.remove.RemoveScmResult;
29  import org.apache.maven.scm.provider.git.GitScmTestUtils;
30  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
31  import org.apache.maven.scm.provider.git.util.GitUtil;
32  import org.apache.maven.scm.repository.ScmRepository;
33  import org.codehaus.plexus.util.FileUtils;
34  import org.codehaus.plexus.util.cli.Commandline;
35  
36  import java.io.File;
37  
38  /**
39   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
40   */
41  public class GitCheckInCommandTest
42      extends ScmTestCase
43  {
44      private File messageFile;
45  
46      private String messageFileString;
47  
48      public void setUp()
49          throws Exception
50      {
51          super.setUp();
52  
53          messageFile = new File( "commit-message" );
54  
55          String path = messageFile.getAbsolutePath();
56          if ( path.indexOf( ' ' ) >= 0 )
57          {
58              path = "\"" + path + "\"";
59          }
60          messageFileString = "-F " + path;
61      }
62  
63      public void testCommandLineWithoutTag()
64          throws Exception
65      {
66          if ( GitUtil.getSettings().isCommitNoVerify() )
67          {
68              testCommandLine( "scm:git:http://foo.com/git/trunk", "git commit --verbose " + messageFileString + " -a" + " --no-verify" );
69          }
70          else
71          {
72              testCommandLine( "scm:git:http://foo.com/git/trunk", "git commit --verbose " + messageFileString + " -a" );
73          }
74      }
75  
76      public void testCommandLineWithUsername()
77          throws Exception
78      {
79          if ( GitUtil.getSettings().isCommitNoVerify() )
80          {
81              testCommandLine( "scm:git:http://anonymous@foo.com/git/trunk", "git commit --verbose " + messageFileString
82                  + " -a" + " --no-verify" );
83          }
84          else
85          {
86              testCommandLine( "scm:git:http://anonymous@foo.com/git/trunk", "git commit --verbose " + messageFileString
87                  + " -a" );
88          }
89      }
90  
91      // Test reproducing SCM-694
92      public void testCheckinAfterRename() throws Exception {
93          File repo = getRepositoryRoot();
94          File checkedOutRepo = getWorkingCopy();
95  
96          if ( !ScmTestCase.isSystemCmd( "git" ) )
97          {
98              ScmTestCase.printSystemCmdUnavail( "git", getName() );
99              return;
100         }
101 
102         GitScmTestUtils.initRepo("src/test/resources/repository/", getRepositoryRoot(), getWorkingDirectory());
103 
104         ScmRepository scmRepository = getScmManager().makeScmRepository(
105             "scm:git:" + repo.toPath().toAbsolutePath().toUri().toASCIIString() );
106         checkoutRepoInto(checkedOutRepo, scmRepository);
107 
108         // Add a default user to the config
109         GitScmTestUtils.setDefaultUser( checkedOutRepo );
110 
111         // Creating foo/bar/wine.xml
112         File fooDir = new File( checkedOutRepo.getAbsolutePath(), "foo" );
113         fooDir.mkdir();
114         File barDir = new File(fooDir.getAbsolutePath(), "bar");
115         barDir.mkdir();
116         File wineFile = new File(barDir.getAbsolutePath(), "wine.xml");
117         FileUtils.fileWrite( wineFile.getAbsolutePath(), "Lacoste castle" );
118 
119         // Adding and commiting file
120         AddScmResult addResult = getScmManager().add( scmRepository, new ScmFileSet( checkedOutRepo, new File( "foo/bar/wine.xml" ) ) );
121         assertResultIsSuccess( addResult );
122         CheckInScmResult checkInScmResult = getScmManager().checkIn(scmRepository, new ScmFileSet(checkedOutRepo), "Created wine file");
123         assertResultIsSuccess( checkInScmResult );
124 
125         // Cloning foo/bar/wine.xml to foo/newbar/wine.xml
126         File newBarDir = new File(fooDir.getAbsolutePath(), "newbar");
127         newBarDir.mkdir();
128         File movedWineFile = new File(newBarDir.getAbsolutePath(), "wine.xml");
129         FileUtils.copyFile(wineFile, movedWineFile);
130 
131         // Removing old file, adding new file and commiting...
132         RemoveScmResult removeResult = getScmManager().remove(scmRepository, new ScmFileSet(checkedOutRepo, new File("foo/bar/")), "");
133         assertResultIsSuccess(removeResult);
134         addResult = getScmManager().add(scmRepository, new ScmFileSet(checkedOutRepo, new File("foo/newbar/wine.xml")));
135         assertResultIsSuccess(addResult);
136         checkInScmResult = getScmManager().checkIn(scmRepository, new ScmFileSet(checkedOutRepo), "moved wine.xml from foo/bar/ to foo/newbar/");
137         assertResultIsSuccess(checkInScmResult);
138         assertTrue("Renamed file has not been commited!", checkInScmResult.getCheckedInFiles().size() != 0);
139     }
140 
141     // Test FileSet in configuration
142     public void testCheckinWithFileSet() throws Exception {
143         File repo = getRepositoryRoot();
144         File checkedOutRepo = getWorkingCopy();
145 
146         if ( !ScmTestCase.isSystemCmd( "git" ) )
147         {
148             ScmTestCase.printSystemCmdUnavail( "git", getName() );
149             return;
150         }
151 
152         GitScmTestUtils.initRepo( "src/test/resources/repository/", getRepositoryRoot(), getWorkingDirectory() );
153 
154         ScmRepository scmRepository = getScmManager().makeScmRepository(
155             "scm:git:" + repo.toPath().toAbsolutePath().toUri().toASCIIString() );
156         checkoutRepoInto( checkedOutRepo, scmRepository );
157 
158         // Add a default user to the config
159         GitScmTestUtils.setDefaultUser( checkedOutRepo );
160 
161         // Creating beer.xml and whiskey.xml
162         File beerFile = new File( checkedOutRepo.getAbsolutePath(), "beer.xml" );
163         FileUtils.fileWrite( beerFile.getAbsolutePath(), "1/2 litre" );
164         File whiskeyFile = new File( checkedOutRepo.getAbsolutePath(), "whiskey.xml" );
165         FileUtils.fileWrite( whiskeyFile.getAbsolutePath(), "700 ml" );
166 
167         // Adding and commiting beer and whiskey
168         AddScmResult addResult = getScmManager().add( scmRepository, new ScmFileSet( checkedOutRepo, "beer.xml,whiskey.xml" ) );
169         assertResultIsSuccess( addResult );
170         CheckInScmResult checkInScmResult = getScmManager().checkIn( scmRepository,
171             new ScmFileSet( checkedOutRepo, "beer.xml,whiskey.xml" ), "Created beer file" );
172         assertResultIsSuccess( checkInScmResult );
173 
174         // Editing beer and commiting whiskey, should commit nothingi, but succeed
175         FileUtils.fileWrite( beerFile.getAbsolutePath(), "1 litre" );
176 
177         addResult = getScmManager().add( scmRepository, new ScmFileSet( checkedOutRepo, "whiskey.xml" ) );
178         assertResultIsSuccess( addResult );
179         checkInScmResult = getScmManager().checkIn( scmRepository,
180             new ScmFileSet( checkedOutRepo, "whiskey.xml" ), "Checking beer file");
181         assertResultIsSuccess( checkInScmResult );
182     }
183 
184     // ----------------------------------------------------------------------
185     //
186     // ----------------------------------------------------------------------
187 
188     private CheckOutScmResult checkoutRepoInto( File workingCopy, ScmRepository scmRepository )
189         throws Exception {
190         FileUtils.deleteDirectory( workingCopy );
191         workingCopy.mkdir();
192 
193         CheckOutScmResult result =
194             getScmManager().checkOut( scmRepository, new ScmFileSet( workingCopy ), (ScmVersion) null );
195 
196         assertResultIsSuccess( result );
197         return result;
198     }
199 
200     private void testCommandLine( String scmUrl, String commandLine )
201         throws Exception
202     {
203         File workingDirectory = getTestFile( "target/git-checkin-command-test" );
204 
205         ScmRepository repository = getScmManager().makeScmRepository(scmUrl);
206 
207         GitScmProviderRepository gitRepository = (GitScmProviderRepository) repository.getProviderRepository();
208 
209         Commandline cl =
210             GitCheckInCommand.createCommitCommandLine( gitRepository, new ScmFileSet( workingDirectory ), messageFile );
211 
212         assertCommandLine( commandLine, workingDirectory, cl );
213     }
214 }