List of usage examples for org.eclipse.jgit.lib RefUpdate forceUpdate
public Result forceUpdate() throws IOException
From source file:com.smartitengineering.version.impl.jgit.JGitImpl.java
License:Open Source License
protected void performCommit(final Commit newCommit, final Tree head) throws IOException { ObjectId[] parentIds;/*www . j a v a2 s .com*/ ObjectId currentHeadId = getWriteRepository().resolve(Constants.HEAD); if (currentHeadId != null) { parentIds = new ObjectId[] { currentHeadId }; } else { parentIds = new ObjectId[0]; } org.eclipse.jgit.lib.Commit commit = new org.eclipse.jgit.lib.Commit(getWriteRepository(), parentIds); commit.setTree(head); commit.setTreeId(head.getId()); PersonIdent person = new PersonIdent(newCommit.getAuthor().getName(), newCommit.getAuthor().getEmail()); commit.setAuthor(person); commit.setCommitter(person); commit.setMessage(newCommit.getCommitMessage()); ObjectId newCommitId = getObjectWriter().writeCommit(commit); if (newCommit instanceof MutableCommit) { MutableCommit mutableCommit = (MutableCommit) newCommit; mutableCommit.setCommitId(ObjectId.toString(newCommitId)); mutableCommit.setCommitTime(commit.getCommitter().getWhen()); commit.setCommitId(newCommitId); if (commit.getParentIds().length > 0) { mutableCommit.setParentCommitId(ObjectId.toString(commit.getParentIds()[0])); } else { mutableCommit.setParentCommitId(ObjectId.toString(ObjectId.zeroId())); } } else { throw new IllegalArgumentException("SPI not implemented by API!"); } RefUpdate refUpdate = getWriteRepository().updateRef(Constants.HEAD); refUpdate.setNewObjectId(commit.getCommitId()); refUpdate.setRefLogMessage(commit.getMessage(), false); refUpdate.forceUpdate(); }
From source file:io.jenkins.blueocean.blueocean_git_pipeline.GitBareRepoReadSaveRequest.java
License:Open Source License
@Override void save() throws IOException { invokeOnScm(new GitSCMFileSystem.FSFunction<Void>() { @Override//from w w w . ja va 2 s. c om public Void invoke(Repository repo) throws IOException, InterruptedException { String localBranchRef = LOCAL_REF_BASE + sourceBranch; ObjectId branchHead = repo.resolve(localBranchRef); try { // Get committer info User user = User.current(); if (user == null) { throw new ServiceException.UnauthorizedException("Not authenticated"); } String mailAddress = MailAddressResolver.resolve(user); StandardCredentials credential = getCredential(); // Make sure up-to-date and credentials work GitUtils.fetch(repo, credential); GitUtils.commit(repo, localBranchRef, filePath, contents, user.getId(), mailAddress, commitMessage, TimeZone.getDefault(), new Date()); GitUtils.push(gitSource.getRemote(), repo, credential, localBranchRef, REMOTE_REF_BASE + branch); return null; } finally { // always roll back to undo our local changes try { if (branchHead != null) { // branchHead may be null if this was an empty repo RefUpdate rollback = repo.updateRef(localBranchRef); rollback.setNewObjectId(branchHead); rollback.forceUpdate(); } } catch (Exception ex) { log.log(Level.SEVERE, "Unable to roll back repo after save failure", ex); } } } }); }
From source file:net.erdfelt.android.sdkfido.git.internal.GitCloneCommand.java
License:Apache License
private void checkoutRef(Ref ref) throws IOException { LOG.info("checkoutRef: ref:" + ref); if (ref == null) { throw new IllegalArgumentException("Unable to checkout from a null ref"); }//from ww w .ja va2 s . c om if (!Constants.HEAD.equals(ref.getName())) { RefUpdate u = repo.updateRef(Constants.HEAD); u.disableRefLog(); u.link(ref.getName()); } ObjectId commitId = ref.getObjectId(); RevCommit commit = parseCommit(commitId); RefUpdate u = repo.updateRef(Constants.HEAD); u.setNewObjectId(commit.getId()); u.forceUpdate(); DirCache dc = repo.lockDirCache(); DirCacheCheckout co = new DirCacheCheckout(repo, dc, commit.getTree()); co.checkout(); }
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.// www.j a va2 s. c o m * * @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
protected void postClone(final String remoteUrl, final String branchRef) throws GitWrapException { try {//from w w w . ja va 2 s .c o m final FetchResult fetchResult = getLatestFetchResult(); final Ref remoteHead = fetchResult.getAdvertisedRef(branchRef); if (remoteHead != null && remoteHead.getObjectId() != null) { final RevWalk walk = new RevWalk(repository); final RevCommit commit = walk.parseCommit(remoteHead.getObjectId()); final RefUpdate u; u = repository.updateRef(Constants.HEAD); u.setNewObjectId(commit); u.forceUpdate(); } } catch (final IOException e) { throw new GitWrapException("Failed to clone from: %s. Reason: %s", e, remoteUrl, e.getMessage()); } }
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;// w w w . ja v a2s . com } 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.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 av a2 s .c om 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.CloneOperation.java
License:Open Source License
private void doCheckout(final IProgressMonitor monitor) throws IOException { final Ref head = fetchResult.getAdvertisedRef(branch); if (head == null || head.getObjectId() == null) return;//from ww w . j a v a2s .c om final RevWalk rw = new RevWalk(local); final RevCommit mapCommit; try { mapCommit = rw.parseCommit(head.getObjectId()); } finally { rw.release(); } final RefUpdate u; u = local.updateRef(Constants.HEAD); u.setNewObjectId(mapCommit.getId()); u.forceUpdate(); monitor.setTaskName(CoreText.CloneOperation_checkingOutFiles); DirCacheCheckout dirCacheCheckout = new DirCacheCheckout(local, null, local.lockDirCache(), mapCommit.getTree()); dirCacheCheckout.setFailOnConflict(true); boolean result = dirCacheCheckout.checkout(); if (!result) // this should never happen when writing in an empty folder throw new IOException("Internal error occured on checking out files"); //$NON-NLS-1$ monitor.setTaskName(CoreText.CloneOperation_writingIndex); }
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();// w ww.ja v a2s. 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. java 2 s .c o 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); } }