changeset 525:0be5be8d57e9

Repository checkout support, first iteration
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 11 Jan 2013 18:12:39 +0100
parents 57b2c9eb3c69
children 2f9ed6bcefa2
files cmdline/org/tmatesoft/hg/console/Main.java src/org/tmatesoft/hg/core/HgBadArgumentException.java src/org/tmatesoft/hg/core/HgCheckoutCommand.java src/org/tmatesoft/hg/internal/DirstateBuilder.java src/org/tmatesoft/hg/internal/EncodingHelper.java src/org/tmatesoft/hg/internal/Internals.java src/org/tmatesoft/hg/internal/WorkingDirFileWriter.java src/org/tmatesoft/hg/repo/HgDirstate.java
diffstat 8 files changed, 393 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/cmdline/org/tmatesoft/hg/console/Main.java	Fri Jan 11 18:10:29 2013 +0100
+++ b/cmdline/org/tmatesoft/hg/console/Main.java	Fri Jan 11 18:12:39 2013 +0100
@@ -29,6 +29,7 @@
 
 import org.tmatesoft.hg.core.HgChangeset;
 import org.tmatesoft.hg.core.HgChangesetTreeHandler;
+import org.tmatesoft.hg.core.HgCheckoutCommand;
 import org.tmatesoft.hg.core.HgException;
 import org.tmatesoft.hg.core.HgFileRenameHandlerMixin;
 import org.tmatesoft.hg.core.HgFileRevision;
@@ -98,11 +99,12 @@
 
 	public static void main(String[] args) throws Exception {
 		Main m = new Main(args);
+		m.testCheckout();
 //		m.tryExtensions();
 //		m.dumpBookmarks();
 //		m.readConfigFile();
 //		m.dumpCommitLastMessage();
-		m.buildFileLog();
+//		m.buildFileLog();
 //		m.testConsoleLog();
 //		m.testTreeTraversal();
 //		m.testRevisionMap();
@@ -123,6 +125,11 @@
 //		m.bunchOfTests();
 	}
 	
+	private void testCheckout() throws Exception {
+		HgCheckoutCommand coCmd = new HgCheckoutCommand(hgRepo);
+		coCmd.changeset(17).execute();
+	}
+	
 	private void tryExtensions() throws Exception {
 		HgExtensionsManager em = hgRepo.getExtensions();
 		if (!em.isEnabled(HgExt.Rebase)) {
@@ -174,7 +181,7 @@
 	private void buildFileLog() throws Exception {
 		final long start = System.nanoTime();
 		HgLogCommand cmd = new HgLogCommand(hgRepo);
-		cmd.file("file1b.txt", true, true);
+		cmd.file("a2.txt", true, false);
 		final int[] count = new int[] { 0 };
 		class MyHandler implements HgChangesetTreeHandler, Adaptable {
 			public void treeElement(HgChangesetTreeHandler.TreeElement entry) {
--- a/src/org/tmatesoft/hg/core/HgBadArgumentException.java	Fri Jan 11 18:10:29 2013 +0100
+++ b/src/org/tmatesoft/hg/core/HgBadArgumentException.java	Fri Jan 11 18:12:39 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011 TMate Software Ltd
+ * Copyright (c) 2011-2012 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
@@ -40,4 +40,10 @@
 		super.setRevision(r);
 		return this;
 	}
+
+	@Override
+	public HgBadArgumentException setRevisionIndex(int rev) {
+		super.setRevisionIndex(rev);
+		return this;
+	}
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/tmatesoft/hg/core/HgCheckoutCommand.java	Fri Jan 11 18:12:39 2013 +0100
@@ -0,0 +1,153 @@
+/*
+ * Copyright (c) 2012-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.core;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.channels.FileChannel;
+
+import org.tmatesoft.hg.internal.DirstateBuilder;
+import org.tmatesoft.hg.internal.Experimental;
+import org.tmatesoft.hg.internal.Internals;
+import org.tmatesoft.hg.internal.WorkingDirFileWriter;
+import org.tmatesoft.hg.repo.HgDataFile;
+import org.tmatesoft.hg.repo.HgInvalidRevisionException;
+import org.tmatesoft.hg.repo.HgInvalidStateException;
+import org.tmatesoft.hg.repo.HgManifest;
+import org.tmatesoft.hg.repo.HgRepository;
+import org.tmatesoft.hg.repo.HgRuntimeException;
+import org.tmatesoft.hg.repo.HgManifest.Flags;
+import org.tmatesoft.hg.util.CancelledException;
+import org.tmatesoft.hg.util.Path;
+
+/**
+ * WORK IN PROGRESS.
+ * 
+ * Update working directory to specific state, 'hg checkout' counterpart.
+ * For the time being, only 'clean' checkout is supported ('hg co --clean')
+ * 
+ * @since 1.1
+ * @author Artem Tikhomirov
+ * @author TMate Software Ltd.
+ */
+@Experimental(reason="Work in progress")
+public class HgCheckoutCommand extends HgAbstractCommand<HgCheckoutCommand>{
+
+	private final HgRepository repo;
+	private int revisionToCheckout = HgRepository.BAD_REVISION;
+
+	public HgCheckoutCommand(HgRepository hgRepo) {
+		repo = hgRepo;
+	}
+	
+	/**
+	 * Select revision to check out
+	 * 
+	 * @param nodeid revision
+	 * @return <code>this</code> for convenience
+	 * @throws HgBadArgumentException if failed to find supplied changeset 
+	 */
+	public HgCheckoutCommand changeset(Nodeid nodeid) throws HgBadArgumentException {
+		try {
+			return changeset(repo.getChangelog().getRevisionIndex(nodeid));
+		} catch (HgInvalidRevisionException ex) {
+			throw new HgBadArgumentException("Can't find revision", ex).setRevision(nodeid);
+		}
+	}
+
+	/**
+	 * Select revision to check out using local revision index
+	 * 
+	 * @param changesetIndex local revision index
+	 * @return <code>this</code> for convenience
+	 * @throws HgBadArgumentException if failed to find supplied changeset 
+	 */
+	public HgCheckoutCommand changeset(int changesetIndex) throws HgBadArgumentException {
+		int lastCsetIndex = repo.getChangelog().getLastRevision();
+		if (changesetIndex < 0 || changesetIndex > lastCsetIndex) {
+			throw new HgBadArgumentException(String.format("Bad revision index %d, value from [0..%d] expected", changesetIndex, lastCsetIndex), null).setRevisionIndex(changesetIndex);
+		}
+		revisionToCheckout = changesetIndex;
+		return this;
+	}
+
+	/**
+	 * 
+	 * @throws HgIOException to indicate troubles updating files in working copy
+	 * @throws HgException
+	 * @throws CancelledException
+	 */
+	public void execute() throws HgException, CancelledException {
+		Internals internalRepo = Internals.getInstance(repo);
+		// remove tracked files from wd (perhaps, just forget 'Added'?)
+		// TODO
+		final DirstateBuilder dirstateBuilder = new DirstateBuilder(internalRepo.buildFileNameEncodingHelper());
+		final Exception[] failure = new Exception[1];
+		HgManifest.Inspector worker = new HgManifest.Inspector() {
+			
+			public boolean next(Nodeid nid, Path fname, Flags flags) {
+				try {
+					HgDataFile df = repo.getFileNode(fname);
+					int fileRevIndex = df.getRevisionIndex(nid);
+					// check out files based on manifest
+					// FIXME links!
+					WorkingDirFileWriter workingDirWriter = new WorkingDirFileWriter(repo);
+					workingDirWriter.processFile(df, fileRevIndex);
+					// new dirstate based on manifest
+					dirstateBuilder.recordNormal(fname, flags, workingDirWriter.bytesWritten());
+					return true;
+				} catch (IOException ex) {
+					failure[0] = ex;
+				} catch (HgRuntimeException ex) {
+					failure[0] = ex;
+				}
+				return false;
+			}
+			
+			public boolean end(int manifestRevision) {
+				return false;
+			}
+			
+			public boolean begin(int mainfestRevision, Nodeid nid, int changelogRevision) {
+				return true;
+			}
+		};
+		dirstateBuilder.parents(repo.getChangelog().getRevision(revisionToCheckout), null);
+		repo.getManifest().walk(revisionToCheckout, revisionToCheckout, worker);
+		if (failure[0] != null) {
+			if (failure[0] instanceof IOException) {
+				throw new HgIOException("Failed to write down file revision", failure[0], /*FIXME file*/null);
+			}
+			if (failure[0] instanceof HgRuntimeException) {
+				throw new HgLibraryFailureException((HgRuntimeException) failure[0]);
+			}
+			HgInvalidStateException e = new HgInvalidStateException("Unexpected exception");
+			e.initCause(failure[0]);
+			throw e;
+		}
+		File dirstateFile = internalRepo.getFileFromRepoDir("dirstate");
+		try {
+			FileChannel dirstate = new FileOutputStream(dirstateFile).getChannel();
+			dirstateBuilder.serialize(dirstate);
+			dirstate.close();
+		} catch (IOException ex) {
+			throw new HgIOException("Can't write down new directory state", ex, dirstateFile);
+		}
+		// FIXME write down branch file
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/tmatesoft/hg/internal/DirstateBuilder.java	Fri Jan 11 18:12:39 2013 +0100
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2012-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.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.WritableByteChannel;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.tmatesoft.hg.core.Nodeid;
+import org.tmatesoft.hg.repo.HgDirstate;
+import org.tmatesoft.hg.repo.HgManifest.Flags;
+import org.tmatesoft.hg.util.Path;
+
+/**
+ * Facility to build a dirstate file as described in {@linkplain http://mercurial.selenic.com/wiki/DirState}
+ * 
+ * @see http://mercurial.selenic.com/wiki/DirState
+ * @see HgDirstate
+ * @author Artem Tikhomirov
+ * @author TMate Software Ltd.
+ */
+public class DirstateBuilder {
+	private List<HgDirstate.Record> normal = new ArrayList<HgDirstate.Record>();
+	private Nodeid parent1, parent2;
+	private final EncodingHelper encodingHelper;
+	
+	public DirstateBuilder(EncodingHelper encHelper) {
+		encodingHelper = encHelper;
+	}
+	
+	public void parents(Nodeid p1, Nodeid p2) {
+		parent1 = p1 == null ? Nodeid.NULL : p1;
+		parent2 = p2 == null ? Nodeid.NULL : p2;
+	}
+	
+	public void recordNormal(Path fname, Flags flags, int bytesWritten) {
+		// Mercurial seems to write "n   0  -1   unset fname" on `hg --clean co -rev <earlier rev>`
+		// and the reason for 'force lookup' I suspect is a slight chance of simultaneous modification
+		// of the file by user that doesn't alter its size the very second dirstate is being written
+		// (or the file is being updated and the update brought in changes that didn't alter the file size - 
+		// with size and timestamp set, later `hg status` won't notice these changes)
+		
+		// However, as long as we use this class to write clean copies of the files, we can put all the fields
+		// right away.
+		int fmode = flags == Flags.RegularFile ? 0666 : 0777; // FIXME actual unix flags
+		int mtime = (int) (System.currentTimeMillis() / 1000);
+		normal.add(new HgDirstate.Record(fmode, bytesWritten, mtime,fname, null));
+		
+	}
+
+	public void serialize(WritableByteChannel dest) throws IOException {
+		assert parent1 != null : "Parent(s) of the working directory shall be set first";
+		ByteBuffer bb = ByteBuffer.allocate(256);
+		bb.put(parent1.toByteArray());
+		bb.put(parent2.toByteArray());
+		bb.flip();
+		// header
+		int written = dest.write(bb);
+		if (written != bb.limit()) {
+			throw new IOException("Incomplete write");
+		}
+		bb.clear();
+		// entries
+		for (HgDirstate.Record r : normal) {
+			// normal entry is 1+4+4+4+4+fname.length bytes
+			byte[] fname = encodingHelper.toDirstate(r.name().toString());
+			bb = ensureCapacity(bb, 17 + fname.length);
+			bb.put((byte) 'n');
+			bb.putInt(r.mode());
+			bb.putInt(r.size());
+			bb.putInt(r.modificationTime());
+			bb.putInt(fname.length);
+			bb.put(fname);
+			bb.flip();
+			written = dest.write(bb);
+			if (written != bb.limit()) {
+				throw new IOException("Incomplete write");
+			}
+			bb.clear();
+		}
+	}
+
+	private static ByteBuffer ensureCapacity(ByteBuffer buf, int cap) {
+		if (buf.capacity() >= cap) {
+			return buf;
+		}
+		return ByteBuffer.allocate(cap);
+	}
+}
--- a/src/org/tmatesoft/hg/internal/EncodingHelper.java	Fri Jan 11 18:10:29 2013 +0100
+++ b/src/org/tmatesoft/hg/internal/EncodingHelper.java	Fri Jan 11 18:12:39 2013 +0100
@@ -65,8 +65,36 @@
 			// perhaps, can return byte[0] in this case?
 			throw new IllegalArgumentException();
 		}
+		return encodeWithSystemDefaultFallback(s);
+	}
+
+	/**
+	 * Translate file names from dirstate to amazing Unicode string 
+	 */
+	public String fromDirstate(byte[] data, int start, int length) {
+		return decodeWithSystemDefaultFallback(data, start, length);
+	}
+	
+	public byte[] toDirstate(String fname) {
+		if (fname == null) {
+			throw new IllegalArgumentException();
+		}
+		return encodeWithSystemDefaultFallback(fname);
+	}
+
+	private String decodeWithSystemDefaultFallback(byte[] data, int start, int length) {
 		try {
-			// synchonized(encoder) {
+			return decoder.decode(ByteBuffer.wrap(data, start, length)).toString();
+		} catch (CharacterCodingException ex) {
+			sessionContext.getLog().dump(getClass(), Error, ex, String.format("Use of charset %s failed, resort to system default", charset().name()));
+			// resort to system-default
+			return new String(data, start, length);
+		}
+	}
+	
+	private byte[] encodeWithSystemDefaultFallback(String s) {
+		try {
+			// synchronized(encoder) {
 			ByteBuffer bb = encoder.encode(CharBuffer.wrap(s));
 			// }
 			byte[] rv = new byte[bb.remaining()];
@@ -79,23 +107,6 @@
 		}
 	}
 
-	/**
-	 * Translate file names from dirstate to amazing Unicode string 
-	 */
-	public String fromDirstate(byte[] data, int start, int length) {
-		return decodeWithSystemDefaultFallback(data, start, length);
-	}
-
-	private String decodeWithSystemDefaultFallback(byte[] data, int start, int length) {
-		try {
-			return decoder.decode(ByteBuffer.wrap(data, start, length)).toString();
-		} catch (CharacterCodingException ex) {
-			sessionContext.getLog().dump(getClass(), Error, ex, String.format("Use of charset %s failed, resort to system default", charset().name()));
-			// resort to system-default
-			return new String(data, start, length);
-		}
-	}
-
 	private Charset charset() {
 		return encoder.charset();
 	}
--- a/src/org/tmatesoft/hg/internal/Internals.java	Fri Jan 11 18:10:29 2013 +0100
+++ b/src/org/tmatesoft/hg/internal/Internals.java	Fri Jan 11 18:12:39 2013 +0100
@@ -31,6 +31,7 @@
 
 import org.tmatesoft.hg.core.SessionContext;
 import org.tmatesoft.hg.repo.HgDataFile;
+import org.tmatesoft.hg.repo.HgInternals;
 import org.tmatesoft.hg.repo.HgRuntimeException;
 import org.tmatesoft.hg.repo.HgRepoConfig.ExtensionsSection;
 import org.tmatesoft.hg.repo.HgRepository;
@@ -410,6 +411,10 @@
 		return shallCacheRevlogsInRepo;
 	}
 	
+	public static Internals getInstance(HgRepository repo) {
+		return HgInternals.getImplementationRepo(repo);
+	}
+	
 	public static <T> CharSequence join(Iterable<T> col, CharSequence separator) {
 		if (col == null) {
 			return String.valueOf(col);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/tmatesoft/hg/internal/WorkingDirFileWriter.java	Fri Jan 11 18:12:39 2013 +0100
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2012-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.FileOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+
+import org.tmatesoft.hg.repo.HgDataFile;
+import org.tmatesoft.hg.repo.HgRepository;
+import org.tmatesoft.hg.util.ByteChannel;
+import org.tmatesoft.hg.util.CancelledException;
+import org.tmatesoft.hg.util.Path;
+import org.tmatesoft.hg.util.LogFacility.Severity;
+
+/**
+ * 
+ * @author Artem Tikhomirov
+ * @author TMate Software Ltd.
+ */
+public class WorkingDirFileWriter implements ByteChannel {
+
+	
+	private final HgRepository repo;
+	private File dest;
+	private FileChannel destChannel;
+	private int totalBytesWritten;
+
+	public WorkingDirFileWriter(HgRepository hgRepo) {
+		repo = hgRepo;
+	}
+	
+	public void processFile(HgDataFile df, int fileRevIndex) throws IOException {
+		try {
+			prepare(df.getPath());
+			df.contentWithFilters(fileRevIndex, this);
+		} catch (CancelledException ex) {
+			repo.getSessionContext().getLog().dump(getClass(), Severity.Error, ex, "Our impl doesn't throw cancellation");
+		}
+		finish();
+	}
+
+	public void prepare(Path fname) throws IOException {
+		String fpath = fname.toString();
+		dest = new File(repo.getWorkingDir(), fpath);
+		if (fpath.indexOf('/') != -1) {
+			dest.getParentFile().mkdirs();
+		}
+		destChannel = new FileOutputStream(dest).getChannel();
+		totalBytesWritten = 0;
+	}
+
+	public int write(ByteBuffer buffer) throws IOException, CancelledException {
+		int written = destChannel.write(buffer);
+		totalBytesWritten += written;
+		return written;
+	}
+
+	public void finish() throws IOException {
+		destChannel.close();
+		dest = null;
+	}
+	
+	public int bytesWritten() {
+		return totalBytesWritten;
+	}
+}
--- a/src/org/tmatesoft/hg/repo/HgDirstate.java	Fri Jan 11 18:10:29 2013 +0100
+++ b/src/org/tmatesoft/hg/repo/HgDirstate.java	Fri Jan 11 18:12:39 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010-2012 TMate Software Ltd
+ * Copyright (c) 2010-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
@@ -325,7 +325,7 @@
 		// Thus, can't compare directly to HgDataFile.length()
 		private final Path name1, name2;
 
-		/*package-local*/ Record(int fmode, int fsize, int ftime, Path name1, Path name2) {
+		public Record(int fmode, int fsize, int ftime, Path name1, Path name2) {
 			mode = fmode;
 			size = fsize;
 			time = ftime;