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.*;
020import java.util.*;
021
022import org.eclipse.jgit.api.*;
023import org.eclipse.jgit.api.errors.*;
024import org.eclipse.jgit.internal.storage.file.*;
025import org.eclipse.jgit.lib.*;
026import org.eclipse.jgit.transport.*;
027
028@SuppressWarnings({ "javadoc", "unused" })
029public class GitControl {
030
031   private String localPath, remotePath;
032   private Repository localRepo;
033   private Git git;
034   private CredentialsProvider cp;
035   private String name = "username";
036   private String password = "password";
037
038   public GitControl(String localPath, String remotePath) throws IOException {
039      this.localPath = localPath;
040      this.remotePath = remotePath;
041      this.localRepo = new FileRepository(localPath + "/.git");
042      cp = new UsernamePasswordCredentialsProvider(this.name, this.password);
043      git = new Git(localRepo);
044   }
045
046   public void addToRepo() throws IOException, NoFilepatternException, GitAPIException {
047      var add = git.add();
048      add.addFilepattern(".").call();
049   }
050
051   public void branch(String name) throws RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException, CheckoutConflictException, GitAPIException {
052      git.checkout().setName(name).setStartPoint("origin/".concat(name)).call();
053   }
054
055   @SuppressWarnings("resource")
056   public void cloneRepo() throws IOException, NoFilepatternException, GitAPIException {
057      Git.cloneRepository().setURI(remotePath).setDirectory(new File(localPath)).call();
058   }
059
060   public void commitToRepo(String message)
061      throws IOException, NoHeadException, NoMessageException, ConcurrentRefUpdateException, JGitInternalException, WrongRepositoryStateException, GitAPIException {
062      git.commit().setMessage(message).call();
063   }
064
065   public void pullFromRepo() throws IOException, WrongRepositoryStateException, InvalidConfigurationException, DetachedHeadException, InvalidRemoteException, CanceledException, RefNotFoundException,
066      NoHeadException, GitAPIException {
067      git.pull().call();
068   }
069
070   public void pushToRepo() throws IOException, JGitInternalException, InvalidRemoteException, GitAPIException {
071      var pc = git.push();
072      pc.setCredentialsProvider(cp).setForce(true).setPushAll();
073      try {
074         var it = pc.call().iterator();
075         if (it.hasNext()) {
076            System.out.println(it.next().toString());
077         }
078      } catch (InvalidRemoteException e) {
079         e.printStackTrace();
080      }
081   }
082}