buildframework/helium/sf/java/scm/src/com/nokia/maven/scm/provider/hg/command/update/HgUpdateCommand.java
changeset 628 7c4a911dc066
parent 587 85df38eb4012
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
    15  *
    15  *
    16  */
    16  */
    17 
    17 
    18 package com.nokia.maven.scm.provider.hg.command.update;
    18 package com.nokia.maven.scm.provider.hg.command.update;
    19 
    19 
       
    20 import java.io.File;
       
    21 import java.util.ArrayList;
       
    22 import java.util.List;
       
    23 import java.util.Map;
       
    24 
       
    25 import org.apache.maven.scm.ScmException;
       
    26 import org.apache.maven.scm.ScmFile;
       
    27 import org.apache.maven.scm.ScmFileSet;
       
    28 import org.apache.maven.scm.ScmFileStatus;
       
    29 import org.apache.maven.scm.ScmResult;
       
    30 import org.apache.maven.scm.ScmVersion;
       
    31 import org.apache.maven.scm.command.update.UpdateScmResult;
       
    32 import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
    20 import org.apache.maven.scm.provider.ScmProviderRepository;
    33 import org.apache.maven.scm.provider.ScmProviderRepository;
    21 import org.apache.maven.scm.provider.hg.HgUtils;
    34 import org.apache.maven.scm.provider.hg.HgUtils;
    22 import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
    35 import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
    23 import org.apache.maven.scm.provider.hg.command.HgConsumer;
    36 import org.apache.maven.scm.provider.hg.command.HgConsumer;
    24 import org.apache.maven.scm.provider.hg.command.diff.HgDiffConsumer;
    37 import org.apache.maven.scm.provider.hg.command.diff.HgDiffConsumer;
    25 import org.apache.maven.scm.ScmFileSet;
       
    26 import org.apache.maven.scm.command.update.UpdateScmResult;
       
    27 import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
       
    28 import org.apache.maven.scm.ScmFile;
       
    29 import org.apache.maven.scm.ScmFileStatus;
       
    30 import org.apache.maven.scm.ScmResult;
       
    31 import org.apache.maven.scm.ScmVersion;
       
    32 import org.apache.maven.scm.ScmException;
       
    33 import org.codehaus.plexus.util.StringUtils;
    38 import org.codehaus.plexus.util.StringUtils;
    34 
    39 
    35 import java.util.ArrayList;
    40 import com.nokia.maven.scm.provider.hg.HgUtilsInternal;
    36 import java.util.List;
       
    37 import java.util.Map;
       
    38 
       
    39 import java.io.File;
       
    40 
    41 
    41 /**
    42 /**
    42  */
    43  */
    43 public class HgUpdateCommand extends
    44 public class HgUpdateCommand extends
    44         org.apache.maven.scm.provider.hg.command.update.HgUpdateCommand {
    45     org.apache.maven.scm.provider.hg.command.update.HgUpdateCommand {
    45 
    46 
    46     /** {@inheritDoc} */
    47     /** {@inheritDoc} */
    47     @SuppressWarnings("unchecked")
    48     @SuppressWarnings("unchecked")
    48     protected UpdateScmResult executeUpdateCommand(ScmProviderRepository repo,
    49     protected UpdateScmResult executeUpdateCommand(ScmProviderRepository repo, ScmFileSet fileSet,
    49             ScmFileSet fileSet, ScmVersion tag) throws ScmException {
    50         ScmVersion tag) throws ScmException {
    50         File workingDir = fileSet.getBasedir();
    51         File workingDir = fileSet.getBasedir();
    51 
    52 
    52         // Find changes from last revision
    53         // Find changes from last revision
    53         int previousRevision = HgUtils.getCurrentRevisionNumber(getLogger(),
    54         long previousRevision = HgUtilsInternal.getCurrentRevisionNumber(getLogger(), workingDir);
    54                 workingDir);
       
    55         // Update branch
    55         // Update branch
    56         String[] updateCmd = new String[] {
    56         String[] updateCmd = new String[] { "update", HgCommandConstants.REVISION_OPTION,
    57                 "update",
    57             tag != null && !StringUtils.isEmpty(tag.getName()) ? tag.getName() : "tip" };
    58                 HgCommandConstants.REVISION_OPTION,
    58         ScmResult updateResult = HgUtils.execute(new HgConsumer(getLogger()), getLogger(), workingDir, updateCmd);
    59                 tag != null && !StringUtils.isEmpty(tag.getName()) ? tag
       
    60                         .getName() : "tip" };
       
    61         ScmResult updateResult = HgUtils.execute(new HgConsumer(getLogger()),
       
    62                 getLogger(), workingDir, updateCmd);
       
    63 
    59 
    64         if (!updateResult.isSuccess()) {
    60         if (!updateResult.isSuccess()) {
    65             return new UpdateScmResult(null, null, updateResult);
    61             return new UpdateScmResult(null, null, updateResult);
    66         }
    62         }
    67 
    63 
    68         // Find changes from last revision
    64         // Find changes from last revision
    69         int currentRevision = HgUtils.getCurrentRevisionNumber(getLogger(),
    65         long currentRevision = HgUtilsInternal.getCurrentRevisionNumber(getLogger(),
    70                 workingDir);
    66                 workingDir);
       
    67                 
       
    68         // No point of calculating a delta if nothing has been updated.
       
    69         getLogger().debug("Changeset before update: " + String.format("%012x", previousRevision));
       
    70         getLogger().debug("Changeset after update: " + String.format("%012x", currentRevision));
       
    71         if (previousRevision == currentRevision) {
       
    72             return new UpdateScmResultWithRevision(new ArrayList<ScmFile>(), new ArrayList(), String
       
    73                     .valueOf(currentRevision), updateResult);            
       
    74         }
       
    75         
    71         List<ScmFile> updatedFiles = new ArrayList<ScmFile>();
    76         List<ScmFile> updatedFiles = new ArrayList<ScmFile>();
    72         List changes = new ArrayList();
    77         List changes = new ArrayList();
    73         String[] diffCmd = null;
    78         String[] diffCmd = null;
    74         if (currentRevision == 0) {
    79         if (currentRevision == 0) {
    75             diffCmd = new String[] { HgCommandConstants.DIFF_CMD,
    80             diffCmd = new String[] { HgCommandConstants.DIFF_CMD, "-c", "" + String.format("%012x", currentRevision)};
    76                     "-c", "" + currentRevision};
       
    77         } else {
    81         } else {
    78             diffCmd = new String[] { HgCommandConstants.DIFF_CMD,
    82             diffCmd = new String[] { HgCommandConstants.DIFF_CMD,
    79                     HgCommandConstants.REVISION_OPTION, "" + previousRevision,
    83                 HgCommandConstants.REVISION_OPTION, "" + String.format("%012x", previousRevision),
    80                     HgCommandConstants.REVISION_OPTION, "" + currentRevision};
    84                 HgCommandConstants.REVISION_OPTION, "" + String.format("%012x", currentRevision)};
    81         }
    85         }
    82         HgDiffConsumer diffConsumer = new HgDiffConsumer(getLogger(),
    86         HgDiffConsumer diffConsumer = new HgDiffConsumer(getLogger(), workingDir);
    83                     workingDir);
    87         updateResult = HgUtils.execute(diffConsumer, getLogger(), workingDir, diffCmd);
    84         updateResult = HgUtils.execute(diffConsumer, getLogger(),
       
    85                 workingDir, diffCmd);
       
    86 
    88 
    87         // Now translate between diff and update file status
    89         // Now translate between diff and update file status
    88         List<ScmFile> diffFiles = diffConsumer.getChangedFiles();
    90         List<ScmFile> diffFiles = diffConsumer.getChangedFiles();
    89         Map diffChanges = diffConsumer.getDifferences();
    91         Map diffChanges = diffConsumer.getDifferences();
    90         for (ScmFile diffFile : diffFiles) {
    92         for (ScmFile diffFile : diffFiles) {
    93                 updatedFiles.add(new ScmFile(diffFile.getPath(), ScmFileStatus.PATCHED));
    95                 updatedFiles.add(new ScmFile(diffFile.getPath(), ScmFileStatus.PATCHED));
    94             } else {
    96             } else {
    95                 updatedFiles.add(diffFile);
    97                 updatedFiles.add(diffFile);
    96             }
    98             }
    97         }
    99         }
       
   100 
    98         return new UpdateScmResultWithRevision(updatedFiles, changes, String
   101         return new UpdateScmResultWithRevision(updatedFiles, changes, String
    99                 .valueOf(currentRevision), updateResult);
   102                 .valueOf(currentRevision), new ScmResult(updateResult.getCommandLine(),
       
   103                         updateResult.getCommandOutput(), updateResult.getProviderMessage(), true));
   100     }
   104     }
   101 
   105 
   102 }
   106 }