Blame include/git2/object.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_object_h__
Packit ae9e2a
#define INCLUDE_git_object_h__
Packit ae9e2a
Packit ae9e2a
#include "common.h"
Packit ae9e2a
#include "types.h"
Packit ae9e2a
#include "oid.h"
Packit ae9e2a
#include "buffer.h"
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * @file git2/object.h
Packit ae9e2a
 * @brief Git revision object management routines
Packit ae9e2a
 * @defgroup git_object Git revision object management routines
Packit ae9e2a
 * @ingroup Git
Packit ae9e2a
 * @{
Packit ae9e2a
 */
Packit ae9e2a
GIT_BEGIN_DECL
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Lookup a reference to one of the objects in a repository.
Packit ae9e2a
 *
Packit ae9e2a
 * The generated reference is owned by the repository and
Packit ae9e2a
 * should be closed with the `git_object_free` method
Packit ae9e2a
 * instead of free'd manually.
Packit ae9e2a
 *
Packit ae9e2a
 * The 'type' parameter must match the type of the object
Packit ae9e2a
 * in the odb; the method will fail otherwise.
Packit ae9e2a
 * The special value 'GIT_OBJ_ANY' may be passed to let
Packit ae9e2a
 * the method guess the object's type.
Packit ae9e2a
 *
Packit ae9e2a
 * @param object pointer to the looked-up object
Packit ae9e2a
 * @param repo the repository to look up the object
Packit ae9e2a
 * @param id the unique identifier for the object
Packit ae9e2a
 * @param type the type of the object
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_object_lookup(
Packit ae9e2a
		git_object **object,
Packit ae9e2a
		git_repository *repo,
Packit ae9e2a
		const git_oid *id,
Packit ae9e2a
		git_otype type);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Lookup a reference to one of the objects in a repository,
Packit ae9e2a
 * given a prefix of its identifier (short id).
Packit ae9e2a
 *
Packit ae9e2a
 * The object obtained will be so that its identifier
Packit ae9e2a
 * matches the first 'len' hexadecimal characters
Packit ae9e2a
 * (packets of 4 bits) of the given 'id'.
Packit ae9e2a
 * 'len' must be at least GIT_OID_MINPREFIXLEN, and
Packit ae9e2a
 * long enough to identify a unique object matching
Packit ae9e2a
 * the prefix; otherwise the method will fail.
Packit ae9e2a
 *
Packit ae9e2a
 * The generated reference is owned by the repository and
Packit ae9e2a
 * should be closed with the `git_object_free` method
Packit ae9e2a
 * instead of free'd manually.
Packit ae9e2a
 *
Packit ae9e2a
 * The 'type' parameter must match the type of the object
Packit ae9e2a
 * in the odb; the method will fail otherwise.
Packit ae9e2a
 * The special value 'GIT_OBJ_ANY' may be passed to let
Packit ae9e2a
 * the method guess the object's type.
Packit ae9e2a
 *
Packit ae9e2a
 * @param object_out pointer where to store the looked-up object
Packit ae9e2a
 * @param repo the repository to look up the object
Packit ae9e2a
 * @param id a short identifier for the object
Packit ae9e2a
 * @param len the length of the short identifier
Packit ae9e2a
 * @param type the type of the object
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_object_lookup_prefix(
Packit ae9e2a
		git_object **object_out,
Packit ae9e2a
		git_repository *repo,
Packit ae9e2a
		const git_oid *id,
Packit ae9e2a
		size_t len,
Packit ae9e2a
		git_otype type);
Packit ae9e2a
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Lookup an object that represents a tree entry.
Packit ae9e2a
 *
Packit ae9e2a
 * @param out buffer that receives a pointer to the object (which must be freed
Packit ae9e2a
 *            by the caller)
Packit ae9e2a
 * @param treeish root object that can be peeled to a tree
Packit ae9e2a
 * @param path relative path from the root object to the desired object
Packit ae9e2a
 * @param type type of object desired
Packit ae9e2a
 * @return 0 on success, or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_object_lookup_bypath(
Packit ae9e2a
		git_object **out,
Packit ae9e2a
		const git_object *treeish,
Packit ae9e2a
		const char *path,
Packit ae9e2a
		git_otype type);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Get the id (SHA1) of a repository object
Packit ae9e2a
 *
Packit ae9e2a
 * @param obj the repository object
Packit ae9e2a
 * @return the SHA1 id
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(const git_oid *) git_object_id(const git_object *obj);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Get a short abbreviated OID string for the object
Packit ae9e2a
 *
Packit ae9e2a
 * This starts at the "core.abbrev" length (default 7 characters) and
Packit ae9e2a
 * iteratively extends to a longer string if that length is ambiguous.
Packit ae9e2a
 * The result will be unambiguous (at least until new objects are added to
Packit ae9e2a
 * the repository).
Packit ae9e2a
 *
Packit ae9e2a
 * @param out Buffer to write string into
Packit ae9e2a
 * @param obj The object to get an ID for
Packit ae9e2a
 * @return 0 on success, <0 for error
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_object_short_id(git_buf *out, const git_object *obj);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Get the object type of an object
Packit ae9e2a
 *
Packit ae9e2a
 * @param obj the repository object
Packit ae9e2a
 * @return the object's type
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(git_otype) git_object_type(const git_object *obj);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Get the repository that owns this object
Packit ae9e2a
 *
Packit ae9e2a
 * Freeing or calling `git_repository_close` on the
Packit ae9e2a
 * returned pointer will invalidate the actual object.
Packit ae9e2a
 *
Packit ae9e2a
 * Any other operation may be run on the repository without
Packit ae9e2a
 * affecting the object.
Packit ae9e2a
 *
Packit ae9e2a
 * @param obj the object
Packit ae9e2a
 * @return the repository who owns this object
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(git_repository *) git_object_owner(const git_object *obj);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Close an open object
Packit ae9e2a
 *
Packit ae9e2a
 * This method instructs the library to close an existing
Packit ae9e2a
 * object; note that git_objects are owned and cached by the repository
Packit ae9e2a
 * so the object may or may not be freed after this library call,
Packit ae9e2a
 * depending on how aggressive is the caching mechanism used
Packit ae9e2a
 * by the repository.
Packit ae9e2a
 *
Packit ae9e2a
 * IMPORTANT:
Packit ae9e2a
 * It *is* necessary to call this method when you stop using
Packit ae9e2a
 * an object. Failure to do so will cause a memory leak.
Packit ae9e2a
 *
Packit ae9e2a
 * @param object the object to close
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(void) git_object_free(git_object *object);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Convert an object type to its string representation.
Packit ae9e2a
 *
Packit ae9e2a
 * The result is a pointer to a string in static memory and
Packit ae9e2a
 * should not be free()'ed.
Packit ae9e2a
 *
Packit ae9e2a
 * @param type object type to convert.
Packit ae9e2a
 * @return the corresponding string representation.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(const char *) git_object_type2string(git_otype type);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Convert a string object type representation to it's git_otype.
Packit ae9e2a
 *
Packit ae9e2a
 * @param str the string to convert.
Packit ae9e2a
 * @return the corresponding git_otype.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(git_otype) git_object_string2type(const char *str);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Determine if the given git_otype is a valid loose object type.
Packit ae9e2a
 *
Packit ae9e2a
 * @param type object type to test.
Packit ae9e2a
 * @return true if the type represents a valid loose object type,
Packit ae9e2a
 * false otherwise.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_object_typeisloose(git_otype type);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Get the size in bytes for the structure which
Packit ae9e2a
 * acts as an in-memory representation of any given
Packit ae9e2a
 * object type.
Packit ae9e2a
 *
Packit ae9e2a
 * For all the core types, this would the equivalent
Packit ae9e2a
 * of calling `sizeof(git_commit)` if the core types
Packit ae9e2a
 * were not opaque on the external API.
Packit ae9e2a
 *
Packit ae9e2a
 * @param type object type to get its size
Packit ae9e2a
 * @return size in bytes of the object
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(size_t) git_object__size(git_otype type);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Recursively peel an object until an object of the specified type is met.
Packit ae9e2a
 *
Packit ae9e2a
 * If the query cannot be satisfied due to the object model,
Packit ae9e2a
 * GIT_EINVALIDSPEC will be returned (e.g. trying to peel a blob to a
Packit ae9e2a
 * tree).
Packit ae9e2a
 *
Packit ae9e2a
 * If you pass `GIT_OBJ_ANY` as the target type, then the object will
Packit ae9e2a
 * be peeled until the type changes. A tag will be peeled until the
Packit ae9e2a
 * referenced object is no longer a tag, and a commit will be peeled
Packit ae9e2a
 * to a tree. Any other object type will return GIT_EINVALIDSPEC.
Packit ae9e2a
 *
Packit ae9e2a
 * If peeling a tag we discover an object which cannot be peeled to
Packit ae9e2a
 * the target type due to the object model, GIT_EPEEL will be
Packit ae9e2a
 * returned.
Packit ae9e2a
 *
Packit ae9e2a
 * You must free the returned object.
Packit ae9e2a
 *
Packit ae9e2a
 * @param peeled Pointer to the peeled git_object
Packit ae9e2a
 * @param object The object to be processed
Packit ae9e2a
 * @param target_type The type of the requested object (a GIT_OBJ_ value)
Packit ae9e2a
 * @return 0 on success, GIT_EINVALIDSPEC, GIT_EPEEL, or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_object_peel(
Packit ae9e2a
	git_object **peeled,
Packit ae9e2a
	const git_object *object,
Packit ae9e2a
	git_otype target_type);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Create an in-memory copy of a Git object. The copy must be
Packit ae9e2a
 * explicitly free'd or it will leak.
Packit ae9e2a
 *
Packit ae9e2a
 * @param dest Pointer to store the copy of the object
Packit ae9e2a
 * @param source Original object to copy
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_object_dup(git_object **dest, git_object *source);
Packit ae9e2a
Packit ae9e2a
/** @} */
Packit ae9e2a
GIT_END_DECL
Packit ae9e2a
Packit ae9e2a
#endif