Blame include/git2/blob.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_blob_h__
Packit ae9e2a
#define INCLUDE_git_blob_h__
Packit ae9e2a
Packit ae9e2a
#include "common.h"
Packit ae9e2a
#include "types.h"
Packit ae9e2a
#include "oid.h"
Packit ae9e2a
#include "object.h"
Packit ae9e2a
#include "buffer.h"
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * @file git2/blob.h
Packit ae9e2a
 * @brief Git blob load and write routines
Packit ae9e2a
 * @defgroup git_blob Git blob load and write routines
Packit ae9e2a
 * @ingroup Git
Packit ae9e2a
 * @{
Packit ae9e2a
 */
Packit ae9e2a
GIT_BEGIN_DECL
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Lookup a blob object from a repository.
Packit ae9e2a
 *
Packit ae9e2a
 * @param blob pointer to the looked up blob
Packit ae9e2a
 * @param repo the repo to use when locating the blob.
Packit ae9e2a
 * @param id identity of the blob to locate.
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_blob_lookup(git_blob **blob, git_repository *repo, const git_oid *id);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Lookup a blob object from a repository,
Packit ae9e2a
 * given a prefix of its identifier (short id).
Packit ae9e2a
 *
Packit ae9e2a
 * @see git_object_lookup_prefix
Packit ae9e2a
 *
Packit ae9e2a
 * @param blob pointer to the looked up blob
Packit ae9e2a
 * @param repo the repo to use when locating the blob.
Packit ae9e2a
 * @param id identity of the blob to locate.
Packit ae9e2a
 * @param len the length of the short identifier
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_blob_lookup_prefix(git_blob **blob, git_repository *repo, const git_oid *id, size_t len);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Close an open blob
Packit ae9e2a
 *
Packit ae9e2a
 * This is a wrapper around git_object_free()
Packit ae9e2a
 *
Packit ae9e2a
 * IMPORTANT:
Packit ae9e2a
 * It *is* necessary to call this method when you stop
Packit ae9e2a
 * using a blob. Failure to do so will cause a memory leak.
Packit ae9e2a
 *
Packit ae9e2a
 * @param blob the blob to close
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(void) git_blob_free(git_blob *blob);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Get the id of a blob.
Packit ae9e2a
 *
Packit ae9e2a
 * @param blob a previously loaded blob.
Packit ae9e2a
 * @return SHA1 hash for this blob.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(const git_oid *) git_blob_id(const git_blob *blob);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Get the repository that contains the blob.
Packit ae9e2a
 *
Packit ae9e2a
 * @param blob A previously loaded blob.
Packit ae9e2a
 * @return Repository that contains this blob.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(git_repository *) git_blob_owner(const git_blob *blob);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Get a read-only buffer with the raw content of a blob.
Packit ae9e2a
 *
Packit ae9e2a
 * A pointer to the raw content of a blob is returned;
Packit ae9e2a
 * this pointer is owned internally by the object and shall
Packit ae9e2a
 * not be free'd. The pointer may be invalidated at a later
Packit ae9e2a
 * time.
Packit ae9e2a
 *
Packit ae9e2a
 * @param blob pointer to the blob
Packit ae9e2a
 * @return the pointer
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(const void *) git_blob_rawcontent(const git_blob *blob);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Get the size in bytes of the contents of a blob
Packit ae9e2a
 *
Packit ae9e2a
 * @param blob pointer to the blob
Packit ae9e2a
 * @return size on bytes
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Get a buffer with the filtered content of a blob.
Packit ae9e2a
 *
Packit ae9e2a
 * This applies filters as if the blob was being checked out to the
Packit ae9e2a
 * working directory under the specified filename.  This may apply
Packit ae9e2a
 * CRLF filtering or other types of changes depending on the file
Packit ae9e2a
 * attributes set for the blob and the content detected in it.
Packit ae9e2a
 *
Packit ae9e2a
 * The output is written into a `git_buf` which the caller must free
Packit ae9e2a
 * when done (via `git_buf_free`).
Packit ae9e2a
 *
Packit ae9e2a
 * If no filters need to be applied, then the `out` buffer will just
Packit ae9e2a
 * be populated with a pointer to the raw content of the blob.  In
Packit ae9e2a
 * that case, be careful to *not* free the blob until done with the
Packit ae9e2a
 * buffer or copy it into memory you own.
Packit ae9e2a
 *
Packit ae9e2a
 * @param out The git_buf to be filled in
Packit ae9e2a
 * @param blob Pointer to the blob
Packit ae9e2a
 * @param as_path Path used for file attribute lookups, etc.
Packit ae9e2a
 * @param check_for_binary_data Should this test if blob content contains
Packit ae9e2a
 *        NUL bytes / looks like binary data before applying filters?
Packit ae9e2a
 * @return 0 on success or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_blob_filtered_content(
Packit ae9e2a
	git_buf *out,
Packit ae9e2a
	git_blob *blob,
Packit ae9e2a
	const char *as_path,
Packit ae9e2a
	int check_for_binary_data);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Read a file from the working folder of a repository
Packit ae9e2a
 * and write it to the Object Database as a loose blob
Packit ae9e2a
 *
Packit ae9e2a
 * @param id return the id of the written blob
Packit ae9e2a
 * @param repo repository where the blob will be written.
Packit ae9e2a
 *	this repository cannot be bare
Packit ae9e2a
 * @param relative_path file from which the blob will be created,
Packit ae9e2a
 *	relative to the repository's working dir
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_blob_create_fromworkdir(git_oid *id, git_repository *repo, const char *relative_path);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Read a file from the filesystem and write its content
Packit ae9e2a
 * to the Object Database as a loose blob
Packit ae9e2a
 *
Packit ae9e2a
 * @param id return the id of the written blob
Packit ae9e2a
 * @param repo repository where the blob will be written.
Packit ae9e2a
 *	this repository can be bare or not
Packit ae9e2a
 * @param path file from which the blob will be created
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_blob_create_fromdisk(git_oid *id, git_repository *repo, const char *path);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Create a stream to write a new blob into the object db
Packit ae9e2a
 *
Packit ae9e2a
 * This function may need to buffer the data on disk and will in
Packit ae9e2a
 * general not be the right choice if you know the size of the data
Packit ae9e2a
 * to write. If you have data in memory, use
Packit ae9e2a
 * `git_blob_create_frombuffer()`. If you do not, but know the size of
Packit ae9e2a
 * the contents (and don't want/need to perform filtering), use
Packit ae9e2a
 * `git_odb_open_wstream()`.
Packit ae9e2a
 *
Packit ae9e2a
 * Don't close this stream yourself but pass it to
Packit ae9e2a
 * `git_blob_create_fromstream_commit()` to commit the write to the
Packit ae9e2a
 * object db and get the object id.
Packit ae9e2a
 *
Packit ae9e2a
 * If the `hintpath` parameter is filled, it will be used to determine
Packit ae9e2a
 * what git filters should be applied to the object before it is written
Packit ae9e2a
 * to the object database.
Packit ae9e2a
 *
Packit ae9e2a
 * @param out the stream into which to write
Packit ae9e2a
 * @param repo Repository where the blob will be written.
Packit ae9e2a
 *        This repository can be bare or not.
Packit ae9e2a
 * @param hintpath If not NULL, will be used to select data filters
Packit ae9e2a
 *        to apply onto the content of the blob to be created.
Packit ae9e2a
 * @return 0 or error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_blob_create_fromstream(
Packit ae9e2a
	git_writestream **out,
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	const char *hintpath);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Close the stream and write the blob to the object db
Packit ae9e2a
 *
Packit ae9e2a
 * The stream will be closed and freed.
Packit ae9e2a
 *
Packit ae9e2a
 * @param out the id of the new blob
Packit ae9e2a
 * @param stream the stream to close
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_blob_create_fromstream_commit(
Packit ae9e2a
	git_oid *out,
Packit ae9e2a
	git_writestream *stream);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Write an in-memory buffer to the ODB as a blob
Packit ae9e2a
 *
Packit ae9e2a
 * @param id return the id of the written blob
Packit ae9e2a
 * @param repo repository where to blob will be written
Packit ae9e2a
 * @param buffer data to be written into the blob
Packit ae9e2a
 * @param len length of the data
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_blob_create_frombuffer(
Packit ae9e2a
	git_oid *id, git_repository *repo, const void *buffer, size_t len);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Determine if the blob content is most certainly binary or not.
Packit ae9e2a
 *
Packit ae9e2a
 * The heuristic used to guess if a file is binary is taken from core git:
Packit ae9e2a
 * Searching for NUL bytes and looking for a reasonable ratio of printable
Packit ae9e2a
 * to non-printable characters among the first 8000 bytes.
Packit ae9e2a
 *
Packit ae9e2a
 * @param blob The blob which content should be analyzed
Packit ae9e2a
 * @return 1 if the content of the blob is detected
Packit ae9e2a
 * as binary; 0 otherwise.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_blob_is_binary(const git_blob *blob);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Create an in-memory copy of a blob. The copy must be explicitly
Packit ae9e2a
 * free'd or it will leak.
Packit ae9e2a
 *
Packit ae9e2a
 * @param out Pointer to store the copy of the object
Packit ae9e2a
 * @param source Original object to copy
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_blob_dup(git_blob **out, git_blob *source);
Packit ae9e2a
Packit ae9e2a
/** @} */
Packit ae9e2a
GIT_END_DECL
Packit ae9e2a
#endif