-
Notifications
You must be signed in to change notification settings - Fork 7
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
8 changed files
with
360 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
104 changes: 104 additions & 0 deletions
104
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,104 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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.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 class DiffParser { | ||
/** The path of checkstyle repository. */ | ||
private final String repositoryPath; | ||
|
||
/** | ||
* Creates a new {@link DiffParser} instance. | ||
* @param repositoryPath the path of checkstyle repository | ||
*/ | ||
public DiffParser(String repositoryPath) { | ||
this.repositoryPath = repositoryPath; | ||
} | ||
|
||
/** | ||
* Parses the diff between a given branch and the master. | ||
* @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 List<GitChange> parse(String branchName) throws IOException, GitAPIException { | ||
final File gitDir = new File(repositoryPath, ".git"); | ||
try (Repository repository = new FileRepositoryBuilder().setGitDir(gitDir) | ||
.readEnvironment().findGitDir().build()) { | ||
try (Git git = new Git(repository)) { | ||
final AbstractTreeIterator featureTreeParser = | ||
prepareTreeParser(repository, Constants.R_HEADS + branchName); | ||
final AbstractTreeIterator masterTreeParser = | ||
prepareTreeParser(repository, Constants.R_HEADS + "master"); | ||
return git.diff() | ||
.setOldTree(masterTreeParser) | ||
.setNewTree(featureTreeParser) | ||
.call() | ||
.stream() | ||
.map(GitChange::new) | ||
.collect(Collectors.toList()); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 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); | ||
try (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(); | ||
try (ObjectReader reader = repository.newObjectReader()) { | ||
treeParser.reset(reader, tree.getId()); | ||
} | ||
walk.dispose(); | ||
return treeParser; | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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 org.eclipse.jgit.diff.DiffEntry; | ||
|
||
/** | ||
* Represents git changes of a file. | ||
* @author LuoLiangchen | ||
*/ | ||
public class GitChange { | ||
/** The path of the changed file. */ | ||
private final String path; | ||
|
||
/** | ||
* Creates a new {@code GitChange} instance from a {@code DiffEntry}. | ||
* @param diff the {@code DiffEntry} | ||
*/ | ||
GitChange(DiffEntry diff) { | ||
path = diff.getNewPath(); | ||
} | ||
|
||
/** | ||
* 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; |
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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 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 testParse() throws Exception { | ||
final DiffParser diffParser = new DiffParser(repository.getDirectory().getParent()); | ||
final List<GitChange> changes = diffParser.parse("foo"); | ||
assertEquals(1, changes.size()); | ||
assertEquals("HelloWorld", changes.iterator().next().getPath()); | ||
} | ||
} |
Oops, something went wrong.