comparison src/org/tmatesoft/hg/repo/HgTags.java @ 104:54562de502f7

Preliminary tags implementation
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 28 Jan 2011 17:51:54 +0100
parents a3a2e5deb320
children b2cfbe46f9b6
comparison
equal deleted inserted replaced
103:0b2dcca7de9f 104:54562de502f7
14 * the terms of a license other than GNU General Public License 14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com 15 * contact TMate Software at support@hg4j.com
16 */ 16 */
17 package org.tmatesoft.hg.repo; 17 package org.tmatesoft.hg.repo;
18 18
19 import java.util.Collections; 19 import java.io.BufferedReader;
20 import java.io.File;
21 import java.io.FileReader;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.LinkedList;
20 import java.util.List; 26 import java.util.List;
27 import java.util.Map;
28 import java.util.TreeMap;
21 29
22 import org.tmatesoft.hg.core.Nodeid; 30 import org.tmatesoft.hg.core.Nodeid;
23 31
24 /** 32 /**
25 * FIXME Place-holder, implement 33 * @see http://mercurial.selenic.com/wiki/TagDesign
26 * 34 *
27 * @author Artem Tikhomirov 35 * @author Artem Tikhomirov
28 * @author TMate Software Ltd. 36 * @author TMate Software Ltd.
29 */ 37 */
30 public class HgTags { 38 public class HgTags {
39 // global tags come from ".hgtags"
40 // local come from ".hg/localtags"
41
42 private final Map<Nodeid, List<String>> globalToName;
43 private final Map<Nodeid, List<String>> localToName;
44 private final Map<String, List<Nodeid>> globalFromName;
45 private final Map<String, List<Nodeid>> localFromName;
46
47
48 /*package-local*/ HgTags() {
49 globalToName = new HashMap<Nodeid, List<String>>();
50 localToName = new HashMap<Nodeid, List<String>>();
51 globalFromName = new TreeMap<String, List<Nodeid>>();
52 localFromName = new TreeMap<String, List<Nodeid>>();
53 }
54
55 /*package-local*/ void readLocal(File localTags) throws IOException {
56 if (localTags == null || localTags.isDirectory()) {
57 throw new IllegalArgumentException(String.valueOf(localTags));
58 }
59 read(localTags, localToName, localFromName);
60 }
61
62 /*package-local*/ void readGlobal(File globalTags) throws IOException {
63 if (globalTags == null || globalTags.isDirectory()) {
64 throw new IllegalArgumentException(String.valueOf(globalTags));
65 }
66 read(globalTags, globalToName, globalFromName);
67 }
68
69 private void read(File f, Map<Nodeid,List<String>> nid2name, Map<String, List<Nodeid>> name2nid) throws IOException {
70 if (!f.canRead()) {
71 return;
72 }
73 BufferedReader r = null;
74 try {
75 r = new BufferedReader(new FileReader(f));
76 read(r, nid2name, name2nid);
77 } finally {
78 if (r != null) {
79 r.close();
80 }
81 }
82 }
83
84 private void read(BufferedReader reader, Map<Nodeid,List<String>> nid2name, Map<String, List<Nodeid>> name2nid) throws IOException {
85 String line;
86 while ((line = reader.readLine()) != null) {
87 line = line.trim();
88 if (line.length() == 0) {
89 continue;
90 }
91 if (line.length() < 40+2 /*nodeid, space and at least single-char tagname*/) {
92 System.out.println("Bad tags line:" + line); // FIXME log or otherwise report (IStatus analog?)
93 continue;
94 }
95 int spacePos = line.indexOf(' ');
96 if (spacePos != -1) {
97 assert spacePos == 40;
98 final byte[] nodeidBytes = line.substring(0, spacePos).getBytes();
99 Nodeid nid = Nodeid.fromAscii(nodeidBytes, 0, nodeidBytes.length);
100 String tagName = line.substring(spacePos+1);
101 List<Nodeid> nids = name2nid.get(tagName);
102 if (nids == null) {
103 nids = new LinkedList<Nodeid>();
104 // tagName is substring of full line, thus need a copy to let the line be GC'ed
105 // new String(tagName.toCharArray()) is more expressive, but results in 1 extra arraycopy
106 tagName = new String(tagName);
107 name2nid.put(tagName, nids);
108 }
109 // XXX repo.getNodeidCache().nodeid(nid);
110 ((LinkedList<Nodeid>) nids).addFirst(nid);
111 List<String> revTags = nid2name.get(nid);
112 if (revTags == null) {
113 revTags = new LinkedList<String>();
114 nid2name.put(nid, revTags);
115 }
116 revTags.add(tagName);
117 } else {
118 System.out.println("Bad tags line:" + line); // FIXME see above
119 }
120 }
121 }
31 122
32 public List<String> tags(Nodeid nid) { 123 public List<String> tags(Nodeid nid) {
33 return Collections.emptyList(); 124 ArrayList<String> rv = new ArrayList<String>(5);
125 List<String> l;
126 if ((l = localToName.get(nid)) != null) {
127 rv.addAll(l);
128 }
129 if ((l = globalToName.get(nid)) != null) {
130 rv.addAll(l);
131 }
132 return rv;
34 } 133 }
35 134
36 public boolean isTagged(Nodeid nid) { 135 public boolean isTagged(Nodeid nid) {
37 // TODO implement 136 return localToName.containsKey(nid) || globalToName.containsKey(nid);
38 return false; 137 }
138
139 public List<Nodeid> tagged(String tagName) {
140 ArrayList<Nodeid> rv = new ArrayList<Nodeid>(5);
141 List<Nodeid> l;
142 if ((l = localFromName.get(tagName)) != null) {
143 rv.addAll(l);
144 }
145 if ((l = globalFromName.get(tagName)) != null) {
146 rv.addAll(l);
147 }
148 return rv;
39 } 149 }
40 } 150 }