tikhomirov@2: /** tikhomirov@2: * Copyright (c) 2010 Artem Tikhomirov tikhomirov@2: */ tikhomirov@2: package com.tmate.hgkit.fs; tikhomirov@2: tikhomirov@2: import java.io.File; tikhomirov@4: import java.util.ArrayList; tikhomirov@4: import java.util.Arrays; tikhomirov@4: import java.util.Collections; tikhomirov@4: import java.util.Iterator; tikhomirov@4: import java.util.LinkedList; tikhomirov@4: import java.util.List; tikhomirov@2: tikhomirov@2: import com.tmate.hgkit.ll.HgRepository; tikhomirov@2: import com.tmate.hgkit.ll.LocalHgRepo; tikhomirov@2: tikhomirov@2: /** tikhomirov@2: * @author artem tikhomirov@2: */ tikhomirov@2: public class RepositoryLookup { tikhomirov@4: tikhomirov@4: public HgRepository detect(Options opts) throws Exception { tikhomirov@4: if (opts.repoLocation != null) { tikhomirov@4: return detect(opts.repoLocation); tikhomirov@4: } tikhomirov@4: return detectFromWorkingDir(); tikhomirov@4: } tikhomirov@4: tikhomirov@2: public HgRepository detect(String[] commandLineArgs) throws Exception { tikhomirov@4: return detect(Options.parse(commandLineArgs)); tikhomirov@2: } tikhomirov@2: tikhomirov@2: public HgRepository detectFromWorkingDir() throws Exception { tikhomirov@2: return detect(System.getProperty("user.dir")); tikhomirov@2: } tikhomirov@2: tikhomirov@2: public HgRepository detect(String location) throws Exception /*FIXME Exception type, RepoInitException? */ { tikhomirov@2: File dir = new File(location); tikhomirov@2: File repository; tikhomirov@2: do { tikhomirov@2: repository = new File(dir, ".hg"); tikhomirov@2: if (repository.exists() && repository.isDirectory()) { tikhomirov@2: break; tikhomirov@2: } tikhomirov@2: repository = null; tikhomirov@2: dir = dir.getParentFile(); tikhomirov@2: tikhomirov@2: } while(dir != null); tikhomirov@2: if (repository == null) { tikhomirov@2: return new LocalHgRepo(location); tikhomirov@2: } tikhomirov@2: return new LocalHgRepo(repository); tikhomirov@2: } tikhomirov@4: tikhomirov@4: public static class Options { tikhomirov@4: tikhomirov@4: public String repoLocation; tikhomirov@4: public List files; tikhomirov@48: public int limit = -1; tikhomirov@4: tikhomirov@4: public static Options parse(String[] commandLineArgs) { tikhomirov@4: Options rv = new Options(); tikhomirov@4: List args = Arrays.asList(commandLineArgs); tikhomirov@4: LinkedList files = new LinkedList(); tikhomirov@4: for (Iterator it = args.iterator(); it.hasNext(); ) { tikhomirov@4: String arg = it.next(); tikhomirov@4: if (arg.charAt(0) == '-') { tikhomirov@4: // option tikhomirov@4: if (arg.length() == 1) { tikhomirov@4: throw new IllegalArgumentException("Bad option: -"); tikhomirov@4: } tikhomirov@4: switch ((int) arg.charAt(1)) { tikhomirov@4: case (int) 'R' : { tikhomirov@4: if (! it.hasNext()) { tikhomirov@4: throw new IllegalArgumentException("Need repo location"); tikhomirov@4: } tikhomirov@4: rv.repoLocation = it.next(); tikhomirov@4: break; tikhomirov@4: } tikhomirov@48: case (int) 'l' : { tikhomirov@48: if (!it.hasNext()) { tikhomirov@48: throw new IllegalArgumentException(); tikhomirov@48: } tikhomirov@48: rv.limit = Integer.parseInt(it.next()); tikhomirov@48: break; tikhomirov@48: } tikhomirov@4: } tikhomirov@4: } else { tikhomirov@4: // filename tikhomirov@4: files.add(arg); tikhomirov@4: } tikhomirov@4: } tikhomirov@4: if (!files.isEmpty()) { tikhomirov@4: rv.files = new ArrayList(files); tikhomirov@4: } else { tikhomirov@4: rv.files = Collections.emptyList(); tikhomirov@4: } tikhomirov@4: return rv; tikhomirov@4: } tikhomirov@4: } tikhomirov@2: }