2 * Copyright 2006 Shawn Pearce <spearce@spearce.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.spearce
.jgit
.lib_tst
;
19 import java
.io
.FileInputStream
;
20 import java
.io
.FileReader
;
21 import java
.io
.FileWriter
;
22 import java
.io
.IOException
;
24 import org
.spearce
.jgit
.lib
.Commit
;
25 import org
.spearce
.jgit
.lib
.FileTreeEntry
;
26 import org
.spearce
.jgit
.lib
.ObjectId
;
27 import org
.spearce
.jgit
.lib
.ObjectWriter
;
28 import org
.spearce
.jgit
.lib
.PersonIdent
;
29 import org
.spearce
.jgit
.lib
.Repository
;
30 import org
.spearce
.jgit
.lib
.RepositoryConfig
;
31 import org
.spearce
.jgit
.lib
.Tree
;
32 import org
.spearce
.jgit
.lib
.TreeEntry
;
33 import org
.spearce
.jgit
.lib
.WriteTree
;
34 import org
.spearce
.jgit
.lib
.XInputStream
;
36 public class T0003_Basic
extends RepositoryTestCase
38 public void test001_Initalize() throws IOException
40 final File gitdir
= new File(trash
, ".git");
41 final File objects
= new File(gitdir
, "objects");
42 final File objects_pack
= new File(objects
, "pack");
43 final File objects_info
= new File(objects
, "info");
44 final File refs
= new File(gitdir
, "refs");
45 final File refs_heads
= new File(refs
, "heads");
46 final File refs_tags
= new File(refs
, "tags");
47 final File HEAD
= new File(gitdir
, "HEAD");
49 assertTrue("Exists " + trash
, trash
.isDirectory());
50 assertTrue("Exists " + objects
, objects
.isDirectory());
51 assertTrue("Exists " + objects_pack
, objects_pack
.isDirectory());
52 assertTrue("Exists " + objects_info
, objects_info
.isDirectory());
53 assertEquals(2, objects
.listFiles().length
);
54 assertTrue("Exists " + refs
, refs
.isDirectory());
55 assertTrue("Exists " + refs_heads
, refs_heads
.isDirectory());
56 assertTrue("Exists " + refs_tags
, refs_tags
.isDirectory());
57 assertTrue("Exists " + HEAD
, HEAD
.isFile());
58 assertEquals(23, HEAD
.length());
61 public void test002_WriteEmptyTree() throws IOException
63 // One of our test packs contains the empty tree object. If the pack is
64 // open when we create it we won't write the object file out as a loose
65 // object (as it already exists in the pack).
69 final Tree t
= new Tree(db
);
70 t
.accept(new WriteTree(trash
, db
), TreeEntry
.MODIFIED_ONLY
);
71 assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", t
.getId()
73 final File o
= new File(
74 new File(new File(trash_git
, "objects"), "4b"),
75 "825dc642cb6eb9a060e54bf8d69288fbee4904");
76 assertTrue("Exists " + o
, o
.isFile());
77 assertTrue("Read-only " + o
, !o
.canWrite());
78 assertEquals(9, o
.length());
81 public void test002_WriteEmptyTree2() throws IOException
83 // File shouldn't exist as it is in a test pack.
85 final Tree t
= new Tree(db
);
86 t
.accept(new WriteTree(trash
, db
), TreeEntry
.MODIFIED_ONLY
);
87 assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", t
.getId()
89 final File o
= new File(
90 new File(new File(trash_git
, "objects"), "4b"),
91 "825dc642cb6eb9a060e54bf8d69288fbee4904");
92 assertFalse("Exists " + o
, o
.isFile());
95 public void test003_WriteShouldBeEmptyTree() throws IOException
97 final Tree t
= new Tree(db
);
98 final ObjectId emptyId
= new ObjectWriter(db
).writeBlob(new byte[0]);
99 t
.addFile("should-be-empty").setId(emptyId
);
100 t
.accept(new WriteTree(trash
, db
), TreeEntry
.MODIFIED_ONLY
);
101 assertEquals("7bb943559a305bdd6bdee2cef6e5df2413c3d30a", t
.getId()
106 new File(new File(trash_git
, "objects"), "7b"),
107 "b943559a305bdd6bdee2cef6e5df2413c3d30a");
108 assertTrue("Exists " + o
, o
.isFile());
109 assertTrue("Read-only " + o
, !o
.canWrite());
112 new File(new File(trash_git
, "objects"), "e6"),
113 "9de29bb2d1d6434b8b29ae775ad8c2e48c5391");
114 assertTrue("Exists " + o
, o
.isFile());
115 assertTrue("Read-only " + o
, !o
.canWrite());
118 public void test004_CheckNewConfig() throws IOException
120 final RepositoryConfig c
= db
.getConfig();
122 assertEquals("0", c
.getString("core", "repositoryformatversion"));
123 assertEquals("0", c
.getString("CoRe", "REPOSITORYFoRmAtVeRsIoN"));
124 assertEquals("true", c
.getString("core", "filemode"));
125 assertEquals("true", c
.getString("cOrE", "fIlEModE"));
126 assertNull(c
.getString("notavalue", "reallyNotAValue"));
130 public void test005_ReadSimpleConfig() throws IOException
132 final RepositoryConfig c
= db
.getConfig();
135 assertEquals("0", c
.getString("core", "repositoryformatversion"));
136 assertEquals("0", c
.getString("CoRe", "REPOSITORYFoRmAtVeRsIoN"));
137 assertEquals("true", c
.getString("core", "filemode"));
138 assertEquals("true", c
.getString("cOrE", "fIlEModE"));
139 assertNull(c
.getString("notavalue", "reallyNotAValue"));
142 public void test006_ReadUglyConfig() throws IOException
144 final RepositoryConfig c
= db
.getConfig();
145 final File cfg
= new File(db
.getDirectory(), "config");
146 final FileWriter pw
= new FileWriter(cfg
);
147 final String configStr
= " [core];comment\n\tfilemode = yes\n"
149 + " email = A U Thor <thor@example.com> # Just an example...\n"
150 + " name = \"A Thor \\\\ \\\"\\t \"\n"
151 + " defaultCheckInComment = a many line\\n\\\ncomment\\n\\\n"
156 assertEquals("yes", c
.getString("core", "filemode"));
157 assertEquals("A U Thor <thor@example.com>", c
158 .getString("user", "email"));
159 assertEquals("A Thor \\ \"\t ", c
.getString("user", "name"));
160 assertEquals("a many line\ncomment\n to test", c
.getString(
162 "defaultCheckInComment"));
164 final FileReader fr
= new FileReader(cfg
);
165 final char[] cbuf
= new char[configStr
.length()];
168 assertEquals(configStr
, new String(cbuf
));
171 public void test007_Open() throws IOException
173 final Repository db2
= new Repository(db
.getDirectory());
174 assertEquals(db
.getDirectory(), db2
.getDirectory());
175 assertEquals(db
.getObjectsDirectory(), db2
.getObjectsDirectory());
176 assertNotSame(db
.getConfig(), db2
.getConfig());
179 public void test008_FailOnWrongVersion() throws IOException
181 final File cfg
= new File(db
.getDirectory(), "config");
182 final FileWriter pw
= new FileWriter(cfg
);
183 final String badvers
= "ihopethisisneveraversion";
184 final String configStr
= "[core]\n"
185 + "\trepositoryFormatVersion="
193 new Repository(db
.getDirectory());
194 fail("incorrectly opened a bad repository");
196 catch (IOException ioe
)
198 assertTrue(ioe
.getMessage().indexOf("format") > 0);
199 assertTrue(ioe
.getMessage().indexOf(badvers
) > 0);
203 public void test009_CreateCommitOldFormat() throws IOException
205 writeTrashFile(".git/config", "[core]\n" + "legacyHeaders=1\n");
206 db
.getConfig().load();
208 final Tree t
= new Tree(db
);
209 final FileTreeEntry f
= t
.addFile("i-am-a-file");
210 writeTrashFile(f
.getName(), "and this is the data in me\n");
211 t
.accept(new WriteTree(trash
, db
), TreeEntry
.MODIFIED_ONLY
);
213 new ObjectId("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"),
216 final Commit c
= new Commit(db
);
217 c
.setAuthor(new PersonIdent(jauthor
, 1154236443000L, -4 * 60));
218 c
.setCommitter(new PersonIdent(jcommitter
, 1154236443000L, -4 * 60));
219 c
.setMessage("A Commit\n");
221 assertEquals(t
.getTreeId(), c
.getTreeId());
223 final ObjectId cmtid
= new ObjectId(
224 "803aec4aba175e8ab1d666873c984c0308179099");
225 assertEquals(cmtid
, c
.getCommitId());
227 // Verify the commit we just wrote is in the correct format.
228 final XInputStream xis
= new XInputStream(new FileInputStream(db
232 assertEquals(0x78, xis
.readUInt8());
233 assertEquals(0x9c, xis
.readUInt8());
234 assertTrue(0x789c % 31 == 0);
241 // Verify we can read it.
242 final Commit c2
= db
.mapCommit(cmtid
);
244 assertEquals(c
.getMessage(), c2
.getMessage());
245 assertEquals(c
.getTreeId(), c2
.getTreeId());
246 assertEquals(c
.getAuthor(), c2
.getAuthor());
247 assertEquals(c
.getCommitter(), c2
.getCommitter());
250 public void test010_CreateCommitNewFormat() throws IOException
252 writeTrashFile(".git/config", "[core]\n" + "legacyHeaders=0\n");
253 db
.getConfig().load();
255 final Tree t
= new Tree(db
);
256 final FileTreeEntry f
= t
.addFile("i-am-a-file");
257 writeTrashFile(f
.getName(), "and this is the data in me\n");
258 t
.accept(new WriteTree(trash
, db
), TreeEntry
.MODIFIED_ONLY
);
260 new ObjectId("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"),
263 final Commit c
= new Commit(db
);
264 c
.setAuthor(new PersonIdent(jauthor
, 1154236443000L, -4 * 60));
265 c
.setCommitter(new PersonIdent(jcommitter
, 1154236443000L, -4 * 60));
266 c
.setMessage("A Commit\n");
268 assertEquals(t
.getTreeId(), c
.getTreeId());
270 final ObjectId cmtid
= new ObjectId(
271 "803aec4aba175e8ab1d666873c984c0308179099");
272 assertEquals(cmtid
, c
.getCommitId());
274 // Verify the commit we just wrote is in the correct format.
275 final XInputStream xis
= new XInputStream(new FileInputStream(db
279 // 'pack style' commit header: 177 bytes
280 assertEquals(0x91, xis
.readUInt8());
281 assertEquals(0x0b, xis
.readUInt8());
283 assertEquals(0x78, xis
.readUInt8());
284 assertTrue(((0x78 << 8) | xis
.readUInt8()) % 31 == 0);
291 // Verify we can read it.
292 final Commit c2
= db
.mapCommit(cmtid
);
294 assertEquals(c
.getMessage(), c2
.getMessage());
295 assertEquals(c
.getTreeId(), c2
.getTreeId());
296 assertEquals(c
.getAuthor(), c2
.getAuthor());
297 assertEquals(c
.getCommitter(), c2
.getCommitter());
300 public void test011_CreateCommitNewFormatIsDefault() throws IOException
302 db
.getConfig().load();
304 final Tree t
= new Tree(db
);
305 final FileTreeEntry f
= t
.addFile("i-am-a-file");
306 writeTrashFile(f
.getName(), "and this is the data in me\n");
307 t
.accept(new WriteTree(trash
, db
), TreeEntry
.MODIFIED_ONLY
);
309 new ObjectId("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"),
312 final Commit c
= new Commit(db
);
313 c
.setAuthor(new PersonIdent(jauthor
, 1154236443000L, -4 * 60));
314 c
.setCommitter(new PersonIdent(jcommitter
, 1154236443000L, -4 * 60));
315 c
.setMessage("A Commit\n");
317 assertEquals(t
.getTreeId(), c
.getTreeId());
319 final ObjectId cmtid
= new ObjectId(
320 "803aec4aba175e8ab1d666873c984c0308179099");
321 assertEquals(cmtid
, c
.getCommitId());
323 // Verify the commit we just wrote is in the correct format.
324 final XInputStream xis
= new XInputStream(new FileInputStream(db
328 // 'pack style' commit header: 177 bytes
329 assertEquals(0x91, xis
.readUInt8());
330 assertEquals(0x0b, xis
.readUInt8());
332 assertEquals(0x78, xis
.readUInt8());
333 assertTrue(((0x78 << 8) | xis
.readUInt8()) % 31 == 0);
340 // Verify we can read it.
341 final Commit c2
= db
.mapCommit(cmtid
);
343 assertEquals(c
.getMessage(), c2
.getMessage());
344 assertEquals(c
.getTreeId(), c2
.getTreeId());
345 assertEquals(c
.getAuthor(), c2
.getAuthor());
346 assertEquals(c
.getCommitter(), c2
.getCommitter());