Coverage Report - com.aragost.javahg.commands.TagsCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
TagsCommand
90%
30/33
77%
14/18
3
 
 1  
 /*
 2  
  * #%L
 3  
  * JavaHg
 4  
  * %%
 5  
  * Copyright (C) 2011 aragost Trifork ag
 6  
  * %%
 7  
  * Permission is hereby granted, free of charge, to any person obtaining a copy
 8  
  * of this software and associated documentation files (the "Software"), to deal
 9  
  * in the Software without restriction, including without limitation the rights
 10  
  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  
  * copies of the Software, and to permit persons to whom the Software is
 12  
  * furnished to do so, subject to the following conditions:
 13  
  *
 14  
  * The above copyright notice and this permission notice shall be included in
 15  
  * all copies or substantial portions of the Software.
 16  
  *
 17  
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18  
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19  
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20  
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21  
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22  
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23  
  * THE SOFTWARE.
 24  
  * #L%
 25  
  */
 26  
 package com.aragost.javahg.commands;
 27  
 
 28  
 import java.util.Iterator;
 29  
 import java.util.List;
 30  
 import java.util.Map;
 31  
 import java.util.regex.Matcher;
 32  
 import java.util.regex.Pattern;
 33  
 
 34  
 import com.aragost.javahg.Changeset;
 35  
 import com.aragost.javahg.Repository;
 36  
 import com.aragost.javahg.commands.flags.TagsCommandFlags;
 37  
 import com.aragost.javahg.internals.UnexpectedCommandOutputException;
 38  
 import com.google.common.collect.Lists;
 39  
 import com.google.common.collect.Maps;
 40  
 
 41  
 /**
 42  
  * Command class for executing <tt>hg tags</tt>. Set flags from
 43  
  * {@link TagsCommandFlags} and see the {@link #execute()} method for
 44  
  * how to run the command.
 45  
  */
 46  
 public class TagsCommand extends TagsCommandFlags {
 47  
 
 48  
     /**
 49  
      * Tags pattern. Matching groups: 1: name 2: nodeId 3: local
 50  
      */
 51  1
     private static final Pattern TAGS_PATTERN = Pattern.compile("^(.*) (?:[-0-9]+):([a-f0-9]+)( local)?$"); //$NON-NLS-1$
 52  
 
 53  
     protected boolean includeTip;
 54  
 
 55  
     public TagsCommand(Repository repository) {
 56  2
         super(repository);
 57  2
         cmdAppend("-v");
 58  2
         withDebugFlag();
 59  2
     }
 60  
 
 61  
     /**
 62  
      * Whether tip should be included. Default false
 63  
      * 
 64  
      * @return this
 65  
      */
 66  
     public TagsCommand includeTip() {
 67  0
         this.includeTip = true;
 68  0
         return this;
 69  
     }
 70  
 
 71  
     /**
 72  
      * Return a map mapping tag names to changeset with the tag.
 73  
      * <p>
 74  
      * The command 'hg tags' returns the pseudo tag 'tip', this is not
 75  
      * returned here.
 76  
      * 
 77  
      * @return map mapping tag name to changeset
 78  
      */
 79  
     public List<Tag> execute() {
 80  1
         List<Tag> result = Lists.newArrayList();
 81  1
         Repository repository = getRepository();
 82  1
         for (Iterator<String> iter = launchIterator(); iter.hasNext();) {
 83  5
             String line = iter.next();
 84  5
             Tag tag = fromLine(repository, line);
 85  5
             if (includeTip || !tag.getName().equals("tip")) {
 86  4
                 result.add(tag);
 87  
             }
 88  5
         }
 89  1
         return result;
 90  
     }
 91  
 
 92  
     /**
 93  
      * Return alternative mapping of changeset node hash to list of
 94  
      * tags.
 95  
      * <p>
 96  
      * The command 'hg tags' returns the pseudo tag 'tip', this is not
 97  
      * returned here.
 98  
      * 
 99  
      * @return map mapping changeset to tags at that node.
 100  
      */
 101  
     public Map<Changeset, List<Tag>> executeReverse() {
 102  1
         Map<Changeset, List<Tag>> result = Maps.newHashMap();
 103  1
         Repository repository = getRepository();
 104  1
         for (Iterator<String> iter = launchIterator(); iter.hasNext();) {
 105  5
             Tag tag = fromLine(repository, iter.next());
 106  
 
 107  5
             if (includeTip || !tag.getName().equals("tip")) {
 108  4
                 Changeset changeset = tag.getChangeset();
 109  4
                 List<Tag> tagList = result.get(changeset);
 110  4
                 if (tagList == null) {
 111  3
                     tagList = Lists.newArrayList();
 112  3
                     result.put(changeset, tagList);
 113  
                 }
 114  4
                 tagList.add(tag);
 115  
             }
 116  5
         }
 117  1
         return result;
 118  
     }
 119  
 
 120  
     private Tag fromLine(Repository repository, String line) {
 121  10
         Matcher m = TAGS_PATTERN.matcher(line);
 122  10
         if (m.matches()) {
 123  10
             return new Tag(m.group(1).trim(), repository.changeset(m.group(2)),
 124  
                     m.group(3) != null);
 125  
         }
 126  0
         throw new UnexpectedCommandOutputException(this, line);
 127  
     }
 128  
 }