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 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 super(repository);
57 cmdAppend("-v");
58 withDebugFlag();
59 }
60
61
62
63
64
65
66 public TagsCommand includeTip() {
67 this.includeTip = true;
68 return this;
69 }
70
71
72
73
74
75
76
77
78
79 public List<Tag> execute() {
80 List<Tag> result = Lists.newArrayList();
81 Repository repository = getRepository();
82 for (Iterator<String> iter = launchIterator(); iter.hasNext();) {
83 String line = iter.next();
84 Tag tag = fromLine(repository, line);
85 if (includeTip || !tag.getName().equals("tip")) {
86 result.add(tag);
87 }
88 }
89 return result;
90 }
91
92
93
94
95
96
97
98
99
100
101 public Map<Changeset, List<Tag>> executeReverse() {
102 Map<Changeset, List<Tag>> result = Maps.newHashMap();
103 Repository repository = getRepository();
104 for (Iterator<String> iter = launchIterator(); iter.hasNext();) {
105 Tag tag = fromLine(repository, iter.next());
106
107 if (includeTip || !tag.getName().equals("tip")) {
108 Changeset changeset = tag.getChangeset();
109 List<Tag> tagList = result.get(changeset);
110 if (tagList == null) {
111 tagList = Lists.newArrayList();
112 result.put(changeset, tagList);
113 }
114 tagList.add(tag);
115 }
116 }
117 return result;
118 }
119
120 private Tag fromLine(Repository repository, String line) {
121 Matcher m = TAGS_PATTERN.matcher(line);
122 if (m.matches()) {
123 return new Tag(m.group(1).trim(), repository.changeset(m.group(2)),
124 m.group(3) != null);
125 }
126 throw new UnexpectedCommandOutputException(this, line);
127 }
128 }