annotate 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
rev   line source
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2012 TMate Software Ltd
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.repo;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import java.io.File;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20 import java.util.ArrayList;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.util.Collection;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.util.Collections;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.util.LinkedHashMap;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import java.util.Map;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 import org.tmatesoft.hg.core.Nodeid;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 import org.tmatesoft.hg.internal.LineReader;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 import org.tmatesoft.hg.util.LogFacility;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 /**
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 *
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 * @author Artem Tikhomirov
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 * @author TMate Software Ltd.
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 */
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 public final class HgBookmarks {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 private final HgRepository repo;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 private Map<String, Nodeid> bookmarks = Collections.emptyMap();
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 private String activeBookmark;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 HgBookmarks(HgRepository hgRepo) {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 repo = hgRepo;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 /*package-local*/ void read() throws HgInvalidControlFileException {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 final LogFacility log = repo.getContext().getLog();
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 File all = new File (repo.getRepositoryRoot(), HgRepositoryFiles.Bookmarks.getName());
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 LinkedHashMap<String, Nodeid> bm = new LinkedHashMap<String, Nodeid>();
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 if (all.canRead()) {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 LineReader lr1 = new LineReader(all, log);
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 ArrayList<String> c = new ArrayList<String>();
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 lr1.read(new LineReader.SimpleLineCollector(), c);
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 for (String s : c) {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 int x = s.indexOf(' ');
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 try {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 if (x > 0) {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 Nodeid nid = Nodeid.fromAscii(s.substring(0, x));
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 String name = new String(s.substring(x+1));
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 if (repo.getChangelog().isKnown(nid)) {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 // copy name part not to drag complete line
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 bm.put(name, nid);
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 } else {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 log.dump(getClass(), LogFacility.Severity.Info, "Bookmark %s points to non-existent revision %s, ignored.", name, nid);
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 } else {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 log.dump(getClass(), LogFacility.Severity.Warn, "Can't parse bookmark entry: %s", s);
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 } catch (IllegalArgumentException ex) {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 log.dump(getClass(), LogFacility.Severity.Warn, ex, String.format("Can't parse bookmark entry: %s", s));
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 bookmarks = bm;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 } else {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 bookmarks = Collections.emptyMap();
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 activeBookmark = null;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 File active = new File(repo.getRepositoryRoot(), HgRepositoryFiles.BookmarksCurrent.getName());
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 if (active.canRead()) {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 LineReader lr2 = new LineReader(active, repo.getContext().getLog());
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 ArrayList<String> c = new ArrayList<String>(2);
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 lr2.read(new LineReader.SimpleLineCollector(), c);
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 if (c.size() > 0) {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 activeBookmark = c.get(0);
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 /**
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 * Tell name of the active bookmark
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 * @return <code>null</code> if none active
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 */
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 public String getActiveBookmarkName() {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
93 return activeBookmark;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
94 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 /**
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97 * Retrieve revision associated with the named bookmark.
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98 *
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 * @param bookmarkName name of the bookmark
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 * @return revision or <code>null</code> if bookmark is not known
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 */
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 public Nodeid getRevision(String bookmarkName) {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 return bookmarks.get(bookmarkName);
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
104 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 /**
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
107 * Retrieve all bookmarks known in the repository
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
108 * @return collection with names, never <code>null</code>
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109 */
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 public Collection<String> getAllBookmarks() {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
111 // bookmarks are initialized with atomic assignment,
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
112 // hence can use view (not a synchronized copy) here
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
113 return Collections.unmodifiableSet(bookmarks.keySet());
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
114 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
115 }