Browse Source

Adds DiffEntry.scan(TreeWalk, boolean) method

Adds method into DiffEntry class that allows to specify whether changed
trees are included in scanning result list. By default changed trees
aren't added, but in some cases having changed tree would be useful.

Also adds check for tree count in TreeWalk and when it is different from
two it will thrown an IllegalArgumentException.

This change is required by egit
I7ddb21e7ff54333dd6d7ace3209bbcf83da2b219

Change-Id: I5a680a73e1cffa18ade3402cc86008f46c1da1f1
Signed-off-by: Dariusz Luksza <dariusz@luksza.org>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
tags/v1.1.0.201109011030-rc2
Dariusz Luksza 12 years ago
parent
commit
679cab9b32

+ 2
- 1
org.eclipse.jgit.test/META-INF/MANIFEST.MF View File

@@ -39,7 +39,8 @@ Import-Package: org.eclipse.jgit;version="[1.1.0,1.2.0)",
org.eclipse.jgit.util;version="[1.1.0,1.2.0)",
org.eclipse.jgit.util.io;version="[1.1.0,1.2.0)",
org.junit;version="[4.0.0,5.0.0)",
org.hamcrest.core;version="[1.1.0,2.0.0)"
org.hamcrest.core;version="[1.1.0,2.0.0)",
org.hamcrest;version="[1.1.0,2.0.0)"
Require-Bundle: com.jcraft.jsch;bundle-version="[0.1.37,0.2.0)"
Export-Package: org.eclipse.jgit.lib

+ 298
- 0
org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffEntryTest.java View File

@@ -0,0 +1,298 @@
/*
* Copyright (C) 2011, Dariusz Luksza <dariusz@luksza.org>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.diff;

import static org.eclipse.jgit.diff.DiffEntry.DEV_NULL;
import static org.eclipse.jgit.util.FileUtils.delete;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.util.List;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
import org.eclipse.jgit.lib.RepositoryTestCase;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.EmptyTreeIterator;
import org.eclipse.jgit.treewalk.FileTreeIterator;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.util.FileUtils;
import org.junit.Test;

public class DiffEntryTest extends RepositoryTestCase {

@Test
public void shouldListAddedFileInInitialCommit() throws Exception {
// given
writeTrashFile("a.txt", "content");
Git git = new Git(db);
git.add().addFilepattern("a.txt").call();
RevCommit c = git.commit().setMessage("initial commit").call();

// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(new EmptyTreeIterator());
walk.addTree(c.getTree());
List<DiffEntry> result = DiffEntry.scan(walk);

// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));

DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.ADD));
assertThat(entry.getNewPath(), is("a.txt"));
assertThat(entry.getOldPath(), is(DEV_NULL));
}

@Test
public void shouldListAddedFileBetweenTwoCommits() throws Exception {
// given
Git git = new Git(db);
RevCommit c1 = git.commit().setMessage("initial commit").call();
writeTrashFile("a.txt", "content");
git.add().addFilepattern("a.txt").call();
RevCommit c2 = git.commit().setMessage("second commit").call();

// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk);

// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));

DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.ADD));
assertThat(entry.getNewPath(), is("a.txt"));
assertThat(entry.getOldPath(), is(DEV_NULL));
}

@Test
public void shouldListModificationBetweenTwoCommits() throws Exception {
// given
Git git = new Git(db);
File file = writeTrashFile("a.txt", "content");
git.add().addFilepattern("a.txt").call();
RevCommit c1 = git.commit().setMessage("initial commit").call();
write(file, "new content");
RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
.call();

// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk);

// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));

DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a.txt"));
}

@Test
public void shouldListDeletionBetweenTwoCommits() throws Exception {
// given
Git git = new Git(db);
File file = writeTrashFile("a.txt", "content");
git.add().addFilepattern("a.txt").call();
RevCommit c1 = git.commit().setMessage("initial commit").call();
delete(file);
RevCommit c2 = git.commit().setAll(true).setMessage("delete a.txt")
.call();

// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk);

// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));

DiffEntry entry = result.get(0);
assertThat(entry.getOldPath(), is("a.txt"));
assertThat(entry.getNewPath(), is(DEV_NULL));
assertThat(entry.getChangeType(), is(ChangeType.DELETE));
}

@Test
public void shouldListModificationInDirWithoutModifiedTrees()
throws Exception {
// given
Git git = new Git(db);
File tree = new File(new File(db.getWorkTree(), "a"), "b");
FileUtils.mkdirs(tree);
File file = new File(tree, "c.txt");
FileUtils.createNewFile(file);
write(file, "content");
git.add().addFilepattern("a").call();
RevCommit c1 = git.commit().setMessage("initial commit").call();
write(file, "new line");
RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
.call();

// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
walk.setRecursive(true);
List<DiffEntry> result = DiffEntry.scan(walk);

// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));

DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a/b/c.txt"));
}

@Test
public void shouldListModificationInDirWithModifiedTrees() throws Exception {
// given
Git git = new Git(db);
File tree = new File(new File(db.getWorkTree(), "a"), "b");
FileUtils.mkdirs(tree);
File file = new File(tree, "c.txt");
FileUtils.createNewFile(file);
write(file, "content");
git.add().addFilepattern("a").call();
RevCommit c1 = git.commit().setMessage("initial commit").call();
write(file, "new line");
RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
.call();

// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk, true);

// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(3)));

DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a"));

entry = result.get(1);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a/b"));

entry = result.get(2);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a/b/c.txt"));
}

@Test
public void shouldListChangesInWorkingTree() throws Exception {
// given
writeTrashFile("a.txt", "content");
Git git = new Git(db);
git.add().addFilepattern("a.txt").call();
RevCommit c = git.commit().setMessage("initial commit").call();
writeTrashFile("b.txt", "new line");

// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(c.getTree());
walk.addTree(new FileTreeIterator(db));
List<DiffEntry> result = DiffEntry.scan(walk, true);

// then
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0);

assertThat(entry.getChangeType(), is(ChangeType.ADD));
assertThat(entry.getNewPath(), is("b.txt"));
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowIAEWhenTreeWalkHasLessThanTwoTrees()
throws Exception {
// given - we don't need anything here

// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(new EmptyTreeIterator());
DiffEntry.scan(walk);
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowIAEWhenTreeWalkHasMoreThanTwoTrees()
throws Exception {
// given - we don't need anything here

// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(new EmptyTreeIterator());
walk.addTree(new EmptyTreeIterator());
walk.addTree(new EmptyTreeIterator());
DiffEntry.scan(walk);
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowIAEWhenScanShouldIncludeTreesAndWalkIsRecursive()
throws Exception {
// given - we don't need anything here

// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(new EmptyTreeIterator());
walk.addTree(new EmptyTreeIterator());
walk.setRecursive(true);
DiffEntry.scan(walk, true);
}

}

+ 2
- 0
org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties View File

@@ -492,3 +492,5 @@ year=year
years=years
yearsAgo={0} years ago
yearsMonthsAgo={0} {1}, {2} {3} ago
treeWalkMustHaveExactlyTwoTrees=TreeWalk should have exactly two trees.
cannotBeRecursiveWhenTreesAreIncluded=TreeWalk shouldn't be recursive when tree objects are included.

+ 2
- 0
org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java View File

@@ -552,4 +552,6 @@ public class JGitText extends TranslationBundle {
/***/ public String years;
/***/ public String yearsAgo;
/***/ public String yearsMonthsAgo;
/***/ public String treeWalkMustHaveExactlyTwoTrees;
/***/ public String cannotBeRecursiveWhenTreesAreIncluded;
}

+ 37
- 1
org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffEntry.java View File

@@ -48,6 +48,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.jgit.JGitText;
import org.eclipse.jgit.lib.AbbreviatedObjectId;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.FileMode;
@@ -106,8 +107,40 @@ public class DiffEntry {
* @return headers describing the changed files.
* @throws IOException
* the repository cannot be accessed.
* @throws IllegalArgumentException
* When given TreeWalk doesn't have exactly two trees.
*/
public static List<DiffEntry> scan(TreeWalk walk) throws IOException {
return scan(walk, false);
}

/**
* Convert the TreeWalk into DiffEntry headers, depending on
* {@code includeTrees} it will add tree objects into result or not.
*
* @param walk
* the TreeWalk to walk through. Must have exactly two trees and
* when {@code includeTrees} parameter is {@code true} it can't
* be recursive.
* @param includeTrees
* include tree object's.
* @return headers describing the changed files.
* @throws IOException
* the repository cannot be accessed.
* @throws IllegalArgumentException
* when {@code includeTrees} is true and given TreeWalk is
* recursive. Or when given TreeWalk doesn't have exactly two
* trees
*/
public static List<DiffEntry> scan(TreeWalk walk, boolean includeTrees)
throws IOException {
if (walk.getTreeCount() != 2)
throw new IllegalArgumentException(
JGitText.get().treeWalkMustHaveExactlyTwoTrees);
if (includeTrees && walk.isRecursive())
throw new IllegalArgumentException(
JGitText.get().cannotBeRecursiveWhenTreesAreIncluded);

List<DiffEntry> r = new ArrayList<DiffEntry>();
MutableObjectId idBuf = new MutableObjectId();
while (walk.next()) {
@@ -133,13 +166,16 @@ public class DiffEntry {
entry.changeType = ChangeType.DELETE;
r.add(entry);

} else {
} else if (!entry.oldId.equals(entry.newId)) {
entry.changeType = ChangeType.MODIFY;
if (RenameDetector.sameType(entry.oldMode, entry.newMode))
r.add(entry);
else
r.addAll(breakModify(entry));
}

if (includeTrees && walk.isSubtree())
walk.enterSubtree();
}
return r;
}

Loading…
Cancel
Save