diff --git a/README.md b/README.md index 57c7c26..a90c5a7 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,19 @@ Project project = new Project(user, "rails"); // project github.com/rails/rails List contributors = searchGitHub.getAllProjectContributors(project); ``` +## Local Data Extraction + +In addition to the metadata extraction allowed via the GitHub API, Groundhog covers local data extraction onto repositories via a Git interface + +You can, for example, count the number of commits in a project that include a Java file, via a `GitCommitExtractor` object: + +```java +GitCommitExtractor extractor = new GitCommitExtractor(); +File project = new File("/tmp/elasticsearch"); + +extractor.numberOfCommitsWithExtension(project, "java"); +``` + ## Documentation Groundhog features a [Wiki], where you can browse for more information. diff --git a/pom.xml b/pom.xml index a724a05..631e676 100644 --- a/pom.xml +++ b/pom.xml @@ -173,7 +173,12 @@ junit junit 4.11 - + + + org.gitective + gitective-core + 0.9.9 + @@ -270,4 +275,4 @@ - \ No newline at end of file + diff --git a/src/java/main/br/ufpe/cin/groundhog/Project.java b/src/java/main/br/ufpe/cin/groundhog/Project.java index e7e5110..efec53c 100644 --- a/src/java/main/br/ufpe/cin/groundhog/Project.java +++ b/src/java/main/br/ufpe/cin/groundhog/Project.java @@ -85,6 +85,7 @@ public Project(String name, String description) { public Project(User user, String name) { this.user = user; this.name = name; + this.scmURL = "https://github.com/" + user.getLogin() + "/" + name + ".git"; } /** diff --git a/src/java/main/br/ufpe/cin/groundhog/crawler/CrawlGitHub.java b/src/java/main/br/ufpe/cin/groundhog/crawler/CrawlGitHub.java index 1e5959c..983cf53 100644 --- a/src/java/main/br/ufpe/cin/groundhog/crawler/CrawlGitHub.java +++ b/src/java/main/br/ufpe/cin/groundhog/crawler/CrawlGitHub.java @@ -16,7 +16,6 @@ * @author fjsj, gustavopinto */ public class CrawlGitHub extends ForgeCrawler { - private final static Logger logger = LoggerFactory.getLogger(CrawlGitHub.class); private final GitClient gitClient; @@ -35,7 +34,6 @@ public File downloadProject(Project project) throws DownloadException { File projectDestinationFolder = new File(destinationFolder, projectName); logger.info(String.format("Downloading %s project..", project.getName())); - try { this.gitClient.clone(projectUrl, projectDestinationFolder); diff --git a/src/java/main/br/ufpe/cin/groundhog/extractor/GitCommitExtractor.java b/src/java/main/br/ufpe/cin/groundhog/extractor/GitCommitExtractor.java new file mode 100644 index 0000000..3e051fb --- /dev/null +++ b/src/java/main/br/ufpe/cin/groundhog/extractor/GitCommitExtractor.java @@ -0,0 +1,91 @@ +package br.ufpe.cin.groundhog.extractor; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jgit.lib.PersonIdent; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.storage.file.FileRepositoryBuilder; +import org.gitective.core.CommitFinder; +import org.gitective.core.PathFilterUtils; +import org.gitective.core.filter.commit.AndCommitFilter; +import org.gitective.core.filter.commit.AuthorFilter; +import org.gitective.core.filter.commit.CommitCountFilter; +import org.gitective.core.filter.commit.CommitListFilter; + +import br.ufpe.cin.groundhog.Commit; +import br.ufpe.cin.groundhog.User; + +/** + * Extract Commit data from Git repositories + * @author Rodrigo Alves + * + */ +public class GitCommitExtractor { + + public List extractCommits(File project) { + CommitListFilter list = new CommitListFilter(); + + String path = project.getAbsolutePath() + "/.git"; + CommitFinder finder = new CommitFinder(path); + + finder.setFilter(list).find(); + + for (RevCommit rev : list.getCommits()){ + System.out.println(rev.getName() + " " + rev.getAuthorIdent().getName() + " " + rev.getShortMessage()); + } + + return null; + } + + /** + * A method that returns the number of commits that contain files with a given file extension + * Example usage: + * + * File project = new File("/tmp/project"); + * numberOfCommitsWithExtension(project, ".java") // Returns the number of commits in project that includes Java files + * + * @param project A {@link file} object for the directory where the Git repository is located + * @param extension The extension to be searched within the commits + * @return the number of commits that includes files with the given extension + */ + public int numberOfCommitsWithExtension(File project, String extension) { + CommitCountFilter commits = new CommitCountFilter(); + String path = project.getAbsolutePath() + "/.git"; + + CommitFinder finder = new CommitFinder(path); + finder.setFilter(PathFilterUtils.andSuffix("." + extension)); + finder.setMatcher(commits); + finder.find(); + + return (int) commits.getCount(); + } + + /** + * Extracts only the commits from a given {@link User} + * TODO: implement this method + * @return a {@link List} of {@link Commit} objects + * @throws IOException + */ + public List extractCommitFromUser(User user, File project) throws IOException { + List commits = new ArrayList<>(); + + String path = project.getAbsolutePath(); + + CommitFinder finder = new CommitFinder(path); + FileRepositoryBuilder builder = new FileRepositoryBuilder(); + Repository repository = builder.setGitDir(new File(path)).readEnvironment() + .findGitDir() + .build(); + + CommitListFilter list = new CommitListFilter(); + AndCommitFilter filters = new AndCommitFilter(); + PersonIdent author = new PersonIdent(user.getName(), user.getEmail()); + + filters.add(new AuthorFilter(author)); + return commits; + } +} \ No newline at end of file diff --git a/src/java/main/br/ufpe/cin/groundhog/main/TestMain.java b/src/java/main/br/ufpe/cin/groundhog/main/TestMain.java index 2fd6f29..2b081f6 100644 --- a/src/java/main/br/ufpe/cin/groundhog/main/TestMain.java +++ b/src/java/main/br/ufpe/cin/groundhog/main/TestMain.java @@ -21,6 +21,7 @@ import br.ufpe.cin.groundhog.crawler.CrawlGoogleCode; import br.ufpe.cin.groundhog.crawler.CrawlSourceForge; import br.ufpe.cin.groundhog.crawler.ForgeCrawler; +import br.ufpe.cin.groundhog.extractor.GitCommitExtractor; import br.ufpe.cin.groundhog.http.HttpModule; import br.ufpe.cin.groundhog.http.Requests; import br.ufpe.cin.groundhog.parser.java.JavaParser; @@ -184,6 +185,23 @@ public static void googleCodeExample(String term) throws Exception { public static void main(String[] args) throws Exception { // gitHubExample("restfulie-java"); +// File folder = new File("/tmp"); +// CrawlGitHub crawler = new CrawlGitHub(new GitClient(), folder); +// Project pr = new Project(new User("gustavopinto"), "groundhog-case-study"); +// +// System.out.println("url e: " + pr.getScmURL()); +// +// crawler.downloadProject(pr); + + File project = new File("/Users/rodrigovieira/Desktop/groundhog-case-study"); + + GitCommitExtractor extractor = new GitCommitExtractor(); + extractor.extractCommits(project); + + System.out.println(extractor.numberOfCommitsWithExtension(project, "md")); + + + System.out.println("Pronto!"); // sourceForgeExample(); // googleCodeExample("facebook-java-api"); // Google Code SVN // googleCodeExample("guava-libraries"); // Google Code Git