[maven-scm] copy for tag maven-scm-1.0

git-svn-id: https://svn.apache.org/repos/asf/maven/scm/tags/maven-scm-1.0@541012 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/HgScmProvider.java b/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/HgScmProvider.java
index ae420c4..357600b 100644
--- a/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/HgScmProvider.java
+++ b/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/HgScmProvider.java
@@ -31,9 +31,11 @@
 import org.apache.maven.scm.command.remove.RemoveScmResult;
 import org.apache.maven.scm.command.status.StatusScmResult;
 import org.apache.maven.scm.command.update.UpdateScmResult;
+import org.apache.maven.scm.command.tag.TagScmResult;
 import org.apache.maven.scm.provider.AbstractScmProvider;
 import org.apache.maven.scm.provider.ScmProviderRepository;
 import org.apache.maven.scm.provider.hg.command.HgCommand;
+import org.apache.maven.scm.provider.hg.command.tag.HgTagCommand;
 import org.apache.maven.scm.provider.hg.command.add.HgAddCommand;
 import org.apache.maven.scm.provider.hg.command.changelog.HgChangeLogCommand;
 import org.apache.maven.scm.provider.hg.command.checkin.HgCheckInCommand;
@@ -239,6 +241,21 @@
     }
 
     /**
+     * @see org.apache.maven.scm.provider.AbstractScmProvider#checkout(org.apache.maven.scm.provider.ScmProviderRepository,org.apache.maven.scm.ScmFileSet,org.apache.maven.scm.CommandParameters)
+     */
+    public TagScmResult tag( ScmProviderRepository repository, ScmFileSet fileSet,
+                                       CommandParameters parameters )
+        throws ScmException
+    {
+        HgTagCommand command = new HgTagCommand();
+
+        command.setLogger( getLogger() );
+
+        return (TagScmResult) command.execute( repository, fileSet, parameters );
+    }
+
+
+    /**
      * @see org.apache.maven.scm.provider.AbstractScmProvider#diff(org.apache.maven.scm.provider.ScmProviderRepository,org.apache.maven.scm.ScmFileSet,org.apache.maven.scm.CommandParameters)
      */
     public DiffScmResult diff( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
diff --git a/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/HgCommand.java b/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/HgCommand.java
index ace3edd..bfc2bbc 100644
--- a/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/HgCommand.java
+++ b/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/HgCommand.java
@@ -93,7 +93,12 @@
     public static final String REVNO_CMD = "id";
 
     /**
-     * Show inventory of the current working copy or a revision
+     * Tag this revision
+     */
+    public static final String TAG_CMD = "tag";
+
+    /**
+     * Show list of the current working copy or a revision
      */
     public static final String INVENTORY_CMD = "locate";
 
@@ -111,4 +116,5 @@
     public static final String VERSION = "version";
 
     public static final String CHECK = "check";
+    String ALL_OPTION = "-A";
 }
diff --git a/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/HgConsumer.java b/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/HgConsumer.java
index 0b16d8d..13b788a 100644
--- a/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/HgConsumer.java
+++ b/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/HgConsumer.java
@@ -74,13 +74,13 @@
         identifiers.put( "renamed", ScmFileStatus.MODIFIED );
 
         /** Statuses from hg status;
-         *  TODO "! deleted but still tracked"
-         *  d 
          */
         identifiers.put( "A", ScmFileStatus.ADDED );
         identifiers.put( "?", ScmFileStatus.UNKNOWN );
         identifiers.put( "M", ScmFileStatus.MODIFIED );
         identifiers.put( "R", ScmFileStatus.DELETED );
+        identifiers.put( "C", ScmFileStatus.CHECKED_IN );
+        identifiers.put( "!", ScmFileStatus.MISSING );
         identifiers.put( "I", ScmFileStatus.UNKNOWN ); // not precisely the same, but i think semantics work? - rwd
 
         messages.put( "hg: WARNING:", "WARNING" );
diff --git a/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/inventory/HgListCommand.java b/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/inventory/HgListCommand.java
new file mode 100644
index 0000000..9b6eda4
--- /dev/null
+++ b/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/inventory/HgListCommand.java
@@ -0,0 +1,71 @@
+package org.apache.maven.scm.provider.hg.command.inventory;
+
+import org.apache.maven.scm.provider.hg.command.HgCommand;
+import org.apache.maven.scm.provider.hg.HgUtils;
+import org.apache.maven.scm.provider.ScmProviderRepository;
+import org.apache.maven.scm.command.list.AbstractListCommand;
+import org.apache.maven.scm.command.list.ListScmResult;
+import org.apache.maven.scm.*;
+
+import java.io.File;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * Get a list of all files in the repository
+ * 
+ * @author <a href="mailto:ryan@darksleep.com">ryan daum</a>
+ */
+public class HgListCommand extends AbstractListCommand
+        implements HgCommand 
+{
+
+    protected ListScmResult executeListCommand(ScmProviderRepository repository, ScmFileSet fileSet, boolean recursive, ScmVersion scmVersion) throws ScmException {
+
+        if ( fileSet.getFiles().length != 0 )
+        {
+            throw new ScmException( "This provider doesn't support listing subsets of a directory" );
+        }
+        //
+        File workingDir = fileSet.getBasedir();
+
+         // build the command
+        String[] listCmd = new String[]{STATUS_CMD, ALL_OPTION};
+
+        // keep the command about in string form for reporting
+        StringBuffer cmd = new StringBuffer();
+        for (int i = 0; i < listCmd.length; i++) {
+            String s = listCmd[i];
+            cmd.append(s);
+            if (i < listCmd.length - 1)
+                cmd.append(" ");
+        }
+
+        HgListConsumer consumer = new HgListConsumer( getLogger() );
+
+        ScmResult result = HgUtils.execute( consumer, getLogger(), workingDir, listCmd);
+
+        if (result.isSuccess()) {
+            return new ListScmResult(consumer.getFiles(), result);
+        } else {
+            throw new ScmException( "Error while executing command " + cmd.toString() );
+        }
+    }
+}
diff --git a/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/inventory/HgListConsumer.java b/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/inventory/HgListConsumer.java
new file mode 100644
index 0000000..8125a60
--- /dev/null
+++ b/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/inventory/HgListConsumer.java
@@ -0,0 +1,52 @@
+package org.apache.maven.scm.provider.hg.command.inventory;
+
+import org.apache.maven.scm.provider.hg.command.HgConsumer;
+import org.apache.maven.scm.log.ScmLogger;
+import org.apache.maven.scm.ScmFileStatus;
+import org.apache.maven.scm.ScmFile;
+
+import java.util.List;
+import java.util.ArrayList;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * Get a list of all files in the repository
+ *
+ * @author <a href="mailto:ryan@darksleep.com">ryan daum</a>
+ */
+public class HgListConsumer extends HgConsumer {
+
+    private List files = new ArrayList();
+
+    public HgListConsumer( ScmLogger logger )
+    {
+        super( logger );
+    }
+
+    public void doConsume( ScmFileStatus status, String trimmedLine )
+    {
+        files.add(new ScmFile(trimmedLine, status));
+    }
+
+    public List getFiles() {
+        return files;
+    }
+}
diff --git a/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/tag/HgTagCommand.java b/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/tag/HgTagCommand.java
new file mode 100644
index 0000000..2f6a2dc
--- /dev/null
+++ b/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/tag/HgTagCommand.java
@@ -0,0 +1,121 @@
+package org.apache.maven.scm.provider.hg.command.tag;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.scm.*;
+import org.apache.maven.scm.command.add.AddScmResult;
+import org.apache.maven.scm.command.tag.AbstractTagCommand;
+import org.apache.maven.scm.command.tag.TagScmResult;
+import org.apache.maven.scm.command.list.ListScmResult;
+import org.apache.maven.scm.provider.ScmProviderRepository;
+import org.apache.maven.scm.provider.hg.HgUtils;
+import org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository;
+import org.apache.maven.scm.provider.hg.command.HgCommand;
+import org.apache.maven.scm.provider.hg.command.HgConsumer;
+import org.apache.maven.scm.provider.hg.command.inventory.HgListCommand;
+import org.apache.maven.scm.provider.hg.command.inventory.HgListConsumer;
+import org.apache.maven.scm.provider.hg.command.add.HgAddConsumer;
+import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.FileUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * Tag
+ *
+ * @author <a href="mailto:ryan@darksleep.com">ryan daum</a>
+ */
+public class HgTagCommand
+        extends AbstractTagCommand
+        implements HgCommand
+{
+
+    protected ScmResult executeTagCommand(ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String tag, String message) throws ScmException {
+
+        if ( tag == null || StringUtils.isEmpty( tag.trim() ) )
+        {
+            throw new ScmException( "tag must be specified" );
+        }
+
+        if ( fileSet.getFiles().length != 0 )
+        {
+            throw new ScmException( "This provider doesn't support tagging subsets of a directory" );
+        }
+
+        File workingDir = fileSet.getBasedir();
+
+
+        // build the command
+        String[] tagCmd = new String[]{TAG_CMD, MESSAGE_OPTION, message, tag };
+
+        // keep the command about in string form for reporting
+        StringBuffer cmd = joinCmd(tagCmd);
+
+        HgTagConsumer consumer = new HgTagConsumer( getLogger() );
+        ScmResult result = HgUtils.execute( consumer, getLogger(), workingDir, tagCmd);
+        HgScmProviderRepository repository = (HgScmProviderRepository) scmProviderRepository;
+        if (result.isSuccess()) {
+            // now push
+            // Push to parent branch if any
+            if ( !repository.getURI().equals( fileSet.getBasedir().getAbsolutePath() ) )
+            {
+                String[] push_cmd = new String[]{HgCommand.PUSH_CMD, repository.getURI()};
+                result = HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), push_cmd );
+            }
+        } else {
+            throw new ScmException( "Error while executing command " + cmd.toString() );
+        }
+
+        // do an inventory to return the files tagged (all of them)
+        String[] list_cmd = new String[]{HgCommand.INVENTORY_CMD};
+        HgListConsumer listconsumer = new HgListConsumer(getLogger());
+        result =  HgUtils.execute(listconsumer, getLogger(), fileSet.getBasedir(), list_cmd);
+        if (result.isSuccess()) {
+            List files = listconsumer.getFiles();
+            ArrayList fileList = new ArrayList();
+            for ( Iterator i = files.iterator(); i.hasNext(); )
+            {
+                ScmFile f = (ScmFile) i.next();
+
+                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() );
+        }
+    }
+
+    private StringBuffer joinCmd(String[] cmd) {
+        StringBuffer result = new StringBuffer();
+        for (int i = 0; i < cmd.length; i++) {
+            String s = cmd[i];
+            result.append(s);
+            if (i < cmd.length - 1)
+                result.append(" ");
+        }
+        return result;
+    }
+}
diff --git a/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/tag/HgTagConsumer.java b/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/tag/HgTagConsumer.java
new file mode 100644
index 0000000..40341ea
--- /dev/null
+++ b/maven-scm-providers/maven-scm-provider-hg/src/main/java/org/apache/maven/scm/provider/hg/command/tag/HgTagConsumer.java
@@ -0,0 +1,46 @@
+package org.apache.maven.scm.provider.hg.command.tag;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.scm.ScmFile;
+import org.apache.maven.scm.ScmFileStatus;
+import org.apache.maven.scm.log.ScmLogger;
+import org.apache.maven.scm.provider.hg.command.HgConsumer;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:ryan@darksleep.com">ryan daum</a>
+ */
+public class HgTagConsumer
+        extends HgConsumer
+{
+    public HgTagConsumer( ScmLogger logger )
+    {
+        super( logger );
+    }
+
+    public void doConsume( ScmFileStatus status, String trimmedLine )
+    {       
+    }
+
+}
diff --git a/maven-scm-providers/maven-scm-provider-hg/src/test/java/org/apache/maven/scm/provider/hg/command/tag/HgTagCommandTckTest.java b/maven-scm-providers/maven-scm-provider-hg/src/test/java/org/apache/maven/scm/provider/hg/command/tag/HgTagCommandTckTest.java
new file mode 100644
index 0000000..cd750ea
--- /dev/null
+++ b/maven-scm-providers/maven-scm-provider-hg/src/test/java/org/apache/maven/scm/provider/hg/command/tag/HgTagCommandTckTest.java
@@ -0,0 +1,45 @@
+package org.apache.maven.scm.provider.hg.command.tag;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.scm.provider.hg.HgRepoUtils;
+import org.apache.maven.scm.tck.command.tag.TagCommandTckTest;
+
+/**
+ * This test tests the tag command.
+ *
+ * @author <a href="mailto:ryan@darksleep.com">Ryan Daum</a>
+ * @version $Id$
+ */
+public class HgTagCommandTckTest
+        extends TagCommandTckTest
+{
+     public String getScmUrl()
+        throws Exception
+    {
+        return HgRepoUtils.getScmUrl();
+    }
+
+    public void initRepo()
+        throws Exception
+    {
+        HgRepoUtils.initRepo();
+    }
+}