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