Mercurial > hg4j
view src/org/tmatesoft/hg/internal/WorkingCopyContent.java @ 621:99ad1e3a4e4d
RevlogStream: be aware of existence (not HgDataFile), facilitate use of an added HgDataFile over a commit; Rollback: be more sensitive about file changes (file size is not enough: write/rollback leaves it intact); tests
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Sat, 18 May 2013 22:23:57 +0200 |
parents | 7c0d2ce340b8 |
children | 4e6179bde4fc |
line wrap: on
line source
/* * Copyright (c) 2013 TMate Software Ltd * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 2 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * For information on how to redistribute this software under * the terms of a license other than GNU General Public License * contact TMate Software at support@hg4j.com */ package org.tmatesoft.hg.internal; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import org.tmatesoft.hg.core.HgCommitCommand; import org.tmatesoft.hg.core.HgIOException; import org.tmatesoft.hg.repo.HgDataFile; import org.tmatesoft.hg.repo.HgInvalidFileException; import org.tmatesoft.hg.repo.HgInvalidStateException; import org.tmatesoft.hg.repo.HgRepository; import org.tmatesoft.hg.util.ByteChannel; import org.tmatesoft.hg.util.CancelledException; /** * Access content of the working copy. The difference with {@link FileContentSupplier} is that this one doesn't need {@link File} * in the working directory. However, provided this class is used from {@link HgCommitCommand} when "modified" file was detected, * it's odd to expect no file in the working dir. * * @author Artem Tikhomirov * @author TMate Software Ltd. */ public class WorkingCopyContent implements DataSerializer.DataSource { private final HgDataFile file; public WorkingCopyContent(HgDataFile dataFile) { file = dataFile; if (!dataFile.exists()) { throw new IllegalArgumentException(); } } public void serialize(final DataSerializer out) throws HgIOException { final HgIOException failure[] = new HgIOException[1]; try { // TODO #workingCopy API is very limiting, CancelledException is inconvenient, // and absence of HgIOException is very uncomfortable file.workingCopy(new ByteChannel() { public int write(ByteBuffer buffer) throws IOException { try { if (buffer.hasArray()) { out.write(buffer.array(), buffer.position(), buffer.remaining()); } int rv = buffer.remaining(); buffer.position(buffer.limit()); // pretend we've consumed the data return rv; } catch (HgIOException ex) { failure[0] = ex; throw new IOException(ex); } } }); } catch (HgInvalidFileException ex) { if (failure[0] != null) { throw failure[0]; } throw new HgIOException("Write failure", ex, new File(file.getRepo().getWorkingDir(), file.getPath().toString())); } catch (CancelledException ex) { throw new HgInvalidStateException("Our channel doesn't cancel here"); } } public int serializeLength() { return file.getLength(HgRepository.WORKING_COPY); } }