diff src/com/tmate/hgkit/fs/RepositoryLookup.java @ 2:08db726a0fb7

Shaping out low-level Hg structures
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Sun, 19 Dec 2010 05:41:31 +0100
parents
children aa1912c70b36
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/tmate/hgkit/fs/RepositoryLookup.java	Sun Dec 19 05:41:31 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 RepositoryLookup {
+	
+	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);
+	}
+}