# HG changeset patch # User Artem Tikhomirov # Date 1292647655 -3600 # Node ID a3576694a4d1edaa681cab15b89d6b556b02aff4 # Parent dbd663faec1f0175619cf7668bddc6350548b8d6 Repository detection from local/specified directory diff -r dbd663faec1f -r a3576694a4d1 design.txt --- /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 + +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 diff -r dbd663faec1f -r a3576694a4d1 src/com/tmate/hgkit/console/Cat.java --- /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; + } + } +} diff -r dbd663faec1f -r a3576694a4d1 src/com/tmate/hgkit/console/Log.java --- /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); + + } + +} diff -r dbd663faec1f -r a3576694a4d1 src/com/tmate/hgkit/fs/RepositoryFinder.java --- /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); + } +} diff -r dbd663faec1f -r a3576694a4d1 src/com/tmate/hgkit/fs/package.html --- /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 @@ + + +File System operations + + \ No newline at end of file diff -r dbd663faec1f -r a3576694a4d1 src/com/tmate/hgkit/ll/HgRepository.java --- 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(); } diff -r dbd663faec1f -r a3576694a4d1 src/com/tmate/hgkit/ll/LocalHgRepo.java --- /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; + } +} diff -r dbd663faec1f -r a3576694a4d1 src/com/tmate/hgkit/ll/package.html --- /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 @@ + + +Low-level API operations + + \ No newline at end of file