Mercurial > hg4j
view src/com/tmate/hgkit/fs/RepositoryLookup.java @ 35:6061aa826a9e
Complete parsing of the bundle format
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 13 Jan 2011 20:02:21 +0100 |
parents | aa1912c70b36 |
children | e34f90b9ded1 |
line wrap: on
line source
/** * Copyright (c) 2010 Artem Tikhomirov */ package com.tmate.hgkit.fs; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import com.tmate.hgkit.ll.HgRepository; import com.tmate.hgkit.ll.LocalHgRepo; /** * @author artem */ public class RepositoryLookup { public HgRepository detect(Options opts) throws Exception { if (opts.repoLocation != null) { return detect(opts.repoLocation); } return detectFromWorkingDir(); } public HgRepository detect(String[] commandLineArgs) throws Exception { return detect(Options.parse(commandLineArgs)); } 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); } public static class Options { public String repoLocation; public List<String> files; public static Options parse(String[] commandLineArgs) { Options rv = new Options(); List<String> args = Arrays.asList(commandLineArgs); LinkedList<String> files = new LinkedList<String>(); for (Iterator<String> it = args.iterator(); it.hasNext(); ) { String arg = it.next(); if (arg.charAt(0) == '-') { // option if (arg.length() == 1) { throw new IllegalArgumentException("Bad option: -"); } switch ((int) arg.charAt(1)) { case (int) 'R' : { if (! it.hasNext()) { throw new IllegalArgumentException("Need repo location"); } rv.repoLocation = it.next(); break; } } } else { // filename files.add(arg); } } if (!files.isEmpty()) { rv.files = new ArrayList<String>(files); } else { rv.files = Collections.emptyList(); } return rv; } } }