Mercurial > hg4j
annotate src/org/tmatesoft/hg/repo/HgBookmarks.java @ 581:0890628ed51e
Progress/cancel support in new commands
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Mon, 22 Apr 2013 16:02:17 +0200 |
parents | d2f6ab541330 |
children | c56edf42be64 |
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; |
490
b3c16d1aede0
Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
484
diff
changeset
|
27 import org.tmatesoft.hg.internal.Internals; |
484
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
28 import org.tmatesoft.hg.internal.LineReader; |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
29 import org.tmatesoft.hg.util.LogFacility; |
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 * |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
33 * @author Artem Tikhomirov |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
34 * @author TMate Software Ltd. |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
35 */ |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
36 public final class HgBookmarks { |
490
b3c16d1aede0
Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
484
diff
changeset
|
37 private final Internals internalRepo; |
484
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
38 private Map<String, Nodeid> bookmarks = Collections.emptyMap(); |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
39 private String activeBookmark; |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
40 |
490
b3c16d1aede0
Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
484
diff
changeset
|
41 HgBookmarks(Internals internals) { |
b3c16d1aede0
Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
484
diff
changeset
|
42 internalRepo = internals; |
484
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 |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
45 /*package-local*/ void read() throws HgInvalidControlFileException { |
501
d2f6ab541330
Change the way extensions are accessed (with ExtensionsManager now), add preliminary Rebase extension support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
490
diff
changeset
|
46 final LogFacility log = internalRepo.getSessionContext().getLog(); |
490
b3c16d1aede0
Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
484
diff
changeset
|
47 final HgRepository repo = internalRepo.getRepo(); |
b3c16d1aede0
Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
484
diff
changeset
|
48 File all = internalRepo.getFileFromRepoDir(HgRepositoryFiles.Bookmarks.getName()); |
484
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
49 LinkedHashMap<String, Nodeid> bm = new LinkedHashMap<String, Nodeid>(); |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
50 if (all.canRead()) { |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
51 LineReader lr1 = new LineReader(all, log); |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
52 ArrayList<String> c = new ArrayList<String>(); |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
53 lr1.read(new LineReader.SimpleLineCollector(), c); |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
54 for (String s : c) { |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
55 int x = s.indexOf(' '); |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
56 try { |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
57 if (x > 0) { |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
58 Nodeid nid = Nodeid.fromAscii(s.substring(0, x)); |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
59 String name = new String(s.substring(x+1)); |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
60 if (repo.getChangelog().isKnown(nid)) { |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
61 // copy name part not to drag complete line |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
62 bm.put(name, nid); |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
63 } else { |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
64 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
|
65 } |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
66 } else { |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
67 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
|
68 } |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
69 } catch (IllegalArgumentException ex) { |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
70 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
|
71 } |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
72 } |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
73 bookmarks = bm; |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
74 } else { |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
75 bookmarks = Collections.emptyMap(); |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
76 } |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
77 |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
78 activeBookmark = null; |
490
b3c16d1aede0
Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
484
diff
changeset
|
79 File active = internalRepo.getFileFromRepoDir(HgRepositoryFiles.BookmarksCurrent.getName()); |
484
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
80 if (active.canRead()) { |
490
b3c16d1aede0
Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
484
diff
changeset
|
81 LineReader lr2 = new LineReader(active, log); |
484
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
82 ArrayList<String> c = new ArrayList<String>(2); |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
83 lr2.read(new LineReader.SimpleLineCollector(), c); |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
84 if (c.size() > 0) { |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
85 activeBookmark = c.get(0); |
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 |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
90 /** |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
91 * Tell name of the active bookmark |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
92 * @return <code>null</code> if none active |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
93 */ |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
94 public String getActiveBookmarkName() { |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
95 return activeBookmark; |
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 |
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 * Retrieve revision associated with the named bookmark. |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
100 * |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
101 * @param bookmarkName name of the bookmark |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
102 * @return revision or <code>null</code> if bookmark is not known |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
103 */ |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
104 public Nodeid getRevision(String bookmarkName) { |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
105 return bookmarks.get(bookmarkName); |
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 |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
108 /** |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
109 * Retrieve all bookmarks known in the repository |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
110 * @return collection with names, never <code>null</code> |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
111 */ |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
112 public Collection<String> getAllBookmarks() { |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
113 // bookmarks are initialized with atomic assignment, |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
114 // hence can use view (not a synchronized copy) here |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
115 return Collections.unmodifiableSet(bookmarks.keySet()); |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
116 } |
ae4d6604debd
Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
117 } |