View Javadoc
1   package org.apache.maven.scm.provider.git.gitexe.command.update;
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 java.io.File;
23  
24  import org.apache.maven.scm.ScmBranch;
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmFileSet;
27  import org.apache.maven.scm.ScmVersion;
28  import org.apache.maven.scm.command.changelog.ChangeLogCommand;
29  import org.apache.maven.scm.command.update.AbstractUpdateCommand;
30  import org.apache.maven.scm.command.update.UpdateScmResult;
31  import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
32  import org.apache.maven.scm.provider.ScmProviderRepository;
33  import org.apache.maven.scm.provider.git.command.GitCommand;
34  import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
35  import org.apache.maven.scm.provider.git.gitexe.command.changelog.GitChangeLogCommand;
36  import org.apache.maven.scm.provider.git.gitexe.command.diff.GitDiffCommand;
37  import org.apache.maven.scm.provider.git.gitexe.command.diff.GitDiffRawConsumer;
38  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
39  import org.codehaus.plexus.util.cli.CommandLineUtils;
40  import org.codehaus.plexus.util.cli.Commandline;
41  
42  /**
43   * @author Olivier Lamy
44   * @author <a href="mailto:struberg@yahoo.de">struberg</a>
45   * @since 10 august 2008
46   *
47   */
48  public class GitUpdateCommand
49      extends AbstractUpdateCommand
50      implements GitCommand
51  {
52      /** {@inheritDoc} */
53      protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repo, ScmFileSet fileSet,
54                                                      ScmVersion scmVersion )
55          throws ScmException
56      {
57          GitScmProviderRepository repository = (GitScmProviderRepository) repo;
58  
59          if ( GitScmProviderRepository.PROTOCOL_FILE.equals( repository.getFetchInfo().getProtocol() )
60              && repository.getFetchInfo().getPath().indexOf( fileSet.getBasedir().getPath() ) >= 0 )
61          {
62              throw new ScmException( "remote repository must not be the working directory" );
63          }
64  
65          int exitCode;
66  
67          CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
68          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
69  
70          // fir we need to get the current reversion
71          Commandline clRev = createLatestRevisionCommandLine( repository, fileSet.getBasedir(), scmVersion );
72          GitLatestRevisionCommandConsumer consumerRev = new GitLatestRevisionCommandConsumer();
73          exitCode = GitCommandLineUtils.execute( clRev, consumerRev, stderr );
74          if ( exitCode != 0 )
75          {
76              return new UpdateScmResult( clRev.toString(), "The git-log command failed.",
77                      stderr.getOutput(), false );
78          }
79          String origSha1 = consumerRev.getLatestRevision();
80  
81          Commandline cl = createCommandLine( repository, fileSet.getBasedir(), scmVersion );
82          exitCode = GitCommandLineUtils.execute( cl, stdout, stderr );
83          if ( exitCode != 0 )
84          {
85              return new UpdateScmResult( cl.toString(), "The git-pull command failed.",
86                                          stderr.getOutput(), false );
87          }
88  
89          // we also need to log exactly what has been updated
90          GitDiffRawConsumer diffRawConsumer = new GitDiffRawConsumer();
91          Commandline clDiffRaw = GitDiffCommand.createDiffRawCommandLine( fileSet.getBasedir(), origSha1 );
92          exitCode = GitCommandLineUtils.execute( clDiffRaw, diffRawConsumer, stderr );
93          if ( exitCode != 0 )
94          {
95              return new UpdateScmResult( clDiffRaw.toString(), "The git-diff --raw command failed.",
96                      stderr.getOutput(), false );
97          }
98  
99  
100         // now let's get the latest version
101         consumerRev = new GitLatestRevisionCommandConsumer();
102         exitCode = GitCommandLineUtils.execute( clRev, consumerRev, stderr );
103         if ( exitCode != 0 )
104         {
105             return new UpdateScmResult( clRev.toString(), "The git-log command failed.",
106                                         stderr.getOutput(), false );
107         }
108         String latestRevision = consumerRev.getLatestRevision();
109 
110         return new UpdateScmResultWithRevision( cl.toString(), diffRawConsumer.getChangedFiles(), latestRevision );
111     }
112 
113     /** {@inheritDoc} */
114     protected ChangeLogCommand getChangeLogCommand()
115     {
116         return new GitChangeLogCommand();
117     }
118 
119     /**
120      * create the command line for updating the current branch with the info from the foreign repository.
121      */
122     public static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
123                                                  ScmVersion scmVersion )
124     {
125         Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "pull" );
126 
127         cl.createArg().setLine( repository.getFetchUrl() );
128 
129         // now set the branch where we would like to pull from
130         if ( scmVersion instanceof ScmBranch )
131         {
132             cl.createArg().setLine( scmVersion.getName() );
133         }
134 
135         return cl;
136     }
137 
138     /**
139      * @param scmVersion a valid branch or <code>null</code> if the master branch should be taken
140      * @return CommandLine for getting the latest commit on the given branch
141      */
142     public static Commandline createLatestRevisionCommandLine( GitScmProviderRepository repository,
143                                                                File workingDirectory, ScmVersion scmVersion )
144     {
145         Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "log" );
146 
147         // only show exactly 1 commit
148         cl.createArg().setValue( "-n1" );
149 
150         // same as --topo-order, but ensure ordering of merges
151         cl.createArg().setValue( "--date-order" );
152 
153         if ( scmVersion != null && scmVersion instanceof ScmBranch && scmVersion.getName() != null
154             && scmVersion.getName().length() > 0 )
155         {
156             // if any branch is given, lets take em
157             cl.createArg().setValue( scmVersion.getName() );
158         }
159         // otherwise we work on HEAD/current branch
160 
161         return cl;
162     }
163 }