tikhomirov@637: /*
tikhomirov@637: * Copyright (c) 2013 TMate Software Ltd
tikhomirov@637: *
tikhomirov@637: * This program is free software; you can redistribute it and/or modify
tikhomirov@637: * it under the terms of the GNU General Public License as published by
tikhomirov@637: * the Free Software Foundation; version 2 of the License.
tikhomirov@637: *
tikhomirov@637: * This program is distributed in the hope that it will be useful,
tikhomirov@637: * but WITHOUT ANY WARRANTY; without even the implied warranty of
tikhomirov@637: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tikhomirov@637: * GNU General Public License for more details.
tikhomirov@637: *
tikhomirov@637: * For information on how to redistribute this software under
tikhomirov@637: * the terms of a license other than GNU General Public License
tikhomirov@637: * contact TMate Software at support@hg4j.com
tikhomirov@637: */
tikhomirov@637: package org.tmatesoft.hg.core;
tikhomirov@637:
tikhomirov@637: import static org.tmatesoft.hg.internal.RequiresFile.*;
tikhomirov@637:
tikhomirov@637: import java.io.File;
tikhomirov@637:
tikhomirov@637: import org.tmatesoft.hg.internal.RepoInitializer;
tikhomirov@637: import org.tmatesoft.hg.repo.HgLookup;
tikhomirov@637: import org.tmatesoft.hg.repo.HgRepository;
tikhomirov@637: import org.tmatesoft.hg.util.CancelledException;
tikhomirov@637:
tikhomirov@637: /**
tikhomirov@637: * Initialize empty local repository.
tikhomirov@637: *
tikhomirov@637: * Two predefined alternatives are available, {@link #revlogV0() old} and {@link #revlogV1() new} mercurial format respectively.
tikhomirov@637: *
tikhomirov@637: * Specific requirements may be turned off/on as needed if you know what you're doing.
tikhomirov@637: *
tikhomirov@637: * @see http://mercurial.selenic.com/wiki/RequiresFile
tikhomirov@637: * @author Artem Tikhomirov
tikhomirov@637: * @author TMate Software Ltd.
tikhomirov@637: */
tikhomirov@637: public class HgInitCommand extends HgAbstractCommand {
tikhomirov@637: private static final int V1_DEFAULT = REVLOGV1 | STORE | FNCACHE | DOTENCODE;
tikhomirov@637:
tikhomirov@637: private final HgLookup hgLookup;
tikhomirov@637: private File location;
tikhomirov@637: private int requiresFlags;
tikhomirov@637:
tikhomirov@637: public HgInitCommand() {
tikhomirov@637: this(null);
tikhomirov@637: }
tikhomirov@637:
tikhomirov@637: public HgInitCommand(HgLookup lookupEnv) {
tikhomirov@637: hgLookup = lookupEnv;
tikhomirov@637: requiresFlags = V1_DEFAULT;
tikhomirov@637: }
tikhomirov@637:
tikhomirov@637: public HgInitCommand location(File repoLoc) {
tikhomirov@637: location = repoLoc;
tikhomirov@637: return this;
tikhomirov@637: }
tikhomirov@637:
tikhomirov@637: public HgInitCommand revlogV0() {
tikhomirov@637: requiresFlags = REVLOGV0;
tikhomirov@637: return this;
tikhomirov@637: }
tikhomirov@637:
tikhomirov@637: public HgInitCommand revlogV1() {
tikhomirov@637: requiresFlags = V1_DEFAULT;
tikhomirov@637: return this;
tikhomirov@637: }
tikhomirov@637:
tikhomirov@637: public HgInitCommand store(boolean enable) {
tikhomirov@637: return switchFlag(STORE, enable);
tikhomirov@637: }
tikhomirov@637:
tikhomirov@637: public HgInitCommand fncache(boolean enable) {
tikhomirov@637: return switchFlag(FNCACHE, enable);
tikhomirov@637: }
tikhomirov@637:
tikhomirov@637: public HgInitCommand dotencode(boolean enable) {
tikhomirov@637: return switchFlag(DOTENCODE, enable);
tikhomirov@637: }
tikhomirov@637:
tikhomirov@637: public HgRepository execute() throws HgRepositoryNotFoundException, HgException, CancelledException {
tikhomirov@637: if (location == null) {
tikhomirov@637: throw new IllegalArgumentException();
tikhomirov@637: }
tikhomirov@637: File repoDir;
tikhomirov@637: if (".hg".equals(location.getName())) {
tikhomirov@637: repoDir = location;
tikhomirov@637: } else {
tikhomirov@637: repoDir = new File(location, ".hg");
tikhomirov@637: }
tikhomirov@637: new RepoInitializer().setRequires(requiresFlags).initEmptyRepository(repoDir);
tikhomirov@637: return getNewRepository();
tikhomirov@637: }
tikhomirov@637:
tikhomirov@637: public HgRepository getNewRepository() throws HgRepositoryNotFoundException {
tikhomirov@637: HgLookup l = hgLookup == null ? new HgLookup() : hgLookup;
tikhomirov@637: return l.detect(location);
tikhomirov@637: }
tikhomirov@637:
tikhomirov@637: private HgInitCommand switchFlag(int flag, boolean enable) {
tikhomirov@637: if (enable) {
tikhomirov@637: requiresFlags |= flag;
tikhomirov@637: } else {
tikhomirov@637: requiresFlags &= ~flag;
tikhomirov@637: }
tikhomirov@637: return this;
tikhomirov@637: }
tikhomirov@637: }