You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. package org.eclipse.jgit.pgm;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.text.MessageFormat;
  19. import org.eclipse.jgit.api.Git;
  20. import org.eclipse.jgit.api.InitCommand;
  21. import org.eclipse.jgit.api.errors.GitAPIException;
  22. import org.eclipse.jgit.lib.Repository;
  23. import org.eclipse.jgit.pgm.internal.CLIText;
  24. import org.kohsuke.args4j.Argument;
  25. import org.kohsuke.args4j.Option;
  26. @Command(common = true, usage = "usage_CreateAnEmptyGitRepository")
  27. class Init extends TextBuiltin {
  28. @Option(name = "--bare", usage = "usage_CreateABareRepository")
  29. private boolean bare;
  30. @Argument(index = 0, metaVar = "metaVar_directory")
  31. private String directory;
  32. /** {@inheritDoc} */
  33. @Override
  34. protected final boolean requiresRepository() {
  35. return false;
  36. }
  37. /** {@inheritDoc} */
  38. @Override
  39. protected void run() {
  40. InitCommand command = Git.init();
  41. command.setBare(bare);
  42. if (gitdir != null) {
  43. command.setDirectory(new File(gitdir));
  44. }
  45. if (directory != null) {
  46. command.setDirectory(new File(directory));
  47. }
  48. Repository repository;
  49. try {
  50. repository = command.call().getRepository();
  51. outw.println(MessageFormat.format(
  52. CLIText.get().initializedEmptyGitRepositoryIn,
  53. repository.getDirectory().getAbsolutePath()));
  54. } catch (GitAPIException | IOException e) {
  55. throw die(e.getMessage(), e);
  56. }
  57. }
  58. }