Support tag objects
[egit.git] / org.spearce.jgit / tst / org / spearce / jgit / lib / T0003_Basic.java
blob9e6f8057e8120b70f988658416121d8531c47a08
1 /*
2 * Copyright (C) 2006 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org.spearce.jgit.lib;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileReader;
22 import java.io.FileWriter;
23 import java.io.IOException;
25 public class T0003_Basic extends RepositoryTestCase {
26 public void test001_Initalize() {
27 final File gitdir = new File(trash, ".git");
28 final File objects = new File(gitdir, "objects");
29 final File objects_pack = new File(objects, "pack");
30 final File objects_info = new File(objects, "info");
31 final File refs = new File(gitdir, "refs");
32 final File refs_heads = new File(refs, "heads");
33 final File refs_tags = new File(refs, "tags");
34 final File HEAD = new File(gitdir, "HEAD");
36 assertTrue("Exists " + trash, trash.isDirectory());
37 assertTrue("Exists " + objects, objects.isDirectory());
38 assertTrue("Exists " + objects_pack, objects_pack.isDirectory());
39 assertTrue("Exists " + objects_info, objects_info.isDirectory());
40 assertEquals(2, objects.listFiles().length);
41 assertTrue("Exists " + refs, refs.isDirectory());
42 assertTrue("Exists " + refs_heads, refs_heads.isDirectory());
43 assertTrue("Exists " + refs_tags, refs_tags.isDirectory());
44 assertTrue("Exists " + HEAD, HEAD.isFile());
45 assertEquals(23, HEAD.length());
48 public void test002_WriteEmptyTree() throws IOException {
49 // One of our test packs contains the empty tree object. If the pack is
50 // open when we create it we won't write the object file out as a loose
51 // object (as it already exists in the pack).
53 db.closePacks();
55 final Tree t = new Tree(db);
56 t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
57 assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", t.getId()
58 .toString());
59 final File o = new File(new File(new File(trash_git, "objects"), "4b"),
60 "825dc642cb6eb9a060e54bf8d69288fbee4904");
61 assertTrue("Exists " + o, o.isFile());
62 assertTrue("Read-only " + o, !o.canWrite());
63 assertEquals(9, o.length());
66 public void test002_WriteEmptyTree2() throws IOException {
67 // File shouldn't exist as it is in a test pack.
69 final Tree t = new Tree(db);
70 t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
71 assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", t.getId()
72 .toString());
73 final File o = new File(new File(new File(trash_git, "objects"), "4b"),
74 "825dc642cb6eb9a060e54bf8d69288fbee4904");
75 assertFalse("Exists " + o, o.isFile());
78 public void test003_WriteShouldBeEmptyTree() throws IOException {
79 final Tree t = new Tree(db);
80 final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
81 t.addFile("should-be-empty").setId(emptyId);
82 t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
83 assertEquals("7bb943559a305bdd6bdee2cef6e5df2413c3d30a", t.getId()
84 .toString());
86 File o;
87 o = new File(new File(new File(trash_git, "objects"), "7b"),
88 "b943559a305bdd6bdee2cef6e5df2413c3d30a");
89 assertTrue("Exists " + o, o.isFile());
90 assertTrue("Read-only " + o, !o.canWrite());
92 o = new File(new File(new File(trash_git, "objects"), "e6"),
93 "9de29bb2d1d6434b8b29ae775ad8c2e48c5391");
94 assertTrue("Exists " + o, o.isFile());
95 assertTrue("Read-only " + o, !o.canWrite());
98 public void test004_CheckNewConfig() throws IOException {
99 final RepositoryConfig c = db.getConfig();
100 assertNotNull(c);
101 assertEquals("0", c.getString("core", "repositoryformatversion"));
102 assertEquals("0", c.getString("CoRe", "REPOSITORYFoRmAtVeRsIoN"));
103 assertEquals("true", c.getString("core", "filemode"));
104 assertEquals("true", c.getString("cOrE", "fIlEModE"));
105 assertNull(c.getString("notavalue", "reallyNotAValue"));
106 c.load();
109 public void test005_ReadSimpleConfig() throws IOException {
110 final RepositoryConfig c = db.getConfig();
111 assertNotNull(c);
112 c.load();
113 assertEquals("0", c.getString("core", "repositoryformatversion"));
114 assertEquals("0", c.getString("CoRe", "REPOSITORYFoRmAtVeRsIoN"));
115 assertEquals("true", c.getString("core", "filemode"));
116 assertEquals("true", c.getString("cOrE", "fIlEModE"));
117 assertNull(c.getString("notavalue", "reallyNotAValue"));
120 public void test006_ReadUglyConfig() throws IOException {
121 final RepositoryConfig c = db.getConfig();
122 final File cfg = new File(db.getDirectory(), "config");
123 final FileWriter pw = new FileWriter(cfg);
124 final String configStr = " [core];comment\n\tfilemode = yes\n"
125 + "[user]\n"
126 + " email = A U Thor <thor@example.com> # Just an example...\n"
127 + " name = \"A Thor \\\\ \\\"\\t \"\n"
128 + " defaultCheckInComment = a many line\\n\\\ncomment\\n\\\n"
129 + " to test\n";
130 pw.write(configStr);
131 pw.close();
132 c.load();
133 assertEquals("yes", c.getString("core", "filemode"));
134 assertEquals("A U Thor <thor@example.com>", c
135 .getString("user", "email"));
136 assertEquals("A Thor \\ \"\t ", c.getString("user", "name"));
137 assertEquals("a many line\ncomment\n to test", c.getString("user",
138 "defaultCheckInComment"));
139 c.save();
140 final FileReader fr = new FileReader(cfg);
141 final char[] cbuf = new char[configStr.length()];
142 fr.read(cbuf);
143 fr.close();
144 assertEquals(configStr, new String(cbuf));
147 public void test007_Open() throws IOException {
148 final Repository db2 = new Repository(db.getDirectory());
149 assertEquals(db.getDirectory(), db2.getDirectory());
150 assertEquals(db.getObjectsDirectory(), db2.getObjectsDirectory());
151 assertNotSame(db.getConfig(), db2.getConfig());
154 public void test008_FailOnWrongVersion() throws IOException {
155 final File cfg = new File(db.getDirectory(), "config");
156 final FileWriter pw = new FileWriter(cfg);
157 final String badvers = "ihopethisisneveraversion";
158 final String configStr = "[core]\n" + "\trepositoryFormatVersion="
159 + badvers + "\n";
160 pw.write(configStr);
161 pw.close();
163 try {
164 new Repository(db.getDirectory());
165 fail("incorrectly opened a bad repository");
166 } catch (IOException ioe) {
167 assertTrue(ioe.getMessage().indexOf("format") > 0);
168 assertTrue(ioe.getMessage().indexOf(badvers) > 0);
172 public void test009_CreateCommitOldFormat() throws IOException {
173 writeTrashFile(".git/config", "[core]\n" + "legacyHeaders=1\n");
174 db.getConfig().load();
176 final Tree t = new Tree(db);
177 final FileTreeEntry f = t.addFile("i-am-a-file");
178 writeTrashFile(f.getName(), "and this is the data in me\n");
179 t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
180 assertEquals(new ObjectId("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"),
181 t.getTreeId());
183 final Commit c = new Commit(db);
184 c.setAuthor(new PersonIdent(jauthor, 1154236443000L, -4 * 60));
185 c.setCommitter(new PersonIdent(jcommitter, 1154236443000L, -4 * 60));
186 c.setMessage("A Commit\n");
187 c.setTree(t);
188 assertEquals(t.getTreeId(), c.getTreeId());
189 c.commit();
190 final ObjectId cmtid = new ObjectId(
191 "803aec4aba175e8ab1d666873c984c0308179099");
192 assertEquals(cmtid, c.getCommitId());
194 // Verify the commit we just wrote is in the correct format.
195 final XInputStream xis = new XInputStream(new FileInputStream(db
196 .toFile(cmtid)));
197 try {
198 assertEquals(0x78, xis.readUInt8());
199 assertEquals(0x9c, xis.readUInt8());
200 assertTrue(0x789c % 31 == 0);
201 } finally {
202 xis.close();
205 // Verify we can read it.
206 final Commit c2 = db.mapCommit(cmtid);
207 assertNotNull(c2);
208 assertEquals(c.getMessage(), c2.getMessage());
209 assertEquals(c.getTreeId(), c2.getTreeId());
210 assertEquals(c.getAuthor(), c2.getAuthor());
211 assertEquals(c.getCommitter(), c2.getCommitter());
214 public void test010_CreateCommitNewFormat() throws IOException {
215 writeTrashFile(".git/config", "[core]\n" + "legacyHeaders=0\n");
216 db.getConfig().load();
218 final Tree t = new Tree(db);
219 final FileTreeEntry f = t.addFile("i-am-a-file");
220 writeTrashFile(f.getName(), "and this is the data in me\n");
221 t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
222 assertEquals(new ObjectId("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"),
223 t.getTreeId());
225 final Commit c = new Commit(db);
226 c.setAuthor(new PersonIdent(jauthor, 1154236443000L, -4 * 60));
227 c.setCommitter(new PersonIdent(jcommitter, 1154236443000L, -4 * 60));
228 c.setMessage("A Commit\n");
229 c.setTree(t);
230 assertEquals(t.getTreeId(), c.getTreeId());
231 c.commit();
232 final ObjectId cmtid = new ObjectId(
233 "803aec4aba175e8ab1d666873c984c0308179099");
234 assertEquals(cmtid, c.getCommitId());
236 // Verify the commit we just wrote is in the correct format.
237 final XInputStream xis = new XInputStream(new FileInputStream(db
238 .toFile(cmtid)));
239 try {
240 // 'pack style' commit header: 177 bytes
241 assertEquals(0x91, xis.readUInt8());
242 assertEquals(0x0b, xis.readUInt8());
243 // zlib stream start
244 assertEquals(0x78, xis.readUInt8());
245 assertTrue(((0x78 << 8) | xis.readUInt8()) % 31 == 0);
246 } finally {
247 xis.close();
250 // Verify we can read it.
251 final Commit c2 = db.mapCommit(cmtid);
252 assertNotNull(c2);
253 assertEquals(c.getMessage(), c2.getMessage());
254 assertEquals(c.getTreeId(), c2.getTreeId());
255 assertEquals(c.getAuthor(), c2.getAuthor());
256 assertEquals(c.getCommitter(), c2.getCommitter());
259 public void test011_CreateCommitNewFormatIsDefault() throws IOException {
260 db.getConfig().load();
262 final Tree t = new Tree(db);
263 final FileTreeEntry f = t.addFile("i-am-a-file");
264 writeTrashFile(f.getName(), "and this is the data in me\n");
265 t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
266 assertEquals(new ObjectId("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"),
267 t.getTreeId());
269 final Commit c = new Commit(db);
270 c.setAuthor(new PersonIdent(jauthor, 1154236443000L, -4 * 60));
271 c.setCommitter(new PersonIdent(jcommitter, 1154236443000L, -4 * 60));
272 c.setMessage("A Commit\n");
273 c.setTree(t);
274 assertEquals(t.getTreeId(), c.getTreeId());
275 c.commit();
276 final ObjectId cmtid = new ObjectId(
277 "803aec4aba175e8ab1d666873c984c0308179099");
278 assertEquals(cmtid, c.getCommitId());
280 // Verify the commit we just wrote is in the correct format.
281 final XInputStream xis = new XInputStream(new FileInputStream(db
282 .toFile(cmtid)));
283 try {
284 // 'pack style' commit header: 177 bytes
285 assertEquals(0x91, xis.readUInt8());
286 assertEquals(0x0b, xis.readUInt8());
287 // zlib stream start
288 assertEquals(0x78, xis.readUInt8());
289 assertTrue(((0x78 << 8) | xis.readUInt8()) % 31 == 0);
290 } finally {
291 xis.close();
294 // Verify we can read it.
295 final Commit c2 = db.mapCommit(cmtid);
296 assertNotNull(c2);
297 assertEquals(c.getMessage(), c2.getMessage());
298 assertEquals(c.getTreeId(), c2.getTreeId());
299 assertEquals(c.getAuthor(), c2.getAuthor());
300 assertEquals(c.getCommitter(), c2.getCommitter());
303 public void test012_SubtreeExternalSorting() throws IOException {
304 final ObjectId emptyBlob = new ObjectWriter(db).writeBlob(new byte[0]);
305 final Tree t = new Tree(db);
306 final FileTreeEntry e0 = t.addFile("a-");
307 final FileTreeEntry e1 = t.addFile("a-b");
308 final FileTreeEntry e2 = t.addFile("a/b");
309 final FileTreeEntry e3 = t.addFile("a=");
310 final FileTreeEntry e4 = t.addFile("a=b");
312 e0.setId(emptyBlob);
313 e1.setId(emptyBlob);
314 e2.setId(emptyBlob);
315 e3.setId(emptyBlob);
316 e4.setId(emptyBlob);
318 t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
319 assertEquals(new ObjectId("b47a8f0a4190f7572e11212769090523e23eb1ea"),
320 t.getId());
323 public void test020_createBlobTag() throws IOException {
324 final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
325 final Tag t = new Tag(db);
326 t.setObjId(emptyId);
327 t.setType("blob");
328 t.setTag("test020");
329 t.setAuthor(new PersonIdent(jauthor, 1154236443000L, -4 * 60));
330 t.setMessage("test020 tagged\n");
331 t.tag();
332 assertEquals("6759556b09fbb4fd8ae5e315134481cc25d46954", t.getTagId().toString());
334 Tag mapTag = db.mapTag("6759556b09fbb4fd8ae5e315134481cc25d46954");
335 assertEquals("blob", mapTag.getType());
336 assertEquals("test020 tagged\n", mapTag.getMessage());
337 assertEquals(new PersonIdent(jauthor, 1154236443000L, -4 * 60), mapTag.getAuthor());
338 assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag.getObjId().toString());
341 public void test021_createTreeTag() throws IOException {
342 final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
343 final Tree almostEmptyTree = new Tree(db);
344 almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
345 final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
346 final Tag t = new Tag(db);
347 t.setObjId(almostEmptyTreeId);
348 t.setType("tree");
349 t.setTag("test021");
350 t.setAuthor(new PersonIdent(jauthor, 1154236443000L, -4 * 60));
351 t.setMessage("test021 tagged\n");
352 t.tag();
353 assertEquals("b0517bc8dbe2096b419d42424cd7030733f4abe5", t.getTagId().toString());
355 Tag mapTag = db.mapTag("b0517bc8dbe2096b419d42424cd7030733f4abe5");
356 assertEquals("tree", mapTag.getType());
357 assertEquals("test021 tagged\n", mapTag.getMessage());
358 assertEquals(new PersonIdent(jauthor, 1154236443000L, -4 * 60), mapTag.getAuthor());
359 assertEquals("417c01c8795a35b8e835113a85a5c0c1c77f67fb", mapTag.getObjId().toString());
362 public void test022_createCommitTag() throws IOException {
363 final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
364 final Tree almostEmptyTree = new Tree(db);
365 almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
366 final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
367 final Commit almostEmptyCommit = new Commit(db);
368 almostEmptyCommit.setAuthor(new PersonIdent(jauthor, 1154236443000L, -2 * 60)); // not exactly the same
369 almostEmptyCommit.setCommitter(new PersonIdent(jauthor, 1154236443000L, -2 * 60));
370 almostEmptyCommit.setMessage("test022\n");
371 almostEmptyCommit.setTreeId(almostEmptyTreeId);
372 ObjectId almostEmptyCommitId = new ObjectWriter(db).writeCommit(almostEmptyCommit);
373 final Tag t = new Tag(db);
374 t.setObjId(almostEmptyCommitId);
375 t.setType("commit");
376 t.setTag("test022");
377 t.setAuthor(new PersonIdent(jauthor, 1154236443000L, -4 * 60));
378 t.setMessage("test022 tagged\n");
379 t.tag();
380 assertEquals("0ce2ebdb36076ef0b38adbe077a07d43b43e3807", t.getTagId().toString());
382 Tag mapTag = db.mapTag("0ce2ebdb36076ef0b38adbe077a07d43b43e3807");
383 assertEquals("commit", mapTag.getType());
384 assertEquals("test022 tagged\n", mapTag.getMessage());
385 assertEquals(new PersonIdent(jauthor, 1154236443000L, -4 * 60), mapTag.getAuthor());
386 assertEquals("b5d3b45a96b340441f5abb9080411705c51cc86c", mapTag.getObjId().toString());