All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kuali.maven.plugins.fusion.util.GitFusionUtils Maven / Gradle / Ivy

/**
 * 
 */
package org.kuali.maven.plugins.fusion.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;

import org.eclipse.jgit.api.CheckoutCommand;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.CheckoutConflictException;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRefNameException;
import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
import org.eclipse.jgit.api.errors.RefNotFoundException;
import org.eclipse.jgit.lib.CommitBuilder;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.RefUpdate.Result;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author ocleirig
 *
 */
public final class GitFusionUtils {

	private static final Logger log = LoggerFactory.getLogger(GitFusionUtils.class);
	
	/**
	 * 
	 */
	public GitFusionUtils() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * Used to create the fused commit or any commit where we want to be able to set the parents and tree programatically instead of from the working copy.
	 * 
	 * @param objectInserter
	 * @param userName
	 * @param email
	 * @param commitMessage
	 * @param treeId
	 * @param mergeParents
	 * @return
	 * @throws IOException
	 */
	public static ObjectId commit(ObjectInserter objectInserter, String userName, String email, String commitMessage, ObjectId treeId, SetmergeParents) throws IOException {
		
		CommitBuilder commitBuilder = new CommitBuilder();

		PersonIdent ident = new PersonIdent(userName, email);

		commitBuilder.setAuthor(ident);
		commitBuilder.setCommitter(ident);

		commitBuilder.setMessage(commitMessage);

		commitBuilder.setTreeId(treeId);

		commitBuilder.setParentIds(new ArrayList<>(mergeParents));

		commitBuilder.setEncoding("UTF-8");

		ObjectId commitId = objectInserter.insert(commitBuilder);
		
		return commitId;
	}

	public static CommitCommand commit(Repository repo, RevCommit previousCommit, String commitMessage, boolean amend) {
		
		CommitCommand commitCommand = new Git (repo).commit();
		
		String adjustedCommitMessage = commitMessage;
		
		if (amend) {
			
			commitCommand.setAmend(amend);
			
			adjustedCommitMessage = previousCommit.getFullMessage() + "\n" +  "Amended changes";
			
		}
		
		PersonIdent pi;
		commitCommand.setAuthor(pi = new PersonIdent("jcaddel", "[email protected]"));
		
		commitCommand.setCommitter(pi);
		
		commitCommand.setAll(true);
		
		commitCommand.setMessage(adjustedCommitMessage);
		
		return commitCommand;
	}

	
	/**
	 * Create a new branch at the commitId specified and check it out.
	 * 
	 * @param repo
	 * @param branchName
	 * @param commitId
	 * @param create
	 * @param force
	 * @return
	 * @throws GitAPIException 
	 * @throws CheckoutConflictException 
	 * @throws InvalidRefNameException 
	 * @throws RefNotFoundException 
	 * @throws RefAlreadyExistsException 
	 * @throws IOException 
	 */
	public static Ref checkOut(Repository repo, String branchName, boolean create, boolean force) throws RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException, CheckoutConflictException, GitAPIException, IOException {

		if (create && force) {
			Ref existing = repo.getRef(Constants.R_HEADS + branchName);
			
			if (existing != null) {
				deleteBranch (repo, existing.getName());
			}
			
		}
		CheckoutCommand checkOutCommand = new Git (repo).checkout();
		
		checkOutCommand.setName(branchName);
		checkOutCommand.setCreateBranch(create);
		checkOutCommand.setForce(force);
		
		return checkOutCommand.call();
		
		
	}

	public static Ref checkOut(Repository repo, String currentBranchName) throws RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException, CheckoutConflictException, GitAPIException, IOException {
		return checkOut(repo, currentBranchName, false, false);
	}

	public static void deleteBranch(Repository repo, String branchName) throws IOException, RefAlreadyExistsException {
		
		log.info("Deleting existing reference to " + branchName);
		
		RefUpdate ru = repo.updateRef(Constants.R_HEADS + branchName);
		
		ru.setForceUpdate(true);
		
		Result result = ru.delete();
		
		if (result != Result.FORCED)
			throw new RefAlreadyExistsException(branchName + " already exists.  delete result = " + result.toString());
		
	}
	

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy