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.

Lfs.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (C) 2016, Christian Halstrick <christian.halstrick@sap.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 static org.eclipse.jgit.lib.Constants.OBJECTS;
  12. import java.io.IOException;
  13. import java.nio.file.Files;
  14. import java.nio.file.Path;
  15. import org.eclipse.jgit.lfs.lib.AnyLongObjectId;
  16. import org.eclipse.jgit.lfs.lib.Constants;
  17. import org.eclipse.jgit.lib.Repository;
  18. /**
  19. * Class which represents the lfs folder hierarchy inside a {@code .git} folder
  20. *
  21. * @since 4.6
  22. */
  23. public class Lfs {
  24. private Path root;
  25. private Path objDir;
  26. private Path tmpDir;
  27. /**
  28. * Constructor for Lfs.
  29. *
  30. * @param db
  31. * the associated repo
  32. *
  33. * @since 4.11
  34. */
  35. public Lfs(Repository db) {
  36. this.root = db.getDirectory().toPath().resolve(Constants.LFS);
  37. }
  38. /**
  39. * Get the LFS root directory
  40. *
  41. * @return the path to the LFS directory
  42. */
  43. public Path getLfsRoot() {
  44. return root;
  45. }
  46. /**
  47. * Get the path to the temporary directory used by LFS.
  48. *
  49. * @return the path to the temporary directory used by LFS. Will be
  50. * {@code <repo>/.git/lfs/tmp}
  51. */
  52. public Path getLfsTmpDir() {
  53. if (tmpDir == null) {
  54. tmpDir = root.resolve("tmp"); //$NON-NLS-1$
  55. }
  56. return tmpDir;
  57. }
  58. /**
  59. * Get the object directory used by LFS
  60. *
  61. * @return the path to the object directory used by LFS. Will be
  62. * {@code <repo>/.git/lfs/objects}
  63. */
  64. public Path getLfsObjDir() {
  65. if (objDir == null) {
  66. objDir = root.resolve(OBJECTS);
  67. }
  68. return objDir;
  69. }
  70. /**
  71. * Get the media file which stores the original content
  72. *
  73. * @param id
  74. * the id of the mediafile
  75. * @return the file which stores the original content. Its path will look
  76. * like
  77. * {@code "<repo>/.git/lfs/objects/<firstTwoLettersOfID>/<remainingLettersOfID>"}
  78. */
  79. public Path getMediaFile(AnyLongObjectId id) {
  80. String idStr = id.name();
  81. return getLfsObjDir().resolve(idStr.substring(0, 2))
  82. .resolve(idStr.substring(2, 4)).resolve(idStr);
  83. }
  84. /**
  85. * Create a new temp file in the LFS directory
  86. *
  87. * @return a new temporary file in the LFS directory
  88. * @throws java.io.IOException
  89. * when the temp file could not be created
  90. */
  91. public Path createTmpFile() throws IOException {
  92. return Files.createTempFile(getLfsTmpDir(), null, null);
  93. }
  94. }