tikhomirov@10: /*
tikhomirov@74:  * Copyright (c) 2010-2011 TMate Software Ltd
tikhomirov@74:  *  
tikhomirov@74:  * This program is free software; you can redistribute it and/or modify
tikhomirov@74:  * it under the terms of the GNU General Public License as published by
tikhomirov@74:  * the Free Software Foundation; version 2 of the License.
tikhomirov@74:  *
tikhomirov@74:  * This program is distributed in the hope that it will be useful,
tikhomirov@74:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
tikhomirov@74:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
tikhomirov@74:  * GNU General Public License for more details.
tikhomirov@74:  *
tikhomirov@74:  * For information on how to redistribute this software under
tikhomirov@74:  * the terms of a license other than GNU General Public License
tikhomirov@102:  * contact TMate Software at support@hg4j.com
tikhomirov@10:  */
tikhomirov@74: package org.tmatesoft.hg.repo;
tikhomirov@10: 
tikhomirov@10: import java.io.File;
tikhomirov@10: import java.io.IOException;
tikhomirov@10: import java.util.Collections;
tikhomirov@18: import java.util.LinkedHashMap;
tikhomirov@18: import java.util.Map;
tikhomirov@18: import java.util.TreeSet;
tikhomirov@10: 
tikhomirov@231: import org.tmatesoft.hg.core.HgBadStateException;
tikhomirov@231: import org.tmatesoft.hg.core.Nodeid;
tikhomirov@74: import org.tmatesoft.hg.internal.DataAccess;
tikhomirov@74: import org.tmatesoft.hg.internal.DataAccessProvider;
tikhomirov@141: import org.tmatesoft.hg.util.Path;
tikhomirov@74: 
tikhomirov@10: 
tikhomirov@10: /**
tikhomirov@10:  * @see http://mercurial.selenic.com/wiki/DirState
tikhomirov@10:  * @see http://mercurial.selenic.com/wiki/FileFormats#dirstate
tikhomirov@74:  *
tikhomirov@74:  * @author Artem Tikhomirov
tikhomirov@74:  * @author TMate Software Ltd.
tikhomirov@10:  */
tikhomirov@93: class HgDirstate {
tikhomirov@10: 
tikhomirov@59: 	private final DataAccessProvider accessProvider;
tikhomirov@10: 	private final File dirstateFile;
tikhomirov@141: 	// deliberate String, not Path as it seems useless to keep Path here
tikhomirov@18: 	private Map<String, Record> normal;
tikhomirov@18: 	private Map<String, Record> added;
tikhomirov@18: 	private Map<String, Record> removed;
tikhomirov@18: 	private Map<String, Record> merged;
tikhomirov@231: 	private Nodeid p1, p2;
tikhomirov@10: 
tikhomirov@59: 	/*package-local*/ HgDirstate() {
tikhomirov@59: 		// empty instance
tikhomirov@59: 		accessProvider = null;
tikhomirov@59: 		dirstateFile = null;
tikhomirov@59: 	}
tikhomirov@59: 
tikhomirov@59: 	public HgDirstate(DataAccessProvider dap, File dirstate) {
tikhomirov@59: 		accessProvider = dap;
tikhomirov@59: 		dirstateFile = dirstate;
tikhomirov@10: 	}
tikhomirov@10: 
tikhomirov@10: 	private void read() {
tikhomirov@18: 		normal = added = removed = merged = Collections.<String, Record>emptyMap();
tikhomirov@59: 		if (dirstateFile == null || !dirstateFile.exists()) {
tikhomirov@10: 			return;
tikhomirov@10: 		}
tikhomirov@59: 		DataAccess da = accessProvider.create(dirstateFile);
tikhomirov@10: 		if (da.isEmpty()) {
tikhomirov@10: 			return;
tikhomirov@10: 		}
tikhomirov@18: 		// not sure linked is really needed here, just for ease of debug
tikhomirov@18: 		normal = new LinkedHashMap<String, Record>();
tikhomirov@18: 		added = new LinkedHashMap<String, Record>();
tikhomirov@18: 		removed = new LinkedHashMap<String, Record>();
tikhomirov@18: 		merged = new LinkedHashMap<String, Record>();
tikhomirov@10: 		try {
tikhomirov@10: 			byte[] parents = new byte[40];
tikhomirov@10: 			da.readBytes(parents, 0, 40);
tikhomirov@231: 			p1 = Nodeid.fromBinary(parents, 0);
tikhomirov@231: 			p2 = Nodeid.fromBinary(parents, 20);
tikhomirov@10: 			parents = null;
tikhomirov@227: 			// hg init; hg up produces an empty repository where dirstate has parents (40 bytes) only
tikhomirov@227: 			while (!da.isEmpty()) {
tikhomirov@10: 				final byte state = da.readByte();
tikhomirov@10: 				final int fmode = da.readInt();
tikhomirov@10: 				final int size = da.readInt();
tikhomirov@10: 				final int time = da.readInt();
tikhomirov@10: 				final int nameLen = da.readInt();
tikhomirov@10: 				String fn1 = null, fn2 = null;
tikhomirov@10: 				byte[] name = new byte[nameLen];
tikhomirov@10: 				da.readBytes(name, 0, nameLen);
tikhomirov@10: 				for (int i = 0; i < nameLen; i++) {
tikhomirov@10: 					if (name[i] == 0) {
tikhomirov@11: 						fn1 = new String(name, 0, i, "UTF-8"); // XXX unclear from documentation what encoding is used there
tikhomirov@11: 						fn2 = new String(name, i+1, nameLen - i - 1, "UTF-8"); // need to check with different system codepages
tikhomirov@10: 						break;
tikhomirov@10: 					}
tikhomirov@10: 				}
tikhomirov@10: 				if (fn1 == null) {
tikhomirov@10: 					fn1 = new String(name);
tikhomirov@10: 				}
tikhomirov@10: 				Record r = new Record(fmode, size, time, fn1, fn2);
tikhomirov@10: 				if (state == 'n') {
tikhomirov@18: 					normal.put(r.name1, r);
tikhomirov@10: 				} else if (state == 'a') {
tikhomirov@18: 					added.put(r.name1, r);
tikhomirov@10: 				} else if (state == 'r') {
tikhomirov@18: 					removed.put(r.name1, r);
tikhomirov@10: 				} else if (state == 'm') {
tikhomirov@18: 					merged.put(r.name1, r);
tikhomirov@10: 				} else {
tikhomirov@10: 					// FIXME log error?
tikhomirov@10: 				}
tikhomirov@227: 			}
tikhomirov@10: 		} catch (IOException ex) {
tikhomirov@10: 			ex.printStackTrace(); // FIXME log error, clean dirstate?
tikhomirov@10: 		} finally {
tikhomirov@10: 			da.done();
tikhomirov@10: 		}
tikhomirov@10: 	}
tikhomirov@10: 
tikhomirov@231: 	// do not read whole dirstate if all we need is WC parent information
tikhomirov@231: 	private void readParents() {
tikhomirov@231: 		if (dirstateFile == null || !dirstateFile.exists()) {
tikhomirov@231: 			return;
tikhomirov@231: 		}
tikhomirov@231: 		DataAccess da = accessProvider.create(dirstateFile);
tikhomirov@231: 		if (da.isEmpty()) {
tikhomirov@231: 			return;
tikhomirov@231: 		}
tikhomirov@231: 		try {
tikhomirov@231: 			byte[] parents = new byte[40];
tikhomirov@231: 			da.readBytes(parents, 0, 40);
tikhomirov@231: 			p1 = Nodeid.fromBinary(parents, 0);
tikhomirov@231: 			p2 = Nodeid.fromBinary(parents, 20);
tikhomirov@231: 			parents = null;
tikhomirov@231: 		} catch (IOException ex) {
tikhomirov@231: 			throw new HgBadStateException(ex); // XXX in fact, our exception is not the best solution here.
tikhomirov@231: 		} finally {
tikhomirov@231: 			da.done();
tikhomirov@231: 		}
tikhomirov@231: 	}
tikhomirov@231: 	
tikhomirov@231: 	/**
tikhomirov@231: 	 * @return array of length 2 with working copy parents, non null.
tikhomirov@231: 	 */
tikhomirov@231: 	public Nodeid[] parents() {
tikhomirov@231: 		if (p1 == null) {
tikhomirov@231: 			readParents();
tikhomirov@231: 		}
tikhomirov@231: 		Nodeid[] rv = new Nodeid[2];
tikhomirov@231: 		rv[0] = p1;
tikhomirov@231: 		rv[1] = p2;
tikhomirov@231: 		return rv;
tikhomirov@231: 	}
tikhomirov@231: 
tikhomirov@18: 	// new, modifiable collection
tikhomirov@18: 	/*package-local*/ TreeSet<String> all() {
tikhomirov@18: 		read();
tikhomirov@18: 		TreeSet<String> rv = new TreeSet<String>();
tikhomirov@18: 		@SuppressWarnings("unchecked")
tikhomirov@18: 		Map<String, Record>[] all = new Map[] { normal, added, removed, merged };
tikhomirov@18: 		for (int i = 0; i < all.length; i++) {
tikhomirov@18: 			for (Record r : all[i].values()) {
tikhomirov@18: 				rv.add(r.name1);
tikhomirov@18: 			}
tikhomirov@18: 		}
tikhomirov@18: 		return rv;
tikhomirov@18: 	}
tikhomirov@18: 	
tikhomirov@141: 	/*package-local*/ Record checkNormal(Path fname) {
tikhomirov@141: 		return normal.get(fname.toString());
tikhomirov@18: 	}
tikhomirov@18: 
tikhomirov@141: 	/*package-local*/ Record checkAdded(Path fname) {
tikhomirov@141: 		return added.get(fname.toString());
tikhomirov@141: 	}
tikhomirov@141: 	/*package-local*/ Record checkRemoved(Path fname) {
tikhomirov@141: 		return removed.get(fname.toString());
tikhomirov@18: 	}
tikhomirov@18: 	/*package-local*/ Record checkRemoved(String fname) {
tikhomirov@18: 		return removed.get(fname);
tikhomirov@18: 	}
tikhomirov@141: 	/*package-local*/ Record checkMerged(Path fname) {
tikhomirov@141: 		return merged.get(fname.toString());
tikhomirov@18: 	}
tikhomirov@18: 
tikhomirov@18: 
tikhomirov@18: 
tikhomirov@18: 
tikhomirov@93: 	/*package-local*/ void dump() {
tikhomirov@10: 		read();
tikhomirov@10: 		@SuppressWarnings("unchecked")
tikhomirov@18: 		Map<String, Record>[] all = new Map[] { normal, added, removed, merged };
tikhomirov@10: 		char[] x = new char[] {'n', 'a', 'r', 'm' };
tikhomirov@10: 		for (int i = 0; i < all.length; i++) {
tikhomirov@18: 			for (Record r : all[i].values()) {
tikhomirov@14: 				System.out.printf("%c %3o%6d %30tc\t\t%s", x[i], r.mode, r.size, (long) r.time * 1000, r.name1);
tikhomirov@10: 				if (r.name2 != null) {
tikhomirov@10: 					System.out.printf(" --> %s", r.name2);
tikhomirov@10: 				}
tikhomirov@10: 				System.out.println();
tikhomirov@10: 			}
tikhomirov@10: 			System.out.println();
tikhomirov@10: 		}
tikhomirov@10: 	}
tikhomirov@10: 	
tikhomirov@18: 	/*package-local*/ static class Record {
tikhomirov@10: 		final int mode;
tikhomirov@10: 		final int size;
tikhomirov@10: 		final int time;
tikhomirov@10: 		final String name1;
tikhomirov@10: 		final String name2;
tikhomirov@10: 
tikhomirov@10: 		public Record(int fmode, int fsize, int ftime, String name1, String name2) {
tikhomirov@10: 			mode = fmode;
tikhomirov@10: 			size = fsize;
tikhomirov@10: 			time = ftime;
tikhomirov@10: 			this.name1 = name1;
tikhomirov@10: 			this.name2 = name2;
tikhomirov@10: 			
tikhomirov@10: 		}
tikhomirov@10: 	}
tikhomirov@10: }