annotate src/org/tmatesoft/hg/repo/HgBookmarks.java @ 672:d2552e6a5af6

Effective update of HgParentChildMap when repository got few revisions added
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 12 Jul 2013 16:29:06 +0200
parents 6526d8adbc0f
children
rev   line source
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
605
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
2 * Copyright (c) 2012-2013 TMate Software Ltd
484
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
605
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
19 import static org.tmatesoft.hg.util.LogFacility.Severity.Error;
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
20
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.io.File;
605
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
22 import java.io.FileWriter;
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
23 import java.io.IOException;
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import java.util.ArrayList;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 import java.util.Collection;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 import java.util.Collections;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 import java.util.LinkedHashMap;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 import java.util.Map;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29
605
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
30 import org.tmatesoft.hg.core.HgIOException;
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
31 import org.tmatesoft.hg.core.HgRepositoryLockException;
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 import org.tmatesoft.hg.core.Nodeid;
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
33 import org.tmatesoft.hg.internal.FileChangeMonitor;
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
34 import org.tmatesoft.hg.internal.Internals;
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 import org.tmatesoft.hg.internal.LineReader;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 import org.tmatesoft.hg.util.LogFacility;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 /**
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
39 * Access to bookmarks state
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 *
605
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
41 * @see http://mercurial.selenic.com/wiki/Bookmarks
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 * @author Artem Tikhomirov
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 * @author TMate Software Ltd.
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 public final class HgBookmarks {
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
46 private final Internals repo;
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 private Map<String, Nodeid> bookmarks = Collections.emptyMap();
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
48 private String activeBookmark;
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
49 private FileChangeMonitor activeTracker, bmFileTracker;
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50
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
51 HgBookmarks(Internals internals) {
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
52 repo = internals;
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54
628
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
55 /*package-local*/ void read() throws HgRuntimeException {
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
56 readBookmarks();
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
57 readActiveBookmark();
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
58 }
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
59
628
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
60 private void readBookmarks() throws HgRuntimeException {
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
61 final LogFacility log = repo.getLog();
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
62 File all = repo.getRepositoryFile(HgRepositoryFiles.Bookmarks);
628
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
63 try {
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
64 LinkedHashMap<String, Nodeid> bm = new LinkedHashMap<String, Nodeid>();
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
65 if (all.canRead() && all.isFile()) {
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
66 LineReader lr1 = new LineReader(all, log);
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
67 ArrayList<String> c = new ArrayList<String>();
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
68 lr1.read(new LineReader.SimpleLineCollector(), c);
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
69 for (String s : c) {
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
70 int x = s.indexOf(' ');
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
71 try {
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
72 if (x > 0) {
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
73 Nodeid nid = Nodeid.fromAscii(s.substring(0, x));
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
74 String name = new String(s.substring(x+1));
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
75 if (repo.getRepo().getChangelog().isKnown(nid)) {
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
76 // copy name part not to drag complete line
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
77 bm.put(name, nid);
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
78 } else {
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
79 log.dump(getClass(), LogFacility.Severity.Info, "Bookmark %s points to non-existent revision %s, ignored.", name, nid);
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
80 }
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 } else {
628
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
82 log.dump(getClass(), LogFacility.Severity.Warn, "Can't parse bookmark entry: %s", s);
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 }
628
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
84 } catch (IllegalArgumentException ex) {
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
85 log.dump(getClass(), LogFacility.Severity.Warn, ex, String.format("Can't parse bookmark entry: %s", s));
484
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 }
628
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
88 bookmarks = bm;
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
89 } else {
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
90 bookmarks = Collections.emptyMap();
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 }
628
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
92 if (bmFileTracker == null) {
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
93 bmFileTracker = new FileChangeMonitor(all);
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
94 }
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
95 bmFileTracker.touch(this);
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
96 } catch (HgInvalidControlFileException ex) {
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
97 // do not translate HgInvalidControlFileException into another HgInvalidControlFileException
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
98 // but only HgInvalidFileException
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
99 throw ex;
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
100 } catch (HgIOException ex) {
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
101 throw new HgInvalidControlFileException(ex, true);
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 }
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
103 }
628
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
104
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
105 private void readActiveBookmark() throws HgInvalidControlFileException {
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 activeBookmark = null;
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
107 File active = repo.getRepositoryFile(HgRepositoryFiles.BookmarksCurrent);
628
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
108 try {
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
109 if (active.canRead() && active.isFile()) {
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
110 LineReader lr2 = new LineReader(active, repo.getLog());
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
111 ArrayList<String> c = new ArrayList<String>(2);
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
112 lr2.read(new LineReader.SimpleLineCollector(), c);
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
113 if (c.size() > 0) {
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
114 activeBookmark = c.get(0);
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
115 }
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
116 }
628
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
117 if (activeTracker == null) {
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
118 activeTracker = new FileChangeMonitor(active);
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
119 }
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
120 activeTracker.touch(this);
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
121 } catch (HgIOException ex) {
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
122 throw new HgInvalidControlFileException(ex, true);
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
123 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
124 }
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
125
628
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
126 /*package-local*/void reloadIfChanged() throws HgRuntimeException {
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
127 assert activeTracker != null;
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
128 assert bmFileTracker != null;
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
129 if (bmFileTracker.changed(this)) {
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
130 readBookmarks();
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
131 }
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
132 if (activeTracker.changed(this)) {
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
133 readActiveBookmark();
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
134 }
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
135 }
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
136
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
137
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
138 /**
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
139 * Tell name of the active bookmark
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
140 * @return <code>null</code> if none active
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
141 */
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
142 public String getActiveBookmarkName() {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
143 return activeBookmark;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
144 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
145
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
146 /**
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
147 * Retrieve revision associated with the named bookmark.
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
148 *
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
149 * @param bookmarkName name of the bookmark
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
150 * @return revision or <code>null</code> if bookmark is not known
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
151 */
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
152 public Nodeid getRevision(String bookmarkName) {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
153 return bookmarks.get(bookmarkName);
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
154 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
155
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
156 /**
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
157 * Retrieve all bookmarks known in the repository
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
158 * @return collection with names, never <code>null</code>
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
159 */
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
160 public Collection<String> getAllBookmarks() {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
161 // bookmarks are initialized with atomic assignment,
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
162 // hence can use view (not a synchronized copy) here
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
163 return Collections.unmodifiableSet(bookmarks.keySet());
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
164 }
605
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
165
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
166 /**
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
167 * Update currently bookmark with new commit.
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
168 * Note, child has to be descendant of a p1 or p2
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
169 *
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
170 * @param p1 first parent, or <code>null</code>
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
171 * @param p2 second parent, or <code>null</code>
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
172 * @param child new commit, descendant of one of the parents, not <code>null</code>
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
173 * @throws HgIOException if failed to write updated bookmark information
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
174 * @throws HgRepositoryLockException if failed to lock repository for modifications
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
175 */
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
176 public void updateActive(Nodeid p1, Nodeid p2, Nodeid child) throws HgIOException, HgRepositoryLockException {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
177 if (activeBookmark == null) {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
178 return;
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
179 }
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
180 Nodeid activeRev = getRevision(activeBookmark);
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
181 if (!activeRev.equals(p1) && !activeRev.equals(p2)) {
624
507602cb4fb3 FIXMEs and TODOs: pay some technical debt
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 613
diff changeset
182 return; // TestCommit#testNoBookmarkUpdate
605
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
183 }
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
184 if (child.equals(activeRev)) {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
185 return;
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
186 }
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
187 LinkedHashMap<String, Nodeid> copy = new LinkedHashMap<String, Nodeid>(bookmarks);
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
188 copy.put(activeBookmark, child);
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
189 bookmarks = copy;
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
190 write();
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
191 }
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
192
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
193 private void write() throws HgIOException, HgRepositoryLockException {
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
194 File bookmarksFile = repo.getRepositoryFile(HgRepositoryFiles.Bookmarks);
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
195 HgRepositoryLock workingDirLock = repo.getRepo().getWorkingDirLock();
605
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
196 FileWriter fileWriter = null;
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
197 workingDirLock.acquire();
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
198 try {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
199 fileWriter = new FileWriter(bookmarksFile);
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
200 for (String bm : bookmarks.keySet()) {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
201 Nodeid nid = bookmarks.get(bm);
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
202 fileWriter.write(String.format("%s %s\n", nid.toString(), bm));
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
203 }
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
204 fileWriter.flush();
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
205 } catch (IOException ex) {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
206 throw new HgIOException("Failed to serialize bookmarks", ex, bookmarksFile);
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
207 } finally {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
208 try {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
209 if (fileWriter != null) {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
210 fileWriter.close();
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
211 }
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
212 } catch (IOException ex) {
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 605
diff changeset
213 repo.getLog().dump(getClass(), Error, ex, null);
605
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
214 }
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
215 workingDirLock.release();
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
216 }
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
217 }
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
218 }