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.

CheckoutTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.server.fs;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import java.nio.file.Files;
  14. import java.nio.file.Path;
  15. import org.eclipse.jgit.api.Git;
  16. import org.eclipse.jgit.api.errors.JGitInternalException;
  17. import org.eclipse.jgit.junit.JGitTestUtil;
  18. import org.eclipse.jgit.junit.TestRepository;
  19. import org.eclipse.jgit.lfs.BuiltinLFS;
  20. import org.eclipse.jgit.lfs.lib.LongObjectId;
  21. import org.eclipse.jgit.lib.ConfigConstants;
  22. import org.eclipse.jgit.lib.Repository;
  23. import org.eclipse.jgit.lib.StoredConfig;
  24. import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
  25. import org.eclipse.jgit.util.FileUtils;
  26. import org.junit.After;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. public class CheckoutTest extends LfsServerTest {
  30. Git git;
  31. private TestRepository tdb;
  32. @Override
  33. @Before
  34. public void setup() throws Exception {
  35. super.setup();
  36. BuiltinLFS.register();
  37. Path tmp = Files.createTempDirectory("jgit_test_");
  38. Repository db = FileRepositoryBuilder
  39. .create(tmp.resolve(".git").toFile());
  40. db.create();
  41. StoredConfig cfg = db.getConfig();
  42. cfg.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
  43. ConfigConstants.CONFIG_SECTION_LFS,
  44. ConfigConstants.CONFIG_KEY_USEJGITBUILTIN, true);
  45. cfg.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
  46. ConfigConstants.CONFIG_SECTION_LFS,
  47. ConfigConstants.CONFIG_KEY_REQUIRED, false);
  48. cfg.setString(ConfigConstants.CONFIG_SECTION_LFS, null, "url",
  49. server.getURI().toString() + "/lfs");
  50. cfg.save();
  51. tdb = new TestRepository<>(db);
  52. tdb.branch("test").commit()
  53. .add(".gitattributes",
  54. "*.bin filter=lfs diff=lfs merge=lfs -text ")
  55. .add("a.bin",
  56. "version https://git-lfs.github.com/spec/v1\noid sha256:8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414\nsize 7\n")
  57. .create();
  58. git = Git.wrap(db);
  59. tdb.branch("test2").commit().add(".gitattributes",
  60. "*.bin filter=lfs diff=lfs merge=lfs -text ").create();
  61. }
  62. @After
  63. public void cleanup() throws Exception {
  64. tdb.getRepository().close();
  65. FileUtils.delete(tdb.getRepository().getWorkTree(),
  66. FileUtils.RECURSIVE);
  67. }
  68. @Test
  69. public void testUnknownContent() throws Exception {
  70. git.checkout().setName("test").call();
  71. // unknown content. We will see the pointer file
  72. assertEquals(
  73. "version https://git-lfs.github.com/spec/v1\noid sha256:8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414\nsize 7\n",
  74. JGitTestUtil.read(git.getRepository(), "a.bin"));
  75. assertEquals("[POST /lfs/objects/batch 200]",
  76. server.getRequests().toString());
  77. }
  78. @Test(expected = JGitInternalException.class)
  79. public void testUnknownContentRequired() throws Exception {
  80. StoredConfig cfg = tdb.getRepository().getConfig();
  81. cfg.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
  82. ConfigConstants.CONFIG_SECTION_LFS,
  83. ConfigConstants.CONFIG_KEY_REQUIRED, true);
  84. cfg.save();
  85. // must throw
  86. git.checkout().setName("test").call();
  87. }
  88. @Test
  89. public void testKnownContent() throws Exception {
  90. putContent(
  91. LongObjectId.fromString(
  92. "8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414"),
  93. "1234567");
  94. git.checkout().setName("test").call();
  95. // known content. we will see the actual content of the LFS blob.
  96. assertEquals(
  97. "1234567",
  98. JGitTestUtil.read(git.getRepository(), "a.bin"));
  99. assertEquals(
  100. "[PUT /lfs/objects/8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414 200"
  101. + ", POST /lfs/objects/batch 200"
  102. + ", GET /lfs/objects/8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414 200]",
  103. server.getRequests().toString());
  104. git.checkout().setName("test2").call();
  105. assertFalse(JGitTestUtil.check(git.getRepository(), "a.bin"));
  106. git.checkout().setName("test").call();
  107. // unknown content. We will see the pointer file
  108. assertEquals("1234567",
  109. JGitTestUtil.read(git.getRepository(), "a.bin"));
  110. assertEquals(3, server.getRequests().size());
  111. }
  112. }