|
|
@@ -0,0 +1,140 @@
|
|
|
+package info.knacki.pass.jgit;
|
|
|
+
|
|
|
+import org.eclipse.jgit.api.Git;
|
|
|
+import org.eclipse.jgit.api.errors.GitAPIException;
|
|
|
+import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
|
|
|
+import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
|
|
|
+import org.eclipse.jgit.lib.PersonIdent;
|
|
|
+import org.eclipse.jgit.lib.Ref;
|
|
|
+import org.eclipse.jgit.lib.Repository;
|
|
|
+import org.eclipse.jgit.revwalk.RevCommit;
|
|
|
+import org.eclipse.jgit.transport.CredentialsProvider;
|
|
|
+import org.eclipse.jgit.transport.URIish;
|
|
|
+import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.URISyntaxException;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.logging.Level;
|
|
|
+import java.util.logging.Logger;
|
|
|
+
|
|
|
+import info.knacki.gitdroid.GitConfig;
|
|
|
+import info.knacki.gitdroid.GitInterface;
|
|
|
+import info.knacki.gitdroid.callback.OnResponseListener;
|
|
|
+import info.knacki.gitdroid.callback.OnStreamResponseListener;
|
|
|
+import info.knacki.gitdroid.entities.GitCommit;
|
|
|
+import info.knacki.gitdroid.entities.GitObject;
|
|
|
+import info.knacki.gitdroid.entities.GitRef;
|
|
|
+import info.knacki.gitdroid.io.NetworkUtils;
|
|
|
+
|
|
|
+public class JGitWrapper implements GitInterface {
|
|
|
+ private final static Logger log = Logger.getLogger(JGitWrapper.class.getName());
|
|
|
+ private final Git fGit;
|
|
|
+ private final Repository fRepository;
|
|
|
+ private final CredentialsProvider fCredProvider;
|
|
|
+ private final String DEFAULT_REMOTE = "origin";
|
|
|
+ private final PersonIdent fCommiter;
|
|
|
+ private final GitConfig fConfig;
|
|
|
+
|
|
|
+ private JGitWrapper(GitConfig config) throws IOException, URISyntaxException, GitAPIException {
|
|
|
+ fRepository = new InMemoryRepository(new DfsRepositoryDescription());
|
|
|
+ fGit = new Git(fRepository);
|
|
|
+ fGit.remoteAdd()
|
|
|
+ .setName(DEFAULT_REMOTE)
|
|
|
+ .setUri(new URIish(config.GetUrl()))
|
|
|
+ .call();
|
|
|
+ fCredProvider = GetCredentialProvider(config);
|
|
|
+ fCommiter = new PersonIdent(config.GetUsername(), config.GetUserEmail());
|
|
|
+ fConfig = config;
|
|
|
+ }
|
|
|
+
|
|
|
+ private synchronized void init(final Runnable onDone) {
|
|
|
+ new Thread() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ onDone.run();
|
|
|
+ }
|
|
|
+ }.start();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static CredentialsProvider GetCredentialProvider(NetworkUtils.AuthConfig config) {
|
|
|
+ return new UsernamePasswordCredentialsProvider(config.GetUser(), config.GetPassword());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static GitInterface Create(GitConfig config) {
|
|
|
+ try {
|
|
|
+ return new JGitWrapper(config);
|
|
|
+ }
|
|
|
+ catch (Throwable e) {
|
|
|
+ log.log(Level.SEVERE, "Cannot create Git Repository", e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void DoGetRefs(OnResponseListener<GitRef[]> callback) {
|
|
|
+ try {
|
|
|
+ Collection<Ref> branches = fGit.lsRemote()
|
|
|
+ .setCredentialsProvider(fCredProvider)
|
|
|
+ .setRemote(DEFAULT_REMOTE)
|
|
|
+ .setHeads(true)
|
|
|
+ .setTags(false)
|
|
|
+ .call();
|
|
|
+ GitRef[] result = new GitRef[branches.size()];
|
|
|
+ int pos = 0;
|
|
|
+ for (Ref i : branches)
|
|
|
+ result[pos++] = new GitRef(i.getObjectId().name(), i.getName());
|
|
|
+ callback.OnResponse(result);
|
|
|
+ }
|
|
|
+ catch (GitAPIException e) {
|
|
|
+ log.log(Level.SEVERE, "Cannot list references", e);
|
|
|
+ callback.OnError("Cannot list references", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void GetRefs(OnResponseListener<GitRef[]> callback) {
|
|
|
+ init(() -> {
|
|
|
+ DoGetRefs(callback);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void FetchHead(OnStreamResponseListener<GitCommit> response) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void FetchBlob(GitObject.GitBlob blob, OnStreamResponseListener<byte[]> response) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void PushCommitBuilder(GitCommit.Builder commit, OnStreamResponseListener<Void> resp) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void CreateBranch(String branchName, OnStreamResponseListener<GitRef[]> callback) {
|
|
|
+ init(() -> {
|
|
|
+ try {
|
|
|
+ RevCommit emptyCommit = fGit.commit()
|
|
|
+ .setMessage("Branch init")
|
|
|
+ .setAuthor(fCommiter)
|
|
|
+ .setCommitter(fCommiter)
|
|
|
+ .setAllowEmpty(true)
|
|
|
+ .call();
|
|
|
+ fGit.checkout()
|
|
|
+ .setCreateBranch(true)
|
|
|
+ .setStartPoint(emptyCommit)
|
|
|
+ .setOrphan(true)
|
|
|
+ .setName(branchName)
|
|
|
+ .call();
|
|
|
+ DoGetRefs(callback);
|
|
|
+ }
|
|
|
+ catch (GitAPIException e) {
|
|
|
+ log.log(Level.SEVERE, "Cannot list references", e);
|
|
|
+ callback.OnError("Cannot list references", e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|