001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.server.config.repository;
018
019import java.io.File;
020import java.io.IOException;
021import java.util.Iterator;
022
023import org.eclipse.jgit.api.AddCommand;
024import org.eclipse.jgit.api.Git;
025import org.eclipse.jgit.api.PushCommand;
026import org.eclipse.jgit.api.errors.CanceledException;
027import org.eclipse.jgit.api.errors.CheckoutConflictException;
028import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
029import org.eclipse.jgit.api.errors.DetachedHeadException;
030import org.eclipse.jgit.api.errors.GitAPIException;
031import org.eclipse.jgit.api.errors.InvalidConfigurationException;
032import org.eclipse.jgit.api.errors.InvalidRefNameException;
033import org.eclipse.jgit.api.errors.InvalidRemoteException;
034import org.eclipse.jgit.api.errors.JGitInternalException;
035import org.eclipse.jgit.api.errors.NoFilepatternException;
036import org.eclipse.jgit.api.errors.NoHeadException;
037import org.eclipse.jgit.api.errors.NoMessageException;
038import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
039import org.eclipse.jgit.api.errors.RefNotFoundException;
040import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
041import org.eclipse.jgit.internal.storage.file.FileRepository;
042import org.eclipse.jgit.lib.Repository;
043import org.eclipse.jgit.transport.CredentialsProvider;
044import org.eclipse.jgit.transport.PushResult;
045import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
046
047@SuppressWarnings({"javadoc","unused"})
048public class GitControl {
049
050   private String localPath, remotePath;
051   private Repository localRepo;
052   private Git git;
053   private CredentialsProvider cp;
054   private String name = "username";
055   private String password = "password";
056
057   public GitControl(String localPath, String remotePath) throws IOException {
058      this.localPath = localPath;
059      this.remotePath = remotePath;
060      this.localRepo = new FileRepository(localPath + "/.git");
061      cp = new UsernamePasswordCredentialsProvider(this.name, this.password);
062      git = new Git(localRepo);
063   }
064
065   public void cloneRepo() throws IOException, NoFilepatternException, GitAPIException {
066      Git.cloneRepository().setURI(remotePath).setDirectory(new File(localPath)).call();
067   }
068
069   public void addToRepo() throws IOException, NoFilepatternException, GitAPIException {
070      AddCommand add = git.add();
071      add.addFilepattern(".").call();
072   }
073
074   public void commitToRepo(String message) throws IOException, NoHeadException, NoMessageException,
075         ConcurrentRefUpdateException, JGitInternalException, WrongRepositoryStateException, GitAPIException {
076      git.commit().setMessage(message).call();
077   }
078
079   public void branch(String name) throws RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException,
080         CheckoutConflictException, GitAPIException {
081      git.checkout().setName(name).setStartPoint("origin/".concat(name)).call();
082   }
083
084   public void pushToRepo() throws IOException, JGitInternalException, InvalidRemoteException, GitAPIException {
085      PushCommand pc = git.push();
086      pc.setCredentialsProvider(cp).setForce(true).setPushAll();
087      try {
088         Iterator<PushResult> it = pc.call().iterator();
089         if (it.hasNext()) {
090            System.out.println(it.next().toString());
091         }
092      } catch (InvalidRemoteException e) {
093         e.printStackTrace();
094      }
095   }
096
097   public void pullFromRepo()
098         throws IOException, WrongRepositoryStateException, InvalidConfigurationException, DetachedHeadException,
099         InvalidRemoteException, CanceledException, RefNotFoundException, NoHeadException, GitAPIException {
100      git.pull().call();
101   }
102
103}