1 /* 2 * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com> 3 * and other copyright owners as documented in the project's IP log. 4 * 5 * This program and the accompanying materials are made available 6 * under the terms of the Eclipse Distribution License v1.0 which 7 * accompanies this distribution, is reproduced below, and is 8 * available at http://www.eclipse.org/org/documents/edl-v10.php 9 * 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or 13 * without modification, are permitted provided that the following 14 * conditions are met: 15 * 16 * - Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * - Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials provided 22 * with the distribution. 23 * 24 * - Neither the name of the Eclipse Foundation, Inc. nor the 25 * names of its contributors may be used to endorse or promote 26 * products derived from this software without specific prior 27 * written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 42 */ 43 package org.eclipse.jgit.api; 44 45 import java.io.File; 46 import java.io.IOException; 47 import java.text.MessageFormat; 48 import java.util.concurrent.Callable; 49 50 import org.eclipse.jgit.api.errors.GitAPIException; 51 import org.eclipse.jgit.api.errors.JGitInternalException; 52 import org.eclipse.jgit.internal.JGitText; 53 import org.eclipse.jgit.lib.Constants; 54 import org.eclipse.jgit.lib.Repository; 55 import org.eclipse.jgit.lib.RepositoryBuilder; 56 import org.eclipse.jgit.util.SystemReader; 57 58 /** 59 * Create an empty git repository or reinitalize an existing one 60 * 61 * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-init.html" 62 * >Git documentation about init</a> 63 */ 64 public class InitCommand implements Callable<Git> { 65 private File directory; 66 67 private File gitDir; 68 69 private boolean bare; 70 71 /** 72 * Executes the {@code Init} command. 73 * 74 * @return the newly created {@code Git} object with associated repository 75 */ 76 public Git call() throws GitAPIException { 77 try { 78 RepositoryBuilder builder = new RepositoryBuilder(); 79 if (bare) 80 builder.setBare(); 81 builder.readEnvironment(); 82 if (gitDir != null) 83 builder.setGitDir(gitDir); 84 else 85 gitDir = builder.getGitDir(); 86 if (directory != null) { 87 if (bare) 88 builder.setGitDir(directory); 89 else { 90 builder.setWorkTree(directory); 91 if (gitDir == null) 92 builder.setGitDir(new File(directory, Constants.DOT_GIT)); 93 } 94 } else if (builder.getGitDir() == null) { 95 String dStr = SystemReader.getInstance() 96 .getProperty("user.dir"); //$NON-NLS-1$ 97 if (dStr == null) 98 dStr = "."; //$NON-NLS-1$ 99 File d = new File(dStr); 100 if (!bare) 101 d = new File(d, Constants.DOT_GIT); 102 builder.setGitDir(d); 103 } else { 104 // directory was not set but gitDir was set 105 if (!bare) { 106 String dStr = SystemReader.getInstance().getProperty( 107 "user.dir"); //$NON-NLS-1$ 108 if (dStr == null) 109 dStr = "."; //$NON-NLS-1$ 110 builder.setWorkTree(new File(dStr)); 111 } 112 } 113 Repository repository = builder.build(); 114 if (!repository.getObjectDatabase().exists()) 115 repository.create(bare); 116 return new Git(repository); 117 } catch (IOException e) { 118 throw new JGitInternalException(e.getMessage(), e); 119 } 120 } 121 122 /** 123 * The optional directory associated with the init operation. If no 124 * directory is set, we'll use the current directory 125 * 126 * @param directory 127 * the directory to init to 128 * @return this instance 129 * @throws IllegalStateException 130 * if the combination of directory, gitDir and bare is illegal. 131 * E.g. if for a non-bare repository directory and gitDir point 132 * to the same directory of if for a bare repository both 133 * directory and gitDir are specified 134 */ 135 public InitCommand setDirectory(File directory) 136 throws IllegalStateException { 137 validateDirs(directory, gitDir, bare); 138 this.directory = directory; 139 return this; 140 } 141 142 /** 143 * @param gitDir 144 * the repository meta directory 145 * @return this instance 146 * @throws IllegalStateException 147 * if the combination of directory, gitDir and bare is illegal. 148 * E.g. if for a non-bare repository directory and gitDir point 149 * to the same directory of if for a bare repository both 150 * directory and gitDir are specified 151 * @since 3.6 152 */ 153 public InitCommand setGitDir(File gitDir) 154 throws IllegalStateException { 155 validateDirs(directory, gitDir, bare); 156 this.gitDir = gitDir; 157 return this; 158 } 159 160 private static void validateDirs(File directory, File gitDir, boolean bare) 161 throws IllegalStateException { 162 if (directory != null) { 163 if (bare) { 164 if (gitDir != null && !gitDir.equals(directory)) 165 throw new IllegalStateException(MessageFormat.format( 166 JGitText.get().initFailedBareRepoDifferentDirs, 167 gitDir, directory)); 168 } else { 169 if (gitDir != null && gitDir.equals(directory)) 170 throw new IllegalStateException(MessageFormat.format( 171 JGitText.get().initFailedNonBareRepoSameDirs, 172 gitDir, directory)); 173 } 174 } 175 } 176 177 /** 178 * @param bare 179 * whether the repository is bare or not 180 * @throws IllegalStateException 181 * if the combination of directory, gitDir and bare is illegal. 182 * E.g. if for a non-bare repository directory and gitDir point 183 * to the same directory of if for a bare repository both 184 * directory and gitDir are specified 185 * @return this instance 186 */ 187 public InitCommand setBare(boolean bare) { 188 validateDirs(directory, gitDir, bare); 189 this.bare = bare; 190 return this; 191 } 192 }