View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.git.gitexe.command.status;
20  
21  import java.net.URI;
22  
23  import org.apache.maven.scm.ScmException;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.command.status.AbstractStatusCommand;
26  import org.apache.maven.scm.command.status.StatusScmResult;
27  import org.apache.maven.scm.provider.ScmProviderRepository;
28  import org.apache.maven.scm.provider.git.command.GitCommand;
29  import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
30  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
31  import org.codehaus.plexus.util.cli.CommandLineUtils;
32  import org.codehaus.plexus.util.cli.Commandline;
33  import org.slf4j.Logger;
34  
35  /**
36   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
37   *
38   */
39  public class GitStatusCommand extends AbstractStatusCommand implements GitCommand {
40      /** {@inheritDoc} */
41      protected StatusScmResult executeStatusCommand(ScmProviderRepository repo, ScmFileSet fileSet) throws ScmException {
42          int exitCode;
43          CommandLineUtils.StringStreamConsumer stderr;
44  
45          URI relativeRepositoryPath = getRelativeCWD(logger, fileSet);
46  
47          Commandline cl = createCommandLine((GitScmProviderRepository) repo, fileSet);
48  
49          GitStatusConsumer consumer = new GitStatusConsumer(fileSet.getBasedir(), relativeRepositoryPath, fileSet);
50  
51          stderr = new CommandLineUtils.StringStreamConsumer();
52  
53          exitCode = GitCommandLineUtils.execute(cl, consumer, stderr);
54          if (exitCode != 0) {
55              // git-status returns non-zero if nothing to do
56              if (logger.isInfoEnabled()) {
57                  logger.info("nothing added to commit but untracked files present (use \"git add\" to track)");
58              }
59          }
60  
61          return new StatusScmResult(cl.toString(), consumer.getChangedFiles());
62      }
63  
64      // ----------------------------------------------------------------------
65      //
66      // ----------------------------------------------------------------------
67  
68      /**
69       * Get the dir relative to the repository root.
70       *
71       * @param logger the caller command logger.
72       * @param fileSet in which subdir to execute.
73       * @return the relative URI.
74       * @throws ScmException if execute() fails.
75       */
76      public static URI getRelativeCWD(Logger logger, ScmFileSet fileSet) throws ScmException {
77          Commandline clRevparse = createRevparseShowPrefix(fileSet);
78  
79          CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
80          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
81  
82          URI relativeRepositoryPath = null;
83  
84          int exitCode = GitCommandLineUtils.execute(clRevparse, stdout, stderr);
85          if (exitCode != 0) {
86              // git-status returns non-zero if nothing to do
87              if (logger.isInfoEnabled()) {
88                  logger.info("Could not resolve prefix");
89              }
90          } else {
91              relativeRepositoryPath =
92                      GitStatusConsumer.uriFromPath(stdout.getOutput().trim());
93          }
94          return relativeRepositoryPath;
95      }
96  
97      public static Commandline createCommandLine(GitScmProviderRepository repository, ScmFileSet fileSet) {
98          Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "status");
99          cl.addArguments(new String[] {"--porcelain", "."});
100         return cl;
101     }
102 
103     public static Commandline createRevparseShowPrefix(ScmFileSet fileSet) {
104         Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "rev-parse");
105         cl.addArguments(new String[] {"--show-prefix"});
106         return cl;
107     }
108 }