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.plugin;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.ScmFile;
29  import org.apache.maven.scm.command.status.StatusScmResult;
30  import org.apache.maven.scm.repository.ScmRepository;
31  
32  /**
33   * Display the modification status of the files in the configured scm url.
34   *
35   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
36   * @author Olivier Lamy
37   */
38  @Mojo(name = "status", aggregator = true)
39  public class StatusMojo extends AbstractScmMojo {
40      /** {@inheritDoc} */
41      public void execute() throws MojoExecutionException {
42          super.execute();
43  
44          try {
45              ScmRepository repository = getScmRepository();
46  
47              StatusScmResult result = getScmManager().status(repository, getFileSet());
48  
49              checkResult(result);
50  
51              File baseDir = getFileSet().getBasedir();
52  
53              // Determine the maximum length of the status column
54              int maxLen = 0;
55  
56              for (ScmFile file : result.getChangedFiles()) {
57                  maxLen = Math.max(maxLen, file.getStatus().toString().length());
58              }
59  
60              for (ScmFile file : result.getChangedFiles()) {
61                  // right align all of the statuses
62                  getLog().info(StringUtils.leftPad(file.getStatus().toString(), maxLen) + " status for "
63                          + getRelativePath(baseDir, file.getPath()));
64              }
65          } catch (IOException | ScmException e) {
66              throw new MojoExecutionException("Cannot run status command : ", e);
67          }
68      }
69  
70      /**
71       * Formats the filename so that it is a relative directory from the base.
72       *
73       * @param baseDir
74       * @param path
75       * @return The relative path
76       */
77      protected String getRelativePath(File baseDir, String path) {
78          if (path.equals(baseDir.getAbsolutePath())) {
79              return ".";
80          } else if (path.indexOf(baseDir.getAbsolutePath()) == 0) {
81              // the + 1 gets rid of a leading file separator
82              return path.substring(baseDir.getAbsolutePath().length() + 1);
83          } else {
84              return path;
85          }
86      }
87  }