Skip to content

Commit

Permalink
starting some work on my jgit ideas
Browse files Browse the repository at this point in the history
  • Loading branch information
andymeneely committed Jun 1, 2012
1 parent 9d5ebfc commit c6dba9a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<id>andy-maven</id>
<url>https://github.com/apmeneel/andy-maven/raw/master/releases</url>
</repository>
<repository>
<id>jgit-repository</id>
<url>http://download.eclipse.org/jgit/maven</url>
</repository>
</repositories>
<dependencies>
<dependency>
Expand Down Expand Up @@ -62,5 +66,10 @@
<version>3.6</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>1.3.0.201202151440-r</version>
</dependency>
</dependencies>
</project>
54 changes: 54 additions & 0 deletions src/main/java/org/chaoticbits/devactivity/churn/git/CodeChurn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.chaoticbits.devactivity.churn.git;

import java.io.IOException;

import org.eclipse.jgit.api.DiffCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.api.errors.NoHeadException;
import org.eclipse.jgit.blame.BlameResult;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;

public class CodeChurn {

private final Repository repo;

public CodeChurn(Repository gitRepo) {
this.repo = gitRepo;
}

public int compute(String path) {
Git git = new Git(repo);
BlameResult call = git.blame().setFilePath(path).setFollowFileRenames(true).call();

try {
Iterable<RevCommit> commits = git.log().addPath(path).call();

for (RevCommit revCommit : commits) {
String fullMessage = revCommit.getFullMessage();
DiffCommand diff = git.diff();
AbstractTreeIterator newTree = new CanonicalTreeParser();

AbstractTreeIterator oldTree = new CanonicalTreeParser();
diff.setNewTree(newTree).setOldTree(oldTree).call();
}
} catch (NoHeadException e) {
e.printStackTrace();
} catch (JGitInternalException e) {
e.printStackTrace();
} catch (GitAPIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

throw new IllegalStateException("unimplemented!");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.chaoticbits.devactivity.churn.git;

import static org.junit.Assert.*;

import java.io.File;

import org.chaoticbits.devactivity.churn.git.CodeChurn;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.junit.Test;

public class GitCodeChurnTest {

public static final File repoDir = new File("testdata/testgitrepo/.git");

@Test
public void simpleCodeChurn() throws Exception {
Repository gitRepo = new FileRepositoryBuilder().setGitDir(repoDir)
.readEnvironment().findGitDir().build();
int churn = new CodeChurn(gitRepo)
.compute("mancala/player/GreedyPlayer.java");
assertEquals(4, churn);
}
}

0 comments on commit c6dba9a

Please sign in to comment.