1 package org.apache.maven.scm.plugin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.apache.maven.scm.ScmException;
24 import org.apache.maven.scm.ScmFile;
25 import org.apache.maven.scm.command.status.StatusScmResult;
26 import org.apache.maven.scm.repository.ScmRepository;
27 import org.codehaus.plexus.util.StringUtils;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.Iterator;
32
33
34
35
36
37
38
39
40
41
42 public class StatusMojo
43 extends AbstractScmMojo
44 {
45 public void execute()
46 throws MojoExecutionException
47 {
48 super.execute();
49
50 try
51 {
52 ScmRepository repository = getScmRepository();
53
54 StatusScmResult result = getScmManager().status( repository, getFileSet() );
55
56 checkResult( result );
57
58 File baseDir = getFileSet().getBasedir();
59
60
61 int maxLen = 0;
62
63 for ( Iterator iter = result.getChangedFiles().iterator(); iter.hasNext(); )
64 {
65 ScmFile file = (ScmFile) iter.next();
66 maxLen = Math.max( maxLen, file.getStatus().toString().length() );
67 }
68
69 for ( Iterator iter = result.getChangedFiles().iterator(); iter.hasNext(); )
70 {
71 ScmFile file = (ScmFile) iter.next();
72
73
74 getLog().info( StringUtils.leftPad( file.getStatus().toString(), maxLen ) + " status for " +
75 getRelativePath( baseDir, file.getPath() ) );
76 }
77 }
78 catch ( IOException e )
79 {
80 throw new MojoExecutionException( "Cannot run status command : ", e );
81 }
82 catch ( ScmException e )
83 {
84 throw new MojoExecutionException( "Cannot run status command : ", e );
85 }
86 }
87
88
89
90
91
92
93
94
95 protected String getRelativePath( File baseDir, String path )
96 {
97 if ( path.equals( baseDir.getAbsolutePath() ) )
98 {
99 return ".";
100 }
101 else if ( path.indexOf( baseDir.getAbsolutePath() ) == 0 )
102 {
103
104 return path.substring( baseDir.getAbsolutePath().length() + 1 );
105 }
106 else
107 {
108 return path;
109 }
110 }
111 }