changeset 107:7ec89d637f50

CatCommand
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Sat, 29 Jan 2011 02:16:01 +0100
parents 5c43e937e15b
children 0c9804857000
files TODO src/org/tmatesoft/hg/core/CatCommand.java src/org/tmatesoft/hg/core/LogCommand.java
diffstat 3 files changed, 95 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/TODO	Sat Jan 29 01:58:32 2011 +0100
+++ b/TODO	Sat Jan 29 02:16:01 2011 +0100
@@ -19,12 +19,17 @@
   - matchers
 
 * hg cat
+  + CatCommand. File, revision. 
+  - Cat command line client
 
-* hgignore
++ hgignore
   + glob
   + pattern
 
-* Tests with JUnit 
++ Tests with JUnit 
+
+* tags
+  * Tags are read and can be queried (cmdline Log does)
 
 Proposed:
 - LogCommand.revision(int... rev)+ to walk selected revisions only (list->sort(array) on execute, binary search)
@@ -32,7 +37,7 @@
 - LogCommand.match() to specify pattern, no selected file()s only?
 * RepositoryFacade and CommandContext  
 - hgignore: read extra ignore files from config file (ui.ignore)
-- tags
+
 
 
 Read-only support, version 1.1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/tmatesoft/hg/core/CatCommand.java	Sat Jan 29 02:16:01 2011 +0100
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2011 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@svnkit.com
+ */
+package org.tmatesoft.hg.core;
+
+import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION;
+import static org.tmatesoft.hg.repo.HgRepository.TIP;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.tmatesoft.hg.repo.HgDataFile;
+import org.tmatesoft.hg.repo.HgRepository;
+
+/**
+ * Command to obtain content of a 
+ * 
+ * @author Artem Tikhomirov
+ * @author TMate Software Ltd.
+ */
+public class CatCommand {
+
+	private final HgRepository repo;
+	private Path file;
+	private int localRevision = TIP;
+	private Nodeid revision;
+
+	public CatCommand(HgRepository hgRepo) {
+		repo = hgRepo;
+	}
+
+	public CatCommand file(Path fname) {
+		file = fname;
+		return this;
+	}
+
+	// rev can't be WORKING_COPY (if allowed, need to implement in #execute())
+	public CatCommand revision(int rev) {
+		localRevision = rev;
+		revision = null;
+		return this;
+	}
+	
+	public CatCommand revision(Nodeid nodeid) {
+		revision = nodeid;
+		localRevision = BAD_REVISION;
+		return this;
+	}
+
+	public void execute(OutputStream os) throws IOException /*TODO own exception type*/ {
+		if (localRevision == BAD_REVISION && revision == null) {
+			throw new IllegalArgumentException("Either local file revision number or nodeid shall be specified");
+		}
+		if (file == null) {
+			throw new IllegalArgumentException("Name of the file is missing");
+		}
+		if (os == null) {
+			throw new IllegalArgumentException();
+		}
+		HgDataFile dataFile = repo.getFileNode(file);
+		if (!dataFile.exists()) {
+			throw new FileNotFoundException();
+		}
+		byte[] content;
+		if (revision != null) {
+			content = dataFile.content(revision);
+		} else {
+			content = dataFile.content(localRevision);
+		}
+		os.write(content);
+	}
+}
--- a/src/org/tmatesoft/hg/core/LogCommand.java	Sat Jan 29 01:58:32 2011 +0100
+++ b/src/org/tmatesoft/hg/core/LogCommand.java	Sat Jan 29 02:16:01 2011 +0100
@@ -56,7 +56,7 @@
 	private Cset changeset;
 	
 	public LogCommand(HgRepository hgRepo) {
-		this.repo = hgRepo;
+		repo = hgRepo;
 	}
 
 	/**