tikhomirov@74: /* tikhomirov@74: * Copyright (c) 2011 TMate Software Ltd tikhomirov@74: * tikhomirov@74: * This program is free software; you can redistribute it and/or modify tikhomirov@74: * it under the terms of the GNU General Public License as published by tikhomirov@74: * the Free Software Foundation; version 2 of the License. tikhomirov@74: * tikhomirov@74: * This program is distributed in the hope that it will be useful, tikhomirov@74: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@74: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@74: * GNU General Public License for more details. tikhomirov@74: * tikhomirov@74: * For information on how to redistribute this software under tikhomirov@74: * the terms of a license other than GNU General Public License tikhomirov@102: * contact TMate Software at support@hg4j.com tikhomirov@74: */ tikhomirov@74: package org.tmatesoft.hg.core; tikhomirov@74: tikhomirov@74: import java.io.File; tikhomirov@74: tikhomirov@74: import org.tmatesoft.hg.repo.HgRepository; tikhomirov@95: import org.tmatesoft.hg.repo.HgLookup; tikhomirov@74: tikhomirov@74: /** tikhomirov@131: * Starting point for the library. tikhomirov@148: *
Sample use: tikhomirov@148: *
tikhomirov@148: * HgRepoFacade f = new HgRepoFacade(); tikhomirov@148: * f.initFrom(System.getenv("whatever.repo.location")); tikhomirov@148: * HgStatusCommand statusCmd = f.createStatusCommand(); tikhomirov@148: * HgStatusCommand.Handler handler = ...; tikhomirov@148: * statusCmd.execute(handler); tikhomirov@148: *tikhomirov@131: * tikhomirov@74: * @author Artem Tikhomirov tikhomirov@74: * @author TMate Software Ltd. tikhomirov@74: */ tikhomirov@131: public class HgRepoFacade { tikhomirov@74: private HgRepository repo; tikhomirov@74: tikhomirov@131: public HgRepoFacade() { tikhomirov@74: } tikhomirov@143: tikhomirov@148: /** tikhomirov@148: * @param hgRepo tikhomirov@148: * @return true on successful initialization tikhomirov@148: * @throws IllegalArgumentException when argument is null tikhomirov@148: */ tikhomirov@143: public boolean init(HgRepository hgRepo) { tikhomirov@143: if (hgRepo == null) { tikhomirov@143: throw new IllegalArgumentException(); tikhomirov@143: } tikhomirov@143: repo = hgRepo; tikhomirov@143: return !repo.isInvalid(); tikhomirov@143: } tikhomirov@74: tikhomirov@148: /** tikhomirov@148: * Tries to find repository starting from the current working directory. tikhomirov@148: * @return
true
if found valid repository
tikhomirov@148: * @throws HgException in case of errors during repository initialization
tikhomirov@148: */
tikhomirov@148: public boolean init() throws HgException {
tikhomirov@95: repo = new HgLookup().detectFromWorkingDir();
tikhomirov@74: return repo != null && !repo.isInvalid();
tikhomirov@74: }
tikhomirov@74:
tikhomirov@148: /**
tikhomirov@148: * Looks up Mercurial repository starting from specified location and up to filesystem root.
tikhomirov@148: *
tikhomirov@148: * @param repoLocation path to any folder within structure of a Mercurial repository.
tikhomirov@148: * @return true
if found valid repository
tikhomirov@148: * @throws HgException if there are errors accessing specified location
tikhomirov@148: * @throws IllegalArgumentException if argument is null
tikhomirov@148: */
tikhomirov@148: public boolean initFrom(File repoLocation) throws HgException {
tikhomirov@148: if (repoLocation == null) {
tikhomirov@148: throw new IllegalArgumentException();
tikhomirov@148: }
tikhomirov@148: repo = new HgLookup().detect(repoLocation);
tikhomirov@74: return repo != null && !repo.isInvalid();
tikhomirov@74: }
tikhomirov@115:
tikhomirov@115: public HgRepository getRepository() {
tikhomirov@115: if (repo == null) {
tikhomirov@115: throw new IllegalStateException("Call any of #init*() methods first first");
tikhomirov@115: }
tikhomirov@115: return repo;
tikhomirov@115: }
tikhomirov@74:
tikhomirov@131: public HgLogCommand createLogCommand() {
tikhomirov@131: return new HgLogCommand(repo/*, getCommandContext()*/);
tikhomirov@74: }
tikhomirov@108:
tikhomirov@131: public HgStatusCommand createStatusCommand() {
tikhomirov@131: return new HgStatusCommand(repo/*, getCommandContext()*/);
tikhomirov@108: }
tikhomirov@108:
tikhomirov@131: public HgCatCommand createCatCommand() {
tikhomirov@131: return new HgCatCommand(repo);
tikhomirov@131: }
tikhomirov@131:
tikhomirov@131: public HgManifestCommand createManifestCommand() {
tikhomirov@131: return new HgManifestCommand(repo);
tikhomirov@108: }
tikhomirov@74: }