/* * Copyright (C) 2011, 2021 IBM Corporation and others * * 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. * * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.api; import java.io.File; import java.io.InputStream; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.patch.PatchApplier; import org.eclipse.jgit.patch.PatchApplier.Result; /** * Apply a patch to files and/or to the index. * * @see Git documentation about apply * @since 2.0 */ public class ApplyCommand extends GitCommand { private InputStream in; /** * Constructs the command. * * @param local */ ApplyCommand(Repository local) { super(local); if (local == null) { throw new NullPointerException(JGitText.get().repositoryIsRequired); } } /** * Set patch * * @param in * the patch to apply * @return this instance */ public ApplyCommand setPatch(InputStream in) { checkCallable(); this.in = in; return this; } /** * {@inheritDoc} * *

* Executes the {@code ApplyCommand} command with all the options and * parameters collected by the setter methods (e.g. * {@link #setPatch(InputStream)} of this class. Each instance of this class * should only be used for one invocation of the command. Don't call this * method twice on an instance. */ @Override public ApplyResult call() throws GitAPIException { checkCallable(); setCallable(false); ApplyResult r = new ApplyResult(); PatchApplier patchApplier = new PatchApplier(repo); Result applyResult = patchApplier.applyPatch(in); for (String p : applyResult.getPaths()) { r.addUpdatedFile(new File(repo.getWorkTree(), p)); } return r; } }