# HG changeset patch # User Artem Tikhomirov # Date 1296233514 -3600 # Node ID 54562de502f7960accac68b1807451027e7f58f2 # Parent 0b2dcca7de9f726198e9ac2970379dd7ceaf03ac Preliminary tags implementation diff -r 0b2dcca7de9f -r 54562de502f7 src/org/tmatesoft/hg/repo/HgRepository.java --- a/src/org/tmatesoft/hg/repo/HgRepository.java Fri Jan 28 04:57:46 2011 +0100 +++ b/src/org/tmatesoft/hg/repo/HgRepository.java Fri Jan 28 17:51:54 2011 +0100 @@ -119,6 +119,12 @@ public final HgTags getTags() { if (tags == null) { tags = new HgTags(); + try { + tags.readGlobal(new File(repoDir.getParentFile(), ".hgtags")); + tags.readLocal(new File(repoDir, "localtags")); + } catch (IOException ex) { + ex.printStackTrace(); // FIXME log or othewise report + } } return tags; } diff -r 0b2dcca7de9f -r 54562de502f7 src/org/tmatesoft/hg/repo/HgTags.java --- a/src/org/tmatesoft/hg/repo/HgTags.java Fri Jan 28 04:57:46 2011 +0100 +++ b/src/org/tmatesoft/hg/repo/HgTags.java Fri Jan 28 17:51:54 2011 +0100 @@ -16,25 +16,135 @@ */ package org.tmatesoft.hg.repo; -import java.util.Collections; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.TreeMap; import org.tmatesoft.hg.core.Nodeid; /** - * FIXME Place-holder, implement + * @see http://mercurial.selenic.com/wiki/TagDesign * * @author Artem Tikhomirov * @author TMate Software Ltd. */ public class HgTags { + // global tags come from ".hgtags" + // local come from ".hg/localtags" + + private final Map> globalToName; + private final Map> localToName; + private final Map> globalFromName; + private final Map> localFromName; + + + /*package-local*/ HgTags() { + globalToName = new HashMap>(); + localToName = new HashMap>(); + globalFromName = new TreeMap>(); + localFromName = new TreeMap>(); + } + + /*package-local*/ void readLocal(File localTags) throws IOException { + if (localTags == null || localTags.isDirectory()) { + throw new IllegalArgumentException(String.valueOf(localTags)); + } + read(localTags, localToName, localFromName); + } + + /*package-local*/ void readGlobal(File globalTags) throws IOException { + if (globalTags == null || globalTags.isDirectory()) { + throw new IllegalArgumentException(String.valueOf(globalTags)); + } + read(globalTags, globalToName, globalFromName); + } + + private void read(File f, Map> nid2name, Map> name2nid) throws IOException { + if (!f.canRead()) { + return; + } + BufferedReader r = null; + try { + r = new BufferedReader(new FileReader(f)); + read(r, nid2name, name2nid); + } finally { + if (r != null) { + r.close(); + } + } + } + + private void read(BufferedReader reader, Map> nid2name, Map> name2nid) throws IOException { + String line; + while ((line = reader.readLine()) != null) { + line = line.trim(); + if (line.length() == 0) { + continue; + } + if (line.length() < 40+2 /*nodeid, space and at least single-char tagname*/) { + System.out.println("Bad tags line:" + line); // FIXME log or otherwise report (IStatus analog?) + continue; + } + int spacePos = line.indexOf(' '); + if (spacePos != -1) { + assert spacePos == 40; + final byte[] nodeidBytes = line.substring(0, spacePos).getBytes(); + Nodeid nid = Nodeid.fromAscii(nodeidBytes, 0, nodeidBytes.length); + String tagName = line.substring(spacePos+1); + List nids = name2nid.get(tagName); + if (nids == null) { + nids = new LinkedList(); + // tagName is substring of full line, thus need a copy to let the line be GC'ed + // new String(tagName.toCharArray()) is more expressive, but results in 1 extra arraycopy + tagName = new String(tagName); + name2nid.put(tagName, nids); + } + // XXX repo.getNodeidCache().nodeid(nid); + ((LinkedList) nids).addFirst(nid); + List revTags = nid2name.get(nid); + if (revTags == null) { + revTags = new LinkedList(); + nid2name.put(nid, revTags); + } + revTags.add(tagName); + } else { + System.out.println("Bad tags line:" + line); // FIXME see above + } + } + } public List tags(Nodeid nid) { - return Collections.emptyList(); + ArrayList rv = new ArrayList(5); + List l; + if ((l = localToName.get(nid)) != null) { + rv.addAll(l); + } + if ((l = globalToName.get(nid)) != null) { + rv.addAll(l); + } + return rv; } public boolean isTagged(Nodeid nid) { - // TODO implement - return false; + return localToName.containsKey(nid) || globalToName.containsKey(nid); + } + + public List tagged(String tagName) { + ArrayList rv = new ArrayList(5); + List l; + if ((l = localFromName.get(tagName)) != null) { + rv.addAll(l); + } + if ((l = globalFromName.get(tagName)) != null) { + rv.addAll(l); + } + return rv; } }