diff options
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java | 154 |
1 files changed, 95 insertions, 59 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java index 6183da033b..47145a0563 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java @@ -1,51 +1,18 @@ /* - * Copyright (C) 2011-2013, Chris Aniszczyk <caniszczyk@gmail.com> - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2011-2013, Chris Aniszczyk <caniszczyk@gmail.com> and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.api; import java.io.IOException; import java.text.MessageFormat; +import java.util.ArrayList; import java.util.Collection; -import java.util.LinkedList; import org.eclipse.jgit.api.errors.CheckoutConflictException; import org.eclipse.jgit.api.errors.GitAPIException; @@ -58,7 +25,9 @@ import org.eclipse.jgit.dircache.DirCacheEntry; import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.NullProgressMonitor; import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.ProgressMonitor; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.Repository; @@ -121,24 +90,32 @@ public class ResetCommand extends GitCommand<Ref> { private ResetType mode; - private Collection<String> filepaths = new LinkedList<String>(); + private Collection<String> filepaths = new ArrayList<>(); + + private boolean isReflogDisabled; + + private ProgressMonitor monitor = NullProgressMonitor.INSTANCE; /** + * <p> + * Constructor for ResetCommand. + * </p> * * @param repo + * the {@link org.eclipse.jgit.lib.Repository} */ public ResetCommand(Repository repo) { super(repo); } /** + * {@inheritDoc} + * <p> * Executes the {@code Reset} command. Each instance of this class should * only be used for one invocation of the command. Don't call this method * twice on an instance. - * - * @return the Ref after reset - * @throws GitAPIException */ + @Override public Ref call() throws GitAPIException, CheckoutConflictException { checkCallable(); @@ -157,8 +134,8 @@ public class ResetCommand extends GitCommand<Ref> { if (ref != null && commitId == null) { // @TODO throw an InvalidRefNameException. We can't do that // now because this would break the API - throw new JGitInternalException("Invalid ref " + ref - + " specified"); + throw new JGitInternalException(MessageFormat + .format(JGitText.get().invalidRefName, ref)); } final ObjectId commitTree; @@ -171,7 +148,7 @@ public class ResetCommand extends GitCommand<Ref> { // reset [commit] -- paths resetIndexForPaths(commitTree); setCallable(false); - return repo.getRef(Constants.HEAD); + return repo.exactRef(Constants.HEAD); } final Ref result; @@ -181,8 +158,12 @@ public class ResetCommand extends GitCommand<Ref> { ru.setNewObjectId(commitId); String refName = Repository.shortenRefName(getRefOrHEAD()); - String message = refName + ": updating " + Constants.HEAD; //$NON-NLS-1$ - ru.setRefLogMessage(message, false); + if (isReflogDisabled) { + ru.disableRefLog(); + } else { + String message = refName + ": updating " + Constants.HEAD; //$NON-NLS-1$ + ru.setRefLogMessage(message, false); + } if (ru.forceUpdate() == RefUpdate.Result.LOCK_FAILURE) throw new JGitInternalException(MessageFormat.format( JGitText.get().cannotLock, ru.getName())); @@ -190,10 +171,8 @@ public class ResetCommand extends GitCommand<Ref> { ObjectId origHead = ru.getOldObjectId(); if (origHead != null) repo.writeOrigHead(origHead); - result = ru.getRef(); - } else { - result = repo.getRef(Constants.HEAD); } + result = repo.exactRef(Constants.HEAD); if (mode == null) mode = ResetType.MIXED; @@ -233,7 +212,7 @@ public class ResetCommand extends GitCommand<Ref> { } } - private RevCommit parseCommit(final ObjectId commitId) { + private RevCommit parseCommit(ObjectId commitId) { try (RevWalk rw = new RevWalk(repo)) { return rw.parseCommit(commitId); } catch (IOException e) { @@ -253,6 +232,8 @@ public class ResetCommand extends GitCommand<Ref> { } /** + * Set the name of the <code>Ref</code> to reset to + * * @param ref * the ref to reset to, defaults to HEAD if not specified * @return this instance @@ -263,6 +244,8 @@ public class ResetCommand extends GitCommand<Ref> { } /** + * Set the reset mode + * * @param mode * the mode of the reset command * @return this instance @@ -271,12 +254,14 @@ public class ResetCommand extends GitCommand<Ref> { if (!filepaths.isEmpty()) throw new JGitInternalException(MessageFormat.format( JGitText.get().illegalCombinationOfArguments, - "[--mixed | --soft | --hard]", "<paths>...")); //$NON-NLS-1$ + "[--mixed | --soft | --hard]", "<paths>...")); //$NON-NLS-1$ //$NON-NLS-2$ this.mode = mode; return this; } /** + * Repository relative path of file or directory to reset + * * @param path * repository-relative path of file/directory to reset (with * <code>/</code> as separator) @@ -285,22 +270,64 @@ public class ResetCommand extends GitCommand<Ref> { public ResetCommand addPath(String path) { if (mode != null) throw new JGitInternalException(MessageFormat.format( - JGitText.get().illegalCombinationOfArguments, "<paths>...", + JGitText.get().illegalCombinationOfArguments, "<paths>...", //$NON-NLS-1$ "[--mixed | --soft | --hard]")); //$NON-NLS-1$ filepaths.add(path); return this; } + /** + * Whether to disable reflog + * + * @param disable + * if {@code true} disables writing a reflog entry for this reset + * command + * @return this instance + * @since 4.5 + */ + public ResetCommand disableRefLog(boolean disable) { + this.isReflogDisabled = disable; + return this; + } + + /** + * Whether reflog is disabled + * + * @return {@code true} if writing reflog is disabled for this reset command + * @since 4.5 + */ + public boolean isReflogDisabled() { + return this.isReflogDisabled; + } + private String getRefOrHEAD() { - if (ref != null) + if (ref != null) { return ref; - else - return Constants.HEAD; + } + return Constants.HEAD; + } + + /** + * The progress monitor associated with the reset operation. By default, + * this is set to <code>NullProgressMonitor</code> + * + * @see NullProgressMonitor + * @param monitor + * a {@link org.eclipse.jgit.lib.ProgressMonitor} + * @return {@code this} + * @since 4.11 + */ + public ResetCommand setProgressMonitor(ProgressMonitor monitor) { + if (monitor == null) { + monitor = NullProgressMonitor.INSTANCE; + } + this.monitor = monitor; + return this; } private void resetIndexForPaths(ObjectId commitTree) { DirCache dc = null; - try (final TreeWalk tw = new TreeWalk(repo)) { + try (TreeWalk tw = new TreeWalk(repo)) { dc = repo.lockDirCache(); DirCacheBuilder builder = dc.builder(); @@ -362,7 +389,7 @@ public class ResetCommand extends GitCommand<Ref> { DirCacheIterator.class); if (dcIter != null && dcIter.idEqual(cIter)) { DirCacheEntry indexEntry = dcIter.getDirCacheEntry(); - entry.setLastModified(indexEntry.getLastModified()); + entry.setLastModified(indexEntry.getLastModifiedInstant()); entry.setLength(indexEntry.getLength()); } @@ -382,6 +409,7 @@ public class ResetCommand extends GitCommand<Ref> { DirCacheCheckout checkout = new DirCacheCheckout(repo, dc, commitTree); checkout.setFailOnConflict(false); + checkout.setProgressMonitor(monitor); try { checkout.checkout(); } catch (org.eclipse.jgit.errors.CheckoutConflictException cce) { @@ -408,4 +436,12 @@ public class ResetCommand extends GitCommand<Ref> { repo.writeMergeCommitMsg(null); } + @SuppressWarnings("nls") + @Override + public String toString() { + return "ResetCommand [repo=" + repo + ", ref=" + ref + ", mode=" + mode + + ", isReflogDisabled=" + isReflogDisabled + ", filepaths=" + + filepaths + "]"; + } + } |