View Javadoc

1   package org.apache.maven.scm.provider.git.gitexe.command.list;
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.ScmFileStatus;
25  import org.apache.maven.scm.ScmVersion;
26  import org.apache.maven.scm.command.list.AbstractListCommand;
27  import org.apache.maven.scm.command.list.ListScmResult;
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.repository.GitScmProviderRepository;
31  import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
32  import org.codehaus.plexus.util.cli.CommandLineUtils;
33  import org.codehaus.plexus.util.cli.Commandline;
34  
35  import java.io.File;
36  
37  /**
38   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
39   *
40   */
41  public class GitListCommand
42      extends AbstractListCommand
43      implements GitCommand
44  {
45      /** {@inheritDoc} */
46      protected ListScmResult executeListCommand( ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive,
47                                                  ScmVersion scmVersion )
48          throws ScmException
49      {
50          GitScmProviderRepository repository = (GitScmProviderRepository) repo;
51  
52          if ( GitScmProviderRepository.PROTOCOL_FILE.equals( repository.getFetchInfo().getProtocol() )
53              && repository.getFetchInfo().getPath().indexOf( fileSet.getBasedir().getPath() ) >= 0 )
54          {
55              throw new ScmException( "remote repository must not be the working directory" );
56          }
57  
58          int exitCode;
59  
60          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
61          GitListConsumer consumer = new GitListConsumer( getLogger(), fileSet.getBasedir().getParentFile(),
62                                                          ScmFileStatus.CHECKED_IN );
63  
64          Commandline cl = createCommandLine( repository, fileSet.getBasedir() );
65  
66          exitCode = GitCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
67          if ( exitCode != 0 )
68          {
69              return new ListScmResult( cl.toString(), "The git-ls-files command failed.", stderr.getOutput(), false );
70          }
71  
72          return new ListScmResult( cl.toString(), consumer.getListedFiles() );
73      }
74  
75      // ----------------------------------------------------------------------
76      //
77      // ----------------------------------------------------------------------
78  
79      public static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory )
80      {
81          Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "ls-files" );
82  
83          return cl;
84      }
85  
86  }