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