Java tutorial
/* * Copyright 2015-2016 Kirk Rader * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package us.rader.dinodocs; import java.io.File; import java.net.URI; import java.net.URL; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.eclipse.jgit.api.CheckoutCommand; import org.eclipse.jgit.api.CheckoutResult; import org.eclipse.jgit.api.CloneCommand; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.PullCommand; import org.eclipse.jgit.api.PullResult; /** * <a href="https://git-scm.com/">Git</a> {@link Puller}. * * This will perform a Git clone or pull, depending on whether or not the * specified local repository already exists. It uses the * <a href="https://eclipse.org/jgit/">JGit</a> library. */ public class GitPuller extends Puller { /** * Debug logging / tracing. */ private static Logger logger; /** * Name of the remote repository to pull */ private static final String REMOTE_NAME = "origin"; //$NON-NLS-1$ static { logger = Logger.getLogger(GitPuller.class.getName()); } /** * Initialize repository references. * * @param parameters * {@link Parameters} */ public GitPuller(Parameters parameters) { super(parameters); } /** * Fetch the latest sources from the Git hosting server. * * @param remoteRepository * Remote repository {@link URI}. * * @param branch * The name of the branch to fetch. * * @param localRepository * Local repository directory. */ @Override protected final void fetch(URI remoteRepository, String branch, File localRepository) throws Exception { if (!localRepository.exists()) { cloneRepository(remoteRepository, branch, localRepository); } else { pullRepository(localRepository, branch); } } /** * Clone the "master" branch from specified remote repository to the * specified local directory. * * @param remoteRepository * Remote repository {@link URL} * * @param branch * Branch name * * @param localRepository * File system path to local repository * * @throws Exception * Thrown if check out fails */ private void cloneRepository(URI remoteRepository, String branch, File localRepository) throws Exception { String externalForm = remoteRepository.toString(); CloneCommand cloneCommand = Git.cloneRepository(); cloneCommand.setDirectory(localRepository); cloneCommand.setURI(externalForm); cloneCommand.setNoCheckout(false); cloneCommand.setCloneAllBranches(false); List<String> branches = new ArrayList<String>(); branches.add(branch); cloneCommand.setBranchesToClone(branches); cloneCommand.setBranch(branch); try (Git git = cloneCommand.call()) { if (logger.isLoggable(Level.FINE)) { logger.logp(Level.FINE, getClass().getName(), "cloneRepository()", //$NON-NLS-1$ MessageFormat.format("cloned {1} of {0} to \"{0}\"", externalForm, branch, //$NON-NLS-1$ localRepository.getCanonicalPath())); } } } /** * Pull the latest sources for the "master" branch from the "origin" remote * of a local Git repository. * * @param localRepository * File system path to local repository * * @param branch * Branch name * * @throws Exception * Thrown if the Git pull operation fails. */ private void pullRepository(File localRepository, String branch) throws Exception { try (Git git = Git.open(localRepository)) { PullCommand pullCommand = git.pull(); pullCommand.setRemote(REMOTE_NAME); pullCommand.setRemoteBranchName(branch); PullResult pullResult = pullCommand.call(); if (!pullResult.isSuccessful()) { throw new Exception( MessageFormat.format("Failed to pull into {0}", localRepository.getCanonicalPath())); //$NON-NLS-1$ } CheckoutCommand checkoutCommand = git.checkout(); checkoutCommand.setName(branch); checkoutCommand.call(); CheckoutResult checkoutResult = checkoutCommand.getResult(); if (!checkoutResult.getStatus().equals(CheckoutResult.Status.OK)) { throw new Exception(MessageFormat.format("Failed to check out {0}", branch)); //$NON-NLS-1$ } if (logger.isLoggable(Level.FINE)) { logger.logp(Level.FINE, getClass().getName(), "pullRepository)", MessageFormat //$NON-NLS-1$ .format("successfully pulled {0}/{1}", localRepository.getCanonicalPath(), branch)); //$NON-NLS-1$ } } } }