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.

ReflogConfigTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com>
  3. * Copyright (C) 2009, Christian Halstrick, Matthias Sohn, SAP AG
  4. * Copyright (C) 2009-2010, Google Inc. and others
  5. *
  6. * This program and the accompanying materials are made available under the
  7. * terms of the Eclipse Distribution License v. 1.0 which is available at
  8. * https://www.eclipse.org/org/documents/edl-v10.php.
  9. *
  10. * SPDX-License-Identifier: BSD-3-Clause
  11. */
  12. package org.eclipse.jgit.lib;
  13. import static org.junit.Assert.assertEquals;
  14. import static org.junit.Assert.assertTrue;
  15. import java.io.IOException;
  16. import org.eclipse.jgit.junit.RepositoryTestCase;
  17. import org.eclipse.jgit.storage.file.FileBasedConfig;
  18. import org.junit.Test;
  19. public class ReflogConfigTest extends RepositoryTestCase {
  20. @Test
  21. public void testlogAllRefUpdates() throws Exception {
  22. long commitTime = 1154236443000L;
  23. int tz = -4 * 60;
  24. // check that there are no entries in the reflog and turn off writing
  25. // reflogs
  26. assertTrue(db.getReflogReader(Constants.HEAD).getReverseEntries()
  27. .isEmpty());
  28. final FileBasedConfig cfg = db.getConfig();
  29. cfg.setBoolean("core", null, "logallrefupdates", false);
  30. cfg.save();
  31. // do one commit and check that reflog size is 0: no reflogs should be
  32. // written
  33. commit("A Commit\n", commitTime, tz);
  34. commitTime += 60 * 1000;
  35. assertTrue("Reflog for HEAD still contain no entry", db
  36. .getReflogReader(Constants.HEAD).getReverseEntries().isEmpty());
  37. // set the logAllRefUpdates parameter to true and check it
  38. cfg.setBoolean("core", null, "logallrefupdates", true);
  39. cfg.save();
  40. assertEquals(CoreConfig.LogRefUpdates.TRUE,
  41. cfg.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
  42. ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES,
  43. CoreConfig.LogRefUpdates.FALSE));
  44. // do one commit and check that reflog size is increased to 1
  45. commit("A Commit\n", commitTime, tz);
  46. commitTime += 60 * 1000;
  47. assertTrue(
  48. "Reflog for HEAD should contain one entry",
  49. db.getReflogReader(Constants.HEAD).getReverseEntries().size() == 1);
  50. // set the logAllRefUpdates parameter to false and check it
  51. cfg.setBoolean("core", null, "logallrefupdates", false);
  52. cfg.save();
  53. assertEquals(CoreConfig.LogRefUpdates.FALSE,
  54. cfg.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
  55. ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES,
  56. CoreConfig.LogRefUpdates.TRUE));
  57. // do one commit and check that reflog size is 2
  58. commit("A Commit\n", commitTime, tz);
  59. commitTime += 60 * 1000;
  60. assertTrue(
  61. "Reflog for HEAD should contain two entries",
  62. db.getReflogReader(Constants.HEAD).getReverseEntries().size() == 2);
  63. // set the logAllRefUpdates parameter to false and check it
  64. cfg.setEnum("core", null, "logallrefupdates",
  65. CoreConfig.LogRefUpdates.ALWAYS);
  66. cfg.save();
  67. assertEquals(CoreConfig.LogRefUpdates.ALWAYS,
  68. cfg.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
  69. ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES,
  70. CoreConfig.LogRefUpdates.FALSE));
  71. // do one commit and check that reflog size is 3
  72. commit("A Commit\n", commitTime, tz);
  73. assertTrue("Reflog for HEAD should contain three entries",
  74. db.getReflogReader(Constants.HEAD).getReverseEntries()
  75. .size() == 3);
  76. }
  77. private void commit(String commitMsg, long commitTime, int tz)
  78. throws IOException {
  79. final CommitBuilder commit = new CommitBuilder();
  80. commit.setAuthor(new PersonIdent(author, commitTime, tz));
  81. commit.setCommitter(new PersonIdent(committer, commitTime, tz));
  82. commit.setMessage(commitMsg);
  83. ObjectId id;
  84. try (ObjectInserter inserter = db.newObjectInserter()) {
  85. commit.setTreeId(inserter.insert(new TreeFormatter()));
  86. id = inserter.insert(commit);
  87. inserter.flush();
  88. }
  89. int nl = commitMsg.indexOf('\n');
  90. final RefUpdate ru = db.updateRef(Constants.HEAD);
  91. ru.setNewObjectId(id);
  92. ru.setRefLogMessage("commit : "
  93. + ((nl == -1) ? commitMsg : commitMsg.substring(0, nl)), false);
  94. ru.forceUpdate();
  95. }
  96. }