| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 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 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
public class TagsCommand extends TagsCommandFlags { |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 1 | private static final Pattern TAGS_PATTERN = Pattern.compile("^(.*) (?:[-0-9]+):([a-f0-9]+)( local)?$"); |
| 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 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public TagsCommand includeTip() { |
| 67 | 0 | this.includeTip = true; |
| 68 | 0 | return this; |
| 69 | |
} |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 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 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 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 | |
} |