Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

InstallBuiltinLfsCommand.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (C) 2018, Markus Duft <markus.duft@ssi-schaefer.com> 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.lfs;
  11. import java.io.IOException;
  12. import org.eclipse.jgit.api.errors.InvalidConfigurationException;
  13. import org.eclipse.jgit.errors.ConfigInvalidException;
  14. import org.eclipse.jgit.lib.ConfigConstants;
  15. import org.eclipse.jgit.lib.Repository;
  16. import org.eclipse.jgit.lib.StoredConfig;
  17. import org.eclipse.jgit.util.FS;
  18. import org.eclipse.jgit.util.LfsFactory.LfsInstallCommand;
  19. import org.eclipse.jgit.util.SystemReader;
  20. /**
  21. * Installs all required LFS properties for the current user, analogous to 'git
  22. * lfs install', but defaulting to using JGit builtin hooks.
  23. *
  24. * @since 4.11
  25. */
  26. public class InstallBuiltinLfsCommand implements LfsInstallCommand {
  27. private static final String[] ARGS_USER = new String[] { "lfs", "install" }; //$NON-NLS-1$//$NON-NLS-2$
  28. private static final String[] ARGS_LOCAL = new String[] { "lfs", "install", //$NON-NLS-1$//$NON-NLS-2$
  29. "--local" }; //$NON-NLS-1$
  30. private Repository repository;
  31. /**
  32. * {@inheritDoc}
  33. *
  34. * @throws IOException
  35. * if an I/O error occurs while accessing a git config or
  36. * executing {@code git lfs install} in an external process
  37. * @throws InvalidConfigurationException
  38. * if a git configuration is invalid
  39. * @throws InterruptedException
  40. * if the current thread is interrupted while waiting for the
  41. * {@code git lfs install} executed in an external process
  42. */
  43. @Override
  44. public Void call() throws IOException, InvalidConfigurationException,
  45. InterruptedException {
  46. StoredConfig cfg = null;
  47. if (repository == null) {
  48. try {
  49. cfg = SystemReader.getInstance().getUserConfig();
  50. } catch (ConfigInvalidException e) {
  51. throw new InvalidConfigurationException(e.getMessage(), e);
  52. }
  53. } else {
  54. cfg = repository.getConfig();
  55. }
  56. cfg.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
  57. ConfigConstants.CONFIG_SECTION_LFS,
  58. ConfigConstants.CONFIG_KEY_USEJGITBUILTIN, true);
  59. cfg.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
  60. ConfigConstants.CONFIG_SECTION_LFS,
  61. ConfigConstants.CONFIG_KEY_REQUIRED, true);
  62. cfg.save();
  63. // try to run git lfs install, we really don't care if it is present
  64. // and/or works here (yet).
  65. ProcessBuilder builder = FS.DETECTED.runInShell("git", //$NON-NLS-1$
  66. repository == null ? ARGS_USER : ARGS_LOCAL);
  67. if (repository != null) {
  68. builder.directory(repository.isBare() ? repository.getDirectory()
  69. : repository.getWorkTree());
  70. }
  71. FS.DETECTED.runProcess(builder, null, null, (String) null);
  72. return null;
  73. }
  74. /**
  75. * Set the repository to install LFS for
  76. *
  77. * @param repo
  78. * the repository to install LFS into locally instead of the user
  79. * configuration
  80. * @return this command
  81. */
  82. @Override
  83. public LfsInstallCommand setRepository(Repository repo) {
  84. this.repository = repo;
  85. return this;
  86. }
  87. }