List of usage examples for org.eclipse.jgit.lib RefUpdate setRefLogMessage
public void setRefLogMessage(String msg, boolean appendStatus)
From source file:jbyoshi.gitupdate.processor.FastForward.java
License:Apache License
private static boolean tryFastForward(Repository repo, Ref ref, Ref target, Report report) throws GitAPIException, IOException { if (ref == null || target == null) { return false; }//w w w . ja va2 s . com target = repo.peel(target); if (!ref.equals(repo.getRef(Constants.HEAD).getTarget())) { try (RevWalk revWalk = new RevWalk(repo)) { ObjectId targetId = target.getPeeledObjectId(); if (targetId == null) { targetId = target.getObjectId(); } RevCommit targetCommit = revWalk.lookupCommit(targetId); ObjectId sourceId = ref.getObjectId(); RevCommit sourceCommit = revWalk.lookupCommit(sourceId); if (revWalk.isMergedInto(sourceCommit, targetCommit)) { RefUpdate refUpdate = repo.updateRef(ref.getName()); refUpdate.setNewObjectId(targetCommit); refUpdate.setRefLogMessage("Fast forward", false); refUpdate.setExpectedOldObjectId(sourceId); Result rc = refUpdate.update(); switch (rc) { case NEW: case FAST_FORWARD: report.newChild(ref.getName() + " -> " + target.getName()).modified(); return true; case REJECTED: case LOCK_FAILURE: report.newErrorChild(new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, refUpdate.getRef(), rc)); break; case NO_CHANGE: break; default: report.newErrorChild(new JGitInternalException(MessageFormat .format(JGitText.get().updatingRefFailed, ref.getName(), targetId.toString(), rc))); break; } } return false; } } try { MergeResult result = Git.wrap(repo).merge().setFastForward(MergeCommand.FastForwardMode.FF_ONLY) .include(target.getTarget()).call(); if (result.getMergeStatus() == MergeResult.MergeStatus.ALREADY_UP_TO_DATE) { // Ignore } else if (result.getMergeStatus() == MergeResult.MergeStatus.FAST_FORWARD) { report.newChild("Fast-forwarded " + ref.getName() + " to " + target.getName()).modified(); return true; } else { report.newChild("Fast-forward failed: status " + result.getMergeStatus()).error(); } } catch (NoHeadException e) { // Ignore } return false; }
From source file:net.polydawn.mdm.Plumbing.java
License:Open Source License
/** * Create a new "empty" commit in a new branch. If the branch name already exists, * a forced update will be performed./* w w w .j a v a 2s. c om*/ * * @return result of the branch update. * @throws IOException */ public static RefUpdate.Result createOrphanBranch(Repository repo, String branchName) throws IOException { ObjectInserter odi = repo.newObjectInserter(); try { // Create an (empty) tree object to reference from a commit. TreeFormatter tree = new TreeFormatter(); ObjectId treeId = odi.insert(tree); // Create a commit object... most data is nulls or silly placeholders; I expect you'll amend this commit. CommitBuilder commit = new CommitBuilder(); PersonIdent author = new PersonIdent("mdm", ""); commit.setAuthor(author); commit.setCommitter(author); commit.setMessage(""); commit.setTreeId(treeId); // Insert the commit into the repository. ObjectId commitId = odi.insert(commit); odi.flush(); // (Re)extract the commit we just flushed, and update a new branch ref to point to it. RevWalk revWalk = new RevWalk(repo); try { RevCommit revCommit = revWalk.parseCommit(commitId); if (!branchName.startsWith("refs/")) branchName = "refs/heads/" + branchName; RefUpdate ru = repo.updateRef(branchName); ru.setNewObjectId(commitId); ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false); return ru.forceUpdate(); } finally { revWalk.release(); } } finally { odi.release(); } }
From source file:org.commonjava.gitwrap.BareGitRepository.java
License:Open Source License
public BareGitRepository createTag(final String tagSource, final String tagName, final String message, final boolean force) throws GitWrapException { try {// www . ja va 2 s .c o m final ObjectId src = repository.resolve(tagSource); if (src == null) { throw new GitWrapException("Cannot resolve tag-source: %s", tagSource); } if (!force && repository.resolve(tagName) != null) { throw new GitWrapException("Tag: %s already exists!", tagName); } String dest = tagName; if (!dest.startsWith(Constants.R_TAGS)) { dest = Constants.R_TAGS + tagName; } final String tagShort = dest.substring(Constants.R_TAGS.length()); final ObjectLoader sourceLoader = repository.open(src); final ObjectInserter inserter = repository.newObjectInserter(); final TagBuilder tag = new TagBuilder(); tag.setTag(tagShort); tag.setTagger(new PersonIdent(repository)); tag.setObjectId(src, sourceLoader.getType()); tag.setMessage(message); final ObjectId tagId = inserter.insert(tag); tag.setTagId(tagId); final String refName = Constants.R_TAGS + tag.getTag(); final RefUpdate tagRef = repository.updateRef(refName); tagRef.setNewObjectId(tag.getTagId()); tagRef.setForceUpdate(force); tagRef.setRefLogMessage("Tagging source: " + src.name() + " as " + tagName, false); final Result updateResult = tagRef.update(); switch (updateResult) { case NEW: case FAST_FORWARD: case FORCED: { break; } case REJECTED: { throw new GitWrapException("Tag already exists: %s", tagName); } default: { throw new GitWrapException("Cannot lock tag: %s", tagName); } } } catch (final IOException e) { throw new GitWrapException("Failed to add tag: %s", e, e.getMessage()); } return this; }
From source file:org.commonjava.gitwrap.BareGitRepository.java
License:Open Source License
public BareGitRepository createBranch(final String source, final String name) throws GitWrapException { final String refName = (name.startsWith(Constants.R_HEADS) || name.startsWith(Constants.R_TAGS)) ? name : Constants.R_HEADS + name;//from w w w . j a va2 s . c o m try { String src; final Ref from = repository.getRef(source); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Creating branch: " + refName + " from: " + from); } final ObjectId startAt = repository.resolve(source + "^0"); if (from != null) { src = from.getName(); } else { src = startAt.name(); } src = repository.shortenRefName(src); if (!Repository.isValidRefName(refName)) { throw new GitWrapException("Invalid branch name: " + refName); } if (repository.resolve(refName) != null) { throw new GitWrapException("Branch: " + refName + " already exists!"); } final RefUpdate updateRef = repository.updateRef(refName); updateRef.setNewObjectId(startAt); updateRef.setRefLogMessage("branch: Created from " + source, false); final Result updateResult = updateRef.update(); if (updateResult == Result.REJECTED) { throw new GitWrapException("Branch creation rejected for: %s", refName); } } catch (final IOException e) { throw new GitWrapException("Failed to create branch: %s from: %s.\nReason: %s", e, refName, source, e.getMessage()); } return this; }
From source file:org.commonjava.gitwrap.GitRepository.java
License:Open Source License
public GitRepository checkoutBranch(final String name) throws GitWrapException { final String refName; if (name == null) { refName = Constants.HEAD;//from www. jav a2 s. co m } else if (name.startsWith(Constants.R_HEADS) || name.startsWith(Constants.R_TAGS)) { refName = name; } else { refName = Constants.R_HEADS + name; } if (hasBranch(refName)) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Checking out: " + refName); } final FileRepository repository = getRepository(); final boolean detach = !refName.startsWith(Constants.R_HEADS); try { final RevWalk walk = new RevWalk(repository); final RevCommit newCommit = walk.parseCommit(repository.resolve(refName)); final RevCommit oldCommit = walk.parseCommit(repository.resolve(Constants.HEAD)); final GitIndex index = repository.getIndex(); final RevTree newTree = newCommit.getTree(); final RevTree oldTree = oldCommit.getTree(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Checking out: " + newCommit + " (resolved from: " + refName + ")"); } final WorkDirCheckout checkout = new WorkDirCheckout(repository, repository.getWorkTree(), repository.mapTree(oldTree), index, repository.mapTree(newTree)); checkout.checkout(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Writing index..."); } index.write(); final RefUpdate u = repository.updateRef(Constants.HEAD, detach); Result result; if (detach) { u.setNewObjectId(newCommit.getId()); u.setRefLogMessage("Switching to detached branch: " + refName, false); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Updating head ref to point to: " + newCommit.getId()); } result = u.forceUpdate(); } else { u.setRefLogMessage("Switching to linked branch: " + refName, false); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Linking head ref to: " + refName); } result = u.link(refName); } switch (result) { case NEW: case FORCED: case NO_CHANGE: case FAST_FORWARD: break; default: throw new GitWrapException("Error checking out branch: %s. Result: %s", refName, u.getResult().name()); } } catch (final IOException e) { throw new GitWrapException("Failed to checkout branch: %s.\nReason: %s", e, refName, e.getMessage()); } } else { throw new GitWrapException( "Cannot checkout non-existent branch: %s. Perhaps you meant to call createBranch(..)?", refName); } return this; }
From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java
License:Open Source License
@Override public void tagDelete(TagDeleteRequest request) throws GitException { try {//from w ww . ja va 2s. c om String tagName = request.getName(); Ref tagRef = repository.findRef(tagName); if (tagRef == null) { throw new IllegalArgumentException("Tag " + tagName + " not found. "); } RefUpdate updateRef = repository.updateRef(tagRef.getName()); updateRef.setRefLogMessage("tag deleted", false); updateRef.setForceUpdate(true); Result deleteResult = updateRef.delete(); if (deleteResult != Result.FORCED && deleteResult != Result.FAST_FORWARD) { throw new GitException(String.format(ERROR_TAG_DELETE, tagName, deleteResult)); } } catch (IOException exception) { throw new GitException(exception.getMessage(), exception); } }
From source file:org.eclipse.egit.core.op.BranchOperation.java
License:Open Source License
private void updateHeadRef() throws TeamException { boolean detach = false; // in case of a non-local branch or a tag, // we "detach" HEAD, i.e. point it to the // underlying commit instead of to the Ref if (refName == null || !refName.startsWith(Constants.R_HEADS)) detach = true;//from w w w .j ava 2 s. c o m try { RefUpdate u = repository.updateRef(Constants.HEAD, detach); Result res; if (detach) { u.setNewObjectId(newCommit.getId()); // using forceUpdate instead of update avoids // the merge tests which would otherwise make // this fail u.setRefLogMessage(NLS.bind(CoreText.BranchOperation_checkoutMovingTo, newCommit.getId().name()), false); res = u.forceUpdate(); } else { u.setRefLogMessage(NLS.bind(CoreText.BranchOperation_checkoutMovingTo, refName), false); res = u.link(refName); } switch (res) { case NEW: case FORCED: case NO_CHANGE: case FAST_FORWARD: break; default: throw new IOException(u.getResult().name()); } } catch (IOException e) { throw new TeamException(NLS.bind(CoreText.BranchOperation_updatingHeadToRef, refName), e); } }
From source file:org.eclipse.egit.core.op.CommitOperation.java
License:Open Source License
private void doCommits(String actMessage, HashMap<Repository, Tree> treeMap) throws IOException, TeamException { String commitMessage = actMessage; final Date commitDate = new Date(); final TimeZone timeZone = TimeZone.getDefault(); final PersonIdent authorIdent = RawParseUtils.parsePersonIdent(author); final PersonIdent committerIdent = RawParseUtils.parsePersonIdent(committer); for (java.util.Map.Entry<Repository, Tree> entry : treeMap.entrySet()) { Tree tree = entry.getValue();// www. j a va 2s.co m Repository repo = tree.getRepository(); repo.getIndex().write(); writeTreeWithSubTrees(tree); ObjectId currentHeadId = repo.resolve(Constants.HEAD); ObjectId[] parentIds; if (amending) { RevCommit[] parents = previousCommit.getParents(); parentIds = new ObjectId[parents.length]; for (int i = 0; i < parents.length; i++) parentIds[i] = parents[i].getId(); } else { if (currentHeadId != null) parentIds = new ObjectId[] { currentHeadId }; else parentIds = new ObjectId[0]; } if (createChangeId) { ObjectId parentId; if (parentIds.length > 0) parentId = parentIds[0]; else parentId = null; ObjectId changeId = ChangeIdUtil.computeChangeId(tree.getId(), parentId, authorIdent, committerIdent, commitMessage); commitMessage = ChangeIdUtil.insertId(commitMessage, changeId); if (changeId != null) commitMessage = commitMessage.replaceAll( "\nChange-Id: I0000000000000000000000000000000000000000\n", //$NON-NLS-1$ "\nChange-Id: I" + changeId.getName() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ } CommitBuilder commit = new CommitBuilder(); commit.setTreeId(tree.getTreeId()); commit.setParentIds(parentIds); commit.setMessage(commitMessage); commit.setAuthor(new PersonIdent(authorIdent, commitDate, timeZone)); commit.setCommitter(new PersonIdent(committerIdent, commitDate, timeZone)); ObjectInserter inserter = repo.newObjectInserter(); ObjectId commitId; try { commitId = inserter.insert(commit); inserter.flush(); } finally { inserter.release(); } final RefUpdate ru = repo.updateRef(Constants.HEAD); ru.setNewObjectId(commitId); ru.setRefLogMessage(buildReflogMessage(commitMessage), false); if (ru.forceUpdate() == RefUpdate.Result.LOCK_FAILURE) { throw new TeamException(NLS.bind(CoreText.CommitOperation_failedToUpdate, ru.getName(), commitId)); } } }
From source file:org.eclipse.egit.core.op.ResetOperation.java
License:Open Source License
private void writeRef() throws TeamException { try {/*from w w w .j a v a2 s . co m*/ final RefUpdate ru = repository.updateRef(Constants.HEAD); ru.setNewObjectId(commit.getId()); String name = refName; if (name.startsWith("refs/heads/")) //$NON-NLS-1$ name = name.substring(11); if (name.startsWith("refs/remotes/")) //$NON-NLS-1$ name = name.substring(13); String message = "reset --" //$NON-NLS-1$ + type.toString().toLowerCase() + " " + name; //$NON-NLS-1$ ru.setRefLogMessage(message, false); if (ru.forceUpdate() == RefUpdate.Result.LOCK_FAILURE) throw new TeamException(NLS.bind(CoreText.ResetOperation_cantUpdate, ru.getName())); } catch (IOException e) { throw new TeamException(NLS.bind(CoreText.ResetOperation_updatingFailed, Constants.HEAD), e); } }
From source file:org.eclipse.egit.core.test.TestRepository.java
License:Open Source License
/** * Creates a new branch//w ww .ja v a 2 s.com * * @param refName * starting point for the new branch * @param newRefName * @throws IOException */ public void createBranch(String refName, String newRefName) throws IOException { RefUpdate updateRef; updateRef = repository.updateRef(newRefName); Ref startRef = repository.getRef(refName); ObjectId startAt = repository.resolve(refName); String startBranch; if (startRef != null) startBranch = refName; else startBranch = startAt.name(); startBranch = Repository.shortenRefName(startBranch); updateRef.setNewObjectId(startAt); updateRef.setRefLogMessage("branch: Created from " + startBranch, false); //$NON-NLS-1$ updateRef.update(); }