Created
July 5, 2020 04:01
-
-
Save ErShakirAnsari/c9e344aff4c538249eb271c71c095ddb to your computer and use it in GitHub Desktop.
JGit Handler
This file contains hidden or 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
package jgitdemo; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Iterator; | |
import org.eclipse.jgit.api.Git; | |
import org.eclipse.jgit.api.PushCommand; | |
import org.eclipse.jgit.api.errors.GitAPIException; | |
import org.eclipse.jgit.api.errors.InvalidRemoteException; | |
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.transport.CredentialsProvider; | |
import org.eclipse.jgit.transport.PushResult; | |
import org.eclipse.jgit.treewalk.TreeWalk; | |
/** | |
* | |
* @author Shakir | |
*/ | |
public class JGitHandler | |
{ | |
public static void clonePublicRepository(String repoUrI, String folder) throws Exception | |
{ | |
try (Git git = Git.cloneRepository() | |
.setURI(repoUrI) | |
.setDirectory(new File(folder)) | |
.call();) | |
{ | |
System.out.println("JGitHandler.clonePublicRepository() - " + git); | |
} | |
} | |
public static void clonePrivateRepository(String repoUrI, String folder, CredentialsProvider credentialsProvider) throws Exception | |
{ | |
try (Git git = Git.cloneRepository() | |
.setURI(repoUrI) | |
.setDirectory(new File(folder)) | |
.setCredentialsProvider(credentialsProvider) | |
.call();) | |
{ | |
System.out.println("JGitHandler.clonePrivateRepository() - " + git); | |
} | |
} | |
public static Repository getRepositoryFromLocal(String uri) throws IOException | |
{ | |
return new FileRepositoryBuilder() | |
.setGitDir(new File(uri)) | |
.readEnvironment() | |
.findGitDir() | |
.build(); | |
} | |
public static TreeWalk getTreeWalk(Repository repository) throws Exception | |
{ | |
return getTreeWalk(repository, "HEAD"); | |
} | |
public static TreeWalk getTreeWalk(Repository repository, String ref) throws Exception | |
{ | |
Ref head = repository.findRef(ref); | |
// a RevWalk allows to walk over commits based on some filtering that is defined | |
RevWalk revWalk = new RevWalk(repository); | |
RevCommit revCommit = revWalk.parseCommit(head.getObjectId()); | |
RevTree revTree = revCommit.getTree(); | |
System.out.println("Having tree: " + revTree.getName() + "[" + revTree + "]"); | |
// now use a TreeWalk to iterate over all files in the Tree recursively | |
// you can set Filters to narrow down the results if needed | |
TreeWalk treeWalk = new TreeWalk(repository); | |
treeWalk.addTree(revTree); | |
return treeWalk; | |
} | |
public static void listRepositoryContents(Repository repository) throws Exception | |
{ | |
TreeWalk treeWalk = getTreeWalk(repository); | |
treeWalk.setRecursive(true); | |
while (treeWalk.next()) | |
{ | |
System.out.println("found: " + treeWalk.getPathString() + " " + treeWalk.getFileMode()); | |
// System.out.printf( | |
// "path: %s, Commit(mode/oid): %s/%s, Index(mode/oid): %s/%s, Workingtree(mode/oid): %s/%s\n", | |
// treeWalk.getPathString(), treeWalk.getFileMode(0), treeWalk.getObjectId(0), treeWalk.getFileMode(1), treeWalk.getObjectId(1), | |
// treeWalk.getFileMode(2), treeWalk.getObjectId(2)); | |
} | |
} | |
public static void addFile(Repository repository, CredentialsProvider credentialsProvider, String file) throws Exception | |
{ | |
// Create a Repository object | |
try (Git git = new Git(repository);) | |
{ | |
git.add().addFilepattern(file).call(); | |
doCommit(git); | |
doPush(git, credentialsProvider); | |
} | |
} | |
public static void doCommit(Git git) throws GitAPIException | |
{ | |
// RevCommit revCommit = git | |
// .doCommit() | |
// .setAuthor("Shakir", "[email protected]") | |
// .setMessage("Auto doCommit from jgit") | |
// .call(); | |
RevCommit revCommit = git.commit().setMessage("Create readme file").call(); | |
System.out.println("Commit: " + revCommit + ", name: " + revCommit.getName() + ", id: " + revCommit.getId().getName()); | |
} | |
public static void doPush(Git git, CredentialsProvider credentialsProvider) throws GitAPIException | |
{ | |
PushCommand pushCommand = git.push(); | |
pushCommand.setForce(true); | |
if (credentialsProvider != null) | |
{ | |
pushCommand.setCredentialsProvider(credentialsProvider); | |
} | |
pushCommand.setPushAll(); | |
try | |
{ | |
Iterator<PushResult> it = pushCommand.call().iterator(); | |
if (it.hasNext()) | |
{ | |
System.out.println(it.next().toString()); | |
} | |
} catch (InvalidRemoteException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
public static void doPush(Git git) throws GitAPIException | |
{ | |
doPush(git, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment