Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ReflogReaderImpl.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.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.internal.storage.file;
  11. import java.io.File;
  12. import java.io.FileNotFoundException;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. import java.util.Collections;
  16. import java.util.List;
  17. import org.eclipse.jgit.lib.Constants;
  18. import org.eclipse.jgit.lib.ReflogEntry;
  19. import org.eclipse.jgit.lib.ReflogReader;
  20. import org.eclipse.jgit.lib.Repository;
  21. import org.eclipse.jgit.util.IO;
  22. import org.eclipse.jgit.util.RawParseUtils;
  23. /**
  24. * Utility for reading reflog entries
  25. */
  26. class ReflogReaderImpl implements ReflogReader {
  27. private File logName;
  28. /**
  29. * @param db
  30. * @param refname
  31. */
  32. ReflogReaderImpl(Repository db, String refname) {
  33. logName = new File(db.getDirectory(), Constants.LOGS + '/' + refname);
  34. }
  35. /* (non-Javadoc)
  36. * @see org.eclipse.jgit.internal.storage.file.ReflogReaader#getLastEntry()
  37. */
  38. /** {@inheritDoc} */
  39. @Override
  40. public ReflogEntry getLastEntry() throws IOException {
  41. return getReverseEntry(0);
  42. }
  43. /* (non-Javadoc)
  44. * @see org.eclipse.jgit.internal.storage.file.ReflogReaader#getReverseEntries()
  45. */
  46. /** {@inheritDoc} */
  47. @Override
  48. public List<ReflogEntry> getReverseEntries() throws IOException {
  49. return getReverseEntries(Integer.MAX_VALUE);
  50. }
  51. /* (non-Javadoc)
  52. * @see org.eclipse.jgit.internal.storage.file.ReflogReaader#getReverseEntry(int)
  53. */
  54. /** {@inheritDoc} */
  55. @Override
  56. public ReflogEntry getReverseEntry(int number) throws IOException {
  57. if (number < 0)
  58. throw new IllegalArgumentException();
  59. final byte[] log;
  60. try {
  61. log = IO.readFully(logName);
  62. } catch (FileNotFoundException e) {
  63. if (logName.exists()) {
  64. throw e;
  65. }
  66. return null;
  67. }
  68. int rs = RawParseUtils.prevLF(log, log.length);
  69. int current = 0;
  70. while (rs >= 0) {
  71. rs = RawParseUtils.prevLF(log, rs);
  72. if (number == current)
  73. return new ReflogEntryImpl(log, rs < 0 ? 0 : rs + 2);
  74. current++;
  75. }
  76. return null;
  77. }
  78. /* (non-Javadoc)
  79. * @see org.eclipse.jgit.internal.storage.file.ReflogReaader#getReverseEntries(int)
  80. */
  81. /** {@inheritDoc} */
  82. @Override
  83. public List<ReflogEntry> getReverseEntries(int max) throws IOException {
  84. final byte[] log;
  85. try {
  86. log = IO.readFully(logName);
  87. } catch (FileNotFoundException e) {
  88. if (logName.exists()) {
  89. throw e;
  90. }
  91. return Collections.emptyList();
  92. }
  93. int rs = RawParseUtils.prevLF(log, log.length);
  94. List<ReflogEntry> ret = new ArrayList<>();
  95. while (rs >= 0 && max-- > 0) {
  96. rs = RawParseUtils.prevLF(log, rs);
  97. ReflogEntry entry = new ReflogEntryImpl(log, rs < 0 ? 0 : rs + 2);
  98. ret.add(entry);
  99. }
  100. return ret;
  101. }
  102. }