View Javadoc
1   /*
2    * Copyright (C) 2009, Constantine Plotnikov <constantine.plotnikov@gmail.com>
3    * Copyright (C) 2008, Google Inc.
4    * Copyright (C) 2010, Robin Rosenberg <robin.rosenberg@dewire.com>
5    * Copyright (C) 2010, Sasa Zivkov <sasa.zivkov@sap.com>
6    * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
7    * Copyright (C) 2016, RĂ¼diger Herrmann <ruediger.herrmann@gmx.de> and others
8    *
9    * This program and the accompanying materials are made available under the
10   * terms of the Eclipse Distribution License v. 1.0 which is available at
11   * https://www.eclipse.org/org/documents/edl-v10.php.
12   *
13   * SPDX-License-Identifier: BSD-3-Clause
14   */
15  
16  package org.eclipse.jgit.pgm;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.text.MessageFormat;
21  
22  import org.eclipse.jgit.api.Git;
23  import org.eclipse.jgit.api.InitCommand;
24  import org.eclipse.jgit.api.errors.GitAPIException;
25  import org.eclipse.jgit.lib.Repository;
26  import org.eclipse.jgit.pgm.internal.CLIText;
27  import org.kohsuke.args4j.Argument;
28  import org.kohsuke.args4j.Option;
29  
30  @Command(common = true, usage = "usage_CreateAnEmptyGitRepository")
31  class Init extends TextBuiltin {
32  	@Option(name = "--bare", usage = "usage_CreateABareRepository")
33  	private boolean bare;
34  
35  	@Argument(index = 0, metaVar = "metaVar_directory")
36  	private String directory;
37  
38  	/** {@inheritDoc} */
39  	@Override
40  	protected final boolean requiresRepository() {
41  		return false;
42  	}
43  
44  	/** {@inheritDoc} */
45  	@Override
46  	protected void run() {
47  		InitCommand command = Git.init();
48  		command.setBare(bare);
49  		if (gitdir != null) {
50  			command.setDirectory(new File(gitdir));
51  		}
52  		if (directory != null) {
53  			command.setDirectory(new File(directory));
54  		}
55  		Repository repository;
56  		try {
57  			repository = command.call().getRepository();
58  			outw.println(MessageFormat.format(
59  					CLIText.get().initializedEmptyGitRepositoryIn,
60  					repository.getDirectory().getAbsolutePath()));
61  		} catch (GitAPIException | IOException e) {
62  			throw die(e.getMessage(), e);
63  		}
64  	}
65  }