001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.scm.provider.hg.command.update; 020 021import java.io.File; 022import java.util.ArrayList; 023import java.util.List; 024import java.util.Map; 025 026import org.apache.commons.lang3.StringUtils; 027import org.apache.maven.scm.ScmException; 028import org.apache.maven.scm.ScmFile; 029import org.apache.maven.scm.ScmFileSet; 030import org.apache.maven.scm.ScmFileStatus; 031import org.apache.maven.scm.ScmResult; 032import org.apache.maven.scm.ScmVersion; 033import org.apache.maven.scm.command.Command; 034import org.apache.maven.scm.command.changelog.ChangeLogCommand; 035import org.apache.maven.scm.command.update.AbstractUpdateCommand; 036import org.apache.maven.scm.command.update.UpdateScmResult; 037import org.apache.maven.scm.command.update.UpdateScmResultWithRevision; 038import org.apache.maven.scm.provider.ScmProviderRepository; 039import org.apache.maven.scm.provider.hg.HgUtils; 040import org.apache.maven.scm.provider.hg.command.HgCommandConstants; 041import org.apache.maven.scm.provider.hg.command.HgConsumer; 042import org.apache.maven.scm.provider.hg.command.changelog.HgChangeLogCommand; 043import org.apache.maven.scm.provider.hg.command.diff.HgDiffConsumer; 044 045/** 046 * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a> 047 * @author Olivier Lamy 048 */ 049public class HgUpdateCommand extends AbstractUpdateCommand implements Command { 050 /** 051 * {@inheritDoc} 052 */ 053 protected UpdateScmResult executeUpdateCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion tag) 054 throws ScmException { 055 File workingDir = fileSet.getBasedir(); 056 057 String[] updateCmd; 058 // Update branch 059 if (repo.isPushChanges()) { 060 updateCmd = new String[] { 061 HgCommandConstants.PULL_CMD, 062 HgCommandConstants.REVISION_OPTION, 063 tag != null && !StringUtils.isEmpty(tag.getName()) ? tag.getName() : "tip" 064 }; 065 } else { 066 updateCmd = new String[] { 067 HgCommandConstants.UPDATE_CMD, 068 tag != null && !StringUtils.isEmpty(tag.getName()) ? tag.getName() : "tip", 069 HgCommandConstants.CLEAN_OPTION 070 }; 071 } 072 ScmResult updateResult = HgUtils.execute(new HgConsumer(), workingDir, updateCmd); 073 074 if (!updateResult.isSuccess()) { 075 return new UpdateScmResult(null, null, updateResult); 076 } 077 078 // Find changes from last revision 079 int currentRevision = HgUtils.getCurrentRevisionNumber(workingDir); 080 int previousRevision = currentRevision - 1; 081 String[] diffCmd = 082 new String[] {HgCommandConstants.DIFF_CMD, HgCommandConstants.REVISION_OPTION, "" + previousRevision}; 083 HgDiffConsumer diffConsumer = new HgDiffConsumer(workingDir); 084 ScmResult diffResult = HgUtils.execute(diffConsumer, workingDir, diffCmd); 085 086 // Now translate between diff and update file status 087 List<ScmFile> updatedFiles = new ArrayList<>(); 088 List<CharSequence> changes = new ArrayList<>(); 089 List<ScmFile> diffFiles = diffConsumer.getChangedFiles(); 090 Map<String, CharSequence> diffChanges = diffConsumer.getDifferences(); 091 for (ScmFile file : diffFiles) { 092 changes.add(diffChanges.get(file.getPath())); 093 if (file.getStatus() == ScmFileStatus.MODIFIED) { 094 updatedFiles.add(new ScmFile(file.getPath(), ScmFileStatus.PATCHED)); 095 } else { 096 updatedFiles.add(file); 097 } 098 } 099 100 if (repo.isPushChanges()) { 101 String[] hgUpdateCmd = new String[] {HgCommandConstants.UPDATE_CMD}; 102 HgUtils.execute(new HgConsumer(), workingDir, hgUpdateCmd); 103 } 104 105 return new UpdateScmResultWithRevision( 106 updatedFiles, new ArrayList<>(0), String.valueOf(currentRevision), diffResult); 107 } 108 109 protected ChangeLogCommand getChangeLogCommand() { 110 return new HgChangeLogCommand(); 111 } 112}