Merge branch 'stable-4.2'

* stable-4.2:
  DirCacheCheckoutTest: Open Git and TreeWalk in try-with-resource
  CommitCommand: Remove declaration of unthrown exception
  Branch: Fix variable hiding warning
  ApplyCommandTest: Open Git in try-with-resource
  PackFileTest: Open ObjectInserter.Formatter in try-with-resource

Change-Id: I8484b10fad5a4c35fcfaedc1cdf8ccf97471618e
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
Matthias Sohn 2016-02-03 10:30:38 +01:00
commit b0facdc113
5 changed files with 220 additions and 213 deletions

View File

@ -310,24 +310,24 @@ private void delete(List<String> branches, boolean force)
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));
}
}
}

View File

@ -69,8 +69,7 @@ private ApplyResult init(final String name) throws Exception {
private ApplyResult init(final String name, final boolean preExists,
final boolean postExists) throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
if (preExists) {
a = new RawText(readFile(name + "_PreImage"));
write(new File(db.getDirectory().getParent(), name),
@ -80,13 +79,15 @@ private ApplyResult init(final String name, final boolean preExists,
git.commit().setMessage("PreImage").call();
}
if (postExists)
if (postExists) {
b = new RawText(readFile(name + "_PostImage"));
}
return git
.apply()
.setPatch(getTestResource(name + ".patch")).call();
}
}
@Test
public void testAddA1() throws Exception {

View File

@ -191,7 +191,7 @@ public void testWhole_LargeObject() throws Exception {
@Test
public void testDelta_SmallObjectChain() throws Exception {
ObjectInserter.Formatter fmt = new ObjectInserter.Formatter();
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);
@ -247,10 +247,11 @@ public void testDelta_SmallObjectChain() throws Exception {
assertEquals("stream at EOF", -1, in.read());
in.close();
}
}
@Test
public void testDelta_FailsOver2GiB() throws Exception {
ObjectInserter.Formatter fmt = new ObjectInserter.Formatter();
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' });
@ -274,7 +275,8 @@ public void testDelta_FailsOver2GiB() throws Exception {
deflate(pack, delta);
byte[] footer = digest(pack);
File dir = new File(repo.getObjectDatabase().getDirectory(), "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");
@ -306,6 +308,7 @@ public void testDelta_FailsOver2GiB() throws Exception {
packFile.close();
}
}
}
private static byte[] clone(int first, byte[] base) {
byte[] r = new byte[base.length];

View File

@ -140,7 +140,7 @@ private static HashMap<String, String> mkmap(String... args) {
@Test
public void testResetHard() throws IOException, NoFilepatternException,
GitAPIException {
Git git = new Git(db);
try (Git git = new Git(db)) {
writeTrashFile("f", "f()");
writeTrashFile("D/g", "g()");
git.add().addFilepattern(".").call();
@ -187,6 +187,7 @@ public void testResetHard() throws IOException, NoFilepatternException,
assertWorkDir(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h",
"h()", "untracked", "untracked"));
}
}
/**
* Reset hard from unclean condition.
@ -200,7 +201,7 @@ public void testResetHard() throws IOException, NoFilepatternException,
@Test
public void testResetHardFromIndexEntryWithoutFileToTreeWithoutFile()
throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
writeTrashFile("x", "x");
git.add().addFilepattern("x").call();
RevCommit id1 = git.commit().setMessage("c1").call();
@ -216,6 +217,7 @@ public void testResetHardFromIndexEntryWithoutFileToTreeWithoutFile()
git.reset().setMode(ResetType.HARD).setRef(id1.getName()).call();
assertIndex(mkmap("x", "x"));
}
}
/**
* Test first checkout in a repo
@ -224,8 +226,7 @@ public void testResetHardFromIndexEntryWithoutFileToTreeWithoutFile()
*/
@Test
public void testInitialCheckout() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
TestRepository<Repository> db_t = new TestRepository<Repository>(db);
BranchBuilder master = db_t.branch("master");
master.commit().add("f", "1").message("m0").create();
@ -233,6 +234,7 @@ public void testInitialCheckout() throws Exception {
git.checkout().setName("master").call();
assertTrue(new File(db.getWorkTree(), "f").exists());
}
}
private DirCacheCheckout resetHard(RevCommit commit)
throws NoWorkTreeException,
@ -1612,7 +1614,7 @@ public void testFileModeChangeAndContentChangeNoConflict() throws Exception {
public void assertWorkDir(Map<String, String> i)
throws CorruptObjectException,
IOException {
TreeWalk walk = new TreeWalk(db);
try (TreeWalk walk = new TreeWalk(db)) {
walk.setRecursive(false);
walk.addTree(new FileTreeIterator(db));
String expectedValue;
@ -1654,4 +1656,5 @@ public void assertWorkDir(Map<String, String> i)
}
assertEquals("WorkDir has not the right size.", i.size(), nrFiles);
}
}
}

View File

@ -312,7 +312,7 @@ public RevCommit call() throws GitAPIException, NoHeadException,
}
}
private void insertChangeId(ObjectId treeId) throws IOException {
private void insertChangeId(ObjectId treeId) {
ObjectId firstParentId = null;
if (!parents.isEmpty())
firstParentId = parents.get(0);