A Java package for iterating through files in your Git repo and loading them.
File repoPath = new File("demo_project/.git");
String revisionStr = "master";
// = "HEAD";
// = "aec1f2040b0eeef41f0f37535f8b4c6334b2a610";
Giterable gitFiles = new Giterable(repoPath, revisionStr);
GitLoader loader = new GitLoader(repoPath);
for (File file : gitFiles) {
String text = loader.getText(file, revisionStr);
byte[] bytes = loader.getBytes(file, revisionStr);
}
Giterable uses JGit to access Git repositories.
- Download the JGit jar file
org.eclipse.jgit.jar
at: http://www.eclipse.org/jgit/download/ - Download
giterable.jar
. - Add both jars to your classpath when compiling and running your code.
$ javac -cp .:giterable.jar:jgit.jar Demo.java
$ java -cp .:giterable.jar:jgit.jar Demo
Here's how to use Giterable in your code:
import com.kelvingu.giterable.Giterable;
import com.kelvingu.giterable.GitLoader;
import java.io.File;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException {
File repoPath = new File("demo_project/.git");
String revisionStr = "master";
// String revisionStr = "HEAD";
// String revisionStr = "aec1f2040b0eeef41f0f37535f8b4c6334b2a610";
System.out.println("Loading repository at:");
System.out.println(repoPath.getCanonicalPath() + '\n');
Giterable gitFiles = new Giterable(repoPath, revisionStr);
GitLoader loader = new GitLoader(repoPath);
for (File file : gitFiles) {
String text = loader.getText(file, revisionStr);
byte[] bytes = loader.getBytes(file, revisionStr);
System.out.println(file.getPath());
System.out.println("- - - - - - - - - -");
System.out.println(text + '\n');
}
}
}
This produces the following output:
Loading repository at:
demo/demo_project/.git
demo_project/.gitignore
- - - - - - - - - -
.DS_Store
*.swp
demo_project/README
- - - - - - - - - -
README:
1st commit
demo_project/src/file1.txt
- - - - - - - - - -
Contents of file1.txt
demo_project/src/file2.txt
- - - - - - - - - -
Contents of file2.txt
To run the above demo, you can clone this repo and run demo/demo.sh
$ git clone [email protected]:ke1vin/giterable.git
$ cd giterable/demo
$ ./demo.sh