buildframework/helium/sf/java/scm/src/com/nokia/maven/scm/provider/hg/command/tag/HgTagCommand.java
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 588 c7c26511138f
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     1
/*
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     2
 * Licensed to the Apache Software Foundation (ASF) under one
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     3
 * or more contributor license agreements.  See the NOTICE file
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     4
 * distributed with this work for additional information
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     5
 * regarding copyright ownership.  The ASF licenses this file
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     6
 * to you under the Apache License, Version 2.0 (the
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     7
 * "License"); you may not use this file except in compliance
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     8
 * with the License.  You may obtain a copy of the License at
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     9
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    10
 * http://www.apache.org/licenses/LICENSE-2.0
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    11
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    12
 * Unless required by applicable law or agreed to in writing,
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    13
 * software distributed under the License is distributed on an
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    15
 * KIND, either express or implied.  See the License for the
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    16
 * specific language governing permissions and limitations
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    17
 * under the License.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
 */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    20
/* Portion Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    21
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    22
package com.nokia.maven.scm.provider.hg.command.tag;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    23
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
import java.io.File;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    25
import java.util.ArrayList;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    26
import java.util.List;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    27
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
import org.apache.maven.scm.ScmException;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
import org.apache.maven.scm.ScmFile;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
import org.apache.maven.scm.ScmFileSet;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    31
import org.apache.maven.scm.ScmFileStatus;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    32
import org.apache.maven.scm.ScmResult;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    33
import org.apache.maven.scm.command.Command;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    34
import org.apache.maven.scm.command.tag.AbstractTagCommand;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    35
import org.apache.maven.scm.command.tag.TagScmResult;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    36
import org.apache.maven.scm.provider.ScmProviderRepository;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    37
import org.apache.maven.scm.provider.hg.HgUtils;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    38
import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    39
import org.apache.maven.scm.provider.hg.command.HgConsumer;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
import org.apache.maven.scm.provider.hg.command.inventory.HgListConsumer;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
import org.apache.maven.scm.provider.hg.command.tag.HgTagConsumer;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
import org.codehaus.plexus.util.StringUtils;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    44
import com.nokia.maven.scm.provider.hg.repository.HgScmProviderRepository;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    45
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
/**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
 * Tag
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
 * 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
 * @author <a href="mailto:ryan@darksleep.com">ryan daum</a>
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
 * @version $Id: HgTagCommand.java 686566 2008-08-16 21:52:46Z olamy $
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
 */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    52
public class HgTagCommand extends AbstractTagCommand implements Command {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
    /** {@inheritDoc} */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
    @SuppressWarnings("unchecked")
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    55
    protected ScmResult executeTagCommand(ScmProviderRepository scmProviderRepository,
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    56
        ScmFileSet fileSet, String tag, String level) throws ScmException {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
        if (tag == null || StringUtils.isEmpty(tag.trim())) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
            throw new ScmException("tag must be specified");
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    59
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    61
        if (fileSet.getFileList().size() != 0) {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    62
            throw new ScmException("This provider doesn't support tagging subsets of a directory");
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    63
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
        File workingDir = fileSet.getBasedir();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    66
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    67
        // build the command
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
        String[] tagCmd;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
        if (level.equals(new String("local"))) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
            tagCmd = new String[] { HgCommandConstants.TAG_CMD, "--local",
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    71
                HgCommandConstants.MESSAGE_OPTION, "[maven-scm] copy for tag " + tag, tag };
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    72
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    73
        else {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    74
            tagCmd = new String[] { HgCommandConstants.TAG_CMD, HgCommandConstants.MESSAGE_OPTION,
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    75
                "[maven-scm] copy for tag " + tag, tag };
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    77
        // keep the command about in string form for reporting
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    78
        StringBuffer cmd = joinCmd(tagCmd);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    79
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    80
        HgTagConsumer consumer = new HgTagConsumer(getLogger());
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    81
        ScmResult result = HgUtils.execute(consumer, getLogger(), workingDir, tagCmd);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
        HgScmProviderRepository repository = (HgScmProviderRepository) scmProviderRepository;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    83
        if (result.isSuccess()) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    84
            // now push
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    85
            // Push to parent branch if any
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    86
            if (!repository.getURI().equals(fileSet.getBasedir().getAbsolutePath())) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    87
                String[] pushCmd = new String[] { HgCommandConstants.PUSH_CMD, repository.getURI() };
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    88
                result = HgUtils.execute(new HgConsumer(getLogger()), getLogger(), fileSet.getBasedir(), pushCmd);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    89
            }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    90
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    91
        else {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    92
            throw new ScmException("Error while executing command " + cmd.toString());
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    93
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    94
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    95
        // do an inventory to return the files tagged (all of them)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    96
        String[] listCmd = new String[] { HgCommandConstants.INVENTORY_CMD };
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    97
        HgListConsumer listconsumer = new HgListConsumer(getLogger());
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    98
        result = HgUtils.execute(listconsumer, getLogger(), fileSet.getBasedir(), listCmd);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    99
        if (result.isSuccess()) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   100
            List<ScmFile> files = listconsumer.getFiles();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   101
            ArrayList<ScmFile> fileList = new ArrayList<ScmFile>();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   102
            for (ScmFile scmFile : files) {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   103
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   104
                if (!scmFile.getPath().endsWith(".hgtags")) {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   105
                    fileList.add(new ScmFile(scmFile.getPath(), ScmFileStatus.TAGGED));
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   106
                }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   107
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   108
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   109
            return new TagScmResult(fileList, result);
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   110
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   111
        else {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   112
            throw new ScmException("Error while executing command " + cmd.toString());
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   113
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   114
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   115
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   116
    private StringBuffer joinCmd(String[] cmds) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   117
        StringBuffer result = new StringBuffer();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   118
        int i = 0;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   119
        for (String cmd : cmds) {
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   120
            String cmdStr = cmd;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   121
            result.append(cmdStr);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   122
            if (i < cmds.length - 1) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   123
                result.append(" ");
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   124
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   125
            i += 1;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   126
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   127
        return result;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   128
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   129
}