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.

TreeWalkBasicDiffTest.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (C) 2008, Google Inc. 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.treewalk;
  11. import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
  12. import static org.eclipse.jgit.lib.Constants.encode;
  13. import static org.junit.Assert.assertEquals;
  14. import static org.junit.Assert.assertFalse;
  15. import static org.junit.Assert.assertTrue;
  16. import org.eclipse.jgit.junit.RepositoryTestCase;
  17. import org.eclipse.jgit.lib.FileMode;
  18. import org.eclipse.jgit.lib.ObjectId;
  19. import org.eclipse.jgit.lib.ObjectInserter;
  20. import org.eclipse.jgit.lib.TreeFormatter;
  21. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  22. import org.junit.Test;
  23. public class TreeWalkBasicDiffTest extends RepositoryTestCase {
  24. @Test
  25. public void testMissingSubtree_DetectFileAdded_FileModified()
  26. throws Exception {
  27. final ObjectId oldTree, newTree, bFileId, cFileId1, cFileId2;
  28. try (ObjectInserter inserter = db.newObjectInserter()) {
  29. final ObjectId aFileId = inserter.insert(OBJ_BLOB, encode("a"));
  30. bFileId = inserter.insert(OBJ_BLOB, encode("b"));
  31. cFileId1 = inserter.insert(OBJ_BLOB, encode("c-1"));
  32. cFileId2 = inserter.insert(OBJ_BLOB, encode("c-2"));
  33. // Create sub-a/empty, sub-c/empty = hello.
  34. {
  35. TreeFormatter root = new TreeFormatter();
  36. {
  37. TreeFormatter subA = new TreeFormatter();
  38. subA.append("empty", FileMode.REGULAR_FILE, aFileId);
  39. root.append("sub-a", FileMode.TREE, inserter.insert(subA));
  40. }
  41. {
  42. TreeFormatter subC = new TreeFormatter();
  43. subC.append("empty", FileMode.REGULAR_FILE, cFileId1);
  44. root.append("sub-c", FileMode.TREE, inserter.insert(subC));
  45. }
  46. oldTree = inserter.insert(root);
  47. }
  48. // Create sub-a/empty, sub-b/empty, sub-c/empty.
  49. {
  50. TreeFormatter root = new TreeFormatter();
  51. {
  52. TreeFormatter subA = new TreeFormatter();
  53. subA.append("empty", FileMode.REGULAR_FILE, aFileId);
  54. root.append("sub-a", FileMode.TREE, inserter.insert(subA));
  55. }
  56. {
  57. TreeFormatter subB = new TreeFormatter();
  58. subB.append("empty", FileMode.REGULAR_FILE, bFileId);
  59. root.append("sub-b", FileMode.TREE, inserter.insert(subB));
  60. }
  61. {
  62. TreeFormatter subC = new TreeFormatter();
  63. subC.append("empty", FileMode.REGULAR_FILE, cFileId2);
  64. root.append("sub-c", FileMode.TREE, inserter.insert(subC));
  65. }
  66. newTree = inserter.insert(root);
  67. }
  68. inserter.flush();
  69. }
  70. try (TreeWalk tw = new TreeWalk(db)) {
  71. tw.reset(oldTree, newTree);
  72. tw.setRecursive(true);
  73. tw.setFilter(TreeFilter.ANY_DIFF);
  74. assertTrue(tw.next());
  75. assertEquals("sub-b/empty", tw.getPathString());
  76. assertEquals(FileMode.MISSING, tw.getFileMode(0));
  77. assertEquals(FileMode.REGULAR_FILE, tw.getFileMode(1));
  78. assertEquals(ObjectId.zeroId(), tw.getObjectId(0));
  79. assertEquals(bFileId, tw.getObjectId(1));
  80. assertTrue(tw.next());
  81. assertEquals("sub-c/empty", tw.getPathString());
  82. assertEquals(FileMode.REGULAR_FILE, tw.getFileMode(0));
  83. assertEquals(FileMode.REGULAR_FILE, tw.getFileMode(1));
  84. assertEquals(cFileId1, tw.getObjectId(0));
  85. assertEquals(cFileId2, tw.getObjectId(1));
  86. assertFalse(tw.next());
  87. }
  88. }
  89. }