Skip to content

Commit

Permalink
Merging branch ft-new-git-module, which is not finished but the existing
Browse files Browse the repository at this point in the history
work can be merged and be used to solve some questions. Related to #60.

* ft-new-git-module:
  Adding some documentation
  Refactoring method for counting commits that include a given file extension
  filtering commits that have altered java code. r #60
  searching commits with gitective
  Adding Gitective [1] as a dependency and creating the class that will be responsible for the local Git repository analysis within Groundhog.
  • Loading branch information
Rodrigo Alves committed Oct 9, 2013
2 parents 21b81ff + 33c4bec commit 4521a60
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 4 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ Project project = new Project(user, "rails"); // project github.com/rails/rails
List<User> 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.
Expand Down
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,12 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependency>
<dependency>
<groupId>org.gitective</groupId>
<artifactId>gitective-core</artifactId>
<version>0.9.9</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -270,4 +275,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
1 change: 1 addition & 0 deletions src/java/main/br/ufpe/cin/groundhog/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/java/main/br/ufpe/cin/groundhog/crawler/CrawlGitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Commit> 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<Commit> extractCommitFromUser(User user, File project) throws IOException {
List<Commit> 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;
}
}
18 changes: 18 additions & 0 deletions src/java/main/br/ufpe/cin/groundhog/main/TestMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4521a60

Please sign in to comment.