View Javadoc
1   package org.apache.maven.scm.provider.git.gitexe.command.status;
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.net.URI;
23  
24  import org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.ScmFileSet;
26  import org.apache.maven.scm.command.status.AbstractStatusCommand;
27  import org.apache.maven.scm.command.status.StatusScmResult;
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.gitexe.command.GitCommandLineUtils;
31  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
32  import org.codehaus.plexus.util.cli.CommandLineUtils;
33  import org.codehaus.plexus.util.cli.Commandline;
34  
35  /**
36   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
37   *
38   */
39  public class GitStatusCommand
40      extends AbstractStatusCommand
41      implements GitCommand
42  {
43      /** {@inheritDoc} */
44      protected StatusScmResult executeStatusCommand( ScmProviderRepository repo, ScmFileSet fileSet )
45          throws ScmException
46      {
47          Commandline clRevparse = createRevparseShowToplevelCommand( fileSet );
48  
49          CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
50          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
51  
52          URI relativeRepositoryPath = null;
53          
54          int exitCode;
55  
56          exitCode = GitCommandLineUtils.execute( clRevparse, stdout, stderr, getLogger() );
57          if ( exitCode != 0 )
58          {
59              // git-status returns non-zero if nothing to do
60              if ( getLogger().isInfoEnabled() )
61              {
62                  getLogger().info( "Could not resolve toplevel" );
63              }
64          }
65          else
66          {
67              relativeRepositoryPath =
68                  GitStatusConsumer.resolveURI( stdout.getOutput().trim(), fileSet.getBasedir().toURI() );
69          }
70  
71          Commandline cl = createCommandLine( (GitScmProviderRepository) repo, fileSet );
72  
73          GitStatusConsumer consumer = new GitStatusConsumer( getLogger(), fileSet.getBasedir(), relativeRepositoryPath );
74  
75          stderr = new CommandLineUtils.StringStreamConsumer();
76  
77          exitCode = GitCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
78          if ( exitCode != 0 )
79          {
80              // git-status returns non-zero if nothing to do
81              if ( getLogger().isInfoEnabled() )
82              {
83                  getLogger().info( "nothing added to commit but untracked files present (use \"git add\" to track)" );
84              }
85          }
86  
87          return new StatusScmResult( cl.toString(), consumer.getChangedFiles() );
88      }
89  
90      // ----------------------------------------------------------------------
91      //
92      // ----------------------------------------------------------------------
93  
94      public static Commandline createCommandLine( GitScmProviderRepository repository, ScmFileSet fileSet )
95      {
96          Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "status" );
97          cl.addArguments( new String[] { "--porcelain", "." } );
98          return cl;
99      }
100     
101     public static Commandline createRevparseShowToplevelCommand( ScmFileSet fileSet )
102     {
103         Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "rev-parse" );
104         cl.addArguments( new String[] { "--show-toplevel" } );
105         return cl;
106     }
107 }