Skip to content

cherrypickCommit

Applies a cherrypick of cherrypick_commit against our_commit and returns the resulting Index, without modifying the working directory or repository state. This method does not write any changes to disk or update HEAD. it is useful for computing what the cherrypick result would look like without actually applying it.

Signature

ts
class Repository {
  cherrypickCommit(
    cherrypickCommit: Commit,
    ourCommit: Commit,
    mainline: number,
    mergeOptions?: MergeOptions | undefined | null,
  ): Index;
}

Parameters

  • cherrypickCommitrequired · Commit

    The commit to cherrypick.

  • ourCommitrequired · Commit

    The commit to cherrypick against (usually HEAD).

  • mainlinerequired · number

    The parent of the cherrypick commit, if it is a merge (1-based).

  • mergeOptionsnull | MergeOptions

    Options for merge conflict resolution.

    • diff3Styleboolean

      Create diff3-style file

    • failOnConflictboolean

      If a conflict occurs, exit immediately instead of attempting to continue resolving conflicts

    • filFavorFileFavor

      Specify a side to favor for resolving conflicts

    • findRenamesboolean

      Detect file renames

    • ignoreWhitespaceboolean

      Ignore all whitespace

    • ignoreWhitespaceChangeboolean

      Ignore changes in amount of whitespace

    • ignoreWhitespaceEolboolean

      Ignore whitespace at end of line

    • minimalboolean

      Take extra time to find minimal diff

    • noRecursiveboolean

      If the commits being merged have multiple merge bases, do not build a recursive merge base (by merging the multiple merge bases), instead simply use the first base.

    • patienceboolean

      Use the "patience diff" algorithm

    • recursionLimitnumber

      Maximum number of times to merge common ancestors to build a virtual merge base when faced with criss-cross merges. When this limit is reached, the next ancestor will simply be used instead of attempting to merge it. The default is unlimited.

    • renameThresholdnumber

      Similarity to consider a file renamed (default 50)

    • simplifyAlnumboolean

      Condense non-alphanumeric regions for simplified diff file

    • skipReucboolean

      Do not write the REUC extension on the generated index

    • standardStyleboolean

      Create standard conflicted merge files

    • targetLimitnumber

      Maximum similarity sources to examine for renames (default 200). If the number of rename candidates (add / delete pairs) is greater than this value, inexact rename detection is aborted. This setting overrides the merge.renameLimit configuration value.

Returns

  • Index

    The index result.

Errors

  • Error

    If the cherrypick commit is a merge and mainline is 0.

  • Error

    If there are conflicts and failOnConflict is true (default).

Examples

ts
// This is an example for cherrypick_commit
import { openRepository } from "es-git";

const repo = await openRepository("./path/to/repo");
const cherry = repo.getCommit("cherrypick-commit");
const target = repo.getCommit("onto-commit");

// Returns the Index resulting from the cherrypick in memory,
// without affecting HEAD or the working tree.
// The mainline parameter indicates which parent to use as the baseline,
// For merge commits, mainline specifies which parent to use as baseline (1 or 2).
// For normal (non-merge) commits, use mainline 0.
const idx = repo.cherrypickCommit(cherry, target, 0);

// You can check for conflicts with idx.hasConflicts()

Released under the MIT License.