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          GitScmTestUtils.initRepo("src/test/resources/repository/", getRepositoryRoot(), getWorkingDirectory());
97  
98          ScmRepository scmRepository = getScmManager().makeScmRepository( "scm:git:file://" + repo.getAbsolutePath() );
99          checkoutRepoInto(checkedOutRepo, scmRepository);
100 
101         // Add a default user to the config
102         GitScmTestUtils.setDefaultUser( checkedOutRepo );
103 
104         // Creating foo/bar/wine.xml
105         File fooDir = new File( checkedOutRepo.getAbsolutePath() + File.separator + "foo" );
106         fooDir.mkdir();
107         File barDir = new File(fooDir.getAbsolutePath() + File.separator + "bar");
108         barDir.mkdir();
109         File wineFile = new File(barDir.getAbsolutePath() + File.separator + "wine.xml");
110         FileUtils.fileWrite( wineFile.getAbsolutePath(), "Lacoste castle" );
111 
112         // Adding and commiting file
113         AddScmResult addResult = getScmManager().add( scmRepository, new ScmFileSet( checkedOutRepo, new File( "foo/bar/wine.xml" ) ) );
114         assertResultIsSuccess( addResult );
115         CheckInScmResult checkInScmResult = getScmManager().checkIn(scmRepository, new ScmFileSet(checkedOutRepo), "Created wine file");
116         assertResultIsSuccess( checkInScmResult );
117 
118         // Cloning foo/bar/wine.xml to foo/newbar/wine.xml
119         File newBarDir = new File(fooDir.getAbsolutePath() + File.separator + "newbar");
120         newBarDir.mkdir();
121         File movedWineFile = new File(newBarDir.getAbsolutePath() + File.separator + "wine.xml");
122         FileUtils.copyFile(wineFile, movedWineFile);
123 
124         // Removing old file, adding new file and commiting...
125         RemoveScmResult removeResult = getScmManager().remove(scmRepository, new ScmFileSet(checkedOutRepo, new File("foo/bar/")), "");
126         assertResultIsSuccess(removeResult);
127         addResult = getScmManager().add(scmRepository, new ScmFileSet(checkedOutRepo, new File("foo/newbar/wine.xml")));
128         assertResultIsSuccess(addResult);
129         checkInScmResult = getScmManager().checkIn(scmRepository, new ScmFileSet(checkedOutRepo), "moved wine.xml from foo/bar/ to foo/newbar/");
130         assertResultIsSuccess(checkInScmResult);
131         assertTrue("Renamed file has not been commited !", checkInScmResult.getCheckedInFiles().size() != 0);
132     }
133 
134     // ----------------------------------------------------------------------
135     //
136     // ----------------------------------------------------------------------
137 
138     private CheckOutScmResult checkoutRepoInto( File workingCopy, ScmRepository scmRepository )
139         throws Exception {
140         FileUtils.deleteDirectory( workingCopy );
141         workingCopy.mkdir();
142 
143         CheckOutScmResult result =
144             getScmManager().checkOut( scmRepository, new ScmFileSet( workingCopy ), (ScmVersion) null );
145 
146         assertResultIsSuccess( result );
147         return result;
148     }
149 
150     private void testCommandLine( String scmUrl, String commandLine )
151         throws Exception
152     {
153         File workingDirectory = getTestFile( "target/git-checkin-command-test" );
154 
155         ScmRepository repository = getScmManager().makeScmRepository(scmUrl);
156 
157         GitScmProviderRepository gitRepository = (GitScmProviderRepository) repository.getProviderRepository();
158 
159         Commandline cl =
160             GitCheckInCommand.createCommitCommandLine( gitRepository, new ScmFileSet( workingDirectory ), messageFile );
161 
162         assertCommandLine( commandLine, workingDirectory, cl );
163     }
164 }