View Javadoc
1   package org.apache.maven.scm.provider.starteam.command.diff;
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 org.apache.maven.scm.ScmException;
23  import org.apache.maven.scm.ScmFileSet;
24  import org.apache.maven.scm.ScmVersion;
25  import org.apache.maven.scm.command.diff.AbstractDiffCommand;
26  import org.apache.maven.scm.command.diff.DiffScmResult;
27  import org.apache.maven.scm.provider.ScmProviderRepository;
28  import org.apache.maven.scm.provider.starteam.command.StarteamCommand;
29  import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils;
30  import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
31  import org.codehaus.plexus.util.StringUtils;
32  import org.codehaus.plexus.util.cli.CommandLineUtils;
33  import org.codehaus.plexus.util.cli.Commandline;
34  
35  import java.util.ArrayList;
36  import java.util.List;
37  
38  /**
39   * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a>
40   *
41   */
42  public class StarteamDiffCommand
43      extends AbstractDiffCommand
44      implements StarteamCommand
45  {
46      // ----------------------------------------------------------------------
47      // AbstractDiffCommand Implementation
48      // ----------------------------------------------------------------------
49  
50      /** {@inheritDoc} */
51      protected DiffScmResult executeDiffCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion startVersion,
52                                                  ScmVersion endVersion )
53          throws ScmException
54      {
55          if ( getLogger().isInfoEnabled() )
56          {
57              getLogger().info( "Working directory: " + fileSet.getBasedir().getAbsolutePath() );
58          }
59  
60          if ( fileSet.getFileList().isEmpty() )
61          {
62              throw new ScmException( "This provider doesn't support diff command on a subsets of a directory" );
63          }
64  
65          StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo;
66  
67          StarteamDiffConsumer consumer = new StarteamDiffConsumer( getLogger(), fileSet.getBasedir() );
68  
69          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
70  
71          Commandline cl = createCommandLine( repository, fileSet, startVersion, endVersion );
72  
73          int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
74  
75          if ( exitCode != 0 )
76          {
77              return new DiffScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(), false );
78          }
79  
80          return new DiffScmResult( cl.toString(), consumer.getChangedFiles(), consumer.getDifferences(),
81                                    consumer.getPatch() );
82      }
83  
84      // ----------------------------------------------------------------------
85      //
86      // ----------------------------------------------------------------------
87  
88      public static Commandline createCommandLine( StarteamScmProviderRepository repo, ScmFileSet workingDirectory,
89                                                   ScmVersion startLabel, ScmVersion endLabel )
90          throws ScmException
91      {
92  
93          List<String> args = new ArrayList<String>();
94  
95          args.add( "-filter" );
96          args.add( "M" );
97  
98          if ( startLabel != null && StringUtils.isNotEmpty( startLabel.getName() ) )
99          {
100             args.add( "-vl" );
101 
102             args.add( startLabel.getName() );
103         }
104 
105         if ( endLabel != null && StringUtils.isNotEmpty( endLabel.getName() ) )
106         {
107             args.add( "-vl" );
108 
109             args.add( endLabel.getName() );
110         }
111 
112         if ( endLabel != null && ( startLabel == null || StringUtils.isEmpty( startLabel.getName() ) ) )
113         {
114             throw new ScmException( "Missing start label." );
115         }
116 
117         StarteamCommandLineUtils.addEOLOption( args );
118 
119         return StarteamCommandLineUtils.createStarteamCommandLine( "diff", args, workingDirectory, repo );
120     }
121 }