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.

Archive.java 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (C) 2012 Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.pgm;
  11. import java.io.FileNotFoundException;
  12. import java.io.FileOutputStream;
  13. import java.io.OutputStream;
  14. import org.eclipse.jgit.api.ArchiveCommand;
  15. import org.eclipse.jgit.api.Git;
  16. import org.eclipse.jgit.api.errors.GitAPIException;
  17. import org.eclipse.jgit.archive.ArchiveFormats;
  18. import org.eclipse.jgit.lib.ObjectId;
  19. import org.eclipse.jgit.pgm.internal.CLIText;
  20. import org.kohsuke.args4j.Argument;
  21. import org.kohsuke.args4j.Option;
  22. @Command(common = true, usage = "usage_archive")
  23. class Archive extends TextBuiltin {
  24. static {
  25. ArchiveFormats.registerAll();
  26. }
  27. @Argument(index = 0, metaVar = "metaVar_treeish")
  28. private ObjectId tree;
  29. @Option(name = "--format", metaVar = "metaVar_archiveFormat", usage = "usage_archiveFormat")
  30. private String format;
  31. @Option(name = "--prefix", metaVar = "metaVar_archivePrefix", usage = "usage_archivePrefix")
  32. private String prefix;
  33. @Option(name = "--output", aliases = { "-o" }, metaVar = "metaVar_file", usage = "usage_archiveOutput")
  34. private String output;
  35. /** {@inheritDoc} */
  36. @Override
  37. protected void run() throws Exception {
  38. if (tree == null)
  39. throw die(CLIText.get().treeIsRequired);
  40. OutputStream stream = null;
  41. try {
  42. if (output != null)
  43. stream = new FileOutputStream(output);
  44. else
  45. stream = outs;
  46. try (Git git = new Git(db)) {
  47. ArchiveCommand cmd = git.archive()
  48. .setTree(tree)
  49. .setFormat(format)
  50. .setPrefix(prefix)
  51. .setOutputStream(stream);
  52. if (output != null)
  53. cmd.setFilename(output);
  54. cmd.call();
  55. } catch (GitAPIException e) {
  56. throw die(e.getMessage(), e);
  57. }
  58. } catch (FileNotFoundException e) {
  59. throw die(e.getMessage(), e);
  60. } finally {
  61. if (output != null && stream != null)
  62. stream.close();
  63. }
  64. }
  65. }