forked from checkstyle/regression-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
408 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<FindBugsFilter> | ||
<Match> | ||
<!-- that CLI class so we need system exit code there, but only in main(...) method --> | ||
<Class name="com.puppycrawl.tools.checkstyle.Main" /> | ||
<Method name="main" /> | ||
<Bug pattern="DM_EXIT" /> | ||
</Match> | ||
</FindBugsFilter> | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<FindBugsFilter> | ||
<Match> | ||
<!-- that CLI class so we need system exit code there, but only in main(...) method --> | ||
<Class name="com.puppycrawl.tools.checkstyle.Main"/> | ||
<Method name="main"/> | ||
<Bug pattern="DM_EXIT"/> | ||
</Match> | ||
<Match> | ||
<!-- This is a false positive. It is no problem to do that cast. In fact the cast is | ||
in JGit library. There is no reason to raise this warning. --> | ||
<Class name="com.github.checkstyle.regression.git.DiffParser"/> | ||
<Or> | ||
<Bug pattern="BC_UNCONFIRMED_CAST"/> | ||
<Bug pattern="BC_UNCONFIRMED_CAST_OF_RETURN_VALUE"/> | ||
</Or> | ||
</Match> | ||
</FindBugsFilter> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/main/java/com/github/checkstyle/regression/git/DiffParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2017 the original author or authors. | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
package com.github.checkstyle.regression.git; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.jgit.api.Git; | ||
import org.eclipse.jgit.api.errors.GitAPIException; | ||
import org.eclipse.jgit.diff.DiffEntry; | ||
import org.eclipse.jgit.lib.Constants; | ||
import org.eclipse.jgit.lib.ObjectReader; | ||
import org.eclipse.jgit.lib.Ref; | ||
import org.eclipse.jgit.lib.Repository; | ||
import org.eclipse.jgit.revwalk.RevCommit; | ||
import org.eclipse.jgit.revwalk.RevTree; | ||
import org.eclipse.jgit.revwalk.RevWalk; | ||
import org.eclipse.jgit.storage.file.FileRepositoryBuilder; | ||
import org.eclipse.jgit.treewalk.AbstractTreeIterator; | ||
import org.eclipse.jgit.treewalk.CanonicalTreeParser; | ||
|
||
/** | ||
* Parses git diff between feature branch and master for the further use. | ||
* @author LuoLiangchen | ||
*/ | ||
public final class DiffParser { | ||
/** Prevents instantiation. */ | ||
private DiffParser() { | ||
} | ||
|
||
/** | ||
* Parses the diff between a given branch and the master in the give repository path. | ||
* @param repositoryPath the path of checkstyle repository | ||
* @param branchName the name of the branch to be compared with master | ||
* @return a list of {@link GitChange} to represent the changes | ||
* @throws IOException JGit library exception | ||
* @throws GitAPIException JGit library exception | ||
*/ | ||
public static List<GitChange> parse(String repositoryPath, String branchName) | ||
throws IOException, GitAPIException { | ||
final File gitDir = new File(repositoryPath, ".git"); | ||
final Repository repository = new FileRepositoryBuilder().setGitDir(gitDir) | ||
.readEnvironment().findGitDir().build(); | ||
final Git git = new Git(repository); | ||
final AbstractTreeIterator featureTreeParser = | ||
prepareTreeParser(repository, Constants.R_HEADS + branchName); | ||
final AbstractTreeIterator masterTreeParser = | ||
prepareTreeParser(repository, Constants.R_HEADS + "master"); | ||
final List<GitChange> returnValue = git.diff() | ||
.setOldTree(masterTreeParser) | ||
.setNewTree(featureTreeParser) | ||
.call() | ||
.stream() | ||
.map(DiffParser::convertDiffEntryToGitChange) | ||
.collect(Collectors.toList()); | ||
git.close(); | ||
repository.close(); | ||
return returnValue; | ||
} | ||
|
||
/** | ||
* Creates a tree parser from a commit, to be used by diff command. | ||
* @param repository the repository to parse diff | ||
* @param ref the name of the ref to the commit; e.g., "refs/heads/master" | ||
* @return the tree parser | ||
* @throws IOException JGit library exception | ||
*/ | ||
private static AbstractTreeIterator prepareTreeParser(Repository repository, String ref) | ||
throws IOException { | ||
final Ref head = repository.exactRef(ref); | ||
final RevWalk walk = new RevWalk(repository); | ||
final RevCommit commit = walk.parseCommit(head.getObjectId()); | ||
final RevTree tree = walk.parseTree(commit.getTree().getId()); | ||
final CanonicalTreeParser treeParser = new CanonicalTreeParser(); | ||
final ObjectReader reader = repository.newObjectReader(); | ||
treeParser.reset(reader, tree.getId()); | ||
reader.close(); | ||
walk.dispose(); | ||
walk.close(); | ||
return treeParser; | ||
} | ||
|
||
/** | ||
* Converts a {@link DiffEntry} to {@link GitChange} for the further use. | ||
* @param diffEntry the {@link DiffEntry} instance to be converted | ||
* @return the {@link GitChange} instance converted from the given {@link DiffEntry} | ||
*/ | ||
private static GitChange convertDiffEntryToGitChange(DiffEntry diffEntry) { | ||
return new GitChange(diffEntry.getNewPath()); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/github/checkstyle/regression/git/GitChange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2017 the original author or authors. | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
package com.github.checkstyle.regression.git; | ||
|
||
/** | ||
* Represents git changes of a file. | ||
* @author LuoLiangchen | ||
*/ | ||
public class GitChange { | ||
/** The path of the changed file. */ | ||
private final String path; | ||
|
||
/** | ||
* Creates a new {@link GitChange} instance. | ||
* @param path the path of the changed file | ||
*/ | ||
GitChange(String path) { | ||
this.path = path; | ||
} | ||
|
||
/** | ||
* Gets the path of the changed file. | ||
* @return the path of the changed file | ||
*/ | ||
public String getPath() { | ||
return path; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/github/checkstyle/regression/git/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2017 the original author or authors. | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
/** | ||
* Contains git related classes. | ||
*/ | ||
package com.github.checkstyle.regression.git; |
66 changes: 66 additions & 0 deletions
66
src/test/java/com/github/checkstyle/regression/git/DiffParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2017 the original author or authors. | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
package com.github.checkstyle.regression.git; | ||
|
||
import static com.github.checkstyle.regression.internal.TestUtils.assertUtilsClassHasPrivateConstructor; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.List; | ||
|
||
import org.eclipse.jgit.lib.Repository; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.github.checkstyle.regression.internal.GitUtils; | ||
|
||
public class DiffParserTest { | ||
private Repository repository; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
repository = GitUtils.createNewRepository(); | ||
final File helloWorld = GitUtils.addAnEmptyFileAndCommit(repository, "HelloWorld"); | ||
GitUtils.createNewBranchAndCheckout(repository, "foo"); | ||
Files.write(helloWorld.toPath(), "hello world!".getBytes(), StandardOpenOption.APPEND); | ||
GitUtils.addAllAndCommit(repository, "append text to HelloWorld"); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
repository.close(); | ||
} | ||
|
||
@Test | ||
public void testIsProperUtilsClass() throws Exception { | ||
assertUtilsClassHasPrivateConstructor(DiffParser.class); | ||
} | ||
|
||
@Test | ||
public void testParse() throws Exception { | ||
final List<GitChange> changes = DiffParser.parse( | ||
repository.getDirectory().getParent(), "foo"); | ||
assertEquals(1, changes.size()); | ||
assertEquals("HelloWorld", changes.iterator().next().getPath()); | ||
} | ||
} |
Oops, something went wrong.