Blame include/git2/blame.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
Packit Service 20376f
#ifndef INCLUDE_git_blame_h__
Packit Service 20376f
#define INCLUDE_git_blame_h__
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "oid.h"
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * @file git2/blame.h
Packit Service 20376f
 * @brief Git blame routines
Packit Service 20376f
 * @defgroup git_blame Git blame 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
 * Flags for indicating option behavior for git_blame APIs.
Packit Service 20376f
 */
Packit Service 20376f
typedef enum {
Packit Service 20376f
	/** Normal blame, the default */
Packit Service 20376f
	GIT_BLAME_NORMAL = 0,
Packit Service 20376f
	/** Track lines that have moved within a file (like `git blame -M`).
Packit Service 20376f
	 * NOT IMPLEMENTED. */
Packit Service 20376f
	GIT_BLAME_TRACK_COPIES_SAME_FILE = (1<<0),
Packit Service 20376f
	/** Track lines that have moved across files in the same commit (like `git blame -C`).
Packit Service 20376f
	 * NOT IMPLEMENTED. */
Packit Service 20376f
	GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES = (1<<1),
Packit Service 20376f
	/** Track lines that have been copied from another file that exists in the
Packit Service 20376f
	 * same commit (like `git blame -CC`). Implies SAME_FILE.
Packit Service 20376f
	 * NOT IMPLEMENTED. */
Packit Service 20376f
	GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES = (1<<2),
Packit Service 20376f
	/** Track lines that have been copied from another file that exists in *any*
Packit Service 20376f
	 * commit (like `git blame -CCC`). Implies SAME_COMMIT_COPIES.
Packit Service 20376f
	 * NOT IMPLEMENTED. */
Packit Service 20376f
	GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES = (1<<3),
Packit Service 20376f
	/** Restrict the search of commits to those reachable following only the
Packit Service 20376f
	 * first parents. */
Packit Service 20376f
	GIT_BLAME_FIRST_PARENT = (1<<4),
Packit Service 20376f
} git_blame_flag_t;
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Blame options structure
Packit Service 20376f
 *
Packit Service 20376f
 * Use zeros to indicate default settings.  It's easiest to use the
Packit Service 20376f
 * `GIT_BLAME_OPTIONS_INIT` macro:
Packit Service 20376f
 *     git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
Packit Service 20376f
 *
Packit Service 20376f
 * - `flags` is a combination of the `git_blame_flag_t` values above.
Packit Service 20376f
 * - `min_match_characters` is the lower bound on the number of alphanumeric
Packit Service 20376f
 *   characters that must be detected as moving/copying within a file for it to
Packit Service 20376f
 *   associate those lines with the parent commit. The default value is 20.
Packit Service 20376f
 *   This value only takes effect if any of the `GIT_BLAME_TRACK_COPIES_*`
Packit Service 20376f
 *   flags are specified.
Packit Service 20376f
 * - `newest_commit` is the id of the newest commit to consider.  The default
Packit Service 20376f
 *                   is HEAD.
Packit Service 20376f
 * - `oldest_commit` is the id of the oldest commit to consider.  The default
Packit Service 20376f
 *                   is the first commit encountered with a NULL parent.
Packit Service 20376f
 *	- `min_line` is the first line in the file to blame.  The default is 1 (line
Packit Service 20376f
 *	             numbers start with 1).
Packit Service 20376f
 *	- `max_line` is the last line in the file to blame.  The default is the last
Packit Service 20376f
 *	             line of the file.
Packit Service 20376f
 */
Packit Service 20376f
typedef struct git_blame_options {
Packit Service 20376f
	unsigned int version;
Packit Service 20376f
Packit Service 20376f
	uint32_t flags;
Packit Service 20376f
	uint16_t min_match_characters;
Packit Service 20376f
	git_oid newest_commit;
Packit Service 20376f
	git_oid oldest_commit;
Packit Service 20376f
	size_t min_line;
Packit Service 20376f
	size_t max_line;
Packit Service 20376f
} git_blame_options;
Packit Service 20376f
Packit Service 20376f
#define GIT_BLAME_OPTIONS_VERSION 1
Packit Service 20376f
#define GIT_BLAME_OPTIONS_INIT {GIT_BLAME_OPTIONS_VERSION}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Initializes a `git_blame_options` with default values. Equivalent to
Packit Service 20376f
 * creating an instance with GIT_BLAME_OPTIONS_INIT.
Packit Service 20376f
 *
Packit Service 20376f
 * @param opts The `git_blame_options` struct to initialize
Packit Service 20376f
 * @param version Version of struct; pass `GIT_BLAME_OPTIONS_VERSION`
Packit Service 20376f
 * @return Zero on success; -1 on failure.
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_blame_init_options(
Packit Service 20376f
	git_blame_options *opts,
Packit Service 20376f
	unsigned int version);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Structure that represents a blame hunk.
Packit Service 20376f
 *
Packit Service 20376f
 * - `lines_in_hunk` is the number of lines in this hunk
Packit Service 20376f
 * - `final_commit_id` is the OID of the commit where this line was last
Packit Service 20376f
 *   changed.
Packit Service 20376f
 * - `final_start_line_number` is the 1-based line number where this hunk
Packit Service 20376f
 *   begins, in the final version of the file
Packit Service 20376f
 * - `orig_commit_id` is the OID of the commit where this hunk was found.  This
Packit Service 20376f
 *   will usually be the same as `final_commit_id`, except when
Packit Service 20376f
 *   `GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES` has been specified.
Packit Service 20376f
 * - `orig_path` is the path to the file where this hunk originated, as of the
Packit Service 20376f
 *   commit specified by `orig_commit_id`.
Packit Service 20376f
 * - `orig_start_line_number` is the 1-based line number where this hunk begins
Packit Service 20376f
 *   in the file named by `orig_path` in the commit specified by
Packit Service 20376f
 *   `orig_commit_id`.
Packit Service 20376f
 * - `boundary` is 1 iff the hunk has been tracked to a boundary commit (the
Packit Service 20376f
 *   root, or the commit specified in git_blame_options.oldest_commit)
Packit Service 20376f
 */
Packit Service 20376f
typedef struct git_blame_hunk {
Packit Service 20376f
	size_t lines_in_hunk;
Packit Service 20376f
Packit Service 20376f
	git_oid final_commit_id;
Packit Service 20376f
	size_t final_start_line_number;
Packit Service 20376f
	git_signature *final_signature;
Packit Service 20376f
Packit Service 20376f
	git_oid orig_commit_id;
Packit Service 20376f
	const char *orig_path;
Packit Service 20376f
	size_t orig_start_line_number;
Packit Service 20376f
	git_signature *orig_signature;
Packit Service 20376f
Packit Service 20376f
	char boundary;
Packit Service 20376f
} git_blame_hunk;
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
/* Opaque structure to hold blame results */
Packit Service 20376f
typedef struct git_blame git_blame;
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Gets the number of hunks that exist in the blame structure.
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(uint32_t) git_blame_get_hunk_count(git_blame *blame);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Gets the blame hunk at the given index.
Packit Service 20376f
 *
Packit Service 20376f
 * @param blame the blame structure to query
Packit Service 20376f
 * @param index index of the hunk to retrieve
Packit Service 20376f
 * @return the hunk at the given index, or NULL on error
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(const git_blame_hunk*) git_blame_get_hunk_byindex(
Packit Service 20376f
		git_blame *blame,
Packit Service 20376f
		uint32_t index);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Gets the hunk that relates to the given line number in the newest commit.
Packit Service 20376f
 *
Packit Service 20376f
 * @param blame the blame structure to query
Packit Service 20376f
 * @param lineno the (1-based) line number to find a hunk for
Packit Service 20376f
 * @return the hunk that contains the given line, or NULL on error
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(const git_blame_hunk*) git_blame_get_hunk_byline(
Packit Service 20376f
		git_blame *blame,
Packit Service 20376f
		size_t lineno);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Get the blame for a single file.
Packit Service 20376f
 *
Packit Service 20376f
 * @param out pointer that will receive the blame object
Packit Service 20376f
 * @param repo repository whose history is to be walked
Packit Service 20376f
 * @param path path to file to consider
Packit Service 20376f
 * @param options options for the blame operation.  If NULL, this is treated as
Packit Service 20376f
 *                though GIT_BLAME_OPTIONS_INIT were passed.
Packit Service 20376f
 * @return 0 on success, or an error code. (use giterr_last for information
Packit Service 20376f
 *         about the error.)
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_blame_file(
Packit Service 20376f
		git_blame **out,
Packit Service 20376f
		git_repository *repo,
Packit Service 20376f
		const char *path,
Packit Service 20376f
		git_blame_options *options);
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Get blame data for a file that has been modified in memory. The `reference`
Packit Service 20376f
 * parameter is a pre-calculated blame for the in-odb history of the file. This
Packit Service 20376f
 * means that once a file blame is completed (which can be expensive), updating
Packit Service 20376f
 * the buffer blame is very fast.
Packit Service 20376f
 *
Packit Service 20376f
 * Lines that differ between the buffer and the committed version are marked as
Packit Service 20376f
 * having a zero OID for their final_commit_id.
Packit Service 20376f
 *
Packit Service 20376f
 * @param out pointer that will receive the resulting blame data
Packit Service 20376f
 * @param reference cached blame from the history of the file (usually the output
Packit Service 20376f
 *                  from git_blame_file)
Packit Service 20376f
 * @param buffer the (possibly) modified contents of the file
Packit Service 20376f
 * @param buffer_len number of valid bytes in the buffer
Packit Service 20376f
 * @return 0 on success, or an error code. (use giterr_last for information
Packit Service 20376f
 *         about the error)
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_blame_buffer(
Packit Service 20376f
		git_blame **out,
Packit Service 20376f
		git_blame *reference,
Packit Service 20376f
		const char *buffer,
Packit Service 20376f
		size_t buffer_len);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Free memory allocated by git_blame_file or git_blame_buffer.
Packit Service 20376f
 *
Packit Service 20376f
 * @param blame the blame structure to free
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(void) git_blame_free(git_blame *blame);
Packit Service 20376f
Packit Service 20376f
/** @} */
Packit Service 20376f
GIT_END_DECL
Packit Service 20376f
#endif
Packit Service 20376f