View Javadoc
1   package org.apache.maven.scm.provider.bazaar.command.update;
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 java.io.File;
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.maven.scm.ChangeSet;
29  import org.apache.maven.scm.ScmException;
30  import org.apache.maven.scm.ScmFile;
31  import org.apache.maven.scm.ScmFileSet;
32  import org.apache.maven.scm.ScmFileStatus;
33  import org.apache.maven.scm.ScmResult;
34  import org.apache.maven.scm.ScmVersion;
35  import org.apache.maven.scm.command.Command;
36  import org.apache.maven.scm.command.changelog.ChangeLogCommand;
37  import org.apache.maven.scm.command.update.AbstractUpdateCommand;
38  import org.apache.maven.scm.command.update.UpdateScmResult;
39  import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
40  import org.apache.maven.scm.provider.ScmProviderRepository;
41  import org.apache.maven.scm.provider.bazaar.BazaarUtils;
42  import org.apache.maven.scm.provider.bazaar.command.BazaarConstants;
43  import org.apache.maven.scm.provider.bazaar.command.BazaarConsumer;
44  import org.apache.maven.scm.provider.bazaar.command.changelog.BazaarChangeLogCommand;
45  import org.apache.maven.scm.provider.bazaar.command.diff.BazaarDiffConsumer;
46  import org.codehaus.plexus.util.StringUtils;
47  
48  /**
49   * @author <a href="mailto:torbjorn@smorgrav.org">Torbj�rn Eikli Sm�rgrav</a>
50   *
51   */
52  public class BazaarUpdateCommand
53      extends AbstractUpdateCommand
54      implements Command
55  {
56  
57      /** {@inheritDoc} */
58      protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version )
59          throws ScmException
60      {
61  
62          if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
63          {
64              throw new ScmException( "This provider can't handle tags." );
65          }
66  
67          File workingDir = fileSet.getBasedir();
68  
69          // Update branch
70          String[] updateCmd = new String[] { BazaarConstants.PULL_CMD };
71          ScmResult updateResult = BazaarUtils.execute( new BazaarConsumer( getLogger() ), getLogger(), workingDir,
72                                                        updateCmd );
73  
74          if ( !updateResult.isSuccess() )
75          {
76              return new UpdateScmResult( null, null, updateResult );
77          }
78  
79          // Find changes from last revision
80          int currentRevision = BazaarUtils.getCurrentRevisionNumber( getLogger(), workingDir );
81          int previousRevision = currentRevision - 1;
82          String[] diffCmd =
83              new String[] { BazaarConstants.DIFF_CMD, BazaarConstants.REVISION_OPTION, "" + previousRevision };
84          BazaarDiffConsumer diffConsumer = new BazaarDiffConsumer( getLogger(), workingDir );
85          ScmResult diffResult = BazaarUtils.execute( diffConsumer, getLogger(), workingDir, diffCmd );
86  
87          // Now translate between diff and update file status
88          List<ScmFile> updatedFiles = new ArrayList<ScmFile>();
89          List<CharSequence> changes = new ArrayList<CharSequence>();
90          List<ScmFile> diffFiles = diffConsumer.getChangedFiles();
91          Map<String, CharSequence> diffChanges = diffConsumer.getDifferences();
92          for ( Iterator<ScmFile> it = diffFiles.iterator(); it.hasNext(); )
93          {
94              ScmFile file = it.next();
95              changes.add( diffChanges.get( file ) );
96              if ( file.getStatus() == ScmFileStatus.MODIFIED )
97              {
98                  updatedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.PATCHED ) );
99              }
100             else
101             {
102                 updatedFiles.add( file );
103             }
104         }
105 
106         return new UpdateScmResultWithRevision( updatedFiles, new ArrayList<ChangeSet>(0), String.valueOf( currentRevision ), diffResult );
107     }
108 
109     /** {@inheritDoc} */
110     protected ChangeLogCommand getChangeLogCommand()
111     {
112         BazaarChangeLogCommand command = new BazaarChangeLogCommand();
113         command.setLogger( getLogger() );
114         return command;
115     }
116 }