tikhomirov@1: /** tikhomirov@1: * Copyright (c) 2010 Artem Tikhomirov tikhomirov@1: */ tikhomirov@1: package com.tmate.hgkit.fs; tikhomirov@1: tikhomirov@1: import java.io.File; tikhomirov@1: tikhomirov@1: import com.tmate.hgkit.ll.HgRepository; tikhomirov@1: import com.tmate.hgkit.ll.LocalHgRepo; tikhomirov@1: tikhomirov@1: /** tikhomirov@1: * @author artem tikhomirov@1: */ tikhomirov@1: public class RepositoryFinder { tikhomirov@1: tikhomirov@1: public HgRepository detect(String[] commandLineArgs) throws Exception { tikhomirov@1: if (commandLineArgs.length == 0) { tikhomirov@1: return detectFromWorkingDir(); tikhomirov@1: } tikhomirov@1: return detect(commandLineArgs[0]); tikhomirov@1: } tikhomirov@1: tikhomirov@1: public HgRepository detectFromWorkingDir() throws Exception { tikhomirov@1: return detect(System.getProperty("user.dir")); tikhomirov@1: } tikhomirov@1: tikhomirov@1: public HgRepository detect(String location) throws Exception /*FIXME Exception type, RepoInitException? */ { tikhomirov@1: File dir = new File(location); tikhomirov@1: File repository; tikhomirov@1: do { tikhomirov@1: repository = new File(dir, ".hg"); tikhomirov@1: if (repository.exists() && repository.isDirectory()) { tikhomirov@1: break; tikhomirov@1: } tikhomirov@1: repository = null; tikhomirov@1: dir = dir.getParentFile(); tikhomirov@1: tikhomirov@1: } while(dir != null); tikhomirov@1: if (repository == null) { tikhomirov@1: return new LocalHgRepo(location); tikhomirov@1: } tikhomirov@1: return new LocalHgRepo(repository); tikhomirov@1: } tikhomirov@1: }