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 package org.eclipse.jgit.internal.storage.file;
44
45 import static java.nio.charset.StandardCharsets.UTF_8;
46 import static org.junit.Assert.assertEquals;
47
48 import java.io.File;
49 import java.io.FileInputStream;
50 import java.io.FileNotFoundException;
51 import java.io.IOException;
52
53 import org.eclipse.jgit.lib.ObjectId;
54 import org.eclipse.jgit.lib.PersonIdent;
55 import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
56 import org.junit.Test;
57
58 public class ReflogWriterTest extends SampleDataRepositoryTestCase {
59
60 private static String oneLine = "da85355dfc525c9f6f3927b876f379f46ccf826e 3e7549db262d1e836d9bf0af7e22355468f1717c"
61 + " John Doe <john@doe.com> 1243028200 +0200\tstash: Add message with line feeds\n";
62
63 @Test
64 public void shouldFilterLineFeedFromMessage() throws Exception {
65 ReflogWriter writer =
66 new ReflogWriter((RefDirectory) db.getRefDatabase());
67 PersonIdent ident = new PersonIdent("John Doe", "john@doe.com",
68 1243028200000L, 120);
69 ObjectId oldId = ObjectId
70 .fromString("da85355dfc525c9f6f3927b876f379f46ccf826e");
71 ObjectId newId = ObjectId
72 .fromString("3e7549db262d1e836d9bf0af7e22355468f1717c");
73
74 writer.log("refs/heads/master", oldId, newId, ident,
75 "stash: Add\nmessage\r\nwith line feeds");
76
77 byte[] buffer = new byte[oneLine.getBytes(UTF_8).length];
78 readReflog(buffer);
79 assertEquals(oneLine, new String(buffer, UTF_8));
80 }
81
82 private void readReflog(byte[] buffer)
83 throws FileNotFoundException, IOException {
84 File logfile = new File(db.getDirectory(), "logs/refs/heads/master");
85 if (!logfile.getParentFile().mkdirs()
86 && !logfile.getParentFile().isDirectory()) {
87 throw new IOException(
88 "oops, cannot create the directory for the test reflog file"
89 + logfile);
90 }
91 try (FileInputStream fileInputStream = new FileInputStream(logfile)) {
92 fileInputStream.read(buffer);
93 }
94 }
95 }