tikhomirov@74: /* tikhomirov@566: * Copyright (c) 2011-2013 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@295: import org.tmatesoft.hg.internal.BasicSessionContext; 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@571: public class HgRepoFacade implements SessionContext.Source { tikhomirov@74: private HgRepository repo; tikhomirov@295: private final SessionContext context; tikhomirov@74: tikhomirov@131: public HgRepoFacade() { tikhomirov@431: this(new BasicSessionContext(null)); tikhomirov@295: } tikhomirov@295: tikhomirov@295: public HgRepoFacade(SessionContext ctx) { tikhomirov@295: if (ctx == null) { tikhomirov@295: throw new IllegalArgumentException(); tikhomirov@295: } tikhomirov@295: context = ctx; 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@423: * tikhomirov@148: * @return
true
if found valid repository
tikhomirov@423: * @throws HgRepositoryNotFoundException if no repository found in working directory
tikhomirov@148: */
tikhomirov@423: public boolean init() throws HgRepositoryNotFoundException {
tikhomirov@295: repo = new HgLookup(context).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@423: * @throws HgRepositoryNotFoundException if there's no repository at specified location
tikhomirov@148: * @throws IllegalArgumentException if argument is null
tikhomirov@148: */
tikhomirov@423: public boolean initFrom(File repoLocation) throws HgRepositoryNotFoundException {
tikhomirov@148: if (repoLocation == null) {
tikhomirov@148: throw new IllegalArgumentException();
tikhomirov@148: }
tikhomirov@295: repo = new HgLookup(context).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@571:
tikhomirov@571: public SessionContext getSessionContext() {
tikhomirov@571: return context;
tikhomirov@571: }
tikhomirov@637:
tikhomirov@637: /**
tikhomirov@637: * This factory method doesn't need this facade to be initialized with a repository.
tikhomirov@637: * @return command instance, never null
tikhomirov@637: */
tikhomirov@637: public HgInitCommand createInitCommand() {
tikhomirov@637: return new HgInitCommand(new HgLookup(context));
tikhomirov@637: }
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@181:
tikhomirov@181: public HgOutgoingCommand createOutgoingCommand() {
tikhomirov@181: return new HgOutgoingCommand(repo);
tikhomirov@181: }
tikhomirov@181:
tikhomirov@181: public HgIncomingCommand createIncomingCommand() {
tikhomirov@181: return new HgIncomingCommand(repo);
tikhomirov@181: }
tikhomirov@566:
tikhomirov@586: public HgCloneCommand createCloneCommand() {
tikhomirov@586: return new HgCloneCommand();
tikhomirov@586: }
tikhomirov@586:
tikhomirov@586: public HgUpdateConfigCommand createUpdateRepositoryConfigCommand() {
tikhomirov@586: return HgUpdateConfigCommand.forRepository(repo);
tikhomirov@586: }
tikhomirov@586:
tikhomirov@586: public HgAddRemoveCommand createAddRemoveCommand() {
tikhomirov@586: return new HgAddRemoveCommand(repo);
tikhomirov@586: }
tikhomirov@586:
tikhomirov@586: public HgCheckoutCommand createCheckoutCommand() {
tikhomirov@586: return new HgCheckoutCommand(repo);
tikhomirov@586: }
tikhomirov@586:
tikhomirov@586: public HgRevertCommand createRevertCommand() {
tikhomirov@586: return new HgRevertCommand(repo);
tikhomirov@586: }
tikhomirov@566:
tikhomirov@566: public HgAnnotateCommand createAnnotateCommand() {
tikhomirov@566: return new HgAnnotateCommand(repo);
tikhomirov@566: }
tikhomirov@586:
tikhomirov@586: public HgCommitCommand createCommitCommand() {
tikhomirov@586: return new HgCommitCommand(repo);
tikhomirov@586: }
tikhomirov@630:
tikhomirov@630: public HgDiffCommand createDiffCommand() {
tikhomirov@630: return new HgDiffCommand(repo);
tikhomirov@630: }
tikhomirov@654:
tikhomirov@654: public HgPushCommand createPushCommand() {
tikhomirov@654: return new HgPushCommand(repo);
tikhomirov@654: }
tikhomirov@660:
tikhomirov@660: public HgPullCommand createPullCommand() {
tikhomirov@660: return new HgPullCommand(repo);
tikhomirov@660: }
tikhomirov@704:
tikhomirov@704: public HgMergeCommand createMergeCommand() {
tikhomirov@704: return new HgMergeCommand(repo);
tikhomirov@704: }
tikhomirov@74: }