diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Branch.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Branch.java index 045f3571e..bf6ee3a94 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Branch.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Branch.java @@ -310,24 +310,24 @@ class Branch extends TextBuiltin { throws IOException { String current = db.getBranch(); ObjectId head = db.resolve(Constants.HEAD); - for (String branch : branches) { - if (branch.equals(current)) { - throw die(MessageFormat.format(CLIText.get().cannotDeleteTheBranchWhichYouAreCurrentlyOn, branch)); + for (String b : branches) { + if (b.equals(current)) { + throw die(MessageFormat.format(CLIText.get().cannotDeleteTheBranchWhichYouAreCurrentlyOn, b)); } RefUpdate update = db.updateRef((remote ? Constants.R_REMOTES : Constants.R_HEADS) - + branch); + + b); update.setNewObjectId(head); update.setForceUpdate(force || remote); Result result = update.delete(); if (result == Result.REJECTED) { - throw die(MessageFormat.format(CLIText.get().branchIsNotAnAncestorOfYourCurrentHEAD, branch)); + throw die(MessageFormat.format(CLIText.get().branchIsNotAnAncestorOfYourCurrentHEAD, b)); } else if (result == Result.NEW) - throw die(MessageFormat.format(CLIText.get().branchNotFound, branch)); + throw die(MessageFormat.format(CLIText.get().branchNotFound, b)); if (remote) - outw.println(MessageFormat.format(CLIText.get().deletedRemoteBranch, branch)); + outw.println(MessageFormat.format(CLIText.get().deletedRemoteBranch, b)); else if (verbose) - outw.println(MessageFormat.format(CLIText.get().deletedBranch, branch)); + outw.println(MessageFormat.format(CLIText.get().deletedBranch, b)); } } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java index ad3ff60a0..d842046bb 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java @@ -69,23 +69,24 @@ public class ApplyCommandTest extends RepositoryTestCase { private ApplyResult init(final String name, final boolean preExists, final boolean postExists) throws Exception { - Git git = new Git(db); - - if (preExists) { - a = new RawText(readFile(name + "_PreImage")); - write(new File(db.getDirectory().getParent(), name), - a.getString(0, a.size(), false)); - - git.add().addFilepattern(name).call(); - git.commit().setMessage("PreImage").call(); + try (Git git = new Git(db)) { + if (preExists) { + a = new RawText(readFile(name + "_PreImage")); + write(new File(db.getDirectory().getParent(), name), + a.getString(0, a.size(), false)); + + git.add().addFilepattern(name).call(); + git.commit().setMessage("PreImage").call(); + } + + if (postExists) { + b = new RawText(readFile(name + "_PostImage")); + } + + return git + .apply() + .setPatch(getTestResource(name + ".patch")).call(); } - - if (postExists) - b = new RawText(readFile(name + "_PostImage")); - - return git - .apply() - .setPatch(getTestResource(name + ".patch")).call(); } @Test diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackFileTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackFileTest.java index cb80768e5..ba07d6842 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackFileTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackFileTest.java @@ -191,119 +191,122 @@ public class PackFileTest extends LocalDiskRepositoryTestCase { @Test public void testDelta_SmallObjectChain() throws Exception { - ObjectInserter.Formatter fmt = new ObjectInserter.Formatter(); - byte[] data0 = new byte[512]; - Arrays.fill(data0, (byte) 0xf3); - ObjectId id0 = fmt.idFor(Constants.OBJ_BLOB, data0); - - TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64 * 1024); - packHeader(pack, 4); - objectHeader(pack, Constants.OBJ_BLOB, data0.length); - deflate(pack, data0); - - byte[] data1 = clone(0x01, data0); - byte[] delta1 = delta(data0, data1); - ObjectId id1 = fmt.idFor(Constants.OBJ_BLOB, data1); - objectHeader(pack, Constants.OBJ_REF_DELTA, delta1.length); - id0.copyRawTo(pack); - deflate(pack, delta1); - - byte[] data2 = clone(0x02, data1); - byte[] delta2 = delta(data1, data2); - ObjectId id2 = fmt.idFor(Constants.OBJ_BLOB, data2); - objectHeader(pack, Constants.OBJ_REF_DELTA, delta2.length); - id1.copyRawTo(pack); - deflate(pack, delta2); - - byte[] data3 = clone(0x03, data2); - byte[] delta3 = delta(data2, data3); - ObjectId id3 = fmt.idFor(Constants.OBJ_BLOB, data3); - objectHeader(pack, Constants.OBJ_REF_DELTA, delta3.length); - id2.copyRawTo(pack); - deflate(pack, delta3); - - digest(pack); - PackParser ip = index(pack.toByteArray()); - ip.setAllowThin(true); - ip.parse(NullProgressMonitor.INSTANCE); - - assertTrue("has blob", wc.has(id3)); - - ObjectLoader ol = wc.open(id3); - assertNotNull("created loader", ol); - assertEquals(Constants.OBJ_BLOB, ol.getType()); - assertEquals(data3.length, ol.getSize()); - assertFalse("is large", ol.isLarge()); - assertNotNull(ol.getCachedBytes()); - assertArrayEquals(data3, ol.getCachedBytes()); - - ObjectStream in = ol.openStream(); - assertNotNull("have stream", in); - assertEquals(Constants.OBJ_BLOB, in.getType()); - assertEquals(data3.length, in.getSize()); - byte[] act = new byte[data3.length]; - IO.readFully(in, act, 0, data3.length); - assertTrue("same content", Arrays.equals(act, data3)); - assertEquals("stream at EOF", -1, in.read()); - in.close(); + try (ObjectInserter.Formatter fmt = new ObjectInserter.Formatter()) { + byte[] data0 = new byte[512]; + Arrays.fill(data0, (byte) 0xf3); + ObjectId id0 = fmt.idFor(Constants.OBJ_BLOB, data0); + + TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64 * 1024); + packHeader(pack, 4); + objectHeader(pack, Constants.OBJ_BLOB, data0.length); + deflate(pack, data0); + + byte[] data1 = clone(0x01, data0); + byte[] delta1 = delta(data0, data1); + ObjectId id1 = fmt.idFor(Constants.OBJ_BLOB, data1); + objectHeader(pack, Constants.OBJ_REF_DELTA, delta1.length); + id0.copyRawTo(pack); + deflate(pack, delta1); + + byte[] data2 = clone(0x02, data1); + byte[] delta2 = delta(data1, data2); + ObjectId id2 = fmt.idFor(Constants.OBJ_BLOB, data2); + objectHeader(pack, Constants.OBJ_REF_DELTA, delta2.length); + id1.copyRawTo(pack); + deflate(pack, delta2); + + byte[] data3 = clone(0x03, data2); + byte[] delta3 = delta(data2, data3); + ObjectId id3 = fmt.idFor(Constants.OBJ_BLOB, data3); + objectHeader(pack, Constants.OBJ_REF_DELTA, delta3.length); + id2.copyRawTo(pack); + deflate(pack, delta3); + + digest(pack); + PackParser ip = index(pack.toByteArray()); + ip.setAllowThin(true); + ip.parse(NullProgressMonitor.INSTANCE); + + assertTrue("has blob", wc.has(id3)); + + ObjectLoader ol = wc.open(id3); + assertNotNull("created loader", ol); + assertEquals(Constants.OBJ_BLOB, ol.getType()); + assertEquals(data3.length, ol.getSize()); + assertFalse("is large", ol.isLarge()); + assertNotNull(ol.getCachedBytes()); + assertArrayEquals(data3, ol.getCachedBytes()); + + ObjectStream in = ol.openStream(); + assertNotNull("have stream", in); + assertEquals(Constants.OBJ_BLOB, in.getType()); + assertEquals(data3.length, in.getSize()); + byte[] act = new byte[data3.length]; + IO.readFully(in, act, 0, data3.length); + assertTrue("same content", Arrays.equals(act, data3)); + assertEquals("stream at EOF", -1, in.read()); + in.close(); + } } @Test public void testDelta_FailsOver2GiB() throws Exception { - ObjectInserter.Formatter fmt = new ObjectInserter.Formatter(); - byte[] base = new byte[] { 'a' }; - ObjectId idA = fmt.idFor(Constants.OBJ_BLOB, base); - ObjectId idB = fmt.idFor(Constants.OBJ_BLOB, new byte[] { 'b' }); - - PackedObjectInfo a = new PackedObjectInfo(idA); - PackedObjectInfo b = new PackedObjectInfo(idB); - - TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64 * 1024); - packHeader(pack, 2); - a.setOffset(pack.length()); - objectHeader(pack, Constants.OBJ_BLOB, base.length); - deflate(pack, base); - - ByteArrayOutputStream tmp = new ByteArrayOutputStream(); - DeltaEncoder de = new DeltaEncoder(tmp, base.length, 3L << 30); - de.copy(0, 1); - byte[] delta = tmp.toByteArray(); - b.setOffset(pack.length()); - objectHeader(pack, Constants.OBJ_REF_DELTA, delta.length); - idA.copyRawTo(pack); - deflate(pack, delta); - byte[] footer = digest(pack); - - File dir = new File(repo.getObjectDatabase().getDirectory(), "pack"); - File packName = new File(dir, idA.name() + ".pack"); - File idxName = new File(dir, idA.name() + ".idx"); - - FileOutputStream f = new FileOutputStream(packName); - try { - f.write(pack.toByteArray()); - } finally { - f.close(); - } - - f = new FileOutputStream(idxName); - try { - List list = new ArrayList(); - list.add(a); - list.add(b); - Collections.sort(list); - new PackIndexWriterV1(f).write(list, footer); - } finally { - f.close(); - } - - PackFile packFile = new PackFile(packName, PackExt.INDEX.getBit()); - try { - packFile.get(wc, b); - fail("expected LargeObjectException.ExceedsByteArrayLimit"); - } catch (LargeObjectException.ExceedsByteArrayLimit bad) { - assertNull(bad.getObjectId()); - } finally { - packFile.close(); + try (ObjectInserter.Formatter fmt = new ObjectInserter.Formatter()) { + byte[] base = new byte[] { 'a' }; + ObjectId idA = fmt.idFor(Constants.OBJ_BLOB, base); + ObjectId idB = fmt.idFor(Constants.OBJ_BLOB, new byte[] { 'b' }); + + PackedObjectInfo a = new PackedObjectInfo(idA); + PackedObjectInfo b = new PackedObjectInfo(idB); + + TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64 * 1024); + packHeader(pack, 2); + a.setOffset(pack.length()); + objectHeader(pack, Constants.OBJ_BLOB, base.length); + deflate(pack, base); + + ByteArrayOutputStream tmp = new ByteArrayOutputStream(); + DeltaEncoder de = new DeltaEncoder(tmp, base.length, 3L << 30); + de.copy(0, 1); + byte[] delta = tmp.toByteArray(); + b.setOffset(pack.length()); + objectHeader(pack, Constants.OBJ_REF_DELTA, delta.length); + idA.copyRawTo(pack); + deflate(pack, delta); + byte[] footer = digest(pack); + + File dir = new File(repo.getObjectDatabase().getDirectory(), + "pack"); + File packName = new File(dir, idA.name() + ".pack"); + File idxName = new File(dir, idA.name() + ".idx"); + + FileOutputStream f = new FileOutputStream(packName); + try { + f.write(pack.toByteArray()); + } finally { + f.close(); + } + + f = new FileOutputStream(idxName); + try { + List list = new ArrayList(); + list.add(a); + list.add(b); + Collections.sort(list); + new PackIndexWriterV1(f).write(list, footer); + } finally { + f.close(); + } + + PackFile packFile = new PackFile(packName, PackExt.INDEX.getBit()); + try { + packFile.get(wc, b); + fail("expected LargeObjectException.ExceedsByteArrayLimit"); + } catch (LargeObjectException.ExceedsByteArrayLimit bad) { + assertNull(bad.getObjectId()); + } finally { + packFile.close(); + } } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java index 92901f826..c1b882a65 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java @@ -140,52 +140,53 @@ public class DirCacheCheckoutTest extends RepositoryTestCase { @Test public void testResetHard() throws IOException, NoFilepatternException, GitAPIException { - Git git = new Git(db); - writeTrashFile("f", "f()"); - writeTrashFile("D/g", "g()"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("inital").call(); - assertIndex(mkmap("f", "f()", "D/g", "g()")); - - git.branchCreate().setName("topic").call(); - - writeTrashFile("f", "f()\nmaster"); - writeTrashFile("D/g", "g()\ng2()"); - writeTrashFile("E/h", "h()"); - git.add().addFilepattern(".").call(); - RevCommit master = git.commit().setMessage("master-1").call(); - assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()")); - - checkoutBranch("refs/heads/topic"); - assertIndex(mkmap("f", "f()", "D/g", "g()")); - - writeTrashFile("f", "f()\nside"); - assertTrue(new File(db.getWorkTree(), "D/g").delete()); - writeTrashFile("G/i", "i()"); - git.add().addFilepattern(".").call(); - git.add().addFilepattern(".").setUpdate(true).call(); - RevCommit topic = git.commit().setMessage("topic-1").call(); - assertIndex(mkmap("f", "f()\nside", "G/i", "i()")); - - writeTrashFile("untracked", "untracked"); - - resetHard(master); - assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()")); - resetHard(topic); - assertIndex(mkmap("f", "f()\nside", "G/i", "i()")); - assertWorkDir(mkmap("f", "f()\nside", "G/i", "i()", "untracked", - "untracked")); - - assertEquals(MergeStatus.CONFLICTING, git.merge().include(master) - .call().getMergeStatus()); - assertEquals( - "[D/g, mode:100644, stage:1][D/g, mode:100644, stage:3][E/h, mode:100644][G/i, mode:100644][f, mode:100644, stage:1][f, mode:100644, stage:2][f, mode:100644, stage:3]", - indexState(0)); - - resetHard(master); - assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()")); - assertWorkDir(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", - "h()", "untracked", "untracked")); + try (Git git = new Git(db)) { + writeTrashFile("f", "f()"); + writeTrashFile("D/g", "g()"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("inital").call(); + assertIndex(mkmap("f", "f()", "D/g", "g()")); + + git.branchCreate().setName("topic").call(); + + writeTrashFile("f", "f()\nmaster"); + writeTrashFile("D/g", "g()\ng2()"); + writeTrashFile("E/h", "h()"); + git.add().addFilepattern(".").call(); + RevCommit master = git.commit().setMessage("master-1").call(); + assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()")); + + checkoutBranch("refs/heads/topic"); + assertIndex(mkmap("f", "f()", "D/g", "g()")); + + writeTrashFile("f", "f()\nside"); + assertTrue(new File(db.getWorkTree(), "D/g").delete()); + writeTrashFile("G/i", "i()"); + git.add().addFilepattern(".").call(); + git.add().addFilepattern(".").setUpdate(true).call(); + RevCommit topic = git.commit().setMessage("topic-1").call(); + assertIndex(mkmap("f", "f()\nside", "G/i", "i()")); + + writeTrashFile("untracked", "untracked"); + + resetHard(master); + assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()")); + resetHard(topic); + assertIndex(mkmap("f", "f()\nside", "G/i", "i()")); + assertWorkDir(mkmap("f", "f()\nside", "G/i", "i()", "untracked", + "untracked")); + + assertEquals(MergeStatus.CONFLICTING, git.merge().include(master) + .call().getMergeStatus()); + assertEquals( + "[D/g, mode:100644, stage:1][D/g, mode:100644, stage:3][E/h, mode:100644][G/i, mode:100644][f, mode:100644, stage:1][f, mode:100644, stage:2][f, mode:100644, stage:3]", + indexState(0)); + + resetHard(master); + assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()")); + assertWorkDir(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", + "h()", "untracked", "untracked")); + } } /** @@ -200,21 +201,22 @@ public class DirCacheCheckoutTest extends RepositoryTestCase { @Test public void testResetHardFromIndexEntryWithoutFileToTreeWithoutFile() throws Exception { - Git git = new Git(db); - writeTrashFile("x", "x"); - git.add().addFilepattern("x").call(); - RevCommit id1 = git.commit().setMessage("c1").call(); - - writeTrashFile("f/g", "f/g"); - git.rm().addFilepattern("x").call(); - git.add().addFilepattern("f/g").call(); - git.commit().setMessage("c2").call(); - deleteTrashFile("f/g"); - deleteTrashFile("f"); - - // The actual test - git.reset().setMode(ResetType.HARD).setRef(id1.getName()).call(); - assertIndex(mkmap("x", "x")); + try (Git git = new Git(db)) { + writeTrashFile("x", "x"); + git.add().addFilepattern("x").call(); + RevCommit id1 = git.commit().setMessage("c1").call(); + + writeTrashFile("f/g", "f/g"); + git.rm().addFilepattern("x").call(); + git.add().addFilepattern("f/g").call(); + git.commit().setMessage("c2").call(); + deleteTrashFile("f/g"); + deleteTrashFile("f"); + + // The actual test + git.reset().setMode(ResetType.HARD).setRef(id1.getName()).call(); + assertIndex(mkmap("x", "x")); + } } /** @@ -224,14 +226,14 @@ public class DirCacheCheckoutTest extends RepositoryTestCase { */ @Test public void testInitialCheckout() throws Exception { - Git git = new Git(db); - - TestRepository db_t = new TestRepository(db); - BranchBuilder master = db_t.branch("master"); - master.commit().add("f", "1").message("m0").create(); - assertFalse(new File(db.getWorkTree(), "f").exists()); - git.checkout().setName("master").call(); - assertTrue(new File(db.getWorkTree(), "f").exists()); + try (Git git = new Git(db)) { + TestRepository db_t = new TestRepository(db); + BranchBuilder master = db_t.branch("master"); + master.commit().add("f", "1").message("m0").create(); + assertFalse(new File(db.getWorkTree(), "f").exists()); + git.checkout().setName("master").call(); + assertTrue(new File(db.getWorkTree(), "f").exists()); + } } private DirCacheCheckout resetHard(RevCommit commit) @@ -1612,46 +1614,47 @@ public class DirCacheCheckoutTest extends RepositoryTestCase { public void assertWorkDir(Map i) throws CorruptObjectException, IOException { - TreeWalk walk = new TreeWalk(db); - walk.setRecursive(false); - walk.addTree(new FileTreeIterator(db)); - String expectedValue; - String path; - int nrFiles = 0; - FileTreeIterator ft; - while (walk.next()) { - ft = walk.getTree(0, FileTreeIterator.class); - path = ft.getEntryPathString(); - expectedValue = i.get(path); - File file = new File(db.getWorkTree(), path); - assertTrue(file.exists()); - if (file.isFile()) { - assertNotNull("found unexpected file for path " + path - + " in workdir", expectedValue); - FileInputStream is = new FileInputStream(file); - byte[] buffer = new byte[(int) file.length()]; - int offset = 0; - int numRead = 0; - while (offset < buffer.length - && (numRead = is.read(buffer, offset, buffer.length - - offset)) >= 0) { - offset += numRead; - } - is.close(); - assertArrayEquals("unexpected content for path " + path - + " in workDir. ", buffer, i.get(path).getBytes()); - nrFiles++; - } else if (file.isDirectory()) { - if (file.list().length == 0) { - assertEquals("found unexpected empty folder for path " - + path + " in workDir. ", "/", i.get(path)); + try (TreeWalk walk = new TreeWalk(db)) { + walk.setRecursive(false); + walk.addTree(new FileTreeIterator(db)); + String expectedValue; + String path; + int nrFiles = 0; + FileTreeIterator ft; + while (walk.next()) { + ft = walk.getTree(0, FileTreeIterator.class); + path = ft.getEntryPathString(); + expectedValue = i.get(path); + File file = new File(db.getWorkTree(), path); + assertTrue(file.exists()); + if (file.isFile()) { + assertNotNull("found unexpected file for path " + path + + " in workdir", expectedValue); + FileInputStream is = new FileInputStream(file); + byte[] buffer = new byte[(int) file.length()]; + int offset = 0; + int numRead = 0; + while (offset < buffer.length + && (numRead = is.read(buffer, offset, buffer.length + - offset)) >= 0) { + offset += numRead; + } + is.close(); + assertArrayEquals("unexpected content for path " + path + + " in workDir. ", buffer, i.get(path).getBytes()); nrFiles++; + } else if (file.isDirectory()) { + if (file.list().length == 0) { + assertEquals("found unexpected empty folder for path " + + path + " in workDir. ", "/", i.get(path)); + nrFiles++; + } + } + if (walk.isSubtree()) { + walk.enterSubtree(); } } - if (walk.isSubtree()) { - walk.enterSubtree(); - } + assertEquals("WorkDir has not the right size.", i.size(), nrFiles); } - assertEquals("WorkDir has not the right size.", i.size(), nrFiles); } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java index b5057ad28..0abb8ba4e 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java @@ -312,7 +312,7 @@ public class CommitCommand extends GitCommand { } } - private void insertChangeId(ObjectId treeId) throws IOException { + private void insertChangeId(ObjectId treeId) { ObjectId firstParentId = null; if (!parents.isEmpty()) firstParentId = parents.get(0);