annotate src/org/tmatesoft/hg/repo/HgBookmarks.java @ 609:e4a71afd3c71

Test TODOs: test for ConfigFile (covering %include and %unset directives)
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 08 May 2013 17:11:45 +0200
parents c56edf42be64
children 5c68567b3645
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;
605
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
33 import org.tmatesoft.hg.internal.Experimental;
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 /**
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 *
605
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
40 * @see http://mercurial.selenic.com/wiki/Bookmarks
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 * @author Artem Tikhomirov
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 * @author TMate Software Ltd.
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 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
45 private final Internals internalRepo;
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 private Map<String, Nodeid> bookmarks = Collections.emptyMap();
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 private String activeBookmark;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48
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
49 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
50 internalRepo = internals;
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 /*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
54 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
55 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
56 File all = internalRepo.getFileFromRepoDir(HgRepositoryFiles.Bookmarks.getName());
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 LinkedHashMap<String, Nodeid> bm = new LinkedHashMap<String, Nodeid>();
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 if (all.canRead()) {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 LineReader lr1 = new LineReader(all, log);
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 ArrayList<String> c = new ArrayList<String>();
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 lr1.read(new LineReader.SimpleLineCollector(), c);
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 for (String s : c) {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 int x = s.indexOf(' ');
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 try {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 if (x > 0) {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 Nodeid nid = Nodeid.fromAscii(s.substring(0, x));
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 String name = new String(s.substring(x+1));
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 if (repo.getChangelog().isKnown(nid)) {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 // copy name part not to drag complete line
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 bm.put(name, nid);
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 } else {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 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
73 }
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 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
76 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 } catch (IllegalArgumentException ex) {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 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
79 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 }
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 bookmarks = bm;
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 } else {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 bookmarks = Collections.emptyMap();
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 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
87 File active = internalRepo.getFileFromRepoDir(HgRepositoryFiles.BookmarksCurrent.getName());
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 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
89 LineReader lr2 = new LineReader(active, log);
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 ArrayList<String> c = new ArrayList<String>(2);
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 lr2.read(new LineReader.SimpleLineCollector(), c);
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 if (c.size() > 0) {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
93 activeBookmark = c.get(0);
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
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 * Tell name of the active bookmark
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 * @return <code>null</code> if none active
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 String getActiveBookmarkName() {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 return activeBookmark;
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 revision associated with the named bookmark.
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 * @param bookmarkName name of the bookmark
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 * @return revision or <code>null</code> if bookmark is not known
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 Nodeid getRevision(String bookmarkName) {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
113 return bookmarks.get(bookmarkName);
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
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 * Retrieve all bookmarks known in the repository
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
118 * @return collection with names, never <code>null</code>
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
119 */
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
120 public Collection<String> getAllBookmarks() {
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
121 // bookmarks are initialized with atomic assignment,
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
122 // hence can use view (not a synchronized copy) here
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
123 return Collections.unmodifiableSet(bookmarks.keySet());
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
124 }
605
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
125
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
126 /**
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
127 * Update currently bookmark with new commit.
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
128 * 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
129 *
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
130 * @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
131 * @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
132 * @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
133 * @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
134 * @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
135 */
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
136 @Experimental(reason="Provisional API")
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
137 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
138 if (activeBookmark == null) {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
139 return;
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
140 }
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
141 Nodeid activeRev = getRevision(activeBookmark);
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
142 if (!activeRev.equals(p1) && !activeRev.equals(p2)) {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
143 // from the wiki:
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
144 // "active bookmarks are automatically updated when committing to the changeset they are pointing to"
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
145 // FIXME: test ci 1, hg bookmark active, ci 2, hg bookmark -f -r 0 active, ci 3, check active still points to r0
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
146 return;
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
147 }
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
148 if (child.equals(activeRev)) {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
149 return;
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
150 }
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
151 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
152 copy.put(activeBookmark, child);
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
153 bookmarks = copy;
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
154 write();
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
155 }
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
156
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
157 private void write() throws HgIOException, HgRepositoryLockException {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
158 File bookmarksFile = internalRepo.getRepositoryFile(HgRepositoryFiles.Bookmarks);
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
159 HgRepositoryLock workingDirLock = internalRepo.getRepo().getWorkingDirLock();
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
160 FileWriter fileWriter = null;
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
161 workingDirLock.acquire();
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
162 try {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
163 fileWriter = new FileWriter(bookmarksFile);
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
164 for (String bm : bookmarks.keySet()) {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
165 Nodeid nid = bookmarks.get(bm);
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
166 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
167 }
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
168 fileWriter.flush();
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
169 } catch (IOException ex) {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
170 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
171 } finally {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
172 try {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
173 if (fileWriter != null) {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
174 fileWriter.close();
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 } catch (IOException ex) {
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
177 internalRepo.getSessionContext().getLog().dump(getClass(), Error, ex, null);
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
178 }
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
179 workingDirLock.release();
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
180 }
c56edf42be64 Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 501
diff changeset
181 }
484
ae4d6604debd Bookmarks support added
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
182 }