Search in sources :

Example 1 with HgConsumer

use of org.apache.maven.scm.provider.hg.command.HgConsumer in project maven-scm by apache.

the class HgUpdateCommand method executeUpdateCommand.

/**
 * {@inheritDoc}
 */
protected UpdateScmResult executeUpdateCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion tag) throws ScmException {
    File workingDir = fileSet.getBasedir();
    String[] updateCmd;
    // Update branch
    if (repo.isPushChanges()) {
        updateCmd = new String[] { HgCommandConstants.PULL_CMD, HgCommandConstants.REVISION_OPTION, tag != null && !StringUtils.isEmpty(tag.getName()) ? tag.getName() : "tip" };
    } else {
        updateCmd = new String[] { HgCommandConstants.UPDATE_CMD, tag != null && !StringUtils.isEmpty(tag.getName()) ? tag.getName() : "tip", HgCommandConstants.CLEAN_OPTION };
    }
    ScmResult updateResult = HgUtils.execute(new HgConsumer(getLogger()), getLogger(), workingDir, updateCmd);
    if (!updateResult.isSuccess()) {
        return new UpdateScmResult(null, null, updateResult);
    }
    // Find changes from last revision
    int currentRevision = HgUtils.getCurrentRevisionNumber(getLogger(), workingDir);
    int previousRevision = currentRevision - 1;
    String[] diffCmd = new String[] { HgCommandConstants.DIFF_CMD, HgCommandConstants.REVISION_OPTION, "" + previousRevision };
    HgDiffConsumer diffConsumer = new HgDiffConsumer(getLogger(), workingDir);
    ScmResult diffResult = HgUtils.execute(diffConsumer, getLogger(), workingDir, diffCmd);
    // Now translate between diff and update file status
    List<ScmFile> updatedFiles = new ArrayList<ScmFile>();
    List<CharSequence> changes = new ArrayList<CharSequence>();
    List<ScmFile> diffFiles = diffConsumer.getChangedFiles();
    Map<String, CharSequence> diffChanges = diffConsumer.getDifferences();
    for (ScmFile file : diffFiles) {
        changes.add(diffChanges.get(file.getPath()));
        if (file.getStatus() == ScmFileStatus.MODIFIED) {
            updatedFiles.add(new ScmFile(file.getPath(), ScmFileStatus.PATCHED));
        } else {
            updatedFiles.add(file);
        }
    }
    if (repo.isPushChanges()) {
        String[] hgUpdateCmd = new String[] { HgCommandConstants.UPDATE_CMD };
        HgUtils.execute(new HgConsumer(getLogger()), getLogger(), workingDir, hgUpdateCmd);
    }
    return new UpdateScmResultWithRevision(updatedFiles, new ArrayList<ChangeSet>(0), String.valueOf(currentRevision), diffResult);
}
Also used : UpdateScmResult(org.apache.maven.scm.command.update.UpdateScmResult) ScmResult(org.apache.maven.scm.ScmResult) UpdateScmResultWithRevision(org.apache.maven.scm.command.update.UpdateScmResultWithRevision) HgDiffConsumer(org.apache.maven.scm.provider.hg.command.diff.HgDiffConsumer) HgConsumer(org.apache.maven.scm.provider.hg.command.HgConsumer) UpdateScmResult(org.apache.maven.scm.command.update.UpdateScmResult) ArrayList(java.util.ArrayList) ScmFile(org.apache.maven.scm.ScmFile) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File) ChangeSet(org.apache.maven.scm.ChangeSet)

Example 2 with HgConsumer

use of org.apache.maven.scm.provider.hg.command.HgConsumer in project maven-scm by apache.

the class HgBranchCommand method executeBranchCommand.

/**
 * {@inheritDoc}
 */
protected ScmResult executeBranchCommand(ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String branch, ScmBranchParameters scmBranchParameters) throws ScmException {
    if (StringUtils.isBlank(branch)) {
        throw new ScmException("branch must be specified");
    }
    if (!fileSet.getFileList().isEmpty()) {
        throw new ScmException("This provider doesn't support branchging subsets of a directory");
    }
    File workingDir = fileSet.getBasedir();
    // build the command
    String[] branchCmd = new String[] { HgCommandConstants.BRANCH_CMD, branch };
    // keep the command about in string form for reporting
    HgConsumer branchConsumer = new HgConsumer(getLogger()) {

        public void doConsume(ScmFileStatus status, String trimmedLine) {
        // noop
        }
    };
    ScmResult result = HgUtils.execute(branchConsumer, getLogger(), workingDir, branchCmd);
    HgScmProviderRepository repository = (HgScmProviderRepository) scmProviderRepository;
    if (!result.isSuccess()) {
        throw new ScmException("Error while executing command " + joinCmd(branchCmd));
    }
    // First commit.
    String[] commitCmd = new String[] { HgCommandConstants.COMMIT_CMD, HgCommandConstants.MESSAGE_OPTION, scmBranchParameters.getMessage() };
    result = HgUtils.execute(new HgConsumer(getLogger()), getLogger(), workingDir, commitCmd);
    if (!result.isSuccess()) {
        throw new ScmException("Error while executing command " + joinCmd(commitCmd));
    }
    if (repository.isPushChanges()) {
        if (!repository.getURI().equals(fileSet.getBasedir().getAbsolutePath())) {
            String[] pushCmd = new String[] { HgCommandConstants.PUSH_CMD, HgCommandConstants.NEW_BRANCH_OPTION, repository.getURI() };
            result = HgUtils.execute(new HgConsumer(getLogger()), getLogger(), fileSet.getBasedir(), pushCmd);
            if (!result.isSuccess()) {
                throw new ScmException("Error while executing command " + joinCmd(pushCmd));
            }
        }
    }
    // do an inventory to return the files branched (all of them)
    String[] listCmd = new String[] { HgCommandConstants.INVENTORY_CMD };
    HgListConsumer listconsumer = new HgListConsumer(getLogger());
    result = HgUtils.execute(listconsumer, getLogger(), fileSet.getBasedir(), listCmd);
    if (!result.isSuccess()) {
        throw new ScmException("Error while executing command " + joinCmd(listCmd));
    }
    List<ScmFile> files = listconsumer.getFiles();
    List<ScmFile> fileList = new ArrayList<ScmFile>();
    for (ScmFile f : files) {
        fileList.add(new ScmFile(f.getPath(), ScmFileStatus.TAGGED));
    }
    return new BranchScmResult(fileList, result);
}
Also used : ScmException(org.apache.maven.scm.ScmException) BranchScmResult(org.apache.maven.scm.command.branch.BranchScmResult) ScmResult(org.apache.maven.scm.ScmResult) HgConsumer(org.apache.maven.scm.provider.hg.command.HgConsumer) ArrayList(java.util.ArrayList) BranchScmResult(org.apache.maven.scm.command.branch.BranchScmResult) ScmFile(org.apache.maven.scm.ScmFile) ScmFileStatus(org.apache.maven.scm.ScmFileStatus) HgListConsumer(org.apache.maven.scm.provider.hg.command.inventory.HgListConsumer) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File) HgScmProviderRepository(org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository)

Example 3 with HgConsumer

use of org.apache.maven.scm.provider.hg.command.HgConsumer in project maven-scm by apache.

the class HgCheckInCommand method executeCheckInCommand.

/**
 * {@inheritDoc}
 */
protected CheckInScmResult executeCheckInCommand(ScmProviderRepository repo, ScmFileSet fileSet, String message, ScmVersion tag) throws ScmException {
    if (tag != null && !StringUtils.isEmpty(tag.getName())) {
        throw new ScmException("This provider can't handle tags for this operation");
    }
    File workingDir = fileSet.getBasedir();
    String branchName = HgUtils.getCurrentBranchName(getLogger(), workingDir);
    boolean differentOutgoingBranch = repo.isPushChanges() ? HgUtils.differentOutgoingBranchFound(getLogger(), workingDir, branchName) : false;
    // Get files that will be committed (if not specified in fileSet)
    List<ScmFile> commitedFiles = new ArrayList<ScmFile>();
    List<File> files = fileSet.getFileList();
    if (files.isEmpty()) {
        // Either commit all changes
        HgStatusCommand statusCmd = new HgStatusCommand();
        statusCmd.setLogger(getLogger());
        StatusScmResult status = statusCmd.executeStatusCommand(repo, fileSet);
        List<ScmFile> statusFiles = status.getChangedFiles();
        for (ScmFile file : statusFiles) {
            if (file.getStatus() == ScmFileStatus.ADDED || file.getStatus() == ScmFileStatus.DELETED || file.getStatus() == ScmFileStatus.MODIFIED) {
                commitedFiles.add(new ScmFile(file.getPath(), ScmFileStatus.CHECKED_IN));
            }
        }
    } else {
        // Or commit spesific files
        for (File file : files) {
            commitedFiles.add(new ScmFile(file.getPath(), ScmFileStatus.CHECKED_IN));
        }
    }
    // Commit to local branch
    String[] commitCmd = new String[] { HgCommandConstants.COMMIT_CMD, HgCommandConstants.MESSAGE_OPTION, message };
    commitCmd = HgUtils.expandCommandLine(commitCmd, fileSet);
    ScmResult result = HgUtils.execute(new HgConsumer(getLogger()), getLogger(), fileSet.getBasedir(), commitCmd);
    // Push to parent branch if any
    HgScmProviderRepository repository = (HgScmProviderRepository) repo;
    if (repo.isPushChanges()) {
        if (!repository.getURI().equals(fileSet.getBasedir().getAbsolutePath())) {
            String[] pushCmd = new String[] { HgCommandConstants.PUSH_CMD, differentOutgoingBranch ? HgCommandConstants.REVISION_OPTION + branchName : null, repository.getURI() };
            result = HgUtils.execute(new HgConsumer(getLogger()), getLogger(), fileSet.getBasedir(), pushCmd);
        }
        return new CheckInScmResult(commitedFiles, result);
    }
    return new CheckInScmResult(commitedFiles, result);
}
Also used : StatusScmResult(org.apache.maven.scm.command.status.StatusScmResult) ScmException(org.apache.maven.scm.ScmException) ScmResult(org.apache.maven.scm.ScmResult) StatusScmResult(org.apache.maven.scm.command.status.StatusScmResult) CheckInScmResult(org.apache.maven.scm.command.checkin.CheckInScmResult) HgConsumer(org.apache.maven.scm.provider.hg.command.HgConsumer) ArrayList(java.util.ArrayList) CheckInScmResult(org.apache.maven.scm.command.checkin.CheckInScmResult) ScmFile(org.apache.maven.scm.ScmFile) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File) HgStatusCommand(org.apache.maven.scm.provider.hg.command.status.HgStatusCommand) HgScmProviderRepository(org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository)

Example 4 with HgConsumer

use of org.apache.maven.scm.provider.hg.command.HgConsumer in project maven-scm by apache.

the class HgCheckOutCommand method executeCheckOutCommand.

/**
 * {@inheritDoc}
 */
protected CheckOutScmResult executeCheckOutCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion scmVersion, boolean recursive, boolean shallow) throws ScmException {
    HgScmProviderRepository repository = (HgScmProviderRepository) repo;
    String url = repository.getURI();
    File checkoutDir = fileSet.getBasedir();
    try {
        if (getLogger().isInfoEnabled()) {
            getLogger().info("Removing " + checkoutDir);
        }
        FileUtils.deleteDirectory(checkoutDir);
    } catch (IOException e) {
        throw new ScmException("Cannot remove " + checkoutDir);
    }
    // Do the actual checkout
    List<String> cmdList = new ArrayList<String>();
    if (repo.isPushChanges()) {
        cmdList.add(HgCommandConstants.CLONE_CMD);
    } else {
        cmdList.add(HgCommandConstants.UPDATE_CMD);
    }
    if (scmVersion != null && !StringUtils.isEmpty(scmVersion.getName())) {
        cmdList.add(HgCommandConstants.REVISION_OPTION);
        cmdList.add(scmVersion.getName());
    }
    if (!repo.isPushChanges()) {
        cmdList.add(HgCommandConstants.CLEAN_OPTION);
    }
    cmdList.add(url);
    cmdList.add(checkoutDir.getAbsolutePath());
    String[] checkoutCmd = cmdList.toArray(new String[0]);
    HgConsumer checkoutConsumer = new HgConsumer(getLogger());
    HgUtils.execute(checkoutConsumer, getLogger(), checkoutDir.getParentFile(), checkoutCmd);
    // Do inventory to find list of checkedout files
    String[] inventoryCmd = new String[] { HgCommandConstants.INVENTORY_CMD };
    HgCheckOutConsumer consumer = new HgCheckOutConsumer(getLogger(), checkoutDir);
    ScmResult result = HgUtils.execute(consumer, getLogger(), checkoutDir, inventoryCmd);
    return new CheckOutScmResult(consumer.getCheckedOutFiles(), result);
}
Also used : ScmException(org.apache.maven.scm.ScmException) CheckOutScmResult(org.apache.maven.scm.command.checkout.CheckOutScmResult) ScmResult(org.apache.maven.scm.ScmResult) HgConsumer(org.apache.maven.scm.provider.hg.command.HgConsumer) ArrayList(java.util.ArrayList) CheckOutScmResult(org.apache.maven.scm.command.checkout.CheckOutScmResult) IOException(java.io.IOException) HgScmProviderRepository(org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository) File(java.io.File)

Example 5 with HgConsumer

use of org.apache.maven.scm.provider.hg.command.HgConsumer in project maven-scm by apache.

the class HgTagCommand method executeTagCommand.

/**
 * {@inheritDoc}
 */
protected ScmResult executeTagCommand(ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String tag, ScmTagParameters scmTagParameters) throws ScmException {
    if (tag == null || StringUtils.isEmpty(tag.trim())) {
        throw new ScmException("tag must be specified");
    }
    if (!fileSet.getFileList().isEmpty()) {
        throw new ScmException("This provider doesn't support tagging subsets of a directory : " + fileSet.getFileList());
    }
    File workingDir = fileSet.getBasedir();
    // build the command
    String[] tagCmd = new String[] { HgCommandConstants.TAG_CMD, HgCommandConstants.MESSAGE_OPTION, scmTagParameters.getMessage(), tag };
    // keep the command about in string form for reporting
    StringBuilder cmd = joinCmd(tagCmd);
    HgTagConsumer consumer = new HgTagConsumer(getLogger());
    ScmResult result = HgUtils.execute(consumer, getLogger(), workingDir, tagCmd);
    HgScmProviderRepository repository = (HgScmProviderRepository) scmProviderRepository;
    if (result.isSuccess()) {
        if (repository.isPushChanges()) {
            if (!repository.getURI().equals(fileSet.getBasedir().getAbsolutePath())) {
                String branchName = HgUtils.getCurrentBranchName(getLogger(), workingDir);
                boolean differentOutgoingBranch = HgUtils.differentOutgoingBranchFound(getLogger(), workingDir, branchName);
                String[] pushCmd = new String[] { HgCommandConstants.PUSH_CMD, differentOutgoingBranch ? HgCommandConstants.REVISION_OPTION + branchName : null, repository.getURI() };
                result = HgUtils.execute(new HgConsumer(getLogger()), getLogger(), fileSet.getBasedir(), pushCmd);
            }
        }
    } else {
        throw new ScmException("Error while executing command " + cmd.toString());
    }
    // do an inventory to return the files tagged (all of them)
    String[] listCmd = new String[] { HgCommandConstants.INVENTORY_CMD };
    HgListConsumer listconsumer = new HgListConsumer(getLogger());
    result = HgUtils.execute(listconsumer, getLogger(), fileSet.getBasedir(), listCmd);
    if (result.isSuccess()) {
        List<ScmFile> files = listconsumer.getFiles();
        List<ScmFile> fileList = new ArrayList<ScmFile>();
        for (ScmFile f : files) {
            if (!f.getPath().endsWith(".hgtags")) {
                fileList.add(new ScmFile(f.getPath(), ScmFileStatus.TAGGED));
            }
        }
        return new TagScmResult(fileList, result);
    } else {
        throw new ScmException("Error while executing command " + cmd.toString());
    }
}
Also used : ScmException(org.apache.maven.scm.ScmException) ScmResult(org.apache.maven.scm.ScmResult) TagScmResult(org.apache.maven.scm.command.tag.TagScmResult) HgConsumer(org.apache.maven.scm.provider.hg.command.HgConsumer) ArrayList(java.util.ArrayList) ScmFile(org.apache.maven.scm.ScmFile) HgListConsumer(org.apache.maven.scm.provider.hg.command.inventory.HgListConsumer) TagScmResult(org.apache.maven.scm.command.tag.TagScmResult) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File) HgScmProviderRepository(org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository)

Aggregations

File (java.io.File)5 ArrayList (java.util.ArrayList)5 ScmResult (org.apache.maven.scm.ScmResult)5 HgConsumer (org.apache.maven.scm.provider.hg.command.HgConsumer)5 ScmException (org.apache.maven.scm.ScmException)4 ScmFile (org.apache.maven.scm.ScmFile)4 HgScmProviderRepository (org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository)4 HgListConsumer (org.apache.maven.scm.provider.hg.command.inventory.HgListConsumer)2 IOException (java.io.IOException)1 ChangeSet (org.apache.maven.scm.ChangeSet)1 ScmFileStatus (org.apache.maven.scm.ScmFileStatus)1 BranchScmResult (org.apache.maven.scm.command.branch.BranchScmResult)1 CheckInScmResult (org.apache.maven.scm.command.checkin.CheckInScmResult)1 CheckOutScmResult (org.apache.maven.scm.command.checkout.CheckOutScmResult)1 StatusScmResult (org.apache.maven.scm.command.status.StatusScmResult)1 TagScmResult (org.apache.maven.scm.command.tag.TagScmResult)1 UpdateScmResult (org.apache.maven.scm.command.update.UpdateScmResult)1 UpdateScmResultWithRevision (org.apache.maven.scm.command.update.UpdateScmResultWithRevision)1 HgDiffConsumer (org.apache.maven.scm.provider.hg.command.diff.HgDiffConsumer)1 HgStatusCommand (org.apache.maven.scm.provider.hg.command.status.HgStatusCommand)1