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.

Init.java 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.eclipse.jgit.util.StringUtils;
  25. import org.kohsuke.args4j.Argument;
  26. import org.kohsuke.args4j.Option;
  27. @Command(common = true, usage = "usage_CreateAnEmptyGitRepository")
  28. class Init extends TextBuiltin {
  29. @Option(name = "--bare", usage = "usage_CreateABareRepository")
  30. private boolean bare;
  31. @Option(name = "--initial-branch", aliases = { "-b" },
  32. metaVar = "metaVar_branchName", usage = "usage_initialBranch")
  33. private String branch;
  34. @Argument(index = 0, metaVar = "metaVar_directory")
  35. private String directory;
  36. /** {@inheritDoc} */
  37. @Override
  38. protected final boolean requiresRepository() {
  39. return false;
  40. }
  41. /** {@inheritDoc} */
  42. @Override
  43. protected void run() {
  44. InitCommand command = Git.init();
  45. command.setBare(bare);
  46. if (gitdir != null) {
  47. command.setDirectory(new File(gitdir));
  48. }
  49. if (directory != null) {
  50. command.setDirectory(new File(directory));
  51. }
  52. Repository repository;
  53. try {
  54. if (!StringUtils.isEmptyOrNull(branch)) {
  55. command.setInitialBranch(branch);
  56. }
  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. }