comparison src/org/tmatesoft/hg/repo/HgBookmarks.java @ 610:5c68567b3645

Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 09 May 2013 21:06:48 +0200
parents c56edf42be64
children f41dd9a3b8af
comparison
equal deleted inserted replaced
609:e4a71afd3c71 610:5c68567b3645
29 29
30 import org.tmatesoft.hg.core.HgIOException; 30 import org.tmatesoft.hg.core.HgIOException;
31 import org.tmatesoft.hg.core.HgRepositoryLockException; 31 import org.tmatesoft.hg.core.HgRepositoryLockException;
32 import org.tmatesoft.hg.core.Nodeid; 32 import org.tmatesoft.hg.core.Nodeid;
33 import org.tmatesoft.hg.internal.Experimental; 33 import org.tmatesoft.hg.internal.Experimental;
34 import org.tmatesoft.hg.internal.FileChangeMonitor;
34 import org.tmatesoft.hg.internal.Internals; 35 import org.tmatesoft.hg.internal.Internals;
35 import org.tmatesoft.hg.internal.LineReader; 36 import org.tmatesoft.hg.internal.LineReader;
36 import org.tmatesoft.hg.util.LogFacility; 37 import org.tmatesoft.hg.util.LogFacility;
37 38
38 /** 39 /**
40 * Access to bookmarks state
39 * 41 *
40 * @see http://mercurial.selenic.com/wiki/Bookmarks 42 * @see http://mercurial.selenic.com/wiki/Bookmarks
41 * @author Artem Tikhomirov 43 * @author Artem Tikhomirov
42 * @author TMate Software Ltd. 44 * @author TMate Software Ltd.
43 */ 45 */
44 public final class HgBookmarks { 46 public final class HgBookmarks {
45 private final Internals internalRepo; 47 private final Internals repo;
46 private Map<String, Nodeid> bookmarks = Collections.emptyMap(); 48 private Map<String, Nodeid> bookmarks = Collections.emptyMap();
47 private String activeBookmark; 49 private String activeBookmark;
50 private FileChangeMonitor activeTracker, bmFileTracker;
48 51
49 HgBookmarks(Internals internals) { 52 HgBookmarks(Internals internals) {
50 internalRepo = internals; 53 repo = internals;
51 } 54 }
52 55
53 /*package-local*/ void read() throws HgInvalidControlFileException { 56 /*package-local*/ void read() throws HgInvalidControlFileException {
54 final LogFacility log = internalRepo.getSessionContext().getLog(); 57 readBookmarks();
55 final HgRepository repo = internalRepo.getRepo(); 58 readActiveBookmark();
56 File all = internalRepo.getFileFromRepoDir(HgRepositoryFiles.Bookmarks.getName()); 59 }
60
61 private void readBookmarks() throws HgInvalidControlFileException {
62 final LogFacility log = repo.getLog();
63 File all = repo.getRepositoryFile(HgRepositoryFiles.Bookmarks);
57 LinkedHashMap<String, Nodeid> bm = new LinkedHashMap<String, Nodeid>(); 64 LinkedHashMap<String, Nodeid> bm = new LinkedHashMap<String, Nodeid>();
58 if (all.canRead()) { 65 if (all.canRead() && all.isFile()) {
59 LineReader lr1 = new LineReader(all, log); 66 LineReader lr1 = new LineReader(all, log);
60 ArrayList<String> c = new ArrayList<String>(); 67 ArrayList<String> c = new ArrayList<String>();
61 lr1.read(new LineReader.SimpleLineCollector(), c); 68 lr1.read(new LineReader.SimpleLineCollector(), c);
62 for (String s : c) { 69 for (String s : c) {
63 int x = s.indexOf(' '); 70 int x = s.indexOf(' ');
64 try { 71 try {
65 if (x > 0) { 72 if (x > 0) {
66 Nodeid nid = Nodeid.fromAscii(s.substring(0, x)); 73 Nodeid nid = Nodeid.fromAscii(s.substring(0, x));
67 String name = new String(s.substring(x+1)); 74 String name = new String(s.substring(x+1));
68 if (repo.getChangelog().isKnown(nid)) { 75 if (repo.getRepo().getChangelog().isKnown(nid)) {
69 // copy name part not to drag complete line 76 // copy name part not to drag complete line
70 bm.put(name, nid); 77 bm.put(name, nid);
71 } else { 78 } else {
72 log.dump(getClass(), LogFacility.Severity.Info, "Bookmark %s points to non-existent revision %s, ignored.", name, nid); 79 log.dump(getClass(), LogFacility.Severity.Info, "Bookmark %s points to non-existent revision %s, ignored.", name, nid);
73 } 80 }
80 } 87 }
81 bookmarks = bm; 88 bookmarks = bm;
82 } else { 89 } else {
83 bookmarks = Collections.emptyMap(); 90 bookmarks = Collections.emptyMap();
84 } 91 }
92 if (bmFileTracker == null) {
93 bmFileTracker = new FileChangeMonitor(all);
94 }
95 bmFileTracker.touch(this);
96 }
85 97
98 private void readActiveBookmark() throws HgInvalidControlFileException {
86 activeBookmark = null; 99 activeBookmark = null;
87 File active = internalRepo.getFileFromRepoDir(HgRepositoryFiles.BookmarksCurrent.getName()); 100 File active = repo.getRepositoryFile(HgRepositoryFiles.BookmarksCurrent);
88 if (active.canRead()) { 101 if (active.canRead() && active.isFile()) {
89 LineReader lr2 = new LineReader(active, log); 102 LineReader lr2 = new LineReader(active, repo.getLog());
90 ArrayList<String> c = new ArrayList<String>(2); 103 ArrayList<String> c = new ArrayList<String>(2);
91 lr2.read(new LineReader.SimpleLineCollector(), c); 104 lr2.read(new LineReader.SimpleLineCollector(), c);
92 if (c.size() > 0) { 105 if (c.size() > 0) {
93 activeBookmark = c.get(0); 106 activeBookmark = c.get(0);
94 } 107 }
95 } 108 }
96 } 109 if (activeTracker == null) {
110 activeTracker = new FileChangeMonitor(active);
111 }
112 activeTracker.touch(this);
113 }
114
115 /*package-local*/void reloadIfChanged() throws HgInvalidControlFileException {
116 assert activeTracker != null;
117 assert bmFileTracker != null;
118 if (bmFileTracker.changed(this)) {
119 readBookmarks();
120 }
121 if (activeTracker.changed(this)) {
122 readActiveBookmark();
123 }
124 }
125
97 126
98 /** 127 /**
99 * Tell name of the active bookmark 128 * Tell name of the active bookmark
100 * @return <code>null</code> if none active 129 * @return <code>null</code> if none active
101 */ 130 */
153 bookmarks = copy; 182 bookmarks = copy;
154 write(); 183 write();
155 } 184 }
156 185
157 private void write() throws HgIOException, HgRepositoryLockException { 186 private void write() throws HgIOException, HgRepositoryLockException {
158 File bookmarksFile = internalRepo.getRepositoryFile(HgRepositoryFiles.Bookmarks); 187 File bookmarksFile = repo.getRepositoryFile(HgRepositoryFiles.Bookmarks);
159 HgRepositoryLock workingDirLock = internalRepo.getRepo().getWorkingDirLock(); 188 HgRepositoryLock workingDirLock = repo.getRepo().getWorkingDirLock();
160 FileWriter fileWriter = null; 189 FileWriter fileWriter = null;
161 workingDirLock.acquire(); 190 workingDirLock.acquire();
162 try { 191 try {
163 fileWriter = new FileWriter(bookmarksFile); 192 fileWriter = new FileWriter(bookmarksFile);
164 for (String bm : bookmarks.keySet()) { 193 for (String bm : bookmarks.keySet()) {
172 try { 201 try {
173 if (fileWriter != null) { 202 if (fileWriter != null) {
174 fileWriter.close(); 203 fileWriter.close();
175 } 204 }
176 } catch (IOException ex) { 205 } catch (IOException ex) {
177 internalRepo.getSessionContext().getLog().dump(getClass(), Error, ex, null); 206 repo.getLog().dump(getClass(), Error, ex, null);
178 } 207 }
179 workingDirLock.release(); 208 workingDirLock.release();
180 } 209 }
181 } 210 }
182 } 211 }