1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
/*
* Copyright (C) 2009, Robin Rosenberg
* Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.internal.storage.file;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import org.eclipse.jgit.lib.CheckoutEntry;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.RefDatabase;
import org.eclipse.jgit.lib.ReflogEntry;
import org.eclipse.jgit.lib.ReflogReader;
import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
import org.junit.Test;
public class ReflogReaderTest extends SampleDataRepositoryTestCase {
static byte[] oneLine = "da85355dfc525c9f6f3927b876f379f46ccf826e 3e7549db262d1e836d9bf0af7e22355468f1717c A O Thor Too <authortoo@wri.tr> 1243028200 +0200\tcommit: Add a toString for debugging to RemoteRefUpdate\n"
.getBytes(UTF_8);
static byte[] twoLine = ("0000000000000000000000000000000000000000 c6734895958052a9dbc396cff4459dc1a25029ab A U Thor <thor@committer.au> 1243028201 -0100\tbranch: Created from rr/renamebranchv4\n"
+ "c6734895958052a9dbc396cff4459dc1a25029ab 54794942a18a237c57a80719afed44bb78172b10 Same A U Thor <same.author@example.com> 1243028202 +0100\trebase finished: refs/heads/rr/renamebranch5 onto c6e3b9fe2da0293f11eae202ec35fb343191a82d\n")
.getBytes(UTF_8);
static byte[] twoLineWithAppendInProgress = ("0000000000000000000000000000000000000000 c6734895958052a9dbc396cff4459dc1a25029ab A U Thor <thor@committer.au> 1243028201 -0100\tbranch: Created from rr/renamebranchv4\n"
+ "c6734895958052a9dbc396cff4459dc1a25029ab 54794942a18a237c57a80719afed44bb78172b10 Same A U Thor <same.author@example.com> 1243028202 +0100\trebase finished: refs/heads/rr/renamebranch5 onto c6e3b9fe2da0293f11eae202ec35fb343191a82d\n"
+ "54794942a18a237c57a80719afed44bb78172b10 ")
.getBytes(UTF_8);
static byte[] aLine = "1111111111111111111111111111111111111111 3e7549db262d1e836d9bf0af7e22355468f1717c A U Thor <thor@committer.au> 1243028201 -0100\tbranch: change to a\n"
.getBytes(UTF_8);
static byte[] masterLine = "2222222222222222222222222222222222222222 3e7549db262d1e836d9bf0af7e22355468f1717c A U Thor <thor@committer.au> 1243028201 -0100\tbranch: change to master\n"
.getBytes(UTF_8);
static byte[] headLine = "3333333333333333333333333333333333333333 3e7549db262d1e836d9bf0af7e22355468f1717c A U Thor <thor@committer.au> 1243028201 -0100\tbranch: change to HEAD\n"
.getBytes(UTF_8);
static byte[] oneLineWithoutComment = "da85355dfc525c9f6f3927b876f379f46ccf826e 3e7549db262d1e836d9bf0af7e22355468f1717c A O Thor Too <authortoo@wri.tr> 1243028200 +0200\n"
.getBytes(UTF_8);
static byte[] switchBranch = "0d43a6890a19fd657faad1c4cfbe3cb1b47851c3 4809df9c0d8bce5b00955563f77c5a9f25aa0d12 A O Thor Too <authortoo@wri.tr> 1315088009 +0200\tcheckout: moving from new/work to master\n"
.getBytes(UTF_8);
@Test
public void testReadOneLine() throws Exception {
setupReflog("logs/refs/heads/master", oneLine);
ReflogReader reader = new ReflogReaderImpl(db, "refs/heads/master");
ReflogEntry e = reader.getLastEntry();
assertEquals(ObjectId
.fromString("da85355dfc525c9f6f3927b876f379f46ccf826e"), e
.getOldId());
assertEquals(ObjectId
.fromString("3e7549db262d1e836d9bf0af7e22355468f1717c"), e
.getNewId());
assertEquals("A O Thor Too", e.getWho().getName());
assertEquals("authortoo@wri.tr", e.getWho().getEmailAddress());
assertEquals(120, e.getWho().getTimeZoneOffset());
assertEquals("2009-05-22T23:36:40", iso(e.getWho()));
assertEquals("commit: Add a toString for debugging to RemoteRefUpdate",
e.getComment());
}
private static String iso(PersonIdent id) {
final SimpleDateFormat fmt;
fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
fmt.setTimeZone(id.getTimeZone());
return fmt.format(id.getWhen());
}
@Test
public void testReadTwoLine() throws Exception {
setupReflog("logs/refs/heads/master", twoLine);
ReflogReader reader = new ReflogReaderImpl(db, "refs/heads/master");
List<ReflogEntry> reverseEntries = reader.getReverseEntries();
assertEquals(2, reverseEntries.size());
ReflogEntry e = reverseEntries.get(0);
assertEquals(ObjectId
.fromString("c6734895958052a9dbc396cff4459dc1a25029ab"), e
.getOldId());
assertEquals(ObjectId
.fromString("54794942a18a237c57a80719afed44bb78172b10"), e
.getNewId());
assertEquals("Same A U Thor", e.getWho().getName());
assertEquals("same.author@example.com", e.getWho().getEmailAddress());
assertEquals(60, e.getWho().getTimeZoneOffset());
assertEquals("2009-05-22T22:36:42", iso(e.getWho()));
assertEquals(
"rebase finished: refs/heads/rr/renamebranch5 onto c6e3b9fe2da0293f11eae202ec35fb343191a82d",
e.getComment());
e = reverseEntries.get(1);
assertEquals(ObjectId
.fromString("0000000000000000000000000000000000000000"), e
.getOldId());
assertEquals(ObjectId
.fromString("c6734895958052a9dbc396cff4459dc1a25029ab"), e
.getNewId());
assertEquals("A U Thor", e.getWho().getName());
assertEquals("thor@committer.au", e.getWho().getEmailAddress());
assertEquals(-60, e.getWho().getTimeZoneOffset());
assertEquals("2009-05-22T20:36:41", iso(e.getWho()));
assertEquals("branch: Created from rr/renamebranchv4", e.getComment());
}
@Test
public void testReadWhileAppendIsInProgress() throws Exception {
setupReflog("logs/refs/heads/master", twoLineWithAppendInProgress);
ReflogReader reader = new ReflogReaderImpl(db, "refs/heads/master");
List<ReflogEntry> reverseEntries = reader.getReverseEntries();
assertEquals(2, reverseEntries.size());
ReflogEntry e = reverseEntries.get(0);
assertEquals(ObjectId
.fromString("c6734895958052a9dbc396cff4459dc1a25029ab"), e
.getOldId());
assertEquals(ObjectId
.fromString("54794942a18a237c57a80719afed44bb78172b10"), e
.getNewId());
assertEquals("Same A U Thor", e.getWho().getName());
assertEquals("same.author@example.com", e.getWho().getEmailAddress());
assertEquals(60, e.getWho().getTimeZoneOffset());
assertEquals("2009-05-22T22:36:42", iso(e.getWho()));
assertEquals(
"rebase finished: refs/heads/rr/renamebranch5 onto c6e3b9fe2da0293f11eae202ec35fb343191a82d",
e.getComment());
// while similar to testReadTwoLine, we can assume that if we get the last entry
// right, everything else is too
}
@Test
public void testReadRightLog() throws Exception {
setupReflog("logs/refs/heads/a", aLine);
setupReflog("logs/refs/heads/master", masterLine);
setupReflog("logs/HEAD", headLine);
RefDatabase refDb = db.getRefDatabase();
assertEquals("branch: change to master",
refDb.getReflogReader("refs/heads/master").getLastEntry()
.getComment());
assertEquals("branch: change to a",
refDb.getReflogReader("refs/heads/a").getLastEntry()
.getComment());
assertEquals("branch: change to HEAD",
refDb.getReflogReader("HEAD").getLastEntry().getComment());
}
@Test
public void testReadLineWithMissingComment() throws Exception {
setupReflog("logs/refs/heads/master", oneLineWithoutComment);
final ReflogReader reader = db.getRefDatabase()
.getReflogReader("refs/heads/master");
ReflogEntry e = reader.getLastEntry();
assertEquals(ObjectId
.fromString("da85355dfc525c9f6f3927b876f379f46ccf826e"), e
.getOldId());
assertEquals(ObjectId
.fromString("3e7549db262d1e836d9bf0af7e22355468f1717c"), e
.getNewId());
assertEquals("A O Thor Too", e.getWho().getName());
assertEquals("authortoo@wri.tr", e.getWho().getEmailAddress());
assertEquals(120, e.getWho().getTimeZoneOffset());
assertEquals("2009-05-22T23:36:40", iso(e.getWho()));
assertEquals("",
e.getComment());
}
@Test
public void testNoLog() throws Exception {
RefDatabase refDb = db.getRefDatabase();
assertEquals(0,
refDb.getReflogReader("refs/heads/master").getReverseEntries()
.size());
assertNull(refDb.getReflogReader("refs/heads/master").getLastEntry());
}
@Test
public void testCheckout() throws Exception {
setupReflog("logs/HEAD", switchBranch);
List<ReflogEntry> entries = db.getRefDatabase()
.getReflogReader(Constants.HEAD).getReverseEntries();
assertEquals(1, entries.size());
ReflogEntry entry = entries.get(0);
CheckoutEntry checkout = entry.parseCheckout();
assertNotNull(checkout);
assertEquals("master", checkout.getToBranch());
assertEquals("new/work", checkout.getFromBranch());
}
@Test
public void testSpecificEntryNumber() throws Exception {
setupReflog("logs/refs/heads/master", twoLine);
ReflogReader reader = new ReflogReaderImpl(db, "refs/heads/master");
ReflogEntry e = reader.getReverseEntry(0);
assertEquals(
ObjectId.fromString("c6734895958052a9dbc396cff4459dc1a25029ab"),
e.getOldId());
assertEquals(
ObjectId.fromString("54794942a18a237c57a80719afed44bb78172b10"),
e.getNewId());
assertEquals("Same A U Thor", e.getWho().getName());
assertEquals("same.author@example.com", e.getWho().getEmailAddress());
assertEquals(60, e.getWho().getTimeZoneOffset());
assertEquals("2009-05-22T22:36:42", iso(e.getWho()));
assertEquals(
"rebase finished: refs/heads/rr/renamebranch5 onto c6e3b9fe2da0293f11eae202ec35fb343191a82d",
e.getComment());
e = reader.getReverseEntry(1);
assertEquals(
ObjectId.fromString("0000000000000000000000000000000000000000"),
e.getOldId());
assertEquals(
ObjectId.fromString("c6734895958052a9dbc396cff4459dc1a25029ab"),
e.getNewId());
assertEquals("A U Thor", e.getWho().getName());
assertEquals("thor@committer.au", e.getWho().getEmailAddress());
assertEquals(-60, e.getWho().getTimeZoneOffset());
assertEquals("2009-05-22T20:36:41", iso(e.getWho()));
assertEquals("branch: Created from rr/renamebranchv4", e.getComment());
assertNull(reader.getReverseEntry(3));
}
private void setupReflog(String logName, byte[] data)
throws FileNotFoundException, IOException {
File logfile = new File(db.getCommonDirectory(), logName);
if (!logfile.getParentFile().mkdirs()
&& !logfile.getParentFile().isDirectory()) {
throw new IOException(
"oops, cannot create the directory for the test reflog file"
+ logfile);
}
try (FileOutputStream fileOutputStream = new FileOutputStream(logfile)) {
fileOutputStream.write(data);
}
}
}
|