Blame include/git2/patch.h

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#ifndef INCLUDE_git_patch_h__
Packit Service 20376f
#define INCLUDE_git_patch_h__
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "types.h"
Packit Service 20376f
#include "oid.h"
Packit Service 20376f
#include "diff.h"
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * @file git2/patch.h
Packit Service 20376f
 * @brief Patch handling routines.
Packit Service 20376f
 * @ingroup Git
Packit Service 20376f
 * @{
Packit Service 20376f
 */
Packit Service 20376f
GIT_BEGIN_DECL
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * The diff patch is used to store all the text diffs for a delta.
Packit Service 20376f
 *
Packit Service 20376f
 * You can easily loop over the content of patches and get information about
Packit Service 20376f
 * them.
Packit Service 20376f
 */
Packit Service 20376f
typedef struct git_patch git_patch;
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Return a patch for an entry in the diff list.
Packit Service 20376f
 *
Packit Service 20376f
 * The `git_patch` is a newly created object contains the text diffs
Packit Service 20376f
 * for the delta.  You have to call `git_patch_free()` when you are
Packit Service 20376f
 * done with it.  You can use the patch object to loop over all the hunks
Packit Service 20376f
 * and lines in the diff of the one delta.
Packit Service 20376f
 *
Packit Service 20376f
 * For an unchanged file or a binary file, no `git_patch` will be
Packit Service 20376f
 * created, the output will be set to NULL, and the `binary` flag will be
Packit Service 20376f
 * set true in the `git_diff_delta` structure.
Packit Service 20376f
 *
Packit Service 20376f
 * It is okay to pass NULL for either of the output parameters; if you pass
Packit Service 20376f
 * NULL for the `git_patch`, then the text diff will not be calculated.
Packit Service 20376f
 *
Packit Service 20376f
 * @param out Output parameter for the delta patch object
Packit Service 20376f
 * @param diff Diff list object
Packit Service 20376f
 * @param idx Index into diff list
Packit Service 20376f
 * @return 0 on success, other value < 0 on error
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_patch_from_diff(
Packit Service 20376f
	git_patch **out, git_diff *diff, size_t idx);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Directly generate a patch from the difference between two blobs.
Packit Service 20376f
 *
Packit Service 20376f
 * This is just like `git_diff_blobs()` except it generates a patch object
Packit Service 20376f
 * for the difference instead of directly making callbacks.  You can use the
Packit Service 20376f
 * standard `git_patch` accessor functions to read the patch data, and
Packit Service 20376f
 * you must call `git_patch_free()` on the patch when done.
Packit Service 20376f
 *
Packit Service 20376f
 * @param out The generated patch; NULL on error
Packit Service 20376f
 * @param old_blob Blob for old side of diff, or NULL for empty blob
Packit Service 20376f
 * @param old_as_path Treat old blob as if it had this filename; can be NULL
Packit Service 20376f
 * @param new_blob Blob for new side of diff, or NULL for empty blob
Packit Service 20376f
 * @param new_as_path Treat new blob as if it had this filename; can be NULL
Packit Service 20376f
 * @param opts Options for diff, or NULL for default options
Packit Service 20376f
 * @return 0 on success or error code < 0
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_patch_from_blobs(
Packit Service 20376f
	git_patch **out,
Packit Service 20376f
	const git_blob *old_blob,
Packit Service 20376f
	const char *old_as_path,
Packit Service 20376f
	const git_blob *new_blob,
Packit Service 20376f
	const char *new_as_path,
Packit Service 20376f
	const git_diff_options *opts);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Directly generate a patch from the difference between a blob and a buffer.
Packit Service 20376f
 *
Packit Service 20376f
 * This is just like `git_diff_blob_to_buffer()` except it generates a patch
Packit Service 20376f
 * object for the difference instead of directly making callbacks.  You can
Packit Service 20376f
 * use the standard `git_patch` accessor functions to read the patch
Packit Service 20376f
 * data, and you must call `git_patch_free()` on the patch when done.
Packit Service 20376f
 *
Packit Service 20376f
 * @param out The generated patch; NULL on error
Packit Service 20376f
 * @param old_blob Blob for old side of diff, or NULL for empty blob
Packit Service 20376f
 * @param old_as_path Treat old blob as if it had this filename; can be NULL
Packit Service 20376f
 * @param buffer Raw data for new side of diff, or NULL for empty
Packit Service 20376f
 * @param buffer_len Length of raw data for new side of diff
Packit Service 20376f
 * @param buffer_as_path Treat buffer as if it had this filename; can be NULL
Packit Service 20376f
 * @param opts Options for diff, or NULL for default options
Packit Service 20376f
 * @return 0 on success or error code < 0
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_patch_from_blob_and_buffer(
Packit Service 20376f
	git_patch **out,
Packit Service 20376f
	const git_blob *old_blob,
Packit Service 20376f
	const char *old_as_path,
Packit Service 20376f
	const char *buffer,
Packit Service 20376f
	size_t buffer_len,
Packit Service 20376f
	const char *buffer_as_path,
Packit Service 20376f
	const git_diff_options *opts);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Directly generate a patch from the difference between two buffers.
Packit Service 20376f
 *
Packit Service 20376f
 * This is just like `git_diff_buffers()` except it generates a patch
Packit Service 20376f
 * object for the difference instead of directly making callbacks.  You can
Packit Service 20376f
 * use the standard `git_patch` accessor functions to read the patch
Packit Service 20376f
 * data, and you must call `git_patch_free()` on the patch when done.
Packit Service 20376f
 *
Packit Service 20376f
 * @param out The generated patch; NULL on error
Packit Service 20376f
 * @param old_buffer Raw data for old side of diff, or NULL for empty
Packit Service 20376f
 * @param old_len Length of the raw data for old side of the diff
Packit Service 20376f
 * @param old_as_path Treat old buffer as if it had this filename; can be NULL
Packit Service 20376f
 * @param new_buffer Raw data for new side of diff, or NULL for empty
Packit Service 20376f
 * @param new_len Length of raw data for new side of diff
Packit Service 20376f
 * @param new_as_path Treat buffer as if it had this filename; can be NULL
Packit Service 20376f
 * @param opts Options for diff, or NULL for default options
Packit Service 20376f
 * @return 0 on success or error code < 0
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_patch_from_buffers(
Packit Service 20376f
	git_patch **out,
Packit Service 20376f
	const void *old_buffer,
Packit Service 20376f
	size_t old_len,
Packit Service 20376f
	const char *old_as_path,
Packit Service 20376f
	const char *new_buffer,
Packit Service 20376f
	size_t new_len,
Packit Service 20376f
	const char *new_as_path,
Packit Service 20376f
	const git_diff_options *opts);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Free a git_patch object.
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(void) git_patch_free(git_patch *patch);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Get the delta associated with a patch.  This delta points to internal
Packit Service 20376f
 * data and you do not have to release it when you are done with it.
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(const git_diff_delta *) git_patch_get_delta(const git_patch *patch);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Get the number of hunks in a patch
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(size_t) git_patch_num_hunks(const git_patch *patch);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Get line counts of each type in a patch.
Packit Service 20376f
 *
Packit Service 20376f
 * This helps imitate a diff --numstat type of output.  For that purpose,
Packit Service 20376f
 * you only need the `total_additions` and `total_deletions` values, but we
Packit Service 20376f
 * include the `total_context` line count in case you want the total number
Packit Service 20376f
 * of lines of diff output that will be generated.
Packit Service 20376f
 *
Packit Service 20376f
 * All outputs are optional. Pass NULL if you don't need a particular count.
Packit Service 20376f
 *
Packit Service 20376f
 * @param total_context Count of context lines in output, can be NULL.
Packit Service 20376f
 * @param total_additions Count of addition lines in output, can be NULL.
Packit Service 20376f
 * @param total_deletions Count of deletion lines in output, can be NULL.
Packit Service 20376f
 * @param patch The git_patch object
Packit Service 20376f
 * @return 0 on success, <0 on error
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_patch_line_stats(
Packit Service 20376f
	size_t *total_context,
Packit Service 20376f
	size_t *total_additions,
Packit Service 20376f
	size_t *total_deletions,
Packit Service 20376f
	const git_patch *patch);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Get the information about a hunk in a patch
Packit Service 20376f
 *
Packit Service 20376f
 * Given a patch and a hunk index into the patch, this returns detailed
Packit Service 20376f
 * information about that hunk.  Any of the output pointers can be passed
Packit Service 20376f
 * as NULL if you don't care about that particular piece of information.
Packit Service 20376f
 *
Packit Service 20376f
 * @param out Output pointer to git_diff_hunk of hunk
Packit Service 20376f
 * @param lines_in_hunk Output count of total lines in this hunk
Packit Service 20376f
 * @param patch Input pointer to patch object
Packit Service 20376f
 * @param hunk_idx Input index of hunk to get information about
Packit Service 20376f
 * @return 0 on success, GIT_ENOTFOUND if hunk_idx out of range, <0 on error
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_patch_get_hunk(
Packit Service 20376f
	const git_diff_hunk **out,
Packit Service 20376f
	size_t *lines_in_hunk,
Packit Service 20376f
	git_patch *patch,
Packit Service 20376f
	size_t hunk_idx);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Get the number of lines in a hunk.
Packit Service 20376f
 *
Packit Service 20376f
 * @param patch The git_patch object
Packit Service 20376f
 * @param hunk_idx Index of the hunk
Packit Service 20376f
 * @return Number of lines in hunk or GIT_ENOTFOUND if invalid hunk index
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_patch_num_lines_in_hunk(
Packit Service 20376f
	const git_patch *patch,
Packit Service 20376f
	size_t hunk_idx);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Get data about a line in a hunk of a patch.
Packit Service 20376f
 *
Packit Service 20376f
 * Given a patch, a hunk index, and a line index in the hunk, this
Packit Service 20376f
 * will return a lot of details about that line.  If you pass a hunk
Packit Service 20376f
 * index larger than the number of hunks or a line index larger than
Packit Service 20376f
 * the number of lines in the hunk, this will return -1.
Packit Service 20376f
 *
Packit Service 20376f
 * @param out The git_diff_line data for this line
Packit Service 20376f
 * @param patch The patch to look in
Packit Service 20376f
 * @param hunk_idx The index of the hunk
Packit Service 20376f
 * @param line_of_hunk The index of the line in the hunk
Packit Service 20376f
 * @return 0 on success, <0 on failure
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_patch_get_line_in_hunk(
Packit Service 20376f
	const git_diff_line **out,
Packit Service 20376f
	git_patch *patch,
Packit Service 20376f
	size_t hunk_idx,
Packit Service 20376f
	size_t line_of_hunk);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Look up size of patch diff data in bytes
Packit Service 20376f
 *
Packit Service 20376f
 * This returns the raw size of the patch data.  This only includes the
Packit Service 20376f
 * actual data from the lines of the diff, not the file or hunk headers.
Packit Service 20376f
 *
Packit Service 20376f
 * If you pass `include_context` as true (non-zero), this will be the size
Packit Service 20376f
 * of all of the diff output; if you pass it as false (zero), this will
Packit Service 20376f
 * only include the actual changed lines (as if `context_lines` was 0).
Packit Service 20376f
 *
Packit Service 20376f
 * @param patch A git_patch representing changes to one file
Packit Service 20376f
 * @param include_context Include context lines in size if non-zero
Packit Service 20376f
 * @param include_hunk_headers Include hunk header lines if non-zero
Packit Service 20376f
 * @param include_file_headers Include file header lines if non-zero
Packit Service 20376f
 * @return The number of bytes of data
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(size_t) git_patch_size(
Packit Service 20376f
	git_patch *patch,
Packit Service 20376f
	int include_context,
Packit Service 20376f
	int include_hunk_headers,
Packit Service 20376f
	int include_file_headers);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Serialize the patch to text via callback.
Packit Service 20376f
 *
Packit Service 20376f
 * Returning a non-zero value from the callback will terminate the iteration
Packit Service 20376f
 * and return that value to the caller.
Packit Service 20376f
 *
Packit Service 20376f
 * @param patch A git_patch representing changes to one file
Packit Service 20376f
 * @param print_cb Callback function to output lines of the patch.  Will be
Packit Service 20376f
 *                 called for file headers, hunk headers, and diff lines.
Packit Service 20376f
 * @param payload Reference pointer that will be passed to your callbacks.
Packit Service 20376f
 * @return 0 on success, non-zero callback return value, or error code
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_patch_print(
Packit Service 20376f
	git_patch *patch,
Packit Service 20376f
	git_diff_line_cb print_cb,
Packit Service 20376f
	void *payload);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Get the content of a patch as a single diff text.
Packit Service 20376f
 *
Packit Service 20376f
 * @param out The git_buf to be filled in
Packit Service 20376f
 * @param patch A git_patch representing changes to one file
Packit Service 20376f
 * @return 0 on success, <0 on failure.
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_patch_to_buf(
Packit Service 20376f
	git_buf *out,
Packit Service 20376f
	git_patch *patch);
Packit Service 20376f
Packit Service 20376f
GIT_END_DECL
Packit Service 20376f
Packit Service 20376f
/**@}*/
Packit Service 20376f
Packit Service 20376f
#endif