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 @Override 77 public Git call() throws GitAPIException { 78 try { 79 RepositoryBuilder builder = new RepositoryBuilder(); 80 if (bare) 81 builder.setBare(); 82 builder.readEnvironment(); 83 if (gitDir != null) 84 builder.setGitDir(gitDir); 85 else 86 gitDir = builder.getGitDir(); 87 if (directory != null) { 88 if (bare) 89 builder.setGitDir(directory); 90 else { 91 builder.setWorkTree(directory); 92 if (gitDir == null) 93 builder.setGitDir(new File(directory, Constants.DOT_GIT)); 94 } 95 } else if (builder.getGitDir() == null) { 96 String dStr = SystemReader.getInstance() 97 .getProperty("user.dir"); //$NON-NLS-1$ 98 if (dStr == null) 99 dStr = "."; //$NON-NLS-1$ 100 File d = new File(dStr); 101 if (!bare) 102 d = new File(d, Constants.DOT_GIT); 103 builder.setGitDir(d); 104 } else { 105 // directory was not set but gitDir was set 106 if (!bare) { 107 String dStr = SystemReader.getInstance().getProperty( 108 "user.dir"); //$NON-NLS-1$ 109 if (dStr == null) 110 dStr = "."; //$NON-NLS-1$ 111 builder.setWorkTree(new File(dStr)); 112 } 113 } 114 Repository repository = builder.build(); 115 if (!repository.getObjectDatabase().exists()) 116 repository.create(bare); 117 return new Git(repository); 118 } catch (IOException e) { 119 throw new JGitInternalException(e.getMessage(), e); 120 } 121 } 122 123 /** 124 * The optional directory associated with the init operation. If no 125 * directory is set, we'll use the current directory 126 * 127 * @param directory 128 * the directory to init to 129 * @return this instance 130 * @throws IllegalStateException 131 * if the combination of directory, gitDir and bare is illegal. 132 * E.g. if for a non-bare repository directory and gitDir point 133 * to the same directory of if for a bare repository both 134 * directory and gitDir are specified 135 */ 136 public InitCommand setDirectory(File directory) 137 throws IllegalStateException { 138 validateDirs(directory, gitDir, bare); 139 this.directory = directory; 140 return this; 141 } 142 143 /** 144 * @param gitDir 145 * the repository meta directory 146 * @return this instance 147 * @throws IllegalStateException 148 * if the combination of directory, gitDir and bare is illegal. 149 * E.g. if for a non-bare repository directory and gitDir point 150 * to the same directory of if for a bare repository both 151 * directory and gitDir are specified 152 * @since 3.6 153 */ 154 public InitCommand setGitDir(File gitDir) 155 throws IllegalStateException { 156 validateDirs(directory, gitDir, bare); 157 this.gitDir = gitDir; 158 return this; 159 } 160 161 private static void validateDirs(File directory, File gitDir, boolean bare) 162 throws IllegalStateException { 163 if (directory != null) { 164 if (bare) { 165 if (gitDir != null && !gitDir.equals(directory)) 166 throw new IllegalStateException(MessageFormat.format( 167 JGitText.get().initFailedBareRepoDifferentDirs, 168 gitDir, directory)); 169 } else { 170 if (gitDir != null && gitDir.equals(directory)) 171 throw new IllegalStateException(MessageFormat.format( 172 JGitText.get().initFailedNonBareRepoSameDirs, 173 gitDir, directory)); 174 } 175 } 176 } 177 178 /** 179 * @param bare 180 * whether the repository is bare or not 181 * @throws IllegalStateException 182 * if the combination of directory, gitDir and bare is illegal. 183 * E.g. if for a non-bare repository directory and gitDir point 184 * to the same directory of if for a bare repository both 185 * directory and gitDir are specified 186 * @return this instance 187 */ 188 public InitCommand setBare(boolean bare) { 189 validateDirs(directory, gitDir, bare); 190 this.bare = bare; 191 return this; 192 } 193 }