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