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.

LfsGitTest.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (C) 2021, Thomas Wolf <thomas.wolf@paranor.ch> 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.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertTrue;
  13. import java.io.File;
  14. import java.nio.charset.StandardCharsets;
  15. import java.nio.file.Files;
  16. import org.eclipse.jgit.api.Git;
  17. import org.eclipse.jgit.api.ResetCommand.ResetType;
  18. import org.eclipse.jgit.attributes.FilterCommandRegistry;
  19. import org.eclipse.jgit.junit.RepositoryTestCase;
  20. import org.eclipse.jgit.lfs.lib.Constants;
  21. import org.eclipse.jgit.lib.StoredConfig;
  22. import org.junit.AfterClass;
  23. import org.junit.Before;
  24. import org.junit.BeforeClass;
  25. import org.junit.Test;
  26. public class LfsGitTest extends RepositoryTestCase {
  27. private static final String SMUDGE_NAME = org.eclipse.jgit.lib.Constants.BUILTIN_FILTER_PREFIX
  28. + Constants.ATTR_FILTER_DRIVER_PREFIX
  29. + org.eclipse.jgit.lib.Constants.ATTR_FILTER_TYPE_SMUDGE;
  30. private static final String CLEAN_NAME = org.eclipse.jgit.lib.Constants.BUILTIN_FILTER_PREFIX
  31. + Constants.ATTR_FILTER_DRIVER_PREFIX
  32. + org.eclipse.jgit.lib.Constants.ATTR_FILTER_TYPE_CLEAN;
  33. @BeforeClass
  34. public static void installLfs() {
  35. FilterCommandRegistry.register(SMUDGE_NAME, SmudgeFilter.FACTORY);
  36. FilterCommandRegistry.register(CLEAN_NAME, CleanFilter.FACTORY);
  37. }
  38. @AfterClass
  39. public static void removeLfs() {
  40. FilterCommandRegistry.unregister(SMUDGE_NAME);
  41. FilterCommandRegistry.unregister(CLEAN_NAME);
  42. }
  43. private Git git;
  44. @Override
  45. @Before
  46. public void setUp() throws Exception {
  47. super.setUp();
  48. git = new Git(db);
  49. // commit something
  50. writeTrashFile("Test.txt", "Hello world");
  51. git.add().addFilepattern("Test.txt").call();
  52. git.commit().setMessage("Initial commit").call();
  53. // prepare the config for LFS
  54. StoredConfig config = git.getRepository().getConfig();
  55. config.setString("filter", "lfs", "clean", CLEAN_NAME);
  56. config.setString("filter", "lfs", "smudge", SMUDGE_NAME);
  57. config.save();
  58. }
  59. @Test
  60. public void checkoutNonLfsPointer() throws Exception {
  61. String content = "size_t\nsome_function(void* ptr);\n";
  62. File smallFile = writeTrashFile("Test.txt", content);
  63. StringBuilder largeContent = new StringBuilder(
  64. LfsPointer.SIZE_THRESHOLD * 4);
  65. while (largeContent.length() < LfsPointer.SIZE_THRESHOLD * 4) {
  66. largeContent.append(content);
  67. }
  68. File largeFile = writeTrashFile("large.txt", largeContent.toString());
  69. fsTick(largeFile);
  70. git.add().addFilepattern("Test.txt").addFilepattern("large.txt").call();
  71. git.commit().setMessage("Text files").call();
  72. writeTrashFile(".gitattributes", "*.txt filter=lfs");
  73. git.add().addFilepattern(".gitattributes").call();
  74. git.commit().setMessage("attributes").call();
  75. assertTrue(smallFile.delete());
  76. assertTrue(largeFile.delete());
  77. // This reset will run the two text files through the smudge filter
  78. git.reset().setMode(ResetType.HARD).call();
  79. assertTrue(smallFile.exists());
  80. assertTrue(largeFile.exists());
  81. checkFile(smallFile, content);
  82. checkFile(largeFile, largeContent.toString());
  83. // Modify the large file
  84. largeContent.append(content);
  85. writeTrashFile("large.txt", largeContent.toString());
  86. // This should convert largeFile to an LFS pointer
  87. git.add().addFilepattern("large.txt").call();
  88. git.commit().setMessage("Large modified").call();
  89. String lfsPtr = "version https://git-lfs.github.com/spec/v1\n"
  90. + "oid sha256:d041ab19bd7edd899b3c0450d0f61819f96672f0b22d26c9753abc62e1261614\n"
  91. + "size 858\n";
  92. assertEquals("[.gitattributes, mode:100644, content:*.txt filter=lfs]"
  93. + "[Test.txt, mode:100644, content:" + content + ']'
  94. + "[large.txt, mode:100644, content:" + lfsPtr + ']',
  95. indexState(CONTENT));
  96. // Verify the file has been saved
  97. File savedFile = new File(db.getDirectory(), "lfs");
  98. savedFile = new File(savedFile, "objects");
  99. savedFile = new File(savedFile, "d0");
  100. savedFile = new File(savedFile, "41");
  101. savedFile = new File(savedFile,
  102. "d041ab19bd7edd899b3c0450d0f61819f96672f0b22d26c9753abc62e1261614");
  103. String saved = new String(Files.readAllBytes(savedFile.toPath()),
  104. StandardCharsets.UTF_8);
  105. assertEquals(saved, largeContent.toString());
  106. assertTrue(smallFile.delete());
  107. assertTrue(largeFile.delete());
  108. git.reset().setMode(ResetType.HARD).call();
  109. assertTrue(smallFile.exists());
  110. assertTrue(largeFile.exists());
  111. checkFile(smallFile, content);
  112. checkFile(largeFile, largeContent.toString());
  113. assertEquals("[.gitattributes, mode:100644, content:*.txt filter=lfs]"
  114. + "[Test.txt, mode:100644, content:" + content + ']'
  115. + "[large.txt, mode:100644, content:" + lfsPtr + ']',
  116. indexState(CONTENT));
  117. git.add().addFilepattern("Test.txt").call();
  118. git.commit().setMessage("Small committed again").call();
  119. String lfsPtrSmall = "version https://git-lfs.github.com/spec/v1\n"
  120. + "oid sha256:9110463275fb0e2f0e9fdeaf84e598e62915666161145cf08927079119cc7814\n"
  121. + "size 33\n";
  122. assertEquals("[.gitattributes, mode:100644, content:*.txt filter=lfs]"
  123. + "[Test.txt, mode:100644, content:" + lfsPtrSmall + ']'
  124. + "[large.txt, mode:100644, content:" + lfsPtr + ']',
  125. indexState(CONTENT));
  126. assertTrue(git.status().call().isClean());
  127. }
  128. }