Mercurial > jhg
comparison src/org/tmatesoft/hg/repo/HgBookmarks.java @ 605:c56edf42be64
Commit: update active bookmark with new revision
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Mon, 06 May 2013 20:28:21 +0200 |
parents | d2f6ab541330 |
children | 5c68567b3645 |
comparison
equal
deleted
inserted
replaced
604:c3505001a42a | 605:c56edf42be64 |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 TMate Software Ltd | 2 * Copyright (c) 2012-2013 TMate Software Ltd |
3 * | 3 * |
4 * This program is free software; you can redistribute it and/or modify | 4 * This program is free software; you can redistribute it and/or modify |
5 * it under the terms of the GNU General Public License as published by | 5 * it under the terms of the GNU General Public License as published by |
6 * the Free Software Foundation; version 2 of the License. | 6 * the Free Software Foundation; version 2 of the License. |
7 * | 7 * |
14 * the terms of a license other than GNU General Public License | 14 * the terms of a license other than GNU General Public License |
15 * contact TMate Software at support@hg4j.com | 15 * contact TMate Software at support@hg4j.com |
16 */ | 16 */ |
17 package org.tmatesoft.hg.repo; | 17 package org.tmatesoft.hg.repo; |
18 | 18 |
19 import static org.tmatesoft.hg.util.LogFacility.Severity.Error; | |
20 | |
19 import java.io.File; | 21 import java.io.File; |
22 import java.io.FileWriter; | |
23 import java.io.IOException; | |
20 import java.util.ArrayList; | 24 import java.util.ArrayList; |
21 import java.util.Collection; | 25 import java.util.Collection; |
22 import java.util.Collections; | 26 import java.util.Collections; |
23 import java.util.LinkedHashMap; | 27 import java.util.LinkedHashMap; |
24 import java.util.Map; | 28 import java.util.Map; |
25 | 29 |
30 import org.tmatesoft.hg.core.HgIOException; | |
31 import org.tmatesoft.hg.core.HgRepositoryLockException; | |
26 import org.tmatesoft.hg.core.Nodeid; | 32 import org.tmatesoft.hg.core.Nodeid; |
33 import org.tmatesoft.hg.internal.Experimental; | |
27 import org.tmatesoft.hg.internal.Internals; | 34 import org.tmatesoft.hg.internal.Internals; |
28 import org.tmatesoft.hg.internal.LineReader; | 35 import org.tmatesoft.hg.internal.LineReader; |
29 import org.tmatesoft.hg.util.LogFacility; | 36 import org.tmatesoft.hg.util.LogFacility; |
30 | 37 |
31 /** | 38 /** |
32 * | 39 * |
40 * @see http://mercurial.selenic.com/wiki/Bookmarks | |
33 * @author Artem Tikhomirov | 41 * @author Artem Tikhomirov |
34 * @author TMate Software Ltd. | 42 * @author TMate Software Ltd. |
35 */ | 43 */ |
36 public final class HgBookmarks { | 44 public final class HgBookmarks { |
37 private final Internals internalRepo; | 45 private final Internals internalRepo; |
112 public Collection<String> getAllBookmarks() { | 120 public Collection<String> getAllBookmarks() { |
113 // bookmarks are initialized with atomic assignment, | 121 // bookmarks are initialized with atomic assignment, |
114 // hence can use view (not a synchronized copy) here | 122 // hence can use view (not a synchronized copy) here |
115 return Collections.unmodifiableSet(bookmarks.keySet()); | 123 return Collections.unmodifiableSet(bookmarks.keySet()); |
116 } | 124 } |
125 | |
126 /** | |
127 * Update currently bookmark with new commit. | |
128 * Note, child has to be descendant of a p1 or p2 | |
129 * | |
130 * @param p1 first parent, or <code>null</code> | |
131 * @param p2 second parent, or <code>null</code> | |
132 * @param child new commit, descendant of one of the parents, not <code>null</code> | |
133 * @throws HgIOException if failed to write updated bookmark information | |
134 * @throws HgRepositoryLockException if failed to lock repository for modifications | |
135 */ | |
136 @Experimental(reason="Provisional API") | |
137 public void updateActive(Nodeid p1, Nodeid p2, Nodeid child) throws HgIOException, HgRepositoryLockException { | |
138 if (activeBookmark == null) { | |
139 return; | |
140 } | |
141 Nodeid activeRev = getRevision(activeBookmark); | |
142 if (!activeRev.equals(p1) && !activeRev.equals(p2)) { | |
143 // from the wiki: | |
144 // "active bookmarks are automatically updated when committing to the changeset they are pointing to" | |
145 // FIXME: test ci 1, hg bookmark active, ci 2, hg bookmark -f -r 0 active, ci 3, check active still points to r0 | |
146 return; | |
147 } | |
148 if (child.equals(activeRev)) { | |
149 return; | |
150 } | |
151 LinkedHashMap<String, Nodeid> copy = new LinkedHashMap<String, Nodeid>(bookmarks); | |
152 copy.put(activeBookmark, child); | |
153 bookmarks = copy; | |
154 write(); | |
155 } | |
156 | |
157 private void write() throws HgIOException, HgRepositoryLockException { | |
158 File bookmarksFile = internalRepo.getRepositoryFile(HgRepositoryFiles.Bookmarks); | |
159 HgRepositoryLock workingDirLock = internalRepo.getRepo().getWorkingDirLock(); | |
160 FileWriter fileWriter = null; | |
161 workingDirLock.acquire(); | |
162 try { | |
163 fileWriter = new FileWriter(bookmarksFile); | |
164 for (String bm : bookmarks.keySet()) { | |
165 Nodeid nid = bookmarks.get(bm); | |
166 fileWriter.write(String.format("%s %s\n", nid.toString(), bm)); | |
167 } | |
168 fileWriter.flush(); | |
169 } catch (IOException ex) { | |
170 throw new HgIOException("Failed to serialize bookmarks", ex, bookmarksFile); | |
171 } finally { | |
172 try { | |
173 if (fileWriter != null) { | |
174 fileWriter.close(); | |
175 } | |
176 } catch (IOException ex) { | |
177 internalRepo.getSessionContext().getLog().dump(getClass(), Error, ex, null); | |
178 } | |
179 workingDirLock.release(); | |
180 } | |
181 } | |
117 } | 182 } |