changeset 1:a3576694a4d1

Repository detection from local/specified directory
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Sat, 18 Dec 2010 05:47:35 +0100
parents dbd663faec1f
children 08db726a0fb7
files design.txt src/com/tmate/hgkit/console/Cat.java src/com/tmate/hgkit/console/Log.java src/com/tmate/hgkit/fs/RepositoryFinder.java src/com/tmate/hgkit/fs/package.html src/com/tmate/hgkit/ll/HgRepository.java src/com/tmate/hgkit/ll/LocalHgRepo.java src/com/tmate/hgkit/ll/package.html
diffstat 8 files changed, 159 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/design.txt	Sat Dec 18 05:47:35 2010 +0100
@@ -0,0 +1,10 @@
+FileStructureWalker (pass HgFile, HgFolder to callable; which can ask for VCS data from any file)
+External uses: user browses files, selects one and asks for its history 
+Params: tip/revision; 
+Implementation: manifest
+
+Log --rev
+Log <file>
+
+hg cat
+Implementation: logic to find file by name in the repository is the same with Log and other commands 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/tmate/hgkit/console/Cat.java	Sat Dec 18 05:47:35 2010 +0100
@@ -0,0 +1,23 @@
+/**
+ * Copyright (c) 2010 Artem Tikhomirov 
+ */
+package com.tmate.hgkit.console;
+
+import com.tmate.hgkit.fs.RepositoryFinder;
+import com.tmate.hgkit.ll.HgRepository;
+
+/**
+ * @author artem
+ *
+ */
+public class Cat {
+
+	public static void main(String[] args) throws Exception {
+		RepositoryFinder repoLookup = new RepositoryFinder();
+		HgRepository hgRepo = repoLookup.detect(args);
+		if (hgRepo.isInvalid()) {
+			System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation());
+			return;
+		}
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/tmate/hgkit/console/Log.java	Sat Dec 18 05:47:35 2010 +0100
@@ -0,0 +1,26 @@
+/**
+ * Copyright (c) 2010 Artem Tikhomirov 
+ */
+package com.tmate.hgkit.console;
+
+import com.tmate.hgkit.fs.RepositoryFinder;
+import com.tmate.hgkit.ll.HgRepository;
+
+/**
+ * @author artem
+ */
+public class Log {
+
+	public static void main(String[] args) throws Exception {
+		RepositoryFinder repoLookup = new RepositoryFinder();
+		HgRepository hgRepo = repoLookup.detect(args);
+		if (hgRepo.isInvalid()) {
+			System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation());
+			return;
+		}
+		System.out.println(hgRepo.getLocation());
+		//new ChangelogWalker().setFile("hello.c").setRevisionRange(1, 4).accept(new Visitor);
+
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/tmate/hgkit/fs/RepositoryFinder.java	Sat Dec 18 05:47:35 2010 +0100
@@ -0,0 +1,44 @@
+/**
+ * Copyright (c) 2010 Artem Tikhomirov 
+ */
+package com.tmate.hgkit.fs;
+
+import java.io.File;
+
+import com.tmate.hgkit.ll.HgRepository;
+import com.tmate.hgkit.ll.LocalHgRepo;
+
+/**
+ * @author artem
+ */
+public class RepositoryFinder {
+	
+	public HgRepository detect(String[] commandLineArgs) throws Exception {
+		if (commandLineArgs.length == 0) {
+			return detectFromWorkingDir();
+		}
+		return detect(commandLineArgs[0]);
+	}
+
+	public HgRepository detectFromWorkingDir() throws Exception {
+		return detect(System.getProperty("user.dir"));
+	}
+
+	public HgRepository detect(String location) throws Exception /*FIXME Exception type, RepoInitException? */ {
+		File dir = new File(location);
+		File repository;
+		do {
+			repository = new File(dir, ".hg");
+			if (repository.exists() && repository.isDirectory()) {
+				break;
+			}
+			repository = null;
+			dir = dir.getParentFile();
+			
+		} while(dir != null);
+		if (repository == null) {
+			return new LocalHgRepo(location);
+		}
+		return new LocalHgRepo(repository);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/tmate/hgkit/fs/package.html	Sat Dec 18 05:47:35 2010 +0100
@@ -0,0 +1,5 @@
+<html>
+<boody>
+File System operations
+</bidy>
+</html>
\ No newline at end of file
--- a/src/com/tmate/hgkit/ll/HgRepository.java	Fri Dec 17 19:05:59 2010 +0100
+++ b/src/com/tmate/hgkit/ll/HgRepository.java	Sat Dec 18 05:47:35 2010 +0100
@@ -3,14 +3,24 @@
  */
 package com.tmate.hgkit.ll;
 
+
 /**
  * @author artem
  *
  */
-public class HgRepository {
+public abstract class HgRepository {
 
 	
 	private Changelog changelog;
+	private boolean isInvalid = true;
+	
+	public boolean isInvalid() {
+		return this.isInvalid;
+	}
+	
+	protected void setInvalid(boolean invalid) {
+		isInvalid = invalid;
+	}
 
 	public void log() {
 		Changelog clog = getChangelog();
@@ -28,4 +38,6 @@
 		}
 		return this.changelog;
 	}
+
+	public abstract String getLocation();
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/tmate/hgkit/ll/LocalHgRepo.java	Sat Dec 18 05:47:35 2010 +0100
@@ -0,0 +1,33 @@
+/**
+ * Copyright (c) 2010 Artem Tikhomirov 
+ */
+package com.tmate.hgkit.ll;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * @author artem
+ */
+public class LocalHgRepo extends HgRepository {
+
+	private File repoDir;
+	private final String repoLocation;
+
+	public LocalHgRepo(String repositoryPath) {
+		setInvalid(true);
+		repoLocation = repositoryPath;
+	}
+	
+	public LocalHgRepo(File repositoryRoot) throws IOException {
+		assert ".hg".equals(repositoryRoot.getName()) && repositoryRoot.isDirectory();
+		setInvalid(false);
+		repoDir = repositoryRoot;
+		repoLocation = repositoryRoot.getParentFile().getCanonicalPath();
+	}
+
+	@Override
+	public String getLocation() {
+		return repoLocation;
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/tmate/hgkit/ll/package.html	Sat Dec 18 05:47:35 2010 +0100
@@ -0,0 +1,5 @@
+<html>
+<boody>
+Low-level API operations
+</bidy>
+</html>
\ No newline at end of file