comparison src/org/tmatesoft/hg/core/HgInitCommand.java @ 637:4a0bab2c6da1

HgInitCommand: expose repo init functionality
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 07 Jun 2013 12:32:15 +0200
parents
children
comparison
equal deleted inserted replaced
636:ffce73efa2c2 637:4a0bab2c6da1
1 /*
2 * Copyright (c) 2013 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.core;
18
19 import static org.tmatesoft.hg.internal.RequiresFile.*;
20
21 import java.io.File;
22
23 import org.tmatesoft.hg.internal.RepoInitializer;
24 import org.tmatesoft.hg.repo.HgLookup;
25 import org.tmatesoft.hg.repo.HgRepository;
26 import org.tmatesoft.hg.util.CancelledException;
27
28 /**
29 * Initialize empty local repository.
30 * <p>
31 * Two predefined alternatives are available, {@link #revlogV0() old} and {@link #revlogV1() new} mercurial format respectively.
32 * <p>
33 * Specific requirements may be turned off/on as needed if you know what you're doing.
34 *
35 * @see http://mercurial.selenic.com/wiki/RequiresFile
36 * @author Artem Tikhomirov
37 * @author TMate Software Ltd.
38 */
39 public class HgInitCommand extends HgAbstractCommand<HgInitCommand> {
40 private static final int V1_DEFAULT = REVLOGV1 | STORE | FNCACHE | DOTENCODE;
41
42 private final HgLookup hgLookup;
43 private File location;
44 private int requiresFlags;
45
46 public HgInitCommand() {
47 this(null);
48 }
49
50 public HgInitCommand(HgLookup lookupEnv) {
51 hgLookup = lookupEnv;
52 requiresFlags = V1_DEFAULT;
53 }
54
55 public HgInitCommand location(File repoLoc) {
56 location = repoLoc;
57 return this;
58 }
59
60 public HgInitCommand revlogV0() {
61 requiresFlags = REVLOGV0;
62 return this;
63 }
64
65 public HgInitCommand revlogV1() {
66 requiresFlags = V1_DEFAULT;
67 return this;
68 }
69
70 public HgInitCommand store(boolean enable) {
71 return switchFlag(STORE, enable);
72 }
73
74 public HgInitCommand fncache(boolean enable) {
75 return switchFlag(FNCACHE, enable);
76 }
77
78 public HgInitCommand dotencode(boolean enable) {
79 return switchFlag(DOTENCODE, enable);
80 }
81
82 public HgRepository execute() throws HgRepositoryNotFoundException, HgException, CancelledException {
83 if (location == null) {
84 throw new IllegalArgumentException();
85 }
86 File repoDir;
87 if (".hg".equals(location.getName())) {
88 repoDir = location;
89 } else {
90 repoDir = new File(location, ".hg");
91 }
92 new RepoInitializer().setRequires(requiresFlags).initEmptyRepository(repoDir);
93 return getNewRepository();
94 }
95
96 public HgRepository getNewRepository() throws HgRepositoryNotFoundException {
97 HgLookup l = hgLookup == null ? new HgLookup() : hgLookup;
98 return l.detect(location);
99 }
100
101 private HgInitCommand switchFlag(int flag, boolean enable) {
102 if (enable) {
103 requiresFlags |= flag;
104 } else {
105 requiresFlags &= ~flag;
106 }
107 return this;
108 }
109 }