Blame include/git2/pack.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_pack_h__
Packit ae9e2a
#define INCLUDE_git_pack_h__
Packit ae9e2a
Packit ae9e2a
#include "common.h"
Packit ae9e2a
#include "oid.h"
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * @file git2/pack.h
Packit ae9e2a
 * @brief Git pack management routines
Packit ae9e2a
 *
Packit ae9e2a
 * Packing objects
Packit ae9e2a
 * ---------------
Packit ae9e2a
 *
Packit ae9e2a
 * Creation of packfiles requires two steps:
Packit ae9e2a
 *
Packit ae9e2a
 * - First, insert all the objects you want to put into the packfile
Packit ae9e2a
 *   using `git_packbuilder_insert` and `git_packbuilder_insert_tree`.
Packit ae9e2a
 *   It's important to add the objects in recency order ("in the order
Packit ae9e2a
 *   that they are 'reachable' from head").
Packit ae9e2a
 *
Packit ae9e2a
 *   "ANY order will give you a working pack, ... [but it is] the thing
Packit ae9e2a
 *   that gives packs good locality. It keeps the objects close to the
Packit ae9e2a
 *   head (whether they are old or new, but they are _reachable_ from the
Packit ae9e2a
 *   head) at the head of the pack. So packs actually have absolutely
Packit ae9e2a
 *   _wonderful_ IO patterns." - Linus Torvalds
Packit ae9e2a
 *   git.git/Documentation/technical/pack-heuristics.txt
Packit ae9e2a
 *
Packit ae9e2a
 * - Second, use `git_packbuilder_write` or `git_packbuilder_foreach` to
Packit ae9e2a
 *   write the resulting packfile.
Packit ae9e2a
 *
Packit ae9e2a
 *   libgit2 will take care of the delta ordering and generation.
Packit ae9e2a
 *   `git_packbuilder_set_threads` can be used to adjust the number of
Packit ae9e2a
 *   threads used for the process.
Packit ae9e2a
 *
Packit ae9e2a
 * See tests/pack/packbuilder.c for an example.
Packit ae9e2a
 *
Packit ae9e2a
 * @ingroup Git
Packit ae9e2a
 * @{
Packit ae9e2a
 */
Packit ae9e2a
GIT_BEGIN_DECL
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Stages that are reported by the packbuilder progress callback.
Packit ae9e2a
 */
Packit ae9e2a
typedef enum {
Packit ae9e2a
	GIT_PACKBUILDER_ADDING_OBJECTS = 0,
Packit ae9e2a
	GIT_PACKBUILDER_DELTAFICATION = 1,
Packit ae9e2a
} git_packbuilder_stage_t;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Initialize a new packbuilder
Packit ae9e2a
 *
Packit ae9e2a
 * @param out The new packbuilder object
Packit ae9e2a
 * @param repo The repository
Packit ae9e2a
 *
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_packbuilder_new(git_packbuilder **out, git_repository *repo);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Set number of threads to spawn
Packit ae9e2a
 *
Packit ae9e2a
 * By default, libgit2 won't spawn any threads at all;
Packit ae9e2a
 * when set to 0, libgit2 will autodetect the number of
Packit ae9e2a
 * CPUs.
Packit ae9e2a
 *
Packit ae9e2a
 * @param pb The packbuilder
Packit ae9e2a
 * @param n Number of threads to spawn
Packit ae9e2a
 * @return number of actual threads to be used
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(unsigned int) git_packbuilder_set_threads(git_packbuilder *pb, unsigned int n);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Insert a single object
Packit ae9e2a
 *
Packit ae9e2a
 * For an optimal pack it's mandatory to insert objects in recency order,
Packit ae9e2a
 * commits followed by trees and blobs.
Packit ae9e2a
 *
Packit ae9e2a
 * @param pb The packbuilder
Packit ae9e2a
 * @param id The oid of the commit
Packit ae9e2a
 * @param name The name; might be NULL
Packit ae9e2a
 *
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_packbuilder_insert(git_packbuilder *pb, const git_oid *id, const char *name);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Insert a root tree object
Packit ae9e2a
 *
Packit ae9e2a
 * This will add the tree as well as all referenced trees and blobs.
Packit ae9e2a
 *
Packit ae9e2a
 * @param pb The packbuilder
Packit ae9e2a
 * @param id The oid of the root tree
Packit ae9e2a
 *
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_packbuilder_insert_tree(git_packbuilder *pb, const git_oid *id);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Insert a commit object
Packit ae9e2a
 *
Packit ae9e2a
 * This will add a commit as well as the completed referenced tree.
Packit ae9e2a
 *
Packit ae9e2a
 * @param pb The packbuilder
Packit ae9e2a
 * @param id The oid of the commit
Packit ae9e2a
 *
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_packbuilder_insert_commit(git_packbuilder *pb, const git_oid *id);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Insert objects as given by the walk
Packit ae9e2a
 *
Packit ae9e2a
 * Those commits and all objects they reference will be inserted into
Packit ae9e2a
 * the packbuilder.
Packit ae9e2a
 *
Packit ae9e2a
 * @param pb the packbuilder
Packit ae9e2a
 * @param walk the revwalk to use to fill the packbuilder
Packit ae9e2a
 *
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_packbuilder_insert_walk(git_packbuilder *pb, git_revwalk *walk);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Recursively insert an object and its referenced objects
Packit ae9e2a
 *
Packit ae9e2a
 * Insert the object as well as any object it references.
Packit ae9e2a
 *
Packit ae9e2a
 * @param pb the packbuilder
Packit ae9e2a
 * @param id the id of the root object to insert
Packit ae9e2a
 * @param name optional name for the object
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_packbuilder_insert_recur(git_packbuilder *pb, const git_oid *id, const char *name);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Write the contents of the packfile to an in-memory buffer
Packit ae9e2a
 *
Packit ae9e2a
 * The contents of the buffer will become a valid packfile, even though there
Packit ae9e2a
 * will be no attached index
Packit ae9e2a
 *
Packit ae9e2a
 * @param buf Buffer where to write the packfile
Packit ae9e2a
 * @param pb The packbuilder
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_packbuilder_write_buf(git_buf *buf, git_packbuilder *pb);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Write the new pack and corresponding index file to path.
Packit ae9e2a
 *
Packit ae9e2a
 * @param pb The packbuilder
Packit ae9e2a
 * @param path to the directory where the packfile and index should be stored
Packit ae9e2a
 * @param mode permissions to use creating a packfile or 0 for defaults
Packit ae9e2a
 * @param progress_cb function to call with progress information from the indexer (optional)
Packit ae9e2a
 * @param progress_cb_payload payload for the progress callback (optional)
Packit ae9e2a
 *
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_packbuilder_write(
Packit ae9e2a
	git_packbuilder *pb,
Packit ae9e2a
	const char *path,
Packit ae9e2a
	unsigned int mode,
Packit ae9e2a
	git_transfer_progress_cb progress_cb,
Packit ae9e2a
	void *progress_cb_payload);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
* Get the packfile's hash
Packit ae9e2a
*
Packit ae9e2a
* A packfile's name is derived from the sorted hashing of all object
Packit ae9e2a
* names. This is only correct after the packfile has been written.
Packit ae9e2a
*
Packit ae9e2a
* @param pb The packbuilder object
Packit ae9e2a
*/
Packit ae9e2a
GIT_EXTERN(const git_oid *) git_packbuilder_hash(git_packbuilder *pb);
Packit ae9e2a
Packit ae9e2a
typedef int (*git_packbuilder_foreach_cb)(void *buf, size_t size, void *payload);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Create the new pack and pass each object to the callback
Packit ae9e2a
 *
Packit ae9e2a
 * @param pb the packbuilder
Packit ae9e2a
 * @param cb the callback to call with each packed object's buffer
Packit ae9e2a
 * @param payload the callback's data
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_packbuilder_foreach(git_packbuilder *pb, git_packbuilder_foreach_cb cb, void *payload);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Get the total number of objects the packbuilder will write out
Packit ae9e2a
 *
Packit ae9e2a
 * @param pb the packbuilder
Packit ae9e2a
 * @return the number of objects in the packfile
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(size_t) git_packbuilder_object_count(git_packbuilder *pb);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Get the number of objects the packbuilder has already written out
Packit ae9e2a
 *
Packit ae9e2a
 * @param pb the packbuilder
Packit ae9e2a
 * @return the number of objects which have already been written
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(size_t) git_packbuilder_written(git_packbuilder *pb);
Packit ae9e2a
Packit ae9e2a
/** Packbuilder progress notification function */
Packit ae9e2a
typedef int (*git_packbuilder_progress)(
Packit ae9e2a
	int stage,
Packit ae9e2a
	uint32_t current,
Packit ae9e2a
	uint32_t total,
Packit ae9e2a
	void *payload);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Set the callbacks for a packbuilder
Packit ae9e2a
 *
Packit ae9e2a
 * @param pb The packbuilder object
Packit ae9e2a
 * @param progress_cb Function to call with progress information during
Packit ae9e2a
 * pack building. Be aware that this is called inline with pack building
Packit ae9e2a
 * operations, so performance may be affected.
Packit ae9e2a
 * @param progress_cb_payload Payload for progress callback.
Packit ae9e2a
 * @return 0 or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_packbuilder_set_callbacks(
Packit ae9e2a
	git_packbuilder *pb,
Packit ae9e2a
	git_packbuilder_progress progress_cb,
Packit ae9e2a
	void *progress_cb_payload);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Free the packbuilder and all associated data
Packit ae9e2a
 *
Packit ae9e2a
 * @param pb The packbuilder
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(void) git_packbuilder_free(git_packbuilder *pb);
Packit ae9e2a
Packit ae9e2a
/** @} */
Packit ae9e2a
GIT_END_DECL
Packit ae9e2a
#endif