comparison hg4j/src/main/java/org/tmatesoft/hg/repo/HgTags.java @ 213:6ec4af642ba8 gradle

Project uses Gradle for build - actual changes
author Alexander Kitaev <kitaev@gmail.com>
date Tue, 10 May 2011 10:52:53 +0200
parents
children
comparison
equal deleted inserted replaced
212:edb2e2829352 213:6ec4af642ba8
1 /*
2 * Copyright (c) 2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.repo;
18
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;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.TreeMap;
29
30 import org.tmatesoft.hg.core.Nodeid;
31
32 /**
33 * @see http://mercurial.selenic.com/wiki/TagDesign
34 *
35 * @author Artem Tikhomirov
36 * @author TMate Software Ltd.
37 */
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 }
122
123 public List<String> tags(Nodeid nid) {
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;
133 }
134
135 public boolean isTagged(Nodeid nid) {
136 return localToName.containsKey(nid) || globalToName.containsKey(nid);
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;
149 }
150 }