Blame include/git2/diff.h

Packit ae9e2a
/*
Packit ae9e2a
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit ae9e2a
 *
Packit ae9e2a
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit ae9e2a
 * a Linking Exception. For full terms see the included COPYING file.
Packit ae9e2a
 */
Packit ae9e2a
#ifndef INCLUDE_git_diff_h__
Packit ae9e2a
#define INCLUDE_git_diff_h__
Packit ae9e2a
Packit ae9e2a
#include "common.h"
Packit ae9e2a
#include "types.h"
Packit ae9e2a
#include "oid.h"
Packit ae9e2a
#include "tree.h"
Packit ae9e2a
#include "refs.h"
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * @file git2/diff.h
Packit ae9e2a
 * @brief Git tree and file differencing routines.
Packit ae9e2a
 *
Packit ae9e2a
 * Overview
Packit ae9e2a
 * --------
Packit ae9e2a
 *
Packit ae9e2a
 * Calculating diffs is generally done in two phases: building a list of
Packit ae9e2a
 * diffs then traversing it.  This makes is easier to share logic across
Packit ae9e2a
 * the various types of diffs (tree vs tree, workdir vs index, etc.), and
Packit ae9e2a
 * also allows you to insert optional diff post-processing phases,
Packit ae9e2a
 * such as rename detection, in between the steps.  When you are done with
Packit ae9e2a
 * a diff object, it must be freed.
Packit ae9e2a
 *
Packit ae9e2a
 * Terminology
Packit ae9e2a
 * -----------
Packit ae9e2a
 *
Packit ae9e2a
 * To understand the diff APIs, you should know the following terms:
Packit ae9e2a
 *
Packit ae9e2a
 * - A `diff` represents the cumulative list of differences between two
Packit ae9e2a
 *   snapshots of a repository (possibly filtered by a set of file name
Packit ae9e2a
 *   patterns).  This is the `git_diff` object.
Packit ae9e2a
 *
Packit ae9e2a
 * - A `delta` is a file pair with an old and new revision.  The old version
Packit ae9e2a
 *   may be absent if the file was just created and the new version may be
Packit ae9e2a
 *   absent if the file was deleted.  A diff is mostly just a list of deltas.
Packit ae9e2a
 *
Packit ae9e2a
 * - A `binary` file / delta is a file (or pair) for which no text diffs
Packit ae9e2a
 *   should be generated.  A diff can contain delta entries that are
Packit ae9e2a
 *   binary, but no diff content will be output for those files.  There is
Packit ae9e2a
 *   a base heuristic for binary detection and you can further tune the
Packit ae9e2a
 *   behavior with git attributes or diff flags and option settings.
Packit ae9e2a
 *
Packit ae9e2a
 * - A `hunk` is a span of modified lines in a delta along with some stable
Packit ae9e2a
 *   surrounding context.  You can configure the amount of context and other
Packit ae9e2a
 *   properties of how hunks are generated.  Each hunk also comes with a
Packit ae9e2a
 *   header that described where it starts and ends in both the old and new
Packit ae9e2a
 *   versions in the delta.
Packit ae9e2a
 *
Packit ae9e2a
 * - A `line` is a range of characters inside a hunk.  It could be a context
Packit ae9e2a
 *   line (i.e. in both old and new versions), an added line (i.e. only in
Packit ae9e2a
 *   the new version), or a removed line (i.e. only in the old version).
Packit ae9e2a
 *   Unfortunately, we don't know anything about the encoding of data in the
Packit ae9e2a
 *   file being diffed, so we cannot tell you much about the line content.
Packit ae9e2a
 *   Line data will not be NUL-byte terminated, however, because it will be
Packit ae9e2a
 *   just a span of bytes inside the larger file.
Packit ae9e2a
 *
Packit ae9e2a
 * @ingroup Git
Packit ae9e2a
 * @{
Packit ae9e2a
 */
Packit ae9e2a
GIT_BEGIN_DECL
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Flags for diff options.  A combination of these flags can be passed
Packit ae9e2a
 * in via the `flags` value in the `git_diff_options`.
Packit ae9e2a
 */
Packit ae9e2a
typedef enum {
Packit ae9e2a
	/** Normal diff, the default */
Packit ae9e2a
	GIT_DIFF_NORMAL = 0,
Packit ae9e2a
Packit ae9e2a
	/*
Packit ae9e2a
	 * Options controlling which files will be in the diff
Packit ae9e2a
	 */
Packit ae9e2a
Packit ae9e2a
	/** Reverse the sides of the diff */
Packit ae9e2a
	GIT_DIFF_REVERSE = (1u << 0),
Packit ae9e2a
Packit ae9e2a
	/** Include ignored files in the diff */
Packit ae9e2a
	GIT_DIFF_INCLUDE_IGNORED = (1u << 1),
Packit ae9e2a
Packit ae9e2a
	/** Even with GIT_DIFF_INCLUDE_IGNORED, an entire ignored directory
Packit ae9e2a
	 *  will be marked with only a single entry in the diff; this flag
Packit ae9e2a
	 *  adds all files under the directory as IGNORED entries, too.
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_DIFF_RECURSE_IGNORED_DIRS = (1u << 2),
Packit ae9e2a
Packit ae9e2a
	/** Include untracked files in the diff */
Packit ae9e2a
	GIT_DIFF_INCLUDE_UNTRACKED = (1u << 3),
Packit ae9e2a
Packit ae9e2a
	/** Even with GIT_DIFF_INCLUDE_UNTRACKED, an entire untracked
Packit ae9e2a
	 *  directory will be marked with only a single entry in the diff
Packit ae9e2a
	 *  (a la what core Git does in `git status`); this flag adds *all*
Packit ae9e2a
	 *  files under untracked directories as UNTRACKED entries, too.
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_DIFF_RECURSE_UNTRACKED_DIRS = (1u << 4),
Packit ae9e2a
Packit ae9e2a
	/** Include unmodified files in the diff */
Packit ae9e2a
	GIT_DIFF_INCLUDE_UNMODIFIED = (1u << 5),
Packit ae9e2a
Packit ae9e2a
	/** Normally, a type change between files will be converted into a
Packit ae9e2a
	 *  DELETED record for the old and an ADDED record for the new; this
Packit ae9e2a
	 *  options enabled the generation of TYPECHANGE delta records.
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_DIFF_INCLUDE_TYPECHANGE = (1u << 6),
Packit ae9e2a
Packit ae9e2a
	/** Even with GIT_DIFF_INCLUDE_TYPECHANGE, blob->tree changes still
Packit ae9e2a
	 *  generally show as a DELETED blob.  This flag tries to correctly
Packit ae9e2a
	 *  label blob->tree transitions as TYPECHANGE records with new_file's
Packit ae9e2a
	 *  mode set to tree.  Note: the tree SHA will not be available.
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_DIFF_INCLUDE_TYPECHANGE_TREES = (1u << 7),
Packit ae9e2a
Packit ae9e2a
	/** Ignore file mode changes */
Packit ae9e2a
	GIT_DIFF_IGNORE_FILEMODE = (1u << 8),
Packit ae9e2a
Packit ae9e2a
	/** Treat all submodules as unmodified */
Packit ae9e2a
	GIT_DIFF_IGNORE_SUBMODULES = (1u << 9),
Packit ae9e2a
Packit ae9e2a
	/** Use case insensitive filename comparisons */
Packit ae9e2a
	GIT_DIFF_IGNORE_CASE = (1u << 10),
Packit ae9e2a
Packit ae9e2a
	/** May be combined with `GIT_DIFF_IGNORE_CASE` to specify that a file
Packit ae9e2a
	 *  that has changed case will be returned as an add/delete pair.
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_DIFF_INCLUDE_CASECHANGE = (1u << 11),
Packit ae9e2a
Packit ae9e2a
	/** If the pathspec is set in the diff options, this flags indicates
Packit ae9e2a
	 *  that the paths will be treated as literal paths instead of
Packit ae9e2a
	 *  fnmatch patterns.  Each path in the list must either be a full
Packit ae9e2a
	 *  path to a file or a directory.  (A trailing slash indicates that
Packit ae9e2a
	 *  the path will _only_ match a directory).  If a directory is
Packit ae9e2a
	 *  specified, all children will be included.
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_DIFF_DISABLE_PATHSPEC_MATCH = (1u << 12),
Packit ae9e2a
Packit ae9e2a
	/** Disable updating of the `binary` flag in delta records.  This is
Packit ae9e2a
	 *  useful when iterating over a diff if you don't need hunk and data
Packit ae9e2a
	 *  callbacks and want to avoid having to load file completely.
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_DIFF_SKIP_BINARY_CHECK = (1u << 13),
Packit ae9e2a
Packit ae9e2a
	/** When diff finds an untracked directory, to match the behavior of
Packit ae9e2a
	 *  core Git, it scans the contents for IGNORED and UNTRACKED files.
Packit ae9e2a
	 *  If *all* contents are IGNORED, then the directory is IGNORED; if
Packit ae9e2a
	 *  any contents are not IGNORED, then the directory is UNTRACKED.
Packit ae9e2a
	 *  This is extra work that may not matter in many cases.  This flag
Packit ae9e2a
	 *  turns off that scan and immediately labels an untracked directory
Packit ae9e2a
	 *  as UNTRACKED (changing the behavior to not match core Git).
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS = (1u << 14),
Packit ae9e2a
Packit ae9e2a
	/** When diff finds a file in the working directory with stat
Packit ae9e2a
	 * information different from the index, but the OID ends up being the
Packit ae9e2a
	 * same, write the correct stat information into the index.  Note:
Packit ae9e2a
	 * without this flag, diff will always leave the index untouched.
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_DIFF_UPDATE_INDEX = (1u << 15),
Packit ae9e2a
Packit ae9e2a
	/** Include unreadable files in the diff */
Packit ae9e2a
	GIT_DIFF_INCLUDE_UNREADABLE = (1u << 16),
Packit ae9e2a
	
Packit ae9e2a
	/** Include unreadable files in the diff */
Packit ae9e2a
	GIT_DIFF_INCLUDE_UNREADABLE_AS_UNTRACKED = (1u << 17),
Packit ae9e2a
Packit ae9e2a
	/*
Packit ae9e2a
	 * Options controlling how output will be generated
Packit ae9e2a
	 */
Packit ae9e2a
Packit ae9e2a
	/** Treat all files as text, disabling binary attributes & detection */
Packit ae9e2a
	GIT_DIFF_FORCE_TEXT = (1u << 20),
Packit ae9e2a
	/** Treat all files as binary, disabling text diffs */
Packit ae9e2a
	GIT_DIFF_FORCE_BINARY = (1u << 21),
Packit ae9e2a
Packit ae9e2a
	/** Ignore all whitespace */
Packit ae9e2a
	GIT_DIFF_IGNORE_WHITESPACE = (1u << 22),
Packit ae9e2a
	/** Ignore changes in amount of whitespace */
Packit ae9e2a
	GIT_DIFF_IGNORE_WHITESPACE_CHANGE = (1u << 23),
Packit ae9e2a
	/** Ignore whitespace at end of line */
Packit ae9e2a
	GIT_DIFF_IGNORE_WHITESPACE_EOL = (1u << 24),
Packit ae9e2a
Packit ae9e2a
	/** When generating patch text, include the content of untracked
Packit ae9e2a
	 *  files.  This automatically turns on GIT_DIFF_INCLUDE_UNTRACKED but
Packit ae9e2a
	 *  it does not turn on GIT_DIFF_RECURSE_UNTRACKED_DIRS.  Add that
Packit ae9e2a
	 *  flag if you want the content of every single UNTRACKED file.
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_DIFF_SHOW_UNTRACKED_CONTENT = (1u << 25),
Packit ae9e2a
Packit ae9e2a
	/** When generating output, include the names of unmodified files if
Packit ae9e2a
	 *  they are included in the git_diff.  Normally these are skipped in
Packit ae9e2a
	 *  the formats that list files (e.g. name-only, name-status, raw).
Packit ae9e2a
	 *  Even with this, these will not be included in patch format.
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_DIFF_SHOW_UNMODIFIED = (1u << 26),
Packit ae9e2a
Packit ae9e2a
	/** Use the "patience diff" algorithm */
Packit ae9e2a
	GIT_DIFF_PATIENCE = (1u << 28),
Packit ae9e2a
	/** Take extra time to find minimal diff */
Packit ae9e2a
	GIT_DIFF_MINIMAL = (1 << 29),
Packit ae9e2a
Packit ae9e2a
	/** Include the necessary deflate / delta information so that `git-apply`
Packit ae9e2a
	 *  can apply given diff information to binary files.
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_DIFF_SHOW_BINARY = (1 << 30),
Packit ae9e2a
} git_diff_option_t;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * The diff object that contains all individual file deltas.
Packit ae9e2a
 *
Packit ae9e2a
 * This is an opaque structure which will be allocated by one of the diff
Packit ae9e2a
 * generator functions below (such as `git_diff_tree_to_tree`).  You are
Packit ae9e2a
 * responsible for releasing the object memory when done, using the
Packit ae9e2a
 * `git_diff_free()` function.
Packit ae9e2a
 */
Packit ae9e2a
typedef struct git_diff git_diff;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Flags for the delta object and the file objects on each side.
Packit ae9e2a
 *
Packit ae9e2a
 * These flags are used for both the `flags` value of the `git_diff_delta`
Packit ae9e2a
 * and the flags for the `git_diff_file` objects representing the old and
Packit ae9e2a
 * new sides of the delta.  Values outside of this public range should be
Packit ae9e2a
 * considered reserved for internal or future use.
Packit ae9e2a
 */
Packit ae9e2a
typedef enum {
Packit ae9e2a
	GIT_DIFF_FLAG_BINARY     = (1u << 0), /**< file(s) treated as binary data */
Packit ae9e2a
	GIT_DIFF_FLAG_NOT_BINARY = (1u << 1), /**< file(s) treated as text data */
Packit ae9e2a
	GIT_DIFF_FLAG_VALID_ID   = (1u << 2), /**< `id` value is known correct */
Packit ae9e2a
	GIT_DIFF_FLAG_EXISTS     = (1u << 3), /**< file exists at this side of the delta */
Packit ae9e2a
} git_diff_flag_t;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * What type of change is described by a git_diff_delta?
Packit ae9e2a
 *
Packit ae9e2a
 * `GIT_DELTA_RENAMED` and `GIT_DELTA_COPIED` will only show up if you run
Packit ae9e2a
 * `git_diff_find_similar()` on the diff object.
Packit ae9e2a
 *
Packit ae9e2a
 * `GIT_DELTA_TYPECHANGE` only shows up given `GIT_DIFF_INCLUDE_TYPECHANGE`
Packit ae9e2a
 * in the option flags (otherwise type changes will be split into ADDED /
Packit ae9e2a
 * DELETED pairs).
Packit ae9e2a
 */
Packit ae9e2a
typedef enum {
Packit ae9e2a
	GIT_DELTA_UNMODIFIED = 0,  /**< no changes */
Packit ae9e2a
	GIT_DELTA_ADDED = 1,	   /**< entry does not exist in old version */
Packit ae9e2a
	GIT_DELTA_DELETED = 2,	   /**< entry does not exist in new version */
Packit ae9e2a
	GIT_DELTA_MODIFIED = 3,    /**< entry content changed between old and new */
Packit ae9e2a
	GIT_DELTA_RENAMED = 4,     /**< entry was renamed between old and new */
Packit ae9e2a
	GIT_DELTA_COPIED = 5,      /**< entry was copied from another old entry */
Packit ae9e2a
	GIT_DELTA_IGNORED = 6,     /**< entry is ignored item in workdir */
Packit ae9e2a
	GIT_DELTA_UNTRACKED = 7,   /**< entry is untracked item in workdir */
Packit ae9e2a
	GIT_DELTA_TYPECHANGE = 8,  /**< type of entry changed between old and new */
Packit ae9e2a
	GIT_DELTA_UNREADABLE = 9,  /**< entry is unreadable */
Packit ae9e2a
	GIT_DELTA_CONFLICTED = 10, /**< entry in the index is conflicted */
Packit ae9e2a
} git_delta_t;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Description of one side of a delta.
Packit ae9e2a
 *
Packit ae9e2a
 * Although this is called a "file", it could represent a file, a symbolic
Packit ae9e2a
 * link, a submodule commit id, or even a tree (although that only if you
Packit ae9e2a
 * are tracking type changes or ignored/untracked directories).
Packit ae9e2a
 *
Packit ae9e2a
 * The `id` is the `git_oid` of the item.  If the entry represents an
Packit ae9e2a
 * absent side of a diff (e.g. the `old_file` of a `GIT_DELTA_ADDED` delta),
Packit ae9e2a
 * then the oid will be zeroes.
Packit ae9e2a
 *
Packit ae9e2a
 * `path` is the NUL-terminated path to the entry relative to the working
Packit ae9e2a
 * directory of the repository.
Packit ae9e2a
 *
Packit ae9e2a
 * `size` is the size of the entry in bytes.
Packit ae9e2a
 *
Packit ae9e2a
 * `flags` is a combination of the `git_diff_flag_t` types
Packit ae9e2a
 *
Packit ae9e2a
 * `mode` is, roughly, the stat() `st_mode` value for the item.  This will
Packit ae9e2a
 * be restricted to one of the `git_filemode_t` values.
Packit ae9e2a
 *
Packit ae9e2a
 * The `id_abbrev` represents the known length of the `id` field, when
Packit ae9e2a
 * converted to a hex string.  It is generally `GIT_OID_HEXSZ`, unless this
Packit ae9e2a
 * delta was created from reading a patch file, in which case it may be
Packit ae9e2a
 * abbreviated to something reasonable, like 7 characters.
Packit ae9e2a
 */
Packit ae9e2a
typedef struct {
Packit ae9e2a
	git_oid     id;
Packit ae9e2a
	const char *path;
Packit ae9e2a
	git_off_t   size;
Packit ae9e2a
	uint32_t    flags;
Packit ae9e2a
	uint16_t    mode;
Packit ae9e2a
	uint16_t    id_abbrev;
Packit ae9e2a
} git_diff_file;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Description of changes to one entry.
Packit ae9e2a
 *
Packit ae9e2a
 * When iterating over a diff, this will be passed to most callbacks and
Packit ae9e2a
 * you can use the contents to understand exactly what has changed.
Packit ae9e2a
 *
Packit ae9e2a
 * The `old_file` represents the "from" side of the diff and the `new_file`
Packit ae9e2a
 * represents to "to" side of the diff.  What those means depend on the
Packit ae9e2a
 * function that was used to generate the diff and will be documented below.
Packit ae9e2a
 * You can also use the `GIT_DIFF_REVERSE` flag to flip it around.
Packit ae9e2a
 *
Packit ae9e2a
 * Although the two sides of the delta are named "old_file" and "new_file",
Packit ae9e2a
 * they actually may correspond to entries that represent a file, a symbolic
Packit ae9e2a
 * link, a submodule commit id, or even a tree (if you are tracking type
Packit ae9e2a
 * changes or ignored/untracked directories).
Packit ae9e2a
 *
Packit ae9e2a
 * Under some circumstances, in the name of efficiency, not all fields will
Packit ae9e2a
 * be filled in, but we generally try to fill in as much as possible.  One
Packit ae9e2a
 * example is that the "flags" field may not have either the `BINARY` or the
Packit ae9e2a
 * `NOT_BINARY` flag set to avoid examining file contents if you do not pass
Packit ae9e2a
 * in hunk and/or line callbacks to the diff foreach iteration function.  It
Packit ae9e2a
 * will just use the git attributes for those files.
Packit ae9e2a
 *
Packit ae9e2a
 * The similarity score is zero unless you call `git_diff_find_similar()`
Packit ae9e2a
 * which does a similarity analysis of files in the diff.  Use that
Packit ae9e2a
 * function to do rename and copy detection, and to split heavily modified
Packit ae9e2a
 * files in add/delete pairs.  After that call, deltas with a status of
Packit ae9e2a
 * GIT_DELTA_RENAMED or GIT_DELTA_COPIED will have a similarity score
Packit ae9e2a
 * between 0 and 100 indicating how similar the old and new sides are.
Packit ae9e2a
 *
Packit ae9e2a
 * If you ask `git_diff_find_similar` to find heavily modified files to
Packit ae9e2a
 * break, but to not *actually* break the records, then GIT_DELTA_MODIFIED
Packit ae9e2a
 * records may have a non-zero similarity score if the self-similarity is
Packit ae9e2a
 * below the split threshold.  To display this value like core Git, invert
Packit ae9e2a
 * the score (a la `printf("M%03d", 100 - delta->similarity)`).
Packit ae9e2a
 */
Packit ae9e2a
typedef struct {
Packit ae9e2a
	git_delta_t   status;
Packit ae9e2a
	uint32_t      flags;	   /**< git_diff_flag_t values */
Packit ae9e2a
	uint16_t      similarity;  /**< for RENAMED and COPIED, value 0-100 */
Packit ae9e2a
	uint16_t      nfiles;	   /**< number of files in this delta */
Packit ae9e2a
	git_diff_file old_file;
Packit ae9e2a
	git_diff_file new_file;
Packit ae9e2a
} git_diff_delta;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Diff notification callback function.
Packit ae9e2a
 *
Packit ae9e2a
 * The callback will be called for each file, just before the `git_delta_t`
Packit ae9e2a
 * gets inserted into the diff.
Packit ae9e2a
 *
Packit ae9e2a
 * When the callback:
Packit ae9e2a
 * - returns < 0, the diff process will be aborted.
Packit ae9e2a
 * - returns > 0, the delta will not be inserted into the diff, but the
Packit ae9e2a
 *		diff process continues.
Packit ae9e2a
 * - returns 0, the delta is inserted into the diff, and the diff process
Packit ae9e2a
 *		continues.
Packit ae9e2a
 */
Packit ae9e2a
typedef int (*git_diff_notify_cb)(
Packit ae9e2a
	const git_diff *diff_so_far,
Packit ae9e2a
	const git_diff_delta *delta_to_add,
Packit ae9e2a
	const char *matched_pathspec,
Packit ae9e2a
	void *payload);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Diff progress callback.
Packit ae9e2a
 *
Packit ae9e2a
 * Called before each file comparison.
Packit ae9e2a
 *
Packit ae9e2a
 * @param diff_so_far The diff being generated.
Packit ae9e2a
 * @param old_path The path to the old file or NULL.
Packit ae9e2a
 * @param new_path The path to the new file or NULL.
Packit ae9e2a
 * @return Non-zero to abort the diff.
Packit ae9e2a
 */
Packit ae9e2a
typedef int (*git_diff_progress_cb)(
Packit ae9e2a
	const git_diff *diff_so_far,
Packit ae9e2a
	const char *old_path,
Packit ae9e2a
	const char *new_path,
Packit ae9e2a
	void *payload);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Structure describing options about how the diff should be executed.
Packit ae9e2a
 *
Packit ae9e2a
 * Setting all values of the structure to zero will yield the default
Packit ae9e2a
 * values.  Similarly, passing NULL for the options structure will
Packit ae9e2a
 * give the defaults.  The default values are marked below.
Packit ae9e2a
 *
Packit ae9e2a
 * - `flags` is a combination of the `git_diff_option_t` values above
Packit ae9e2a
 * - `context_lines` is the number of unchanged lines that define the
Packit ae9e2a
 *    boundary of a hunk (and to display before and after)
Packit ae9e2a
 * - `interhunk_lines` is the maximum number of unchanged lines between
Packit ae9e2a
 *    hunk boundaries before the hunks will be merged into a one.
Packit ae9e2a
 * - `old_prefix` is the virtual "directory" to prefix to old file names
Packit ae9e2a
 *   in hunk headers (default "a")
Packit ae9e2a
 * - `new_prefix` is the virtual "directory" to prefix to new file names
Packit ae9e2a
 *   in hunk headers (default "b")
Packit ae9e2a
 * - `pathspec` is an array of paths / fnmatch patterns to constrain diff
Packit ae9e2a
 * - `max_size` is a file size (in bytes) above which a blob will be marked
Packit ae9e2a
 *   as binary automatically; pass a negative value to disable.
Packit ae9e2a
 * - `notify_cb` is an optional callback function, notifying the consumer of
Packit ae9e2a
 *   changes to the diff as new deltas are added.
Packit ae9e2a
 * - `progress_cb` is an optional callback function, notifying the consumer of
Packit ae9e2a
 *   which files are being examined as the diff is generated.
Packit ae9e2a
 * - `payload` is the payload to pass to the callback functions.
Packit ae9e2a
 * - `ignore_submodules` overrides the submodule ignore setting for all
Packit ae9e2a
 *   submodules in the diff.
Packit ae9e2a
 */
Packit ae9e2a
typedef struct {
Packit ae9e2a
	unsigned int version;      /**< version for the struct */
Packit ae9e2a
	uint32_t flags;            /**< defaults to GIT_DIFF_NORMAL */
Packit ae9e2a
Packit ae9e2a
	/* options controlling which files are in the diff */
Packit ae9e2a
Packit ae9e2a
	git_submodule_ignore_t ignore_submodules; /**< submodule ignore rule */
Packit ae9e2a
	git_strarray       pathspec;     /**< defaults to include all paths */
Packit ae9e2a
	git_diff_notify_cb   notify_cb;
Packit ae9e2a
	git_diff_progress_cb progress_cb;
Packit ae9e2a
	void                *payload;
Packit ae9e2a
Packit ae9e2a
	/* options controlling how to diff text is generated */
Packit ae9e2a
Packit ae9e2a
	uint32_t    context_lines;    /**< defaults to 3 */
Packit ae9e2a
	uint32_t    interhunk_lines;  /**< defaults to 0 */
Packit ae9e2a
	uint16_t    id_abbrev;       /**< default 'core.abbrev' or 7 if unset */
Packit ae9e2a
	git_off_t   max_size;         /**< defaults to 512MB */
Packit ae9e2a
	const char *old_prefix;       /**< defaults to "a" */
Packit ae9e2a
	const char *new_prefix;       /**< defaults to "b" */
Packit ae9e2a
} git_diff_options;
Packit ae9e2a
Packit ae9e2a
/* The current version of the diff options structure */
Packit ae9e2a
#define GIT_DIFF_OPTIONS_VERSION 1
Packit ae9e2a
Packit ae9e2a
/* Stack initializer for diff options.  Alternatively use
Packit ae9e2a
 * `git_diff_options_init` programmatic initialization.
Packit ae9e2a
 */
Packit ae9e2a
#define GIT_DIFF_OPTIONS_INIT \
Packit ae9e2a
	{GIT_DIFF_OPTIONS_VERSION, 0, GIT_SUBMODULE_IGNORE_UNSPECIFIED, {NULL,0}, NULL, NULL, NULL, 3}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Initializes a `git_diff_options` with default values. Equivalent to
Packit ae9e2a
 * creating an instance with GIT_DIFF_OPTIONS_INIT.
Packit ae9e2a
 *
Packit ae9e2a
 * @param opts The `git_diff_options` struct to initialize
Packit ae9e2a
 * @param version Version of struct; pass `GIT_DIFF_OPTIONS_VERSION`
Packit ae9e2a
 * @return Zero on success; -1 on failure.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_init_options(
Packit ae9e2a
	git_diff_options *opts,
Packit ae9e2a
	unsigned int version);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * When iterating over a diff, callback that will be made per file.
Packit ae9e2a
 *
Packit ae9e2a
 * @param delta A pointer to the delta data for the file
Packit ae9e2a
 * @param progress Goes from 0 to 1 over the diff
Packit ae9e2a
 * @param payload User-specified pointer from foreach function
Packit ae9e2a
 */
Packit ae9e2a
typedef int (*git_diff_file_cb)(
Packit ae9e2a
	const git_diff_delta *delta,
Packit ae9e2a
	float progress,
Packit ae9e2a
	void *payload);
Packit ae9e2a
Packit ae9e2a
#define GIT_DIFF_HUNK_HEADER_SIZE	128
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * When producing a binary diff, the binary data returned will be
Packit ae9e2a
 * either the deflated full ("literal") contents of the file, or
Packit ae9e2a
 * the deflated binary delta between the two sides (whichever is
Packit ae9e2a
 * smaller).
Packit ae9e2a
 */
Packit ae9e2a
typedef enum {
Packit ae9e2a
	/** There is no binary delta. */
Packit ae9e2a
	GIT_DIFF_BINARY_NONE,
Packit ae9e2a
Packit ae9e2a
	/** The binary data is the literal contents of the file. */
Packit ae9e2a
	GIT_DIFF_BINARY_LITERAL,
Packit ae9e2a
Packit ae9e2a
	/** The binary data is the delta from one side to the other. */
Packit ae9e2a
	GIT_DIFF_BINARY_DELTA,
Packit ae9e2a
} git_diff_binary_t;
Packit ae9e2a
Packit ae9e2a
/** The contents of one of the files in a binary diff. */
Packit ae9e2a
typedef struct {
Packit ae9e2a
	/** The type of binary data for this file. */
Packit ae9e2a
	git_diff_binary_t type;
Packit ae9e2a
Packit ae9e2a
	/** The binary data, deflated. */
Packit ae9e2a
	const char *data;
Packit ae9e2a
Packit ae9e2a
	/** The length of the binary data. */
Packit ae9e2a
	size_t datalen;
Packit ae9e2a
Packit ae9e2a
	/** The length of the binary data after inflation. */
Packit ae9e2a
	size_t inflatedlen;
Packit ae9e2a
} git_diff_binary_file;
Packit ae9e2a
Packit ae9e2a
/** Structure describing the binary contents of a diff. */
Packit ae9e2a
typedef struct {
Packit ae9e2a
	/**
Packit ae9e2a
	 * Whether there is data in this binary structure or not.  If this
Packit ae9e2a
	 * is `1`, then this was produced and included binary content.  If
Packit ae9e2a
	 * this is `0` then this was generated knowing only that a binary
Packit ae9e2a
	 * file changed but without providing the data, probably from a patch
Packit ae9e2a
	 * that said `Binary files a/file.txt and b/file.txt differ`.
Packit ae9e2a
	 */
Packit ae9e2a
	unsigned int contains_data;
Packit ae9e2a
	git_diff_binary_file old_file; /**< The contents of the old file. */
Packit ae9e2a
	git_diff_binary_file new_file; /**< The contents of the new file. */
Packit ae9e2a
} git_diff_binary;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
* When iterating over a diff, callback that will be made for
Packit ae9e2a
* binary content within the diff.
Packit ae9e2a
*/
Packit ae9e2a
typedef int(*git_diff_binary_cb)(
Packit ae9e2a
	const git_diff_delta *delta,
Packit ae9e2a
	const git_diff_binary *binary,
Packit ae9e2a
	void *payload);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Structure describing a hunk of a diff.
Packit ae9e2a
 */
Packit ae9e2a
typedef struct {
Packit ae9e2a
	int    old_start;     /** Starting line number in old_file */
Packit ae9e2a
	int    old_lines;     /** Number of lines in old_file */
Packit ae9e2a
	int    new_start;     /** Starting line number in new_file */
Packit ae9e2a
	int    new_lines;     /** Number of lines in new_file */
Packit ae9e2a
	size_t header_len;    /** Number of bytes in header text */
Packit ae9e2a
	char   header[GIT_DIFF_HUNK_HEADER_SIZE];   /** Header text, NUL-byte terminated */
Packit ae9e2a
} git_diff_hunk;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * When iterating over a diff, callback that will be made per hunk.
Packit ae9e2a
 */
Packit ae9e2a
typedef int (*git_diff_hunk_cb)(
Packit ae9e2a
	const git_diff_delta *delta,
Packit ae9e2a
	const git_diff_hunk *hunk,
Packit ae9e2a
	void *payload);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Line origin constants.
Packit ae9e2a
 *
Packit ae9e2a
 * These values describe where a line came from and will be passed to
Packit ae9e2a
 * the git_diff_line_cb when iterating over a diff.  There are some
Packit ae9e2a
 * special origin constants at the end that are used for the text
Packit ae9e2a
 * output callbacks to demarcate lines that are actually part of
Packit ae9e2a
 * the file or hunk headers.
Packit ae9e2a
 */
Packit ae9e2a
typedef enum {
Packit ae9e2a
	/* These values will be sent to `git_diff_line_cb` along with the line */
Packit ae9e2a
	GIT_DIFF_LINE_CONTEXT   = ' ',
Packit ae9e2a
	GIT_DIFF_LINE_ADDITION  = '+',
Packit ae9e2a
	GIT_DIFF_LINE_DELETION  = '-',
Packit ae9e2a
Packit ae9e2a
	GIT_DIFF_LINE_CONTEXT_EOFNL = '=', /**< Both files have no LF at end */
Packit ae9e2a
	GIT_DIFF_LINE_ADD_EOFNL = '>',     /**< Old has no LF at end, new does */
Packit ae9e2a
	GIT_DIFF_LINE_DEL_EOFNL = '<',     /**< Old has LF at end, new does not */
Packit ae9e2a
Packit ae9e2a
	/* The following values will only be sent to a `git_diff_line_cb` when
Packit ae9e2a
	 * the content of a diff is being formatted through `git_diff_print`.
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_DIFF_LINE_FILE_HDR  = 'F',
Packit ae9e2a
	GIT_DIFF_LINE_HUNK_HDR  = 'H',
Packit ae9e2a
	GIT_DIFF_LINE_BINARY    = 'B' /**< For "Binary files x and y differ" */
Packit ae9e2a
} git_diff_line_t;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Structure describing a line (or data span) of a diff.
Packit ae9e2a
 */
Packit ae9e2a
typedef struct {
Packit ae9e2a
	char   origin;       /**< A git_diff_line_t value */
Packit ae9e2a
	int    old_lineno;   /**< Line number in old file or -1 for added line */
Packit ae9e2a
	int    new_lineno;   /**< Line number in new file or -1 for deleted line */
Packit ae9e2a
	int    num_lines;    /**< Number of newline characters in content */
Packit ae9e2a
	size_t content_len;  /**< Number of bytes of data */
Packit ae9e2a
	git_off_t content_offset; /**< Offset in the original file to the content */
Packit ae9e2a
	const char *content; /**< Pointer to diff text, not NUL-byte terminated */
Packit ae9e2a
} git_diff_line;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * When iterating over a diff, callback that will be made per text diff
Packit ae9e2a
 * line. In this context, the provided range will be NULL.
Packit ae9e2a
 *
Packit ae9e2a
 * When printing a diff, callback that will be made to output each line
Packit ae9e2a
 * of text.  This uses some extra GIT_DIFF_LINE_... constants for output
Packit ae9e2a
 * of lines of file and hunk headers.
Packit ae9e2a
 */
Packit ae9e2a
typedef int (*git_diff_line_cb)(
Packit ae9e2a
	const git_diff_delta *delta, /**< delta that contains this data */
Packit ae9e2a
	const git_diff_hunk *hunk,   /**< hunk containing this data */
Packit ae9e2a
	const git_diff_line *line,   /**< line data */
Packit ae9e2a
	void *payload);              /**< user reference data */
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Flags to control the behavior of diff rename/copy detection.
Packit ae9e2a
 */
Packit ae9e2a
typedef enum {
Packit ae9e2a
	/** Obey `diff.renames`. Overridden by any other GIT_DIFF_FIND_... flag. */
Packit ae9e2a
	GIT_DIFF_FIND_BY_CONFIG = 0,
Packit ae9e2a
Packit ae9e2a
	/** Look for renames? (`--find-renames`) */
Packit ae9e2a
	GIT_DIFF_FIND_RENAMES = (1u << 0),
Packit ae9e2a
Packit ae9e2a
	/** Consider old side of MODIFIED for renames? (`--break-rewrites=N`) */
Packit ae9e2a
	GIT_DIFF_FIND_RENAMES_FROM_REWRITES = (1u << 1),
Packit ae9e2a
Packit ae9e2a
	/** Look for copies? (a la `--find-copies`). */
Packit ae9e2a
	GIT_DIFF_FIND_COPIES = (1u << 2),
Packit ae9e2a
Packit ae9e2a
	/** Consider UNMODIFIED as copy sources? (`--find-copies-harder`).
Packit ae9e2a
	 *
Packit ae9e2a
	 * For this to work correctly, use GIT_DIFF_INCLUDE_UNMODIFIED when
Packit ae9e2a
	 * the initial `git_diff` is being generated.
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED = (1u << 3),
Packit ae9e2a
Packit ae9e2a
	/** Mark significant rewrites for split (`--break-rewrites=/M`) */
Packit ae9e2a
	GIT_DIFF_FIND_REWRITES = (1u << 4),
Packit ae9e2a
	/** Actually split large rewrites into delete/add pairs */
Packit ae9e2a
	GIT_DIFF_BREAK_REWRITES = (1u << 5),
Packit ae9e2a
	/** Mark rewrites for split and break into delete/add pairs */
Packit ae9e2a
	GIT_DIFF_FIND_AND_BREAK_REWRITES =
Packit ae9e2a
		(GIT_DIFF_FIND_REWRITES | GIT_DIFF_BREAK_REWRITES),
Packit ae9e2a
Packit ae9e2a
	/** Find renames/copies for UNTRACKED items in working directory.
Packit ae9e2a
	 *
Packit ae9e2a
	 * For this to work correctly, use GIT_DIFF_INCLUDE_UNTRACKED when the
Packit ae9e2a
	 * initial `git_diff` is being generated (and obviously the diff must
Packit ae9e2a
	 * be against the working directory for this to make sense).
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_DIFF_FIND_FOR_UNTRACKED = (1u << 6),
Packit ae9e2a
Packit ae9e2a
	/** Turn on all finding features. */
Packit ae9e2a
	GIT_DIFF_FIND_ALL = (0x0ff),
Packit ae9e2a
Packit ae9e2a
	/** Measure similarity ignoring leading whitespace (default) */
Packit ae9e2a
	GIT_DIFF_FIND_IGNORE_LEADING_WHITESPACE = 0,
Packit ae9e2a
	/** Measure similarity ignoring all whitespace */
Packit ae9e2a
	GIT_DIFF_FIND_IGNORE_WHITESPACE = (1u << 12),
Packit ae9e2a
	/** Measure similarity including all data */
Packit ae9e2a
	GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE = (1u << 13),
Packit ae9e2a
	/** Measure similarity only by comparing SHAs (fast and cheap) */
Packit ae9e2a
	GIT_DIFF_FIND_EXACT_MATCH_ONLY = (1u << 14),
Packit ae9e2a
Packit ae9e2a
	/** Do not break rewrites unless they contribute to a rename.
Packit ae9e2a
	 *
Packit ae9e2a
	 * Normally, GIT_DIFF_FIND_AND_BREAK_REWRITES will measure the self-
Packit ae9e2a
	 * similarity of modified files and split the ones that have changed a
Packit ae9e2a
	 * lot into a DELETE / ADD pair.  Then the sides of that pair will be
Packit ae9e2a
	 * considered candidates for rename and copy detection.
Packit ae9e2a
	 *
Packit ae9e2a
	 * If you add this flag in and the split pair is *not* used for an
Packit ae9e2a
	 * actual rename or copy, then the modified record will be restored to
Packit ae9e2a
	 * a regular MODIFIED record instead of being split.
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_DIFF_BREAK_REWRITES_FOR_RENAMES_ONLY  = (1u << 15),
Packit ae9e2a
Packit ae9e2a
	/** Remove any UNMODIFIED deltas after find_similar is done.
Packit ae9e2a
	 *
Packit ae9e2a
	 * Using GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED to emulate the
Packit ae9e2a
	 * --find-copies-harder behavior requires building a diff with the
Packit ae9e2a
	 * GIT_DIFF_INCLUDE_UNMODIFIED flag.  If you do not want UNMODIFIED
Packit ae9e2a
	 * records in the final result, pass this flag to have them removed.
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_DIFF_FIND_REMOVE_UNMODIFIED = (1u << 16),
Packit ae9e2a
} git_diff_find_t;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Pluggable similarity metric
Packit ae9e2a
 */
Packit ae9e2a
typedef struct {
Packit ae9e2a
	int (*file_signature)(
Packit ae9e2a
		void **out, const git_diff_file *file,
Packit ae9e2a
		const char *fullpath, void *payload);
Packit ae9e2a
	int (*buffer_signature)(
Packit ae9e2a
		void **out, const git_diff_file *file,
Packit ae9e2a
		const char *buf, size_t buflen, void *payload);
Packit ae9e2a
	void (*free_signature)(void *sig, void *payload);
Packit ae9e2a
	int (*similarity)(int *score, void *siga, void *sigb, void *payload);
Packit ae9e2a
	void *payload;
Packit ae9e2a
} git_diff_similarity_metric;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Control behavior of rename and copy detection
Packit ae9e2a
 *
Packit ae9e2a
 * These options mostly mimic parameters that can be passed to git-diff.
Packit ae9e2a
 *
Packit ae9e2a
 * - `rename_threshold` is the same as the -M option with a value
Packit ae9e2a
 * - `copy_threshold` is the same as the -C option with a value
Packit ae9e2a
 * - `rename_from_rewrite_threshold` matches the top of the -B option
Packit ae9e2a
 * - `break_rewrite_threshold` matches the bottom of the -B option
Packit ae9e2a
 * - `rename_limit` is the maximum number of matches to consider for
Packit ae9e2a
 *   a particular file.  This is a little different from the `-l` option
Packit ae9e2a
 *   to regular Git because we will still process up to this many matches
Packit ae9e2a
 *   before abandoning the search.
Packit ae9e2a
 *
Packit ae9e2a
 * The `metric` option allows you to plug in a custom similarity metric.
Packit ae9e2a
 * Set it to NULL for the default internal metric which is based on sampling
Packit ae9e2a
 * hashes of ranges of data in the file.  The default metric is a pretty
Packit ae9e2a
 * good similarity approximation that should work fairly well for both text
Packit ae9e2a
 * and binary data, and is pretty fast with fixed memory overhead.
Packit ae9e2a
 */
Packit ae9e2a
typedef struct {
Packit ae9e2a
	unsigned int version;
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Combination of git_diff_find_t values (default GIT_DIFF_FIND_BY_CONFIG).
Packit ae9e2a
	 * NOTE: if you don't explicitly set this, `diff.renames` could be set
Packit ae9e2a
	 * to false, resulting in `git_diff_find_similar` doing nothing.
Packit ae9e2a
	 */
Packit ae9e2a
	uint32_t flags;
Packit ae9e2a
Packit ae9e2a
	/** Similarity to consider a file renamed (default 50) */
Packit ae9e2a
	uint16_t rename_threshold;
Packit ae9e2a
	/** Similarity of modified to be eligible rename source (default 50) */
Packit ae9e2a
	uint16_t rename_from_rewrite_threshold;
Packit ae9e2a
	/** Similarity to consider a file a copy (default 50) */
Packit ae9e2a
	uint16_t copy_threshold;
Packit ae9e2a
	/** Similarity to split modify into delete/add pair (default 60) */
Packit ae9e2a
	uint16_t break_rewrite_threshold;
Packit ae9e2a
Packit ae9e2a
	/** Maximum similarity sources to examine for a file (somewhat like
Packit ae9e2a
	 *  git-diff's `-l` option or `diff.renameLimit` config) (default 200)
Packit ae9e2a
	 */
Packit ae9e2a
	size_t rename_limit;
Packit ae9e2a
Packit ae9e2a
	/** Pluggable similarity metric; pass NULL to use internal metric */
Packit ae9e2a
	git_diff_similarity_metric *metric;
Packit ae9e2a
} git_diff_find_options;
Packit ae9e2a
Packit ae9e2a
#define GIT_DIFF_FIND_OPTIONS_VERSION 1
Packit ae9e2a
#define GIT_DIFF_FIND_OPTIONS_INIT {GIT_DIFF_FIND_OPTIONS_VERSION}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Initializes a `git_diff_find_options` with default values. Equivalent to
Packit ae9e2a
 * creating an instance with GIT_DIFF_FIND_OPTIONS_INIT.
Packit ae9e2a
 *
Packit ae9e2a
 * @param opts The `git_diff_find_options` struct to initialize
Packit ae9e2a
 * @param version Version of struct; pass `GIT_DIFF_FIND_OPTIONS_VERSION`
Packit ae9e2a
 * @return Zero on success; -1 on failure.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_find_init_options(
Packit ae9e2a
	git_diff_find_options *opts,
Packit ae9e2a
	unsigned int version);
Packit ae9e2a
Packit ae9e2a
/** @name Diff Generator Functions
Packit ae9e2a
 *
Packit ae9e2a
 * These are the functions you would use to create (or destroy) a
Packit ae9e2a
 * git_diff from various objects in a repository.
Packit ae9e2a
 */
Packit ae9e2a
/**@{*/
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Deallocate a diff.
Packit ae9e2a
 *
Packit ae9e2a
 * @param diff The previously created diff; cannot be used after free.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(void) git_diff_free(git_diff *diff);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Create a diff with the difference between two tree objects.
Packit ae9e2a
 *
Packit ae9e2a
 * This is equivalent to `git diff <old-tree> <new-tree>`
Packit ae9e2a
 *
Packit ae9e2a
 * The first tree will be used for the "old_file" side of the delta and the
Packit ae9e2a
 * second tree will be used for the "new_file" side of the delta.  You can
Packit ae9e2a
 * pass NULL to indicate an empty tree, although it is an error to pass
Packit ae9e2a
 * NULL for both the `old_tree` and `new_tree`.
Packit ae9e2a
 *
Packit ae9e2a
 * @param diff Output pointer to a git_diff pointer to be allocated.
Packit ae9e2a
 * @param repo The repository containing the trees.
Packit ae9e2a
 * @param old_tree A git_tree object to diff from, or NULL for empty tree.
Packit ae9e2a
 * @param new_tree A git_tree object to diff to, or NULL for empty tree.
Packit ae9e2a
 * @param opts Structure with options to influence diff or NULL for defaults.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_tree_to_tree(
Packit ae9e2a
	git_diff **diff,
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	git_tree *old_tree,
Packit ae9e2a
	git_tree *new_tree,
Packit ae9e2a
	const git_diff_options *opts); /**< can be NULL for defaults */
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Create a diff between a tree and repository index.
Packit ae9e2a
 *
Packit ae9e2a
 * This is equivalent to `git diff --cached <treeish>` or if you pass
Packit ae9e2a
 * the HEAD tree, then like `git diff --cached`.
Packit ae9e2a
 *
Packit ae9e2a
 * The tree you pass will be used for the "old_file" side of the delta, and
Packit ae9e2a
 * the index will be used for the "new_file" side of the delta.
Packit ae9e2a
 *
Packit ae9e2a
 * If you pass NULL for the index, then the existing index of the `repo`
Packit ae9e2a
 * will be used.  In this case, the index will be refreshed from disk
Packit ae9e2a
 * (if it has changed) before the diff is generated.
Packit ae9e2a
 *
Packit ae9e2a
 * @param diff Output pointer to a git_diff pointer to be allocated.
Packit ae9e2a
 * @param repo The repository containing the tree and index.
Packit ae9e2a
 * @param old_tree A git_tree object to diff from, or NULL for empty tree.
Packit ae9e2a
 * @param index The index to diff with; repo index used if NULL.
Packit ae9e2a
 * @param opts Structure with options to influence diff or NULL for defaults.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_tree_to_index(
Packit ae9e2a
	git_diff **diff,
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	git_tree *old_tree,
Packit ae9e2a
	git_index *index,
Packit ae9e2a
	const git_diff_options *opts); /**< can be NULL for defaults */
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Create a diff between the repository index and the workdir directory.
Packit ae9e2a
 *
Packit ae9e2a
 * This matches the `git diff` command.  See the note below on
Packit ae9e2a
 * `git_diff_tree_to_workdir` for a discussion of the difference between
Packit ae9e2a
 * `git diff` and `git diff HEAD` and how to emulate a `git diff <treeish>`
Packit ae9e2a
 * using libgit2.
Packit ae9e2a
 *
Packit ae9e2a
 * The index will be used for the "old_file" side of the delta, and the
Packit ae9e2a
 * working directory will be used for the "new_file" side of the delta.
Packit ae9e2a
 *
Packit ae9e2a
 * If you pass NULL for the index, then the existing index of the `repo`
Packit ae9e2a
 * will be used.  In this case, the index will be refreshed from disk
Packit ae9e2a
 * (if it has changed) before the diff is generated.
Packit ae9e2a
 *
Packit ae9e2a
 * @param diff Output pointer to a git_diff pointer to be allocated.
Packit ae9e2a
 * @param repo The repository.
Packit ae9e2a
 * @param index The index to diff from; repo index used if NULL.
Packit ae9e2a
 * @param opts Structure with options to influence diff or NULL for defaults.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_index_to_workdir(
Packit ae9e2a
	git_diff **diff,
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	git_index *index,
Packit ae9e2a
	const git_diff_options *opts); /**< can be NULL for defaults */
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Create a diff between a tree and the working directory.
Packit ae9e2a
 *
Packit ae9e2a
 * The tree you provide will be used for the "old_file" side of the delta,
Packit ae9e2a
 * and the working directory will be used for the "new_file" side.
Packit ae9e2a
 *
Packit ae9e2a
 * This is not the same as `git diff <treeish>` or `git diff-index
Packit ae9e2a
 * <treeish>`.  Those commands use information from the index, whereas this
Packit ae9e2a
 * function strictly returns the differences between the tree and the files
Packit ae9e2a
 * in the working directory, regardless of the state of the index.  Use
Packit ae9e2a
 * `git_diff_tree_to_workdir_with_index` to emulate those commands.
Packit ae9e2a
 *
Packit ae9e2a
 * To see difference between this and `git_diff_tree_to_workdir_with_index`,
Packit ae9e2a
 * consider the example of a staged file deletion where the file has then
Packit ae9e2a
 * been put back into the working dir and further modified.  The
Packit ae9e2a
 * tree-to-workdir diff for that file is 'modified', but `git diff` would
Packit ae9e2a
 * show status 'deleted' since there is a staged delete.
Packit ae9e2a
 *
Packit ae9e2a
 * @param diff A pointer to a git_diff pointer that will be allocated.
Packit ae9e2a
 * @param repo The repository containing the tree.
Packit ae9e2a
 * @param old_tree A git_tree object to diff from, or NULL for empty tree.
Packit ae9e2a
 * @param opts Structure with options to influence diff or NULL for defaults.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_tree_to_workdir(
Packit ae9e2a
	git_diff **diff,
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	git_tree *old_tree,
Packit ae9e2a
	const git_diff_options *opts); /**< can be NULL for defaults */
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Create a diff between a tree and the working directory using index data
Packit ae9e2a
 * to account for staged deletes, tracked files, etc.
Packit ae9e2a
 *
Packit ae9e2a
 * This emulates `git diff <tree>` by diffing the tree to the index and
Packit ae9e2a
 * the index to the working directory and blending the results into a
Packit ae9e2a
 * single diff that includes staged deleted, etc.
Packit ae9e2a
 *
Packit ae9e2a
 * @param diff A pointer to a git_diff pointer that will be allocated.
Packit ae9e2a
 * @param repo The repository containing the tree.
Packit ae9e2a
 * @param old_tree A git_tree object to diff from, or NULL for empty tree.
Packit ae9e2a
 * @param opts Structure with options to influence diff or NULL for defaults.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_tree_to_workdir_with_index(
Packit ae9e2a
	git_diff **diff,
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	git_tree *old_tree,
Packit ae9e2a
	const git_diff_options *opts); /**< can be NULL for defaults */
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Create a diff with the difference between two index objects.
Packit ae9e2a
 *
Packit ae9e2a
 * The first index will be used for the "old_file" side of the delta and the
Packit ae9e2a
 * second index will be used for the "new_file" side of the delta.
Packit ae9e2a
 *
Packit ae9e2a
 * @param diff Output pointer to a git_diff pointer to be allocated.
Packit ae9e2a
 * @param repo The repository containing the indexes.
Packit ae9e2a
 * @param old_index A git_index object to diff from.
Packit ae9e2a
 * @param new_index A git_index object to diff to.
Packit ae9e2a
 * @param opts Structure with options to influence diff or NULL for defaults.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_index_to_index(
Packit ae9e2a
	git_diff **diff,
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	git_index *old_index,
Packit ae9e2a
	git_index *new_index,
Packit ae9e2a
	const git_diff_options *opts); /**< can be NULL for defaults */
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Merge one diff into another.
Packit ae9e2a
 *
Packit ae9e2a
 * This merges items from the "from" list into the "onto" list.  The
Packit ae9e2a
 * resulting diff will have all items that appear in either list.
Packit ae9e2a
 * If an item appears in both lists, then it will be "merged" to appear
Packit ae9e2a
 * as if the old version was from the "onto" list and the new version
Packit ae9e2a
 * is from the "from" list (with the exception that if the item has a
Packit ae9e2a
 * pending DELETE in the middle, then it will show as deleted).
Packit ae9e2a
 *
Packit ae9e2a
 * @param onto Diff to merge into.
Packit ae9e2a
 * @param from Diff to merge.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_merge(
Packit ae9e2a
	git_diff *onto,
Packit ae9e2a
	const git_diff *from);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Transform a diff marking file renames, copies, etc.
Packit ae9e2a
 *
Packit ae9e2a
 * This modifies a diff in place, replacing old entries that look
Packit ae9e2a
 * like renames or copies with new entries reflecting those changes.
Packit ae9e2a
 * This also will, if requested, break modified files into add/remove
Packit ae9e2a
 * pairs if the amount of change is above a threshold.
Packit ae9e2a
 *
Packit ae9e2a
 * @param diff diff to run detection algorithms on
Packit ae9e2a
 * @param options Control how detection should be run, NULL for defaults
Packit ae9e2a
 * @return 0 on success, -1 on failure
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_find_similar(
Packit ae9e2a
	git_diff *diff,
Packit ae9e2a
	const git_diff_find_options *options);
Packit ae9e2a
Packit ae9e2a
/**@}*/
Packit ae9e2a
Packit ae9e2a
Packit ae9e2a
/** @name Diff Processor Functions
Packit ae9e2a
 *
Packit ae9e2a
 * These are the functions you apply to a diff to process it
Packit ae9e2a
 * or read it in some way.
Packit ae9e2a
 */
Packit ae9e2a
/**@{*/
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Query how many diff records are there in a diff.
Packit ae9e2a
 *
Packit ae9e2a
 * @param diff A git_diff generated by one of the above functions
Packit ae9e2a
 * @return Count of number of deltas in the list
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(size_t) git_diff_num_deltas(const git_diff *diff);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Query how many diff deltas are there in a diff filtered by type.
Packit ae9e2a
 *
Packit ae9e2a
 * This works just like `git_diff_entrycount()` with an extra parameter
Packit ae9e2a
 * that is a `git_delta_t` and returns just the count of how many deltas
Packit ae9e2a
 * match that particular type.
Packit ae9e2a
 *
Packit ae9e2a
 * @param diff A git_diff generated by one of the above functions
Packit ae9e2a
 * @param type A git_delta_t value to filter the count
Packit ae9e2a
 * @return Count of number of deltas matching delta_t type
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(size_t) git_diff_num_deltas_of_type(
Packit ae9e2a
	const git_diff *diff, git_delta_t type);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Return the diff delta for an entry in the diff list.
Packit ae9e2a
 *
Packit ae9e2a
 * The `git_diff_delta` pointer points to internal data and you do not
Packit ae9e2a
 * have to release it when you are done with it.  It will go away when
Packit ae9e2a
 * the * `git_diff` (or any associated `git_patch`) goes away.
Packit ae9e2a
 *
Packit ae9e2a
 * Note that the flags on the delta related to whether it has binary
Packit ae9e2a
 * content or not may not be set if there are no attributes set for the
Packit ae9e2a
 * file and there has been no reason to load the file data at this point.
Packit ae9e2a
 * For now, if you need those flags to be up to date, your only option is
Packit ae9e2a
 * to either use `git_diff_foreach` or create a `git_patch`.
Packit ae9e2a
 *
Packit ae9e2a
 * @param diff Diff list object
Packit ae9e2a
 * @param idx Index into diff list
Packit ae9e2a
 * @return Pointer to git_diff_delta (or NULL if `idx` out of range)
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(const git_diff_delta *) git_diff_get_delta(
Packit ae9e2a
	const git_diff *diff, size_t idx);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Check if deltas are sorted case sensitively or insensitively.
Packit ae9e2a
 *
Packit ae9e2a
 * @param diff diff to check
Packit ae9e2a
 * @return 0 if case sensitive, 1 if case is ignored
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_is_sorted_icase(const git_diff *diff);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Loop over all deltas in a diff issuing callbacks.
Packit ae9e2a
 *
Packit ae9e2a
 * This will iterate through all of the files described in a diff.  You
Packit ae9e2a
 * should provide a file callback to learn about each file.
Packit ae9e2a
 *
Packit ae9e2a
 * The "hunk" and "line" callbacks are optional, and the text diff of the
Packit ae9e2a
 * files will only be calculated if they are not NULL.  Of course, these
Packit ae9e2a
 * callbacks will not be invoked for binary files on the diff or for
Packit ae9e2a
 * files whose only changed is a file mode change.
Packit ae9e2a
 *
Packit ae9e2a
 * Returning a non-zero value from any of the callbacks will terminate
Packit ae9e2a
 * the iteration and return the value to the user.
Packit ae9e2a
 *
Packit ae9e2a
 * @param diff A git_diff generated by one of the above functions.
Packit ae9e2a
 * @param file_cb Callback function to make per file in the diff.
Packit ae9e2a
 * @param binary_cb Optional callback to make for binary files.
Packit ae9e2a
 * @param hunk_cb Optional callback to make per hunk of text diff.  This
Packit ae9e2a
 *                callback is called to describe a range of lines in the
Packit ae9e2a
 *                diff.  It will not be issued for binary files.
Packit ae9e2a
 * @param line_cb Optional callback to make per line of diff text.  This
Packit ae9e2a
 *                same callback will be made for context lines, added, and
Packit ae9e2a
 *                removed lines, and even for a deleted trailing newline.
Packit ae9e2a
 * @param payload Reference pointer that will be passed to your callbacks.
Packit ae9e2a
 * @return 0 on success, non-zero callback return value, or error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_foreach(
Packit ae9e2a
	git_diff *diff,
Packit ae9e2a
	git_diff_file_cb file_cb,
Packit ae9e2a
	git_diff_binary_cb binary_cb,
Packit ae9e2a
	git_diff_hunk_cb hunk_cb,
Packit ae9e2a
	git_diff_line_cb line_cb,
Packit ae9e2a
	void *payload);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Look up the single character abbreviation for a delta status code.
Packit ae9e2a
 *
Packit ae9e2a
 * When you run `git diff --name-status` it uses single letter codes in
Packit ae9e2a
 * the output such as 'A' for added, 'D' for deleted, 'M' for modified,
Packit ae9e2a
 * etc.  This function converts a git_delta_t value into these letters for
Packit ae9e2a
 * your own purposes.  GIT_DELTA_UNTRACKED will return a space (i.e. ' ').
Packit ae9e2a
 *
Packit ae9e2a
 * @param status The git_delta_t value to look up
Packit ae9e2a
 * @return The single character label for that code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(char) git_diff_status_char(git_delta_t status);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Possible output formats for diff data
Packit ae9e2a
 */
Packit ae9e2a
typedef enum {
Packit ae9e2a
	GIT_DIFF_FORMAT_PATCH        = 1u, /**< full git diff */
Packit ae9e2a
	GIT_DIFF_FORMAT_PATCH_HEADER = 2u, /**< just the file headers of patch */
Packit ae9e2a
	GIT_DIFF_FORMAT_RAW          = 3u, /**< like git diff --raw */
Packit ae9e2a
	GIT_DIFF_FORMAT_NAME_ONLY    = 4u, /**< like git diff --name-only */
Packit ae9e2a
	GIT_DIFF_FORMAT_NAME_STATUS  = 5u, /**< like git diff --name-status */
Packit ae9e2a
} git_diff_format_t;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Iterate over a diff generating formatted text output.
Packit ae9e2a
 *
Packit ae9e2a
 * Returning a non-zero value from the callbacks will terminate the
Packit ae9e2a
 * iteration and return the non-zero value to the caller.
Packit ae9e2a
 *
Packit ae9e2a
 * @param diff A git_diff generated by one of the above functions.
Packit ae9e2a
 * @param format A git_diff_format_t value to pick the text format.
Packit ae9e2a
 * @param print_cb Callback to make per line of diff text.
Packit ae9e2a
 * @param payload Reference pointer that will be passed to your callback.
Packit ae9e2a
 * @return 0 on success, non-zero callback return value, or error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_print(
Packit ae9e2a
	git_diff *diff,
Packit ae9e2a
	git_diff_format_t format,
Packit ae9e2a
	git_diff_line_cb print_cb,
Packit ae9e2a
	void *payload);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Produce the complete formatted text output from a diff into a
Packit ae9e2a
 * buffer.
Packit ae9e2a
 *
Packit ae9e2a
 * @param out A pointer to a user-allocated git_buf that will
Packit ae9e2a
 *            contain the diff text
Packit ae9e2a
 * @param diff A git_diff generated by one of the above functions.
Packit ae9e2a
 * @param format A git_diff_format_t value to pick the text format.
Packit ae9e2a
 * @return 0 on success or error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_to_buf(
Packit ae9e2a
	git_buf *out,
Packit ae9e2a
	git_diff *diff,
Packit ae9e2a
	git_diff_format_t format);
Packit ae9e2a
Packit ae9e2a
/**@}*/
Packit ae9e2a
Packit ae9e2a
Packit ae9e2a
/*
Packit ae9e2a
 * Misc
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Directly run a diff on two blobs.
Packit ae9e2a
 *
Packit ae9e2a
 * Compared to a file, a blob lacks some contextual information. As such,
Packit ae9e2a
 * the `git_diff_file` given to the callback will have some fake data; i.e.
Packit ae9e2a
 * `mode` will be 0 and `path` will be NULL.
Packit ae9e2a
 *
Packit ae9e2a
 * NULL is allowed for either `old_blob` or `new_blob` and will be treated
Packit ae9e2a
 * as an empty blob, with the `oid` set to NULL in the `git_diff_file` data.
Packit ae9e2a
 * Passing NULL for both blobs is a noop; no callbacks will be made at all.
Packit ae9e2a
 *
Packit ae9e2a
 * We do run a binary content check on the blob content and if either blob
Packit ae9e2a
 * looks like binary data, the `git_diff_delta` binary attribute will be set
Packit ae9e2a
 * to 1 and no call to the hunk_cb nor line_cb will be made (unless you pass
Packit ae9e2a
 * `GIT_DIFF_FORCE_TEXT` of course).
Packit ae9e2a
 *
Packit ae9e2a
 * @param old_blob Blob for old side of diff, or NULL for empty blob
Packit ae9e2a
 * @param old_as_path Treat old blob as if it had this filename; can be NULL
Packit ae9e2a
 * @param new_blob Blob for new side of diff, or NULL for empty blob
Packit ae9e2a
 * @param new_as_path Treat new blob as if it had this filename; can be NULL
Packit ae9e2a
 * @param options Options for diff, or NULL for default options
Packit ae9e2a
 * @param file_cb Callback for "file"; made once if there is a diff; can be NULL
Packit ae9e2a
 * @param binary_cb Callback for binary files; can be NULL
Packit ae9e2a
 * @param hunk_cb Callback for each hunk in diff; can be NULL
Packit ae9e2a
 * @param line_cb Callback for each line in diff; can be NULL
Packit ae9e2a
 * @param payload Payload passed to each callback function
Packit ae9e2a
 * @return 0 on success, non-zero callback return value, or error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_blobs(
Packit ae9e2a
	const git_blob *old_blob,
Packit ae9e2a
	const char *old_as_path,
Packit ae9e2a
	const git_blob *new_blob,
Packit ae9e2a
	const char *new_as_path,
Packit ae9e2a
	const git_diff_options *options,
Packit ae9e2a
	git_diff_file_cb file_cb,
Packit ae9e2a
	git_diff_binary_cb binary_cb,
Packit ae9e2a
	git_diff_hunk_cb hunk_cb,
Packit ae9e2a
	git_diff_line_cb line_cb,
Packit ae9e2a
	void *payload);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Directly run a diff between a blob and a buffer.
Packit ae9e2a
 *
Packit ae9e2a
 * As with `git_diff_blobs`, comparing a blob and buffer lacks some context,
Packit ae9e2a
 * so the `git_diff_file` parameters to the callbacks will be faked a la the
Packit ae9e2a
 * rules for `git_diff_blobs()`.
Packit ae9e2a
 *
Packit ae9e2a
 * Passing NULL for `old_blob` will be treated as an empty blob (i.e. the
Packit ae9e2a
 * `file_cb` will be invoked with GIT_DELTA_ADDED and the diff will be the
Packit ae9e2a
 * entire content of the buffer added).  Passing NULL to the buffer will do
Packit ae9e2a
 * the reverse, with GIT_DELTA_REMOVED and blob content removed.
Packit ae9e2a
 *
Packit ae9e2a
 * @param old_blob Blob for old side of diff, or NULL for empty blob
Packit ae9e2a
 * @param old_as_path Treat old blob as if it had this filename; can be NULL
Packit ae9e2a
 * @param buffer Raw data for new side of diff, or NULL for empty
Packit ae9e2a
 * @param buffer_len Length of raw data for new side of diff
Packit ae9e2a
 * @param buffer_as_path Treat buffer as if it had this filename; can be NULL
Packit ae9e2a
 * @param options Options for diff, or NULL for default options
Packit ae9e2a
 * @param file_cb Callback for "file"; made once if there is a diff; can be NULL
Packit ae9e2a
 * @param binary_cb Callback for binary files; can be NULL
Packit ae9e2a
 * @param hunk_cb Callback for each hunk in diff; can be NULL
Packit ae9e2a
 * @param line_cb Callback for each line in diff; can be NULL
Packit ae9e2a
 * @param payload Payload passed to each callback function
Packit ae9e2a
 * @return 0 on success, non-zero callback return value, or error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_blob_to_buffer(
Packit ae9e2a
	const git_blob *old_blob,
Packit ae9e2a
	const char *old_as_path,
Packit ae9e2a
	const char *buffer,
Packit ae9e2a
	size_t buffer_len,
Packit ae9e2a
	const char *buffer_as_path,
Packit ae9e2a
	const git_diff_options *options,
Packit ae9e2a
	git_diff_file_cb file_cb,
Packit ae9e2a
	git_diff_binary_cb binary_cb,
Packit ae9e2a
	git_diff_hunk_cb hunk_cb,
Packit ae9e2a
	git_diff_line_cb line_cb,
Packit ae9e2a
	void *payload);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Directly run a diff between two buffers.
Packit ae9e2a
 *
Packit ae9e2a
 * Even more than with `git_diff_blobs`, comparing two buffer lacks
Packit ae9e2a
 * context, so the `git_diff_file` parameters to the callbacks will be
Packit ae9e2a
 * faked a la the rules for `git_diff_blobs()`.
Packit ae9e2a
 *
Packit ae9e2a
 * @param old_buffer Raw data for old side of diff, or NULL for empty
Packit ae9e2a
 * @param old_len Length of the raw data for old side of the diff
Packit ae9e2a
 * @param old_as_path Treat old buffer as if it had this filename; can be NULL
Packit ae9e2a
 * @param new_buffer Raw data for new side of diff, or NULL for empty
Packit ae9e2a
 * @param new_len Length of raw data for new side of diff
Packit ae9e2a
 * @param new_as_path Treat buffer as if it had this filename; can be NULL
Packit ae9e2a
 * @param options Options for diff, or NULL for default options
Packit ae9e2a
 * @param file_cb Callback for "file"; made once if there is a diff; can be NULL
Packit ae9e2a
 * @param binary_cb Callback for binary files; can be NULL
Packit ae9e2a
 * @param hunk_cb Callback for each hunk in diff; can be NULL
Packit ae9e2a
 * @param line_cb Callback for each line in diff; can be NULL
Packit ae9e2a
 * @param payload Payload passed to each callback function
Packit ae9e2a
 * @return 0 on success, non-zero callback return value, or error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_buffers(
Packit ae9e2a
	const void *old_buffer,
Packit ae9e2a
	size_t old_len,
Packit ae9e2a
	const char *old_as_path,
Packit ae9e2a
	const void *new_buffer,
Packit ae9e2a
	size_t new_len,
Packit ae9e2a
	const char *new_as_path,
Packit ae9e2a
	const git_diff_options *options,
Packit ae9e2a
	git_diff_file_cb file_cb,
Packit ae9e2a
	git_diff_binary_cb binary_cb,
Packit ae9e2a
	git_diff_hunk_cb hunk_cb,
Packit ae9e2a
	git_diff_line_cb line_cb,
Packit ae9e2a
	void *payload);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Read the contents of a git patch file into a `git_diff` object.
Packit ae9e2a
 *
Packit ae9e2a
 * The diff object produced is similar to the one that would be
Packit ae9e2a
 * produced if you actually produced it computationally by comparing
Packit ae9e2a
 * two trees, however there may be subtle differences.  For example,
Packit ae9e2a
 * a patch file likely contains abbreviated object IDs, so the
Packit ae9e2a
 * object IDs in a `git_diff_delta` produced by this function will
Packit ae9e2a
 * also be abbreviated.
Packit ae9e2a
 *
Packit ae9e2a
 * This function will only read patch files created by a git
Packit ae9e2a
 * implementation, it will not read unified diffs produced by
Packit ae9e2a
 * the `diff` program, nor any other types of patch files.
Packit ae9e2a
 *
Packit ae9e2a
 * @param out A pointer to a git_diff pointer that will be allocated.
Packit ae9e2a
 * @param content The contents of a patch file
Packit ae9e2a
 * @param content_len The length of the patch file contents
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_from_buffer(
Packit ae9e2a
	git_diff **out,
Packit ae9e2a
	const char *content,
Packit ae9e2a
	size_t content_len);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * This is an opaque structure which is allocated by `git_diff_get_stats`.
Packit ae9e2a
 * You are responsible for releasing the object memory when done, using the
Packit ae9e2a
 * `git_diff_stats_free()` function.
Packit ae9e2a
 */
Packit ae9e2a
typedef struct git_diff_stats git_diff_stats;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Formatting options for diff stats
Packit ae9e2a
 */
Packit ae9e2a
typedef enum {
Packit ae9e2a
	/** No stats*/
Packit ae9e2a
	GIT_DIFF_STATS_NONE = 0,
Packit ae9e2a
Packit ae9e2a
	/** Full statistics, equivalent of `--stat` */
Packit ae9e2a
	GIT_DIFF_STATS_FULL = (1u << 0),
Packit ae9e2a
Packit ae9e2a
	/** Short statistics, equivalent of `--shortstat` */
Packit ae9e2a
	GIT_DIFF_STATS_SHORT = (1u << 1),
Packit ae9e2a
Packit ae9e2a
	/** Number statistics, equivalent of `--numstat` */
Packit ae9e2a
	GIT_DIFF_STATS_NUMBER = (1u << 2),
Packit ae9e2a
Packit ae9e2a
	/** Extended header information such as creations, renames and mode changes, equivalent of `--summary` */
Packit ae9e2a
	GIT_DIFF_STATS_INCLUDE_SUMMARY = (1u << 3),
Packit ae9e2a
} git_diff_stats_format_t;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Accumulate diff statistics for all patches.
Packit ae9e2a
 *
Packit ae9e2a
 * @param out Structure containg the diff statistics.
Packit ae9e2a
 * @param diff A git_diff generated by one of the above functions.
Packit ae9e2a
 * @return 0 on success; non-zero on error
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_get_stats(
Packit ae9e2a
	git_diff_stats **out,
Packit ae9e2a
	git_diff *diff);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Get the total number of files changed in a diff
Packit ae9e2a
 *
Packit ae9e2a
 * @param stats A `git_diff_stats` generated by one of the above functions.
Packit ae9e2a
 * @return total number of files changed in the diff
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(size_t) git_diff_stats_files_changed(
Packit ae9e2a
	const git_diff_stats *stats);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Get the total number of insertions in a diff
Packit ae9e2a
 *
Packit ae9e2a
 * @param stats A `git_diff_stats` generated by one of the above functions.
Packit ae9e2a
 * @return total number of insertions in the diff
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(size_t) git_diff_stats_insertions(
Packit ae9e2a
	const git_diff_stats *stats);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Get the total number of deletions in a diff
Packit ae9e2a
 *
Packit ae9e2a
 * @param stats A `git_diff_stats` generated by one of the above functions.
Packit ae9e2a
 * @return total number of deletions in the diff
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(size_t) git_diff_stats_deletions(
Packit ae9e2a
	const git_diff_stats *stats);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Print diff statistics to a `git_buf`.
Packit ae9e2a
 *
Packit ae9e2a
 * @param out buffer to store the formatted diff statistics in.
Packit ae9e2a
 * @param stats A `git_diff_stats` generated by one of the above functions.
Packit ae9e2a
 * @param format Formatting option.
Packit ae9e2a
 * @param width Target width for output (only affects GIT_DIFF_STATS_FULL)
Packit ae9e2a
 * @return 0 on success; non-zero on error
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_stats_to_buf(
Packit ae9e2a
	git_buf *out,
Packit ae9e2a
	const git_diff_stats *stats,
Packit ae9e2a
	git_diff_stats_format_t format,
Packit ae9e2a
	size_t width);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Deallocate a `git_diff_stats`.
Packit ae9e2a
 *
Packit ae9e2a
 * @param stats The previously created statistics object;
Packit ae9e2a
 * cannot be used after free.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(void) git_diff_stats_free(git_diff_stats *stats);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Formatting options for diff e-mail generation
Packit ae9e2a
 */
Packit ae9e2a
typedef enum {
Packit ae9e2a
	/** Normal patch, the default */
Packit ae9e2a
	GIT_DIFF_FORMAT_EMAIL_NONE = 0,
Packit ae9e2a
Packit ae9e2a
	/** Don't insert "[PATCH]" in the subject header*/
Packit ae9e2a
	GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER = (1 << 0),
Packit ae9e2a
Packit ae9e2a
} git_diff_format_email_flags_t;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Options for controlling the formatting of the generated e-mail.
Packit ae9e2a
 */
Packit ae9e2a
typedef struct {
Packit ae9e2a
	unsigned int version;
Packit ae9e2a
Packit ae9e2a
	git_diff_format_email_flags_t flags;
Packit ae9e2a
Packit ae9e2a
	/** This patch number */
Packit ae9e2a
	size_t patch_no;
Packit ae9e2a
Packit ae9e2a
	/** Total number of patches in this series */
Packit ae9e2a
	size_t total_patches;
Packit ae9e2a
Packit ae9e2a
	/** id to use for the commit */
Packit ae9e2a
	const git_oid *id;
Packit ae9e2a
Packit ae9e2a
	/** Summary of the change */
Packit ae9e2a
	const char *summary;
Packit ae9e2a
Packit ae9e2a
	/** Commit message's body */
Packit ae9e2a
	const char *body;
Packit ae9e2a
Packit ae9e2a
	/** Author of the change */
Packit ae9e2a
	const git_signature *author;
Packit ae9e2a
} git_diff_format_email_options;
Packit ae9e2a
Packit ae9e2a
#define GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION 1
Packit ae9e2a
#define GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT {GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION, 0, 1, 1, NULL, NULL, NULL, NULL}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Create an e-mail ready patch from a diff.
Packit ae9e2a
 *
Packit ae9e2a
 * @param out buffer to store the e-mail patch in
Packit ae9e2a
 * @param diff containing the commit
Packit ae9e2a
 * @param opts structure with options to influence content and formatting.
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_format_email(
Packit ae9e2a
	git_buf *out,
Packit ae9e2a
	git_diff *diff,
Packit ae9e2a
	const git_diff_format_email_options *opts);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Create an e-mail ready patch for a commit.
Packit ae9e2a
 *
Packit ae9e2a
 * Does not support creating patches for merge commits (yet).
Packit ae9e2a
 *
Packit ae9e2a
 * @param out buffer to store the e-mail patch in
Packit ae9e2a
 * @param repo containing the commit
Packit ae9e2a
 * @param commit pointer to up commit
Packit ae9e2a
 * @param patch_no patch number of the commit
Packit ae9e2a
 * @param total_patches total number of patches in the patch set
Packit ae9e2a
 * @param flags determines the formatting of the e-mail
Packit ae9e2a
 * @param diff_opts structure with options to influence diff or NULL for defaults.
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_commit_as_email(
Packit ae9e2a
	git_buf *out,
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	git_commit *commit,
Packit ae9e2a
	size_t patch_no,
Packit ae9e2a
	size_t total_patches,
Packit ae9e2a
	git_diff_format_email_flags_t flags,
Packit ae9e2a
	const git_diff_options *diff_opts);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Initializes a `git_diff_format_email_options` with default values.
Packit ae9e2a
 *
Packit ae9e2a
 * Equivalent to creating an instance with GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT.
Packit ae9e2a
 *
Packit ae9e2a
 * @param opts The `git_diff_format_email_options` struct to initialize
Packit ae9e2a
 * @param version Version of struct; pass `GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION`
Packit ae9e2a
 * @return Zero on success; -1 on failure.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_diff_format_email_init_options(
Packit ae9e2a
	git_diff_format_email_options *opts,
Packit ae9e2a
	unsigned int version);
Packit ae9e2a
Packit ae9e2a
GIT_END_DECL
Packit ae9e2a
Packit ae9e2a
/** @} */
Packit ae9e2a
Packit ae9e2a
#endif