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.

BuiltinLFS.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2017, 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 java.io.InputStream;
  13. import java.io.PrintStream;
  14. import org.eclipse.jgit.annotations.Nullable;
  15. import org.eclipse.jgit.attributes.Attribute;
  16. import org.eclipse.jgit.hooks.PrePushHook;
  17. import org.eclipse.jgit.lib.ConfigConstants;
  18. import org.eclipse.jgit.lib.ObjectLoader;
  19. import org.eclipse.jgit.lib.Repository;
  20. import org.eclipse.jgit.util.LfsFactory;
  21. /**
  22. * Implementation of {@link LfsFactory}, using built-in (optional) LFS support.
  23. *
  24. * @since 4.11
  25. */
  26. public class BuiltinLFS extends LfsFactory {
  27. private BuiltinLFS() {
  28. SmudgeFilter.register();
  29. CleanFilter.register();
  30. }
  31. /**
  32. * Activates the built-in LFS support.
  33. */
  34. public static void register() {
  35. setInstance(new BuiltinLFS());
  36. }
  37. @Override
  38. public boolean isAvailable() {
  39. return true;
  40. }
  41. @Override
  42. public ObjectLoader applySmudgeFilter(Repository db, ObjectLoader loader,
  43. Attribute attribute) throws IOException {
  44. if (isEnabled(db) && (attribute == null || isEnabled(db, attribute))) {
  45. return LfsBlobFilter.smudgeLfsBlob(db, loader);
  46. }
  47. return loader;
  48. }
  49. @Override
  50. public LfsInputStream applyCleanFilter(Repository db, InputStream input,
  51. long length, Attribute attribute) throws IOException {
  52. if (isEnabled(db, attribute)) {
  53. return new LfsInputStream(LfsBlobFilter.cleanLfsBlob(db, input));
  54. }
  55. return new LfsInputStream(input, length);
  56. }
  57. @Override
  58. @Nullable
  59. public PrePushHook getPrePushHook(Repository repo,
  60. PrintStream outputStream) {
  61. if (isEnabled(repo)) {
  62. return new LfsPrePushHook(repo, outputStream);
  63. }
  64. return null;
  65. }
  66. @Override
  67. @Nullable
  68. public PrePushHook getPrePushHook(Repository repo, PrintStream outputStream,
  69. PrintStream errorStream) {
  70. if (isEnabled(repo)) {
  71. return new LfsPrePushHook(repo, outputStream, errorStream);
  72. }
  73. return null;
  74. }
  75. /**
  76. * @param db
  77. * the repository
  78. * @return whether LFS is requested for the given repo.
  79. */
  80. @Override
  81. public boolean isEnabled(Repository db) {
  82. if (db == null) {
  83. return false;
  84. }
  85. return db.getConfig().getBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
  86. ConfigConstants.CONFIG_SECTION_LFS,
  87. ConfigConstants.CONFIG_KEY_USEJGITBUILTIN,
  88. false);
  89. }
  90. /**
  91. * @param db
  92. * the repository
  93. * @param attribute
  94. * the attribute to check
  95. * @return whether LFS filter is enabled for the given .gitattribute
  96. * attribute.
  97. */
  98. private boolean isEnabled(Repository db, Attribute attribute) {
  99. if (attribute == null) {
  100. return false;
  101. }
  102. return isEnabled(db) && ConfigConstants.CONFIG_SECTION_LFS
  103. .equals(attribute.getValue());
  104. }
  105. @Override
  106. public LfsInstallCommand getInstallCommand() {
  107. return new InstallBuiltinLfsCommand();
  108. }
  109. }