View Javadoc
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.FS;
57  import org.eclipse.jgit.util.SystemReader;
58  
59  /**
60   * Create an empty git repository or reinitalize an existing one
61   *
62   * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-init.html"
63   *      >Git documentation about init</a>
64   */
65  public class InitCommand implements Callable<Git> {
66  	private File directory;
67  
68  	private File gitDir;
69  
70  	private boolean bare;
71  
72  	private FS fs;
73  
74  	/**
75  	 * {@inheritDoc}
76  	 * <p>
77  	 * Executes the {@code Init} command.
78  	 *
79  	 * @return a {@code Git} instance that owns the {@code Repository} that it
80  	 *         wraps.
81  	 */
82  	@Override
83  	public Git call() throws GitAPIException {
84  		try {
85  			RepositoryBuilder builder = new RepositoryBuilder();
86  			if (bare)
87  				builder.setBare();
88  			if (fs != null) {
89  				builder.setFS(fs);
90  			}
91  			builder.readEnvironment();
92  			if (gitDir != null)
93  				builder.setGitDir(gitDir);
94  			else
95  				gitDir = builder.getGitDir();
96  			if (directory != null) {
97  				if (bare)
98  					builder.setGitDir(directory);
99  				else {
100 					builder.setWorkTree(directory);
101 					if (gitDir == null)
102 						builder.setGitDir(new File(directory, Constants.DOT_GIT));
103 				}
104 			} else if (builder.getGitDir() == null) {
105 				String dStr = SystemReader.getInstance()
106 						.getProperty("user.dir"); //$NON-NLS-1$
107 				if (dStr == null)
108 					dStr = "."; //$NON-NLS-1$
109 				File d = new File(dStr);
110 				if (!bare)
111 					d = new File(d, Constants.DOT_GIT);
112 				builder.setGitDir(d);
113 			} else {
114 				// directory was not set but gitDir was set
115 				if (!bare) {
116 					String dStr = SystemReader.getInstance().getProperty(
117 							"user.dir"); //$NON-NLS-1$
118 					if (dStr == null)
119 						dStr = "."; //$NON-NLS-1$
120 					builder.setWorkTree(new File(dStr));
121 				}
122 			}
123 			Repository repository = builder.build();
124 			if (!repository.getObjectDatabase().exists())
125 				repository.create(bare);
126 			return new Git(repository, true);
127 		} catch (IOException e) {
128 			throw new JGitInternalException(e.getMessage(), e);
129 		}
130 	}
131 
132 	/**
133 	 * The optional directory associated with the init operation. If no
134 	 * directory is set, we'll use the current directory
135 	 *
136 	 * @param directory
137 	 *            the directory to init to
138 	 * @return this instance
139 	 * @throws java.lang.IllegalStateException
140 	 *             if the combination of directory, gitDir and bare is illegal.
141 	 *             E.g. if for a non-bare repository directory and gitDir point
142 	 *             to the same directory of if for a bare repository both
143 	 *             directory and gitDir are specified
144 	 */
145 	public InitCommand setDirectory(File directory)
146 			throws IllegalStateException {
147 		validateDirs(directory, gitDir, bare);
148 		this.directory = directory;
149 		return this;
150 	}
151 
152 	/**
153 	 * Set the repository meta directory (.git)
154 	 *
155 	 * @param gitDir
156 	 *            the repository meta directory
157 	 * @return this instance
158 	 * @throws java.lang.IllegalStateException
159 	 *             if the combination of directory, gitDir and bare is illegal.
160 	 *             E.g. if for a non-bare repository directory and gitDir point
161 	 *             to the same directory of if for a bare repository both
162 	 *             directory and gitDir are specified
163 	 * @since 3.6
164 	 */
165 	public InitCommand setGitDir(File gitDir)
166 			throws IllegalStateException {
167 		validateDirs(directory, gitDir, bare);
168 		this.gitDir = gitDir;
169 		return this;
170 	}
171 
172 	private static void validateDirs(File directory, File gitDir, boolean bare)
173 			throws IllegalStateException {
174 		if (directory != null) {
175 			if (bare) {
176 				if (gitDir != null && !gitDir.equals(directory))
177 					throw new IllegalStateException(MessageFormat.format(
178 							JGitText.get().initFailedBareRepoDifferentDirs,
179 							gitDir, directory));
180 			} else {
181 				if (gitDir != null && gitDir.equals(directory))
182 					throw new IllegalStateException(MessageFormat.format(
183 							JGitText.get().initFailedNonBareRepoSameDirs,
184 							gitDir, directory));
185 			}
186 		}
187 	}
188 
189 	/**
190 	 * Set whether the repository is bare or not
191 	 *
192 	 * @param bare
193 	 *            whether the repository is bare or not
194 	 * @throws java.lang.IllegalStateException
195 	 *             if the combination of directory, gitDir and bare is illegal.
196 	 *             E.g. if for a non-bare repository directory and gitDir point
197 	 *             to the same directory of if for a bare repository both
198 	 *             directory and gitDir are specified
199 	 * @return this instance
200 	 */
201 	public InitCommand setBare(boolean bare) {
202 		validateDirs(directory, gitDir, bare);
203 		this.bare = bare;
204 		return this;
205 	}
206 
207 	/**
208 	 * Set the file system abstraction to be used for repositories created by
209 	 * this command.
210 	 *
211 	 * @param fs
212 	 *            the abstraction.
213 	 * @return {@code this} (for chaining calls).
214 	 * @since 4.10
215 	 */
216 	public InitCommand setFs(FS fs) {
217 		this.fs = fs;
218 		return this;
219 	}
220 }