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
|
/*
* Copyright (C) 2020 Thomas Wolf <thomas.wolf@paranor.ch> 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.attributes;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.IO;
import org.eclipse.jgit.util.RawParseUtils;
import org.junit.Test;
/**
* End-to-end tests for some attribute combinations. Writes files, commit them,
* examines the index, deletes the files, performs a hard reset and checks file
* contents again.
*/
public class AttributeFileTests extends RepositoryTestCase {
@Test
public void testTextAutoCoreEolCoreAutoCrLfInput() throws Exception {
FileBasedConfig cfg = db.getConfig();
cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
ConfigConstants.CONFIG_KEY_AUTOCRLF, false);
cfg.save();
final String content = "Line1\nLine2\n";
try (Git git = Git.wrap(db)) {
writeTrashFile(".gitattributes", "* text=auto");
File dummy = writeTrashFile("dummy.txt", content);
git.add().addFilepattern(".").call();
git.commit().setMessage("Commit with LF").call();
assertEquals("Unexpected index state",
"[.gitattributes, mode:100644, content:* text=auto]"
+ "[dummy.txt, mode:100644, content:" + content
+ ']',
indexState(CONTENT));
assertTrue("Should be able to delete " + dummy, dummy.delete());
cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
ConfigConstants.CONFIG_KEY_EOL, "crlf");
cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
ConfigConstants.CONFIG_KEY_AUTOCRLF, "input");
cfg.save();
git.reset().setMode(ResetType.HARD).call();
assertTrue("File " + dummy + "should exist", dummy.isFile());
String textFile = RawParseUtils.decode(IO.readFully(dummy, 512));
assertEquals("Unexpected text content", content, textFile);
}
}
@Test
public void testTextAutoEolLf() throws Exception {
writeTrashFile(".gitattributes", "* text=auto eol=lf");
performTest("Test\r\nFile", "Test\nFile", "Test\nFile");
}
@Test
public void testTextAutoEolCrLf() throws Exception {
writeTrashFile(".gitattributes", "* text=auto eol=crlf");
performTest("Test\r\nFile", "Test\nFile", "Test\r\nFile");
}
private void performTest(String initial, String index, String finalText)
throws Exception {
File dummy = writeTrashFile("dummy.foo", initial);
byte[] data = readTestResource("add.png");
assertTrue("Expected some binary data", data.length > 100);
File binary = writeTrashFile("add.png", "");
Files.write(binary.toPath(), data);
try (Git git = Git.wrap(db)) {
git.add().addFilepattern(".").call();
git.commit().setMessage("test commit").call();
// binary should be unchanged, dummy should match "index"
verifyIndexContent("dummy.foo",
index.getBytes(StandardCharsets.UTF_8));
verifyIndexContent("add.png", data);
assertTrue("Should be able to delete " + dummy, dummy.delete());
assertTrue("Should be able to delete " + binary, binary.delete());
git.reset().setMode(ResetType.HARD).call();
assertTrue("File " + dummy + " should exist", dummy.isFile());
assertTrue("File " + binary + " should exist", binary.isFile());
// binary should be unchanged, dummy should match "finalText"
String textFile = RawParseUtils.decode(IO.readFully(dummy, 512));
assertEquals("Unexpected text content", finalText, textFile);
byte[] binaryFile = IO.readFully(binary, 512);
assertArrayEquals("Unexpected binary content", data, binaryFile);
}
}
private byte[] readTestResource(String name) throws Exception {
try (InputStream in = new BufferedInputStream(
getClass().getResourceAsStream(name))) {
byte[] data = new byte[512];
int read = in.read(data);
if (read == data.length) {
return data;
}
return Arrays.copyOf(data, read);
}
}
private void verifyIndexContent(String path, byte[] expectedContent)
throws Exception {
DirCache dc = db.readDirCache();
for (int i = 0; i < dc.getEntryCount(); ++i) {
DirCacheEntry entry = dc.getEntry(i);
if (path.equals(entry.getPathString())) {
byte[] data = db.open(entry.getObjectId(), Constants.OBJ_BLOB)
.getCachedBytes();
assertArrayEquals("Unexpected index content for " + path,
expectedContent, data);
return;
}
}
fail("Path not found in index: " + path);
}
}
|