View Javadoc

1   package org.apache.maven.scm.provider.git.jgit.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.ScmException;
23  import org.apache.maven.scm.ScmFile;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.ScmVersion;
26  import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
27  import org.apache.maven.scm.command.checkin.CheckInScmResult;
28  import org.apache.maven.scm.provider.ScmProviderRepository;
29  import org.apache.maven.scm.provider.git.command.GitCommand;
30  import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
31  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
32  import org.codehaus.plexus.util.StringUtils;
33  import org.eclipse.jgit.api.AddCommand;
34  import org.eclipse.jgit.api.Git;
35  import org.eclipse.jgit.lib.Constants;
36  import org.eclipse.jgit.revwalk.RevCommit;
37  import org.eclipse.jgit.transport.RefSpec;
38  
39  import java.io.File;
40  import java.util.Collections;
41  import java.util.List;
42  import java.util.Set;
43  
44  /**
45   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
46   * @author Dominik Bartholdi (imod)
47   * @since 1.9
48   */
49  public class JGitCheckInCommand
50      extends AbstractCheckInCommand
51      implements GitCommand
52  {
53      /**
54       * {@inheritDoc}
55       */
56      protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
57                                                        ScmVersion version )
58          throws ScmException
59      {
60  
61          Git git = null;
62          try
63          {
64              File basedir = fileSet.getBasedir();
65              git = Git.open( basedir );
66  
67              boolean doCommit = false;
68  
69              if ( !fileSet.getFileList().isEmpty() )
70              {
71                  doCommit = JGitUtils.addAllFiles( git, fileSet ).size() > 0;
72              }
73              else
74              {
75                  // add all tracked files which are modified manually
76                  Set<String> changeds = git.status().call().getModified();
77                  if ( changeds.isEmpty() )
78                  {
79                      // warn there is nothing to add
80                      getLogger().warn( "there are no files to be added" );
81                      doCommit = false;
82                  }
83                  else
84                  {
85                      AddCommand add = git.add();
86                      for ( String changed : changeds )
87                      {
88                          getLogger().debug( "add manualy: " + changed );
89                          add.addFilepattern( changed );
90                          doCommit = true;
91                      }
92                      add.call();
93                  }
94              }
95  
96              List<ScmFile> checkedInFiles = Collections.emptyList();
97              if ( doCommit )
98              {
99                  RevCommit commitRev = git.commit().setMessage( message ).call();
100                 getLogger().info( "commit done: " + commitRev.getShortMessage() );
101                 checkedInFiles = JGitUtils.getFilesInCommit( git.getRepository(), commitRev );
102                 if ( getLogger().isDebugEnabled() )
103                 {
104                     for ( ScmFile scmFile : checkedInFiles )
105                     {
106                         getLogger().debug( "in commit: " + scmFile );
107                     }
108                 }
109             }
110 
111             if ( repo.isPushChanges() )
112             {
113                 String branch = version != null ? version.getName() : null;
114                 if ( StringUtils.isBlank( branch ) )
115                 {
116                     branch = git.getRepository().getBranch();
117                 }
118                 RefSpec refSpec = new RefSpec( Constants.R_HEADS + branch + ":" + Constants.R_HEADS + branch );
119                 getLogger().info( "push changes to remote... " + refSpec.toString() );
120                 JGitUtils.push( getLogger(), git, (GitScmProviderRepository) repo, refSpec );
121             }
122 
123             return new CheckInScmResult( "JGit checkin", checkedInFiles );
124         }
125         catch ( Exception e )
126         {
127             throw new ScmException( "JGit checkin failure!", e );
128         }
129         finally
130         {
131             JGitUtils.closeRepo( git );
132         }
133     }
134 
135 }