tikhomirov@484: /* tikhomirov@605: * Copyright (c) 2012-2013 TMate Software Ltd tikhomirov@484: * tikhomirov@484: * This program is free software; you can redistribute it and/or modify tikhomirov@484: * it under the terms of the GNU General Public License as published by tikhomirov@484: * the Free Software Foundation; version 2 of the License. tikhomirov@484: * tikhomirov@484: * This program is distributed in the hope that it will be useful, tikhomirov@484: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@484: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@484: * GNU General Public License for more details. tikhomirov@484: * tikhomirov@484: * For information on how to redistribute this software under tikhomirov@484: * the terms of a license other than GNU General Public License tikhomirov@484: * contact TMate Software at support@hg4j.com tikhomirov@484: */ tikhomirov@484: package org.tmatesoft.hg.repo; tikhomirov@484: tikhomirov@605: import static org.tmatesoft.hg.util.LogFacility.Severity.Error; tikhomirov@605: tikhomirov@484: import java.io.File; tikhomirov@605: import java.io.FileWriter; tikhomirov@605: import java.io.IOException; tikhomirov@484: import java.util.ArrayList; tikhomirov@484: import java.util.Collection; tikhomirov@484: import java.util.Collections; tikhomirov@484: import java.util.LinkedHashMap; tikhomirov@484: import java.util.Map; tikhomirov@484: tikhomirov@605: import org.tmatesoft.hg.core.HgIOException; tikhomirov@605: import org.tmatesoft.hg.core.HgRepositoryLockException; tikhomirov@484: import org.tmatesoft.hg.core.Nodeid; tikhomirov@605: import org.tmatesoft.hg.internal.Experimental; tikhomirov@610: import org.tmatesoft.hg.internal.FileChangeMonitor; tikhomirov@490: import org.tmatesoft.hg.internal.Internals; tikhomirov@484: import org.tmatesoft.hg.internal.LineReader; tikhomirov@484: import org.tmatesoft.hg.util.LogFacility; tikhomirov@484: tikhomirov@484: /** tikhomirov@610: * Access to bookmarks state tikhomirov@484: * tikhomirov@605: * @see http://mercurial.selenic.com/wiki/Bookmarks tikhomirov@484: * @author Artem Tikhomirov tikhomirov@484: * @author TMate Software Ltd. tikhomirov@484: */ tikhomirov@484: public final class HgBookmarks { tikhomirov@610: private final Internals repo; tikhomirov@484: private Map bookmarks = Collections.emptyMap(); tikhomirov@610: private String activeBookmark; tikhomirov@610: private FileChangeMonitor activeTracker, bmFileTracker; tikhomirov@484: tikhomirov@490: HgBookmarks(Internals internals) { tikhomirov@610: repo = internals; tikhomirov@484: } tikhomirov@484: tikhomirov@484: /*package-local*/ void read() throws HgInvalidControlFileException { tikhomirov@610: readBookmarks(); tikhomirov@610: readActiveBookmark(); tikhomirov@610: } tikhomirov@610: tikhomirov@610: private void readBookmarks() throws HgInvalidControlFileException { tikhomirov@610: final LogFacility log = repo.getLog(); tikhomirov@610: File all = repo.getRepositoryFile(HgRepositoryFiles.Bookmarks); tikhomirov@484: LinkedHashMap bm = new LinkedHashMap(); tikhomirov@610: if (all.canRead() && all.isFile()) { tikhomirov@484: LineReader lr1 = new LineReader(all, log); tikhomirov@484: ArrayList c = new ArrayList(); tikhomirov@484: lr1.read(new LineReader.SimpleLineCollector(), c); tikhomirov@484: for (String s : c) { tikhomirov@484: int x = s.indexOf(' '); tikhomirov@484: try { tikhomirov@484: if (x > 0) { tikhomirov@484: Nodeid nid = Nodeid.fromAscii(s.substring(0, x)); tikhomirov@484: String name = new String(s.substring(x+1)); tikhomirov@610: if (repo.getRepo().getChangelog().isKnown(nid)) { tikhomirov@484: // copy name part not to drag complete line tikhomirov@484: bm.put(name, nid); tikhomirov@484: } else { tikhomirov@484: log.dump(getClass(), LogFacility.Severity.Info, "Bookmark %s points to non-existent revision %s, ignored.", name, nid); tikhomirov@484: } tikhomirov@484: } else { tikhomirov@484: log.dump(getClass(), LogFacility.Severity.Warn, "Can't parse bookmark entry: %s", s); tikhomirov@484: } tikhomirov@484: } catch (IllegalArgumentException ex) { tikhomirov@484: log.dump(getClass(), LogFacility.Severity.Warn, ex, String.format("Can't parse bookmark entry: %s", s)); tikhomirov@484: } tikhomirov@484: } tikhomirov@484: bookmarks = bm; tikhomirov@484: } else { tikhomirov@484: bookmarks = Collections.emptyMap(); tikhomirov@484: } tikhomirov@610: if (bmFileTracker == null) { tikhomirov@610: bmFileTracker = new FileChangeMonitor(all); tikhomirov@610: } tikhomirov@610: bmFileTracker.touch(this); tikhomirov@610: } tikhomirov@484: tikhomirov@610: private void readActiveBookmark() throws HgInvalidControlFileException { tikhomirov@484: activeBookmark = null; tikhomirov@610: File active = repo.getRepositoryFile(HgRepositoryFiles.BookmarksCurrent); tikhomirov@610: if (active.canRead() && active.isFile()) { tikhomirov@610: LineReader lr2 = new LineReader(active, repo.getLog()); tikhomirov@484: ArrayList c = new ArrayList(2); tikhomirov@484: lr2.read(new LineReader.SimpleLineCollector(), c); tikhomirov@484: if (c.size() > 0) { tikhomirov@484: activeBookmark = c.get(0); tikhomirov@484: } tikhomirov@484: } tikhomirov@610: if (activeTracker == null) { tikhomirov@610: activeTracker = new FileChangeMonitor(active); tikhomirov@610: } tikhomirov@610: activeTracker.touch(this); tikhomirov@484: } tikhomirov@610: tikhomirov@610: /*package-local*/void reloadIfChanged() throws HgInvalidControlFileException { tikhomirov@610: assert activeTracker != null; tikhomirov@610: assert bmFileTracker != null; tikhomirov@610: if (bmFileTracker.changed(this)) { tikhomirov@610: readBookmarks(); tikhomirov@610: } tikhomirov@610: if (activeTracker.changed(this)) { tikhomirov@610: readActiveBookmark(); tikhomirov@610: } tikhomirov@610: } tikhomirov@610: tikhomirov@484: tikhomirov@484: /** tikhomirov@484: * Tell name of the active bookmark tikhomirov@484: * @return null if none active tikhomirov@484: */ tikhomirov@484: public String getActiveBookmarkName() { tikhomirov@484: return activeBookmark; tikhomirov@484: } tikhomirov@484: tikhomirov@484: /** tikhomirov@484: * Retrieve revision associated with the named bookmark. tikhomirov@484: * tikhomirov@484: * @param bookmarkName name of the bookmark tikhomirov@484: * @return revision or null if bookmark is not known tikhomirov@484: */ tikhomirov@484: public Nodeid getRevision(String bookmarkName) { tikhomirov@484: return bookmarks.get(bookmarkName); tikhomirov@484: } tikhomirov@484: tikhomirov@484: /** tikhomirov@484: * Retrieve all bookmarks known in the repository tikhomirov@484: * @return collection with names, never null tikhomirov@484: */ tikhomirov@484: public Collection getAllBookmarks() { tikhomirov@484: // bookmarks are initialized with atomic assignment, tikhomirov@484: // hence can use view (not a synchronized copy) here tikhomirov@484: return Collections.unmodifiableSet(bookmarks.keySet()); tikhomirov@484: } tikhomirov@605: tikhomirov@605: /** tikhomirov@605: * Update currently bookmark with new commit. tikhomirov@605: * Note, child has to be descendant of a p1 or p2 tikhomirov@605: * tikhomirov@605: * @param p1 first parent, or null tikhomirov@605: * @param p2 second parent, or null tikhomirov@605: * @param child new commit, descendant of one of the parents, not null tikhomirov@605: * @throws HgIOException if failed to write updated bookmark information tikhomirov@605: * @throws HgRepositoryLockException if failed to lock repository for modifications tikhomirov@605: */ tikhomirov@605: @Experimental(reason="Provisional API") tikhomirov@605: public void updateActive(Nodeid p1, Nodeid p2, Nodeid child) throws HgIOException, HgRepositoryLockException { tikhomirov@605: if (activeBookmark == null) { tikhomirov@605: return; tikhomirov@605: } tikhomirov@605: Nodeid activeRev = getRevision(activeBookmark); tikhomirov@605: if (!activeRev.equals(p1) && !activeRev.equals(p2)) { tikhomirov@605: // from the wiki: tikhomirov@605: // "active bookmarks are automatically updated when committing to the changeset they are pointing to" tikhomirov@605: // FIXME: test ci 1, hg bookmark active, ci 2, hg bookmark -f -r 0 active, ci 3, check active still points to r0 tikhomirov@605: return; tikhomirov@605: } tikhomirov@605: if (child.equals(activeRev)) { tikhomirov@605: return; tikhomirov@605: } tikhomirov@605: LinkedHashMap copy = new LinkedHashMap(bookmarks); tikhomirov@605: copy.put(activeBookmark, child); tikhomirov@605: bookmarks = copy; tikhomirov@605: write(); tikhomirov@605: } tikhomirov@605: tikhomirov@605: private void write() throws HgIOException, HgRepositoryLockException { tikhomirov@610: File bookmarksFile = repo.getRepositoryFile(HgRepositoryFiles.Bookmarks); tikhomirov@610: HgRepositoryLock workingDirLock = repo.getRepo().getWorkingDirLock(); tikhomirov@605: FileWriter fileWriter = null; tikhomirov@605: workingDirLock.acquire(); tikhomirov@605: try { tikhomirov@605: fileWriter = new FileWriter(bookmarksFile); tikhomirov@605: for (String bm : bookmarks.keySet()) { tikhomirov@605: Nodeid nid = bookmarks.get(bm); tikhomirov@605: fileWriter.write(String.format("%s %s\n", nid.toString(), bm)); tikhomirov@605: } tikhomirov@605: fileWriter.flush(); tikhomirov@605: } catch (IOException ex) { tikhomirov@605: throw new HgIOException("Failed to serialize bookmarks", ex, bookmarksFile); tikhomirov@605: } finally { tikhomirov@605: try { tikhomirov@605: if (fileWriter != null) { tikhomirov@605: fileWriter.close(); tikhomirov@605: } tikhomirov@605: } catch (IOException ex) { tikhomirov@610: repo.getLog().dump(getClass(), Error, ex, null); tikhomirov@605: } tikhomirov@605: workingDirLock.release(); tikhomirov@605: } tikhomirov@605: } tikhomirov@484: }