comparison src/org/tmatesoft/hg/repo/HgBookmarks.java @ 484:ae4d6604debd

Bookmarks support added
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 08 Aug 2012 14:41:44 +0200
parents
children b3c16d1aede0
comparison
equal deleted inserted replaced
483:e31e85cf4d4c 484:ae4d6604debd
1 /*
2 * Copyright (c) 2012 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.File;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25
26 import org.tmatesoft.hg.core.Nodeid;
27 import org.tmatesoft.hg.internal.LineReader;
28 import org.tmatesoft.hg.util.LogFacility;
29
30 /**
31 *
32 * @author Artem Tikhomirov
33 * @author TMate Software Ltd.
34 */
35 public final class HgBookmarks {
36 private final HgRepository repo;
37 private Map<String, Nodeid> bookmarks = Collections.emptyMap();
38 private String activeBookmark;
39
40 HgBookmarks(HgRepository hgRepo) {
41 repo = hgRepo;
42 }
43
44 /*package-local*/ void read() throws HgInvalidControlFileException {
45 final LogFacility log = repo.getContext().getLog();
46 File all = new File (repo.getRepositoryRoot(), HgRepositoryFiles.Bookmarks.getName());
47 LinkedHashMap<String, Nodeid> bm = new LinkedHashMap<String, Nodeid>();
48 if (all.canRead()) {
49 LineReader lr1 = new LineReader(all, log);
50 ArrayList<String> c = new ArrayList<String>();
51 lr1.read(new LineReader.SimpleLineCollector(), c);
52 for (String s : c) {
53 int x = s.indexOf(' ');
54 try {
55 if (x > 0) {
56 Nodeid nid = Nodeid.fromAscii(s.substring(0, x));
57 String name = new String(s.substring(x+1));
58 if (repo.getChangelog().isKnown(nid)) {
59 // copy name part not to drag complete line
60 bm.put(name, nid);
61 } else {
62 log.dump(getClass(), LogFacility.Severity.Info, "Bookmark %s points to non-existent revision %s, ignored.", name, nid);
63 }
64 } else {
65 log.dump(getClass(), LogFacility.Severity.Warn, "Can't parse bookmark entry: %s", s);
66 }
67 } catch (IllegalArgumentException ex) {
68 log.dump(getClass(), LogFacility.Severity.Warn, ex, String.format("Can't parse bookmark entry: %s", s));
69 }
70 }
71 bookmarks = bm;
72 } else {
73 bookmarks = Collections.emptyMap();
74 }
75
76 activeBookmark = null;
77 File active = new File(repo.getRepositoryRoot(), HgRepositoryFiles.BookmarksCurrent.getName());
78 if (active.canRead()) {
79 LineReader lr2 = new LineReader(active, repo.getContext().getLog());
80 ArrayList<String> c = new ArrayList<String>(2);
81 lr2.read(new LineReader.SimpleLineCollector(), c);
82 if (c.size() > 0) {
83 activeBookmark = c.get(0);
84 }
85 }
86 }
87
88 /**
89 * Tell name of the active bookmark
90 * @return <code>null</code> if none active
91 */
92 public String getActiveBookmarkName() {
93 return activeBookmark;
94 }
95
96 /**
97 * Retrieve revision associated with the named bookmark.
98 *
99 * @param bookmarkName name of the bookmark
100 * @return revision or <code>null</code> if bookmark is not known
101 */
102 public Nodeid getRevision(String bookmarkName) {
103 return bookmarks.get(bookmarkName);
104 }
105
106 /**
107 * Retrieve all bookmarks known in the repository
108 * @return collection with names, never <code>null</code>
109 */
110 public Collection<String> getAllBookmarks() {
111 // bookmarks are initialized with atomic assignment,
112 // hence can use view (not a synchronized copy) here
113 return Collections.unmodifiableSet(bookmarks.keySet());
114 }
115 }