diff --git a/git2/frameworks/libgit2.0.17.0.dylib b/git2/frameworks/libgit2.0.17.0.dylib new file mode 100755 index 000000000..1b99207a1 Binary files /dev/null and b/git2/frameworks/libgit2.0.17.0.dylib differ diff --git a/git2/include/git2.h b/git2/include/git2.h new file mode 100644 index 000000000..d75387318 --- /dev/null +++ b/git2/include/git2.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_git_git_h__ +#define INCLUDE_git_git_h__ + +#include "git2/version.h" + +#include "git2/common.h" +#include "git2/threads.h" +#include "git2/errors.h" + +#include "git2/types.h" + +#include "git2/oid.h" +#include "git2/signature.h" +#include "git2/odb.h" + +#include "git2/repository.h" +#include "git2/revwalk.h" +#include "git2/merge.h" +#include "git2/refs.h" +#include "git2/reflog.h" + +#include "git2/object.h" +#include "git2/blob.h" +#include "git2/commit.h" +#include "git2/tag.h" +#include "git2/tree.h" +#include "git2/diff.h" + +#include "git2/index.h" +#include "git2/config.h" +#include "git2/remote.h" + +#include "git2/refspec.h" +#include "git2/net.h" +#include "git2/status.h" +#include "git2/indexer.h" +#include "git2/submodule.h" +#include "git2/notes.h" + +#endif diff --git a/git2/include/git2/attr.h b/git2/include/git2/attr.h new file mode 100644 index 000000000..28ca3bc1c --- /dev/null +++ b/git2/include/git2/attr.h @@ -0,0 +1,224 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_attr_h__ +#define INCLUDE_git_attr_h__ + +#include "common.h" +#include "types.h" + +/** + * @file git2/attr.h + * @brief Git attribute management routines + * @defgroup git_attr Git attribute management routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * GIT_ATTR_TRUE checks if an attribute is set on. In core git + * parlance, this the value for "Set" attributes. + * + * For example, if the attribute file contains: + * + * *.c foo + * + * Then for file `xyz.c` looking up attribute "foo" gives a value for + * which `GIT_ATTR_TRUE(value)` is true. + */ +#define GIT_ATTR_TRUE(attr) ((attr) == git_attr__true) + +/** + * GIT_ATTR_FALSE checks if an attribute is set off. In core git + * parlance, this is the value for attributes that are "Unset" (not to + * be confused with values that a "Unspecified"). + * + * For example, if the attribute file contains: + * + * *.h -foo + * + * Then for file `zyx.h` looking up attribute "foo" gives a value for + * which `GIT_ATTR_FALSE(value)` is true. + */ +#define GIT_ATTR_FALSE(attr) ((attr) == git_attr__false) + +/** + * GIT_ATTR_UNSPECIFIED checks if an attribute is unspecified. This + * may be due to the attribute not being mentioned at all or because + * the attribute was explicitly set unspecified via the `!` operator. + * + * For example, if the attribute file contains: + * + * *.c foo + * *.h -foo + * onefile.c !foo + * + * Then for `onefile.c` looking up attribute "foo" yields a value with + * `GIT_ATTR_UNSPECIFIED(value)` of true. Also, looking up "foo" on + * file `onefile.rb` or looking up "bar" on any file will all give + * `GIT_ATTR_UNSPECIFIED(value)` of true. + */ +#define GIT_ATTR_UNSPECIFIED(attr) (!(attr) || (attr) == git_attr__unset) + +/** + * GIT_ATTR_HAS_VALUE checks if an attribute is set to a value (as + * opposied to TRUE, FALSE or UNSPECIFIED). This would be the case if + * for a file with something like: + * + * *.txt eol=lf + * + * Given this, looking up "eol" for `onefile.txt` will give back the + * string "lf" and `GIT_ATTR_SET_TO_VALUE(attr)` will return true. + */ +#define GIT_ATTR_HAS_VALUE(attr) \ + ((attr) && (attr) != git_attr__unset && \ + (attr) != git_attr__true && (attr) != git_attr__false) + +GIT_EXTERN(const char *) git_attr__true; +GIT_EXTERN(const char *) git_attr__false; +GIT_EXTERN(const char *) git_attr__unset; + +/** + * Check attribute flags: Reading values from index and working directory. + * + * When checking attributes, it is possible to check attribute files + * in both the working directory (if there is one) and the index (if + * there is one). You can explicitly choose where to check and in + * which order using the following flags. + * + * Core git usually checks the working directory then the index, + * except during a checkout when it checks the index first. It will + * use index only for creating archives or for a bare repo (if an + * index has been specified for the bare repo). + */ +#define GIT_ATTR_CHECK_FILE_THEN_INDEX 0 +#define GIT_ATTR_CHECK_INDEX_THEN_FILE 1 +#define GIT_ATTR_CHECK_INDEX_ONLY 2 + +/** + * Check attribute flags: Using the system attributes file. + * + * Normally, attribute checks include looking in the /etc (or system + * equivalent) directory for a `gitattributes` file. Passing this + * flag will cause attribute checks to ignore that file. + */ +#define GIT_ATTR_CHECK_NO_SYSTEM (1 << 2) + +/** + * Look up the value of one git attribute for path. + * + * @param value_out Output of the value of the attribute. Use the GIT_ATTR_... + * macros to test for TRUE, FALSE, UNSPECIFIED, etc. or just + * use the string value for attributes set to a value. You + * should NOT modify or free this value. + * @param repo The repository containing the path. + * @param flags A combination of GIT_ATTR_CHECK... flags. + * @param path The path to check for attributes. Relative paths are + * interpreted relative to the repo root. The file does + * not have to exist, but if it does not, then it will be + * treated as a plain file (not a directory). + * @param name The name of the attribute to look up. + */ +GIT_EXTERN(int) git_attr_get( + const char **value_out, + git_repository *repo, + uint32_t flags, + const char *path, + const char *name); + +/** + * Look up a list of git attributes for path. + * + * Use this if you have a known list of attributes that you want to + * look up in a single call. This is somewhat more efficient than + * calling `git_attr_get()` multiple times. + * + * For example, you might write: + * + * const char *attrs[] = { "crlf", "diff", "foo" }; + * const char **values[3]; + * git_attr_get_many(values, repo, 0, "my/fun/file.c", 3, attrs); + * + * Then you could loop through the 3 values to get the settings for + * the three attributes you asked about. + * + * @param values An array of num_attr entries that will have string + * pointers written into it for the values of the attributes. + * You should not modify or free the values that are written + * into this array (although of course, you should free the + * array itself if you allocated it). + * @param repo The repository containing the path. + * @param flags A combination of GIT_ATTR_CHECK... flags. + * @param path The path inside the repo to check attributes. This + * does not have to exist, but if it does not, then + * it will be treated as a plain file (i.e. not a directory). + * @param num_attr The number of attributes being looked up + * @param names An array of num_attr strings containing attribute names. + */ +GIT_EXTERN(int) git_attr_get_many( + const char **values_out, + git_repository *repo, + uint32_t flags, + const char *path, + size_t num_attr, + const char **names); + +/** + * Loop over all the git attributes for a path. + * + * @param repo The repository containing the path. + * @param flags A combination of GIT_ATTR_CHECK... flags. + * @param path The path inside the repo to check attributes. This + * does not have to exist, but if it does not, then + * it will be treated as a plain file (i.e. not a directory). + * @param callback The function that will be invoked on each attribute + * and attribute value. The name parameter will be the name + * of the attribute and the value will be the value it is + * set to, including possibly NULL if the attribute is + * explicitly set to UNSPECIFIED using the ! sign. This + * will be invoked only once per attribute name, even if + * there are multiple rules for a given file. The highest + * priority rule will be used. + * @param payload Passed on as extra parameter to callback function. + */ +GIT_EXTERN(int) git_attr_foreach( + git_repository *repo, + uint32_t flags, + const char *path, + int (*callback)(const char *name, const char *value, void *payload), + void *payload); + +/** + * Flush the gitattributes cache. + * + * Call this if you have reason to believe that the attributes files on + * disk no longer match the cached contents of memory. This will cause + * the attributes files to be reloaded the next time that an attribute + * access function is called. + */ +GIT_EXTERN(void) git_attr_cache_flush( + git_repository *repo); + +/** + * Add a macro definition. + * + * Macros will automatically be loaded from the top level `.gitattributes` + * file of the repository (plus the build-in "binary" macro). This + * function allows you to add others. For example, to add the default + * macro, you would call: + * + * git_attr_add_macro(repo, "binary", "-diff -crlf"); + */ +GIT_EXTERN(int) git_attr_add_macro( + git_repository *repo, + const char *name, + const char *values); + +/** @} */ +GIT_END_DECL +#endif + diff --git a/git2/include/git2/blob.h b/git2/include/git2/blob.h new file mode 100644 index 000000000..551770678 --- /dev/null +++ b/git2/include/git2/blob.h @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_blob_h__ +#define INCLUDE_git_blob_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" +#include "object.h" + +/** + * @file git2/blob.h + * @brief Git blob load and write routines + * @defgroup git_blob Git blob load and write routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Lookup a blob object from a repository. + * + * @param blob pointer to the looked up blob + * @param repo the repo to use when locating the blob. + * @param id identity of the blob to locate. + * @return 0 or an error code + */ +GIT_INLINE(int) git_blob_lookup(git_blob **blob, git_repository *repo, const git_oid *id) +{ + return git_object_lookup((git_object **)blob, repo, id, GIT_OBJ_BLOB); +} + +/** + * Lookup a blob object from a repository, + * given a prefix of its identifier (short id). + * + * @see git_object_lookup_prefix + * + * @param blob pointer to the looked up blob + * @param repo the repo to use when locating the blob. + * @param id identity of the blob to locate. + * @param len the length of the short identifier + * @return 0 or an error code + */ +GIT_INLINE(int) git_blob_lookup_prefix(git_blob **blob, git_repository *repo, const git_oid *id, unsigned int len) +{ + return git_object_lookup_prefix((git_object **)blob, repo, id, len, GIT_OBJ_BLOB); +} + +/** + * Close an open blob + * + * This is a wrapper around git_object_free() + * + * IMPORTANT: + * It *is* necessary to call this method when you stop + * using a blob. Failure to do so will cause a memory leak. + * + * @param blob the blob to close + */ + +GIT_INLINE(void) git_blob_free(git_blob *blob) +{ + git_object_free((git_object *) blob); +} + + +/** + * Get a read-only buffer with the raw content of a blob. + * + * A pointer to the raw content of a blob is returned; + * this pointer is owned internally by the object and shall + * not be free'd. The pointer may be invalidated at a later + * time. + * + * @param blob pointer to the blob + * @return the pointer; NULL if the blob has no contents + */ +GIT_EXTERN(const void *) git_blob_rawcontent(git_blob *blob); + +/** + * Get the size in bytes of the contents of a blob + * + * @param blob pointer to the blob + * @return size on bytes + */ +GIT_EXTERN(size_t) git_blob_rawsize(git_blob *blob); + +/** + * Read a file from the working folder of a repository + * and write it to the Object Database as a loose blob + * + * @param oid return the id of the written blob + * @param repo repository where the blob will be written. + * this repository cannot be bare + * @param path file from which the blob will be created, + * relative to the repository's working dir + * @return 0 or an error code + */ +GIT_EXTERN(int) git_blob_create_fromfile(git_oid *oid, git_repository *repo, const char *path); + +/** + * Read a file from the filesystem and write its content + * to the Object Database as a loose blob + * + * @param oid return the id of the written blob + * @param repo repository where the blob will be written. + * this repository can be bare or not + * @param path file from which the blob will be created + * @return 0 or an error code + */ +GIT_EXTERN(int) git_blob_create_fromdisk(git_oid *oid, git_repository *repo, const char *path); + + +/** + * Write an in-memory buffer to the ODB as a blob + * + * @param oid return the oid of the written blob + * @param repo repository where to blob will be written + * @param buffer data to be written into the blob + * @param len length of the data + * @return 0 or an error code + */ +GIT_EXTERN(int) git_blob_create_frombuffer(git_oid *oid, git_repository *repo, const void *buffer, size_t len); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/branch.h b/git2/include/git2/branch.h new file mode 100644 index 000000000..e2432bcfc --- /dev/null +++ b/git2/include/git2/branch.h @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_branch_h__ +#define INCLUDE_git_branch_h__ + +#include "common.h" +#include "types.h" + +/** + * @file git2/branch.h + * @brief Git branch parsing routines + * @defgroup git_branch Git branch management + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Create a new branch pointing at a target commit + * + * A new direct reference will be created pointing to + * this target commit. If `force` is true and a reference + * already exists with the given name, it'll be replaced. + * + * @param oid_out Pointer where to store the OID of the target commit. + * + * @param repo Repository where to store the branch. + * + * @param branch_name Name for the branch; this name is + * validated for consistency. It should also not conflict with + * an already existing branch name. + * + * @param target Object to which this branch should point. This object + * must belong to the given `repo` and can either be a git_commit or a + * git_tag. When a git_tag is being passed, it should be dereferencable + * to a git_commit which oid will be used as the target of the branch. + * + * @param force Overwrite existing branch. + * + * @return 0 or an error code. + * A proper reference is written in the refs/heads namespace + * pointing to the provided target commit. + */ +GIT_EXTERN(int) git_branch_create( + git_oid *oid_out, + git_repository *repo, + const char *branch_name, + const git_object *target, + int force); + +/** + * Delete an existing branch reference. + * + * @param repo Repository where lives the branch. + * + * @param branch_name Name of the branch to be deleted; + * this name is validated for consistency. + * + * @param branch_type Type of the considered branch. This should + * be valued with either GIT_BRANCH_LOCAL or GIT_BRANCH_REMOTE. + * + * @return 0 on success, GIT_ENOTFOUND if the branch + * doesn't exist or an error code. + */ +GIT_EXTERN(int) git_branch_delete( + git_repository *repo, + const char *branch_name, + git_branch_t branch_type); + +/** + * Fill a list with all the branches in the Repository + * + * The string array will be filled with the names of the + * matching branches; these values are owned by the user and + * should be free'd manually when no longer needed, using + * `git_strarray_free`. + * + * @param branch_names Pointer to a git_strarray structure + * where the branch names will be stored. + * + * @param repo Repository where to find the branches. + * + * @param list_flags Filtering flags for the branch + * listing. Valid values are GIT_BRANCH_LOCAL, GIT_BRANCH_REMOTE + * or a combination of the two. + * + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_branch_list( + git_strarray *branch_names, + git_repository *repo, + unsigned int list_flags); + +/** + * Move/rename an existing branch reference. + * + * @param repo Repository where lives the branch. + * + * @param old_branch_name Current name of the branch to be moved; + * this name is validated for consistency. + * + * @param new_branch_name Target name of the branch once the move + * is performed; this name is validated for consistency. + * + * @param force Overwrite existing branch. + * + * @return 0 on success, GIT_ENOTFOUND if the branch + * doesn't exist or an error code. + */ +GIT_EXTERN(int) git_branch_move( + git_repository *repo, + const char *old_branch_name, + const char *new_branch_name, + int force); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/commit.h b/git2/include/git2/commit.h new file mode 100644 index 000000000..a6d9bb0e3 --- /dev/null +++ b/git2/include/git2/commit.h @@ -0,0 +1,270 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_commit_h__ +#define INCLUDE_git_commit_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" +#include "object.h" + +/** + * @file git2/commit.h + * @brief Git commit parsing, formatting routines + * @defgroup git_commit Git commit parsing, formatting routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Lookup a commit object from a repository. + * + * @param commit pointer to the looked up commit + * @param repo the repo to use when locating the commit. + * @param id identity of the commit to locate. If the object is + * an annotated tag it will be peeled back to the commit. + * @return 0 or an error code + */ +GIT_INLINE(int) git_commit_lookup(git_commit **commit, git_repository *repo, const git_oid *id) +{ + return git_object_lookup((git_object **)commit, repo, id, GIT_OBJ_COMMIT); +} + +/** + * Lookup a commit object from a repository, + * given a prefix of its identifier (short id). + * + * @see git_object_lookup_prefix + * + * @param commit pointer to the looked up commit + * @param repo the repo to use when locating the commit. + * @param id identity of the commit to locate. If the object is + * an annotated tag it will be peeled back to the commit. + * @param len the length of the short identifier + * @return 0 or an error code + */ +GIT_INLINE(int) git_commit_lookup_prefix(git_commit **commit, git_repository *repo, const git_oid *id, unsigned len) +{ + return git_object_lookup_prefix((git_object **)commit, repo, id, len, GIT_OBJ_COMMIT); +} + +/** + * Close an open commit + * + * This is a wrapper around git_object_free() + * + * IMPORTANT: + * It *is* necessary to call this method when you stop + * using a commit. Failure to do so will cause a memory leak. + * + * @param commit the commit to close + */ + +GIT_INLINE(void) git_commit_free(git_commit *commit) +{ + git_object_free((git_object *) commit); +} + +/** + * Get the id of a commit. + * + * @param commit a previously loaded commit. + * @return object identity for the commit. + */ +GIT_EXTERN(const git_oid *) git_commit_id(git_commit *commit); + +/** + * Get the encoding for the message of a commit, + * as a string representing a standard encoding name. + * + * The encoding may be NULL if the `encoding` header + * in the commit is missing; in that case UTF-8 is assumed. + * + * @param commit a previously loaded commit. + * @return NULL, or the encoding + */ +GIT_EXTERN(const char *) git_commit_message_encoding(git_commit *commit); + +/** + * Get the full message of a commit. + * + * @param commit a previously loaded commit. + * @return the message of a commit + */ +GIT_EXTERN(const char *) git_commit_message(git_commit *commit); + +/** + * Get the commit time (i.e. committer time) of a commit. + * + * @param commit a previously loaded commit. + * @return the time of a commit + */ +GIT_EXTERN(git_time_t) git_commit_time(git_commit *commit); + +/** + * Get the commit timezone offset (i.e. committer's preferred timezone) of a commit. + * + * @param commit a previously loaded commit. + * @return positive or negative timezone offset, in minutes from UTC + */ +GIT_EXTERN(int) git_commit_time_offset(git_commit *commit); + +/** + * Get the committer of a commit. + * + * @param commit a previously loaded commit. + * @return the committer of a commit + */ +GIT_EXTERN(const git_signature *) git_commit_committer(git_commit *commit); + +/** + * Get the author of a commit. + * + * @param commit a previously loaded commit. + * @return the author of a commit + */ +GIT_EXTERN(const git_signature *) git_commit_author(git_commit *commit); + +/** + * Get the tree pointed to by a commit. + * + * @param tree_out pointer where to store the tree object + * @param commit a previously loaded commit. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_commit_tree(git_tree **tree_out, git_commit *commit); + +/** + * Get the id of the tree pointed to by a commit. This differs from + * `git_commit_tree` in that no attempts are made to fetch an object + * from the ODB. + * + * @param commit a previously loaded commit. + * @return the id of tree pointed to by commit. + */ +GIT_EXTERN(const git_oid *) git_commit_tree_oid(git_commit *commit); + +/** + * Get the number of parents of this commit + * + * @param commit a previously loaded commit. + * @return integer of count of parents + */ +GIT_EXTERN(unsigned int) git_commit_parentcount(git_commit *commit); + +/** + * Get the specified parent of the commit. + * + * @param parent Pointer where to store the parent commit + * @param commit a previously loaded commit. + * @param n the position of the parent (from 0 to `parentcount`) + * @return 0 or an error code + */ +GIT_EXTERN(int) git_commit_parent(git_commit **parent, git_commit *commit, unsigned int n); + +/** + * Get the oid of a specified parent for a commit. This is different from + * `git_commit_parent`, which will attempt to load the parent commit from + * the ODB. + * + * @param commit a previously loaded commit. + * @param n the position of the parent (from 0 to `parentcount`) + * @return the id of the parent, NULL on error. + */ +GIT_EXTERN(const git_oid *) git_commit_parent_oid(git_commit *commit, unsigned int n); + +/** + * Create a new commit in the repository using `git_object` + * instances as parameters. + * + * The message will be cleaned up from excess whitespace + * it will be made sure that the last line ends with a '\n'. + * + * @param oid Pointer where to store the OID of the + * newly created commit + * + * @param repo Repository where to store the commit + * + * @param update_ref If not NULL, name of the reference that + * will be updated to point to this commit. If the reference + * is not direct, it will be resolved to a direct reference. + * Use "HEAD" to update the HEAD of the current branch and + * make it point to this commit. If the reference doesn't + * exist yet, it will be created. + * + * @param author Signature representing the author and the authory + * time of this commit + * + * @param committer Signature representing the committer and the + * commit time of this commit + * + * @param message_encoding The encoding for the message in the + * commit, represented with a standard encoding name. + * E.g. "UTF-8". If NULL, no encoding header is written and + * UTF-8 is assumed. + * + * @param message Full message for this commit + * + * @param tree An instance of a `git_tree` object that will + * be used as the tree for the commit. This tree object must + * also be owned by the given `repo`. + * + * @param parent_count Number of parents for this commit + * + * @param parents[] Array of `parent_count` pointers to `git_commit` + * objects that will be used as the parents for this commit. This + * array may be NULL if `parent_count` is 0 (root commit). All the + * given commits must be owned by the `repo`. + * + * @return 0 or an error code + * The created commit will be written to the Object Database and + * the given reference will be updated to point to it + */ +GIT_EXTERN(int) git_commit_create( + git_oid *oid, + git_repository *repo, + const char *update_ref, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_tree *tree, + int parent_count, + const git_commit *parents[]); + +/** + * Create a new commit in the repository using a variable + * argument list. + * + * The message will be cleaned up from excess whitespace + * it will be made sure that the last line ends with a '\n'. + * + * The parents for the commit are specified as a variable + * list of pointers to `const git_commit *`. Note that this + * is a convenience method which may not be safe to export + * for certain languages or compilers + * + * All other parameters remain the same + * + * @see git_commit_create + */ +GIT_EXTERN(int) git_commit_create_v( + git_oid *oid, + git_repository *repo, + const char *update_ref, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_tree *tree, + int parent_count, + ...); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/common.h b/git2/include/git2/common.h new file mode 100644 index 000000000..0e9379804 --- /dev/null +++ b/git2/include/git2/common.h @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_common_h__ +#define INCLUDE_git_common_h__ + +#include +#include + +#ifdef _MSC_VER +# include "inttypes.h" +#else +# include +#endif + +#ifdef __cplusplus +# define GIT_BEGIN_DECL extern "C" { +# define GIT_END_DECL } +#else + /** Start declarations in C mode */ +# define GIT_BEGIN_DECL /* empty */ + /** End declarations in C mode */ +# define GIT_END_DECL /* empty */ +#endif + +/** Declare a public function exported for application use. */ +#if __GNUC__ >= 4 +# define GIT_EXTERN(type) extern \ + __attribute__((visibility("default"))) \ + type +#elif defined(_MSC_VER) +# define GIT_EXTERN(type) __declspec(dllexport) type +#else +# define GIT_EXTERN(type) extern type +#endif + +/** Declare a function as always inlined. */ +#if defined(_MSC_VER) +# define GIT_INLINE(type) static __inline type +#else +# define GIT_INLINE(type) static inline type +#endif + +/** Declare a function's takes printf style arguments. */ +#ifdef __GNUC__ +# define GIT_FORMAT_PRINTF(a,b) __attribute__((format (printf, a, b))) +#else +# define GIT_FORMAT_PRINTF(a,b) /* empty */ +#endif + +#if (defined(_WIN32)) && !defined(__CYGWIN__) +#define GIT_WIN32 1 +#endif + +/** + * @file git2/common.h + * @brief Git common platform definitions + * @defgroup git_common Git common platform definitions + * @ingroup Git + * @{ + */ + +GIT_BEGIN_DECL + +/** + * The separator used in path list strings (ie like in the PATH + * environment variable). A semi-colon ";" is used on Windows, and + * a colon ":" for all other systems. + */ +#ifdef GIT_WIN32 +#define GIT_PATH_LIST_SEPARATOR ';' +#else +#define GIT_PATH_LIST_SEPARATOR ':' +#endif + +/** + * The maximum length of a valid git path. + */ +#define GIT_PATH_MAX 4096 + +typedef struct { + char **strings; + size_t count; +} git_strarray; + +GIT_EXTERN(void) git_strarray_free(git_strarray *array); +GIT_EXTERN(int) git_strarray_copy(git_strarray *tgt, const git_strarray *src); + +/** + * Return the version of the libgit2 library + * being currently used. + * + * @param major Store the major version number + * @param minor Store the minor version number + * @param rev Store the revision (patch) number + */ +GIT_EXTERN(void) git_libgit2_version(int *major, int *minor, int *rev); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/config.h b/git2/include/git2/config.h new file mode 100644 index 000000000..36946c4a5 --- /dev/null +++ b/git2/include/git2/config.h @@ -0,0 +1,356 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_config_h__ +#define INCLUDE_git_config_h__ + +#include "common.h" +#include "types.h" + +/** + * @file git2/config.h + * @brief Git config management routines + * @defgroup git_config Git config management routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Generic backend that implements the interface to + * access a configuration file + */ +struct git_config_file { + struct git_config *cfg; + + /* Open means open the file/database and parse if necessary */ + int (*open)(struct git_config_file *); + int (*get)(struct git_config_file *, const char *key, const char **value); + int (*get_multivar)(struct git_config_file *, const char *key, const char *regexp, int (*fn)(const char *, void *), void *data); + int (*set)(struct git_config_file *, const char *key, const char *value); + int (*set_multivar)(git_config_file *cfg, const char *name, const char *regexp, const char *value); + int (*del)(struct git_config_file *, const char *key); + int (*foreach)(struct git_config_file *, int (*fn)(const char *, const char *, void *), void *data); + void (*free)(struct git_config_file *); +}; + +typedef enum { + GIT_CVAR_FALSE = 0, + GIT_CVAR_TRUE = 1, + GIT_CVAR_INT32, + GIT_CVAR_STRING +} git_cvar_t; + +typedef struct { + git_cvar_t cvar_type; + const char *str_match; + int map_value; +} git_cvar_map; + +/** + * Locate the path to the global configuration file + * + * The user or global configuration file is usually + * located in `$HOME/.gitconfig`. + * + * This method will try to guess the full path to that + * file, if the file exists. The returned path + * may be used on any `git_config` call to load the + * global configuration file. + * + * @param global_config_path Buffer of GIT_PATH_MAX length to store the path + * @return 0 if a global configuration file has been + * found. Its path will be stored in `buffer`. + */ +GIT_EXTERN(int) git_config_find_global(char *global_config_path, size_t length); + +/** + * Locate the path to the system configuration file + * + * If /etc/gitconfig doesn't exist, it will look for + * %PROGRAMFILES%\Git\etc\gitconfig. + + * @param system_config_path Buffer of GIT_PATH_MAX length to store the path + * @return 0 if a system configuration file has been + * found. Its path will be stored in `buffer`. + */ +GIT_EXTERN(int) git_config_find_system(char *system_config_path, size_t length); + +/** + * Open the global configuration file + * + * Utility wrapper that calls `git_config_find_global` + * and opens the located file, if it exists. + * + * @param out Pointer to store the config instance + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_open_global(git_config **out); + +/** + * Create a configuration file backend for ondisk files + * + * These are the normal `.gitconfig` files that Core Git + * processes. Note that you first have to add this file to a + * configuration object before you can query it for configuration + * variables. + * + * @param out the new backend + * @param path where the config file is located + */ +GIT_EXTERN(int) git_config_file__ondisk(struct git_config_file **out, const char *path); + +/** + * Allocate a new configuration object + * + * This object is empty, so you have to add a file to it before you + * can do anything with it. + * + * @param out pointer to the new configuration + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_new(git_config **out); + +/** + * Add a generic config file instance to an existing config + * + * Note that the configuration object will free the file + * automatically. + * + * Further queries on this config object will access each + * of the config file instances in order (instances with + * a higher priority will be accessed first). + * + * @param cfg the configuration to add the file to + * @param file the configuration file (backend) to add + * @param priority the priority the backend should have + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_add_file(git_config *cfg, git_config_file *file, int priority); + +/** + * Add an on-disk config file instance to an existing config + * + * The on-disk file pointed at by `path` will be opened and + * parsed; it's expected to be a native Git config file following + * the default Git config syntax (see man git-config). + * + * Note that the configuration object will free the file + * automatically. + * + * Further queries on this config object will access each + * of the config file instances in order (instances with + * a higher priority will be accessed first). + * + * @param cfg the configuration to add the file to + * @param path path to the configuration file (backend) to add + * @param priority the priority the backend should have + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_add_file_ondisk(git_config *cfg, const char *path, int priority); + + +/** + * Create a new config instance containing a single on-disk file + * + * This method is a simple utility wrapper for the following sequence + * of calls: + * - git_config_new + * - git_config_add_file_ondisk + * + * @param cfg The configuration instance to create + * @param path Path to the on-disk file to open + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_open_ondisk(git_config **cfg, const char *path); + +/** + * Free the configuration and its associated memory and files + * + * @param cfg the configuration to free + */ +GIT_EXTERN(void) git_config_free(git_config *cfg); + +/** + * Get the value of an integer config variable. + * + * @param out pointer to the variable where the value should be stored + * @param cfg where to look for the variable + * @param name the variable's name + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_get_int32(int32_t *out, git_config *cfg, const char *name); + +/** + * Get the value of a long integer config variable. + * + * @param out pointer to the variable where the value should be stored + * @param cfg where to look for the variable + * @param name the variable's name + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_get_int64(int64_t *out, git_config *cfg, const char *name); + +/** + * Get the value of a boolean config variable. + * + * This function uses the usual C convention of 0 being false and + * anything else true. + * + * @param out pointer to the variable where the value should be stored + * @param cfg where to look for the variable + * @param name the variable's name + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_get_bool(int *out, git_config *cfg, const char *name); + +/** + * Get the value of a string config variable. + * + * The string is owned by the variable and should not be freed by the + * user. + * + * @param out pointer to the variable's value + * @param cfg where to look for the variable + * @param name the variable's name + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_get_string(const char **out, git_config *cfg, const char *name); + +/** + * Get each value of a multivar. + * + * The callback will be called on each variable found + * + * @param cfg where to look for the variable + * @param name the variable's name + * @param regexp regular expression to filter which variables we're + * interested in. Use NULL to indicate all + * @param fn the function to be called on each value of the variable + * @param data opaque pointer to pass to the callback + */ +GIT_EXTERN(int) git_config_get_multivar(git_config *cfg, const char *name, const char *regexp, int (*fn)(const char *, void *), void *data); + +/** + * Set the value of an integer config variable. + * + * @param cfg where to look for the variable + * @param name the variable's name + * @param value Integer value for the variable + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_set_int32(git_config *cfg, const char *name, int32_t value); + +/** + * Set the value of a long integer config variable. + * + * @param cfg where to look for the variable + * @param name the variable's name + * @param value Long integer value for the variable + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_set_int64(git_config *cfg, const char *name, int64_t value); + +/** + * Set the value of a boolean config variable. + * + * @param cfg where to look for the variable + * @param name the variable's name + * @param value the value to store + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_set_bool(git_config *cfg, const char *name, int value); + +/** + * Set the value of a string config variable. + * + * A copy of the string is made and the user is free to use it + * afterwards. + * + * @param cfg where to look for the variable + * @param name the variable's name + * @param value the string to store. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_set_string(git_config *cfg, const char *name, const char *value); + + +/** + * Set a multivar + * + * @param cfg where to look for the variable + * @param name the variable's name + * @param regexp a regular expression to indicate which values to replace + * @param value the new value. + */ +GIT_EXTERN(int) git_config_set_multivar(git_config *cfg, const char *name, const char *regexp, const char *value); + +/** + * Delete a config variable + * + * @param cfg the configuration + * @param name the variable to delete + */ +GIT_EXTERN(int) git_config_delete(git_config *cfg, const char *name); + +/** + * Perform an operation on each config variable. + * + * The callback receives the normalized name and value of each variable + * in the config backend, and the data pointer passed to this function. + * As soon as one of the callback functions returns something other than 0, + * this function returns that value. + * + * @param cfg where to get the variables from + * @param callback the function to call on each variable + * @param payload the data to pass to the callback + * @return 0 or the return value of the callback which didn't return 0 + */ +GIT_EXTERN(int) git_config_foreach( + git_config *cfg, + int (*callback)(const char *var_name, const char *value, void *payload), + void *payload); + + +/** + * Query the value of a config variable and return it mapped to + * an integer constant. + * + * This is a helper method to easily map different possible values + * to a variable to integer constants that easily identify them. + * + * A mapping array looks as follows: + * + * git_cvar_map autocrlf_mapping[3] = { + * {GIT_CVAR_FALSE, NULL, GIT_AUTO_CRLF_FALSE}, + * {GIT_CVAR_TRUE, NULL, GIT_AUTO_CRLF_TRUE}, + * {GIT_CVAR_STRING, "input", GIT_AUTO_CRLF_INPUT}, + * {GIT_CVAR_STRING, "default", GIT_AUTO_CRLF_DEFAULT}}; + * + * On any "false" value for the variable (e.g. "false", "FALSE", "no"), the + * mapping will store `GIT_AUTO_CRLF_FALSE` in the `out` parameter. + * + * The same thing applies for any "true" value such as "true", "yes" or "1", storing + * the `GIT_AUTO_CRLF_TRUE` variable. + * + * Otherwise, if the value matches the string "input" (with case insensitive comparison), + * the given constant will be stored in `out`, and likewise for "default". + * + * If not a single match can be made to store in `out`, an error code will be + * returned. + * + * @param out place to store the result of the mapping + * @param cfg config file to get the variables from + * @param name name of the config variable to lookup + * @param maps array of `git_cvar_map` objects specifying the possible mappings + * @param map_n number of mapping objects in `maps` + * @return 0 on success, error code otherwise + */ +GIT_EXTERN(int) git_config_get_mapped(int *out, git_config *cfg, const char *name, git_cvar_map *maps, size_t map_n); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/diff.h b/git2/include/git2/diff.h new file mode 100644 index 000000000..bafe6268c --- /dev/null +++ b/git2/include/git2/diff.h @@ -0,0 +1,362 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_diff_h__ +#define INCLUDE_git_diff_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" +#include "tree.h" +#include "refs.h" + +/** + * @file git2/diff.h + * @brief Git tree and file differencing routines. + * + * Calculating diffs is generally done in two phases: building a diff list + * then traversing the diff list. This makes is easier to share logic + * across the various types of diffs (tree vs tree, workdir vs index, etc.), + * and also allows you to insert optional diff list post-processing phases, + * such as rename detected, in between the steps. When you are done with a + * diff list object, it must be freed. + * + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +enum { + GIT_DIFF_NORMAL = 0, + GIT_DIFF_REVERSE = (1 << 0), + GIT_DIFF_FORCE_TEXT = (1 << 1), + GIT_DIFF_IGNORE_WHITESPACE = (1 << 2), + GIT_DIFF_IGNORE_WHITESPACE_CHANGE = (1 << 3), + GIT_DIFF_IGNORE_WHITESPACE_EOL = (1 << 4), + GIT_DIFF_IGNORE_SUBMODULES = (1 << 5), + GIT_DIFF_PATIENCE = (1 << 6), + GIT_DIFF_INCLUDE_IGNORED = (1 << 7), + GIT_DIFF_INCLUDE_UNTRACKED = (1 << 8), + GIT_DIFF_INCLUDE_UNMODIFIED = (1 << 9), + GIT_DIFF_RECURSE_UNTRACKED_DIRS = (1 << 10), +}; + +/** + * Structure describing options about how the diff should be executed. + * + * Setting all values of the structure to zero will yield the default + * values. Similarly, passing NULL for the options structure will + * give the defaults. The default values are marked below. + * + * @todo Most of the parameters here are not actually supported at this time. + */ +typedef struct { + uint32_t flags; /**< defaults to GIT_DIFF_NORMAL */ + uint16_t context_lines; /**< defaults to 3 */ + uint16_t interhunk_lines; /**< defaults to 3 */ + char *old_prefix; /**< defaults to "a" */ + char *new_prefix; /**< defaults to "b" */ + git_strarray pathspec; /**< defaults to show all paths */ +} git_diff_options; + +/** + * The diff list object that contains all individual file deltas. + */ +typedef struct git_diff_list git_diff_list; + +enum { + GIT_DIFF_FILE_VALID_OID = (1 << 0), + GIT_DIFF_FILE_FREE_PATH = (1 << 1), + GIT_DIFF_FILE_BINARY = (1 << 2), + GIT_DIFF_FILE_NOT_BINARY = (1 << 3), + GIT_DIFF_FILE_FREE_DATA = (1 << 4), + GIT_DIFF_FILE_UNMAP_DATA = (1 << 5) +}; + +/** + * What type of change is described by a git_diff_delta? + */ +typedef enum { + GIT_DELTA_UNMODIFIED = 0, + GIT_DELTA_ADDED = 1, + GIT_DELTA_DELETED = 2, + GIT_DELTA_MODIFIED = 3, + GIT_DELTA_RENAMED = 4, + GIT_DELTA_COPIED = 5, + GIT_DELTA_IGNORED = 6, + GIT_DELTA_UNTRACKED = 7 +} git_delta_t; + +/** + * Description of one side of a diff. + */ +typedef struct { + git_oid oid; + char *path; + uint16_t mode; + git_off_t size; + unsigned int flags; +} git_diff_file; + +/** + * Description of changes to one file. + * + * When iterating over a diff list object, this will generally be passed to + * most callback functions and you can use the contents to understand + * exactly what has changed. + * + * Under some circumstances, not all fields will be filled in, but the code + * generally tries to fill in as much as possible. One example is that the + * "binary" field will not actually look at file contents if you do not + * pass in hunk and/or line callbacks to the diff foreach iteration function. + * It will just use the git attributes for those files. + */ +typedef struct { + git_diff_file old_file; + git_diff_file new_file; + git_delta_t status; + unsigned int similarity; /**< for RENAMED and COPIED, value 0-100 */ + int binary; +} git_diff_delta; + +/** + * When iterating over a diff, callback that will be made per file. + */ +typedef int (*git_diff_file_fn)( + void *cb_data, + git_diff_delta *delta, + float progress); + +/** + * Structure describing a hunk of a diff. + */ +typedef struct { + int old_start; + int old_lines; + int new_start; + int new_lines; +} git_diff_range; + +/** + * When iterating over a diff, callback that will be made per hunk. + */ +typedef int (*git_diff_hunk_fn)( + void *cb_data, + git_diff_delta *delta, + git_diff_range *range, + const char *header, + size_t header_len); + +/** + * Line origin constants. + * + * These values describe where a line came from and will be passed to + * the git_diff_data_fn when iterating over a diff. There are some + * special origin contants at the end that are used for the text + * output callbacks to demarcate lines that are actually part of + * the file or hunk headers. + */ +enum { + /* these values will be sent to `git_diff_data_fn` along with the line */ + GIT_DIFF_LINE_CONTEXT = ' ', + GIT_DIFF_LINE_ADDITION = '+', + GIT_DIFF_LINE_DELETION = '-', + GIT_DIFF_LINE_ADD_EOFNL = '\n', /**< LF was added at end of file */ + GIT_DIFF_LINE_DEL_EOFNL = '\0', /**< LF was removed at end of file */ + /* these values will only be sent to a `git_diff_data_fn` when the content + * of a diff is being formatted (eg. through git_diff_print_patch() or + * git_diff_print_compact(), for instance). + */ + GIT_DIFF_LINE_FILE_HDR = 'F', + GIT_DIFF_LINE_HUNK_HDR = 'H', + GIT_DIFF_LINE_BINARY = 'B' +}; + +/** + * When iterating over a diff, callback that will be made per text diff + * line. In this context, the provided range will be NULL. + * + * When printing a diff, callback that will be made to output each line + * of text. This uses some extra GIT_DIFF_LINE_... constants for output + * of lines of file and hunk headers. + */ +typedef int (*git_diff_data_fn)( + void *cb_data, + git_diff_delta *delta, + git_diff_range *range, + char line_origin, /**< GIT_DIFF_LINE_... value from above */ + const char *content, + size_t content_len); + +/** @name Diff List Generator Functions + * + * These are the functions you would use to create (or destroy) a + * git_diff_list from various objects in a repository. + */ +/**@{*/ + +/** + * Deallocate a diff list. + */ +GIT_EXTERN(void) git_diff_list_free(git_diff_list *diff); + +/** + * Compute a difference between two tree objects. + * + * @param repo The repository containing the trees. + * @param opts Structure with options to influence diff or NULL for defaults. + * @param old_tree A git_tree object to diff from. + * @param new_tree A git_tree object to diff to. + * @param diff A pointer to a git_diff_list pointer that will be allocated. + */ +GIT_EXTERN(int) git_diff_tree_to_tree( + git_repository *repo, + const git_diff_options *opts, /**< can be NULL for defaults */ + git_tree *old_tree, + git_tree *new_tree, + git_diff_list **diff); + +/** + * Compute a difference between a tree and the index. + * + * @param repo The repository containing the tree and index. + * @param opts Structure with options to influence diff or NULL for defaults. + * @param old_tree A git_tree object to diff from. + * @param diff A pointer to a git_diff_list pointer that will be allocated. + */ +GIT_EXTERN(int) git_diff_index_to_tree( + git_repository *repo, + const git_diff_options *opts, /**< can be NULL for defaults */ + git_tree *old_tree, + git_diff_list **diff); + +/** + * Compute a difference between the working directory and the index. + * + * @param repo The repository. + * @param opts Structure with options to influence diff or NULL for defaults. + * @param diff A pointer to a git_diff_list pointer that will be allocated. + */ +GIT_EXTERN(int) git_diff_workdir_to_index( + git_repository *repo, + const git_diff_options *opts, /**< can be NULL for defaults */ + git_diff_list **diff); + +/** + * Compute a difference between the working directory and a tree. + * + * This returns strictly the differences between the tree and the + * files contained in the working directory, regardless of the state + * of files in the index. There is no direct equivalent in C git. + * + * This is *NOT* the same as 'git diff HEAD' or 'git diff '. Those + * commands diff the tree, the index, and the workdir. To emulate those + * functions, call `git_diff_index_to_tree` and `git_diff_workdir_to_index`, + * then call `git_diff_merge` on the results. + * + * @param repo The repository containing the tree. + * @param opts Structure with options to influence diff or NULL for defaults. + * @param old_tree A git_tree object to diff from. + * @param diff A pointer to a git_diff_list pointer that will be allocated. + */ +GIT_EXTERN(int) git_diff_workdir_to_tree( + git_repository *repo, + const git_diff_options *opts, /**< can be NULL for defaults */ + git_tree *old_tree, + git_diff_list **diff); + +/** + * Merge one diff list into another. + * + * This merges items from the "from" list into the "onto" list. The + * resulting diff list will have all items that appear in either list. + * If an item appears in both lists, then it will be "merged" to appear + * as if the old version was from the "onto" list and the new version + * is from the "from" list (with the exception that if the item has a + * pending DELETE in the middle, then it will show as deleted). + * + * @param onto Diff to merge into. + * @param from Diff to merge. + */ +GIT_EXTERN(int) git_diff_merge( + git_diff_list *onto, + const git_diff_list *from); + +/**@}*/ + + +/** @name Diff List Processor Functions + * + * These are the functions you apply to a diff list to process it + * or read it in some way. + */ +/**@{*/ + +/** + * Iterate over a diff list issuing callbacks. + * + * If the hunk and/or line callbacks are not NULL, then this will calculate + * text diffs for all files it thinks are not binary. If those are both + * NULL, then this will not bother with the text diffs, so it can be + * efficient. + */ +GIT_EXTERN(int) git_diff_foreach( + git_diff_list *diff, + void *cb_data, + git_diff_file_fn file_cb, + git_diff_hunk_fn hunk_cb, + git_diff_data_fn line_cb); + +/** + * Iterate over a diff generating text output like "git diff --name-status". + */ +GIT_EXTERN(int) git_diff_print_compact( + git_diff_list *diff, + void *cb_data, + git_diff_data_fn print_cb); + +/** + * Iterate over a diff generating text output like "git diff". + * + * This is a super easy way to generate a patch from a diff. + */ +GIT_EXTERN(int) git_diff_print_patch( + git_diff_list *diff, + void *cb_data, + git_diff_data_fn print_cb); + +/**@}*/ + + +/* + * Misc + */ + +/** + * Directly run a text diff on two blobs. + * + * Compared to a file, a blob lacks some contextual information. As such, the + * `git_diff_file` parameters of the callbacks will be filled accordingly to the following: + * `mode` will be set to 0, `path` will be set to NULL. When dealing with a NULL blob, `oid` + * will be set to 0. + * + * When at least one of the blobs being dealt with is binary, the `git_diff_delta` binary + * attribute will be set to 1 and no call to the hunk_cb nor line_cb will be made. + */ +GIT_EXTERN(int) git_diff_blobs( + git_blob *old_blob, + git_blob *new_blob, + git_diff_options *options, + void *cb_data, + git_diff_file_fn file_cb, + git_diff_hunk_fn hunk_cb, + git_diff_data_fn line_cb); + +GIT_END_DECL + +/** @} */ + +#endif diff --git a/git2/include/git2/errors.h b/git2/include/git2/errors.h new file mode 100644 index 000000000..fb6670004 --- /dev/null +++ b/git2/include/git2/errors.h @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_errors_h__ +#define INCLUDE_git_errors_h__ + +#include "common.h" + +/** + * @file git2/errors.h + * @brief Git error handling routines and variables + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +#ifdef GIT_OLD_ERRORS +enum { + GIT_SUCCESS = 0, + GIT_ENOTOID = -2, + GIT_ENOTFOUND = -3, + GIT_ENOMEM = -4, + GIT_EOSERR = -5, + GIT_EOBJTYPE = -6, + GIT_ENOTAREPO = -7, + GIT_EINVALIDTYPE = -8, + GIT_EMISSINGOBJDATA = -9, + GIT_EPACKCORRUPTED = -10, + GIT_EFLOCKFAIL = -11, + GIT_EZLIB = -12, + GIT_EBUSY = -13, + GIT_EBAREINDEX = -14, + GIT_EINVALIDREFNAME = -15, + GIT_EREFCORRUPTED = -16, + GIT_ETOONESTEDSYMREF = -17, + GIT_EPACKEDREFSCORRUPTED = -18, + GIT_EINVALIDPATH = -19, + GIT_EREVWALKOVER = -20, + GIT_EINVALIDREFSTATE = -21, + GIT_ENOTIMPLEMENTED = -22, + GIT_EEXISTS = -23, + GIT_EOVERFLOW = -24, + GIT_ENOTNUM = -25, + GIT_ESTREAM = -26, + GIT_EINVALIDARGS = -27, + GIT_EOBJCORRUPTED = -28, + GIT_EAMBIGUOUS = -29, + GIT_EPASSTHROUGH = -30, + GIT_ENOMATCH = -31, + GIT_ESHORTBUFFER = -32, +}; +#endif + +/** Generic return codes */ +enum { + GIT_OK = 0, + GIT_ERROR = -1, + GIT_ENOTFOUND = -3, + GIT_EEXISTS = -4, + GIT_EAMBIGUOUS = -5, + GIT_EBUFS = -6, + + GIT_PASSTHROUGH = -30, + GIT_REVWALKOVER = -31, +}; + +typedef struct { + char *message; + int klass; +} git_error; + +typedef enum { + GITERR_NOMEMORY, + GITERR_OS, + GITERR_INVALID, + GITERR_REFERENCE, + GITERR_ZLIB, + GITERR_REPOSITORY, + GITERR_CONFIG, + GITERR_REGEX, + GITERR_ODB, + GITERR_INDEX, + GITERR_OBJECT, + GITERR_NET, + GITERR_TAG, + GITERR_TREE, + GITERR_INDEXER, +} git_error_t; + +/** + * Return the last `git_error` object that was generated for the + * current thread or NULL if no error has occurred. + * + * @return A git_error object. + */ +GIT_EXTERN(const git_error *) giterr_last(void); + +/** + * Clear the last library error that occurred for this thread. + */ +GIT_EXTERN(void) giterr_clear(void); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/index.h b/git2/include/git2/index.h new file mode 100644 index 000000000..6a42c8515 --- /dev/null +++ b/git2/include/git2/index.h @@ -0,0 +1,321 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_index_h__ +#define INCLUDE_git_index_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" + +/** + * @file git2/index.h + * @brief Git index parsing and manipulation routines + * @defgroup git_index Git index parsing and manipulation routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +#define GIT_IDXENTRY_NAMEMASK (0x0fff) +#define GIT_IDXENTRY_STAGEMASK (0x3000) +#define GIT_IDXENTRY_EXTENDED (0x4000) +#define GIT_IDXENTRY_VALID (0x8000) +#define GIT_IDXENTRY_STAGESHIFT 12 + +/* + * Flags are divided into two parts: in-memory flags and + * on-disk ones. Flags in GIT_IDXENTRY_EXTENDED_FLAGS + * will get saved on-disk. + * + * In-memory only flags: + */ +#define GIT_IDXENTRY_UPDATE (1 << 0) +#define GIT_IDXENTRY_REMOVE (1 << 1) +#define GIT_IDXENTRY_UPTODATE (1 << 2) +#define GIT_IDXENTRY_ADDED (1 << 3) + +#define GIT_IDXENTRY_HASHED (1 << 4) +#define GIT_IDXENTRY_UNHASHED (1 << 5) +#define GIT_IDXENTRY_WT_REMOVE (1 << 6) /* remove in work directory */ +#define GIT_IDXENTRY_CONFLICTED (1 << 7) + +#define GIT_IDXENTRY_UNPACKED (1 << 8) +#define GIT_IDXENTRY_NEW_SKIP_WORKTREE (1 << 9) + +/* + * Extended on-disk flags: + */ +#define GIT_IDXENTRY_INTENT_TO_ADD (1 << 13) +#define GIT_IDXENTRY_SKIP_WORKTREE (1 << 14) +/* GIT_IDXENTRY_EXTENDED2 is for future extension */ +#define GIT_IDXENTRY_EXTENDED2 (1 << 15) + +#define GIT_IDXENTRY_EXTENDED_FLAGS (GIT_IDXENTRY_INTENT_TO_ADD | GIT_IDXENTRY_SKIP_WORKTREE) + +/** Time used in a git index entry */ +typedef struct { + git_time_t seconds; + /* nsec should not be stored as time_t compatible */ + unsigned int nanoseconds; +} git_index_time; + +/** Memory representation of a file entry in the index. */ +typedef struct git_index_entry { + git_index_time ctime; + git_index_time mtime; + + unsigned int dev; + unsigned int ino; + unsigned int mode; + unsigned int uid; + unsigned int gid; + git_off_t file_size; + + git_oid oid; + + unsigned short flags; + unsigned short flags_extended; + + char *path; +} git_index_entry; + +/** Representation of an unmerged file entry in the index. */ +typedef struct git_index_entry_unmerged { + unsigned int mode[3]; + git_oid oid[3]; + char *path; +} git_index_entry_unmerged; + +/** + * Create a new bare Git index object as a memory representation + * of the Git index file in 'index_path', without a repository + * to back it. + * + * Since there is no ODB or working directory behind this index, + * any Index methods which rely on these (e.g. index_add) will + * fail with the GIT_EBAREINDEX error code. + * + * If you need to access the index of an actual repository, + * use the `git_repository_index` wrapper. + * + * The index must be freed once it's no longer in use. + * + * @param index the pointer for the new index + * @param index_path the path to the index file in disk + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_open(git_index **index, const char *index_path); + +/** + * Clear the contents (all the entries) of an index object. + * This clears the index object in memory; changes must be manually + * written to disk for them to take effect. + * + * @param index an existing index object + */ +GIT_EXTERN(void) git_index_clear(git_index *index); + +/** + * Free an existing index object. + * + * @param index an existing index object + */ +GIT_EXTERN(void) git_index_free(git_index *index); + +/** + * Update the contents of an existing index object in memory + * by reading from the hard disk. + * + * @param index an existing index object + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_read(git_index *index); + +/** + * Write an existing index object from memory back to disk + * using an atomic file lock. + * + * @param index an existing index object + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_write(git_index *index); + +/** + * Find the first index of any entries which point to given + * path in the Git index. + * + * @param index an existing index object + * @param path path to search + * @return an index >= 0 if found, -1 otherwise + */ +GIT_EXTERN(int) git_index_find(git_index *index, const char *path); + +/** + * Remove all entries with equal path except last added + * + * @param index an existing index object + */ +GIT_EXTERN(void) git_index_uniq(git_index *index); + +/** + * Add or update an index entry from a file in disk + * + * The file `path` must be relative to the repository's + * working folder and must be readable. + * + * This method will fail in bare index instances. + * + * This forces the file to be added to the index, not looking + * at gitignore rules. Those rules can be evaluated through + * the git_status APIs (in status.h) before calling this. + * + * @param index an existing index object + * @param path filename to add + * @param stage stage for the entry + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_add(git_index *index, const char *path, int stage); + +/** + * Add or update an index entry from an in-memory struct + * + * A full copy (including the 'path' string) of the given + * 'source_entry' will be inserted on the index. + * + * @param index an existing index object + * @param source_entry new entry object + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_add2(git_index *index, const git_index_entry *source_entry); + +/** + * Add (append) an index entry from a file in disk + * + * A new entry will always be inserted into the index; + * if the index already contains an entry for such + * path, the old entry will **not** be replaced. + * + * The file `path` must be relative to the repository's + * working folder and must be readable. + * + * This method will fail in bare index instances. + * + * @param index an existing index object + * @param path filename to add + * @param stage stage for the entry + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_append(git_index *index, const char *path, int stage); + +/** + * Add (append) an index entry from an in-memory struct + * + * A new entry will always be inserted into the index; + * if the index already contains an entry for the path + * in the `entry` struct, the old entry will **not** be + * replaced. + * + * A full copy (including the 'path' string) of the given + * 'source_entry' will be inserted on the index. + * + * @param index an existing index object + * @param source_entry new entry object + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_append2(git_index *index, const git_index_entry *source_entry); + +/** + * Remove an entry from the index + * + * @param index an existing index object + * @param position position of the entry to remove + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_remove(git_index *index, int position); + + +/** + * Get a pointer to one of the entries in the index + * + * This entry can be modified, and the changes will be written + * back to disk on the next write() call. + * + * The entry should not be freed by the caller. + * + * @param index an existing index object + * @param n the position of the entry + * @return a pointer to the entry; NULL if out of bounds + */ +GIT_EXTERN(git_index_entry *) git_index_get(git_index *index, unsigned int n); + +/** + * Get the count of entries currently in the index + * + * @param index an existing index object + * @return integer of count of current entries + */ +GIT_EXTERN(unsigned int) git_index_entrycount(git_index *index); + +/** + * Get the count of unmerged entries currently in the index + * + * @param index an existing index object + * @return integer of count of current unmerged entries + */ +GIT_EXTERN(unsigned int) git_index_entrycount_unmerged(git_index *index); + +/** + * Get an unmerged entry from the index. + * + * The returned entry is read-only and should not be modified + * of freed by the caller. + * + * @param index an existing index object + * @param path path to search + * @return the unmerged entry; NULL if not found + */ +GIT_EXTERN(const git_index_entry_unmerged *) git_index_get_unmerged_bypath(git_index *index, const char *path); + +/** + * Get an unmerged entry from the index. + * + * The returned entry is read-only and should not be modified + * of freed by the caller. + * + * @param index an existing index object + * @param n the position of the entry + * @return a pointer to the unmerged entry; NULL if out of bounds + */ +GIT_EXTERN(const git_index_entry_unmerged *) git_index_get_unmerged_byindex(git_index *index, unsigned int n); + +/** + * Return the stage number from a git index entry + * + * This entry is calculated from the entrie's flag + * attribute like this: + * + * (entry->flags & GIT_IDXENTRY_STAGEMASK) >> GIT_IDXENTRY_STAGESHIFT + * + * @param entry The entry + * @returns the stage number + */ +GIT_EXTERN(int) git_index_entry_stage(const git_index_entry *entry); + +/** + * Read a tree into the index file + * + * The current index contents will be replaced by the specified tree. + * + * @param index an existing index object + * @param tree tree to read + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_read_tree(git_index *index, git_tree *tree); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/indexer.h b/git2/include/git2/indexer.h new file mode 100644 index 000000000..14bd0e402 --- /dev/null +++ b/git2/include/git2/indexer.h @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef _INCLUDE_git_indexer_h__ +#define _INCLUDE_git_indexer_h__ + +#include "common.h" +#include "oid.h" + +GIT_BEGIN_DECL + +/** + * This is passed as the first argument to the callback to allow the + * user to see the progress. + */ +typedef struct git_indexer_stats { + unsigned int total; + unsigned int processed; +} git_indexer_stats; + + +typedef struct git_indexer git_indexer; +typedef struct git_indexer_stream git_indexer_stream; + +/** + * Create a new streaming indexer instance + * + * @param out where to store the inexer instance + * @param path to the gitdir (metadata directory) + */ +GIT_EXTERN(int) git_indexer_stream_new(git_indexer_stream **out, const char *gitdir); + +/** + * Add data to the indexer + * + * @param idx the indexer + * @param data the data to add + * @param size the size of the data + * @param stats stat storage + */ +GIT_EXTERN(int) git_indexer_stream_add(git_indexer_stream *idx, const void *data, size_t size, git_indexer_stats *stats); + +/** + * Finalize the pack and index + * + * Resolve any pending deltas and write out the index file + * + * @param idx the indexer + */ +GIT_EXTERN(int) git_indexer_stream_finalize(git_indexer_stream *idx, git_indexer_stats *stats); + +/** + * Get the packfile's hash + * + * A packfile's name is derived from the sorted hashing of all object + * names. This is only correct after the index has been finalized. + * + * @param idx the indexer instance + */ +GIT_EXTERN(const git_oid *) git_indexer_stream_hash(git_indexer_stream *idx); + +/** + * Free the indexer and its resources + * + * @param idx the indexer to free + */ +GIT_EXTERN(void) git_indexer_stream_free(git_indexer_stream *idx); + +/** + * Create a new indexer instance + * + * @param out where to store the indexer instance + * @param packname the absolute filename of the packfile to index + */ +GIT_EXTERN(int) git_indexer_new(git_indexer **out, const char *packname); + +/** + * Iterate over the objects in the packfile and extract the information + * + * Indexing a packfile can be very expensive so this function is + * expected to be run in a worker thread and the stats used to provide + * feedback the user. + * + * @param idx the indexer instance + * @param stats storage for the running state + */ +GIT_EXTERN(int) git_indexer_run(git_indexer *idx, git_indexer_stats *stats); + +/** + * Write the index file to disk. + * + * The file will be stored as pack-$hash.idx in the same directory as + * the packfile. + * + * @param idx the indexer instance + */ +GIT_EXTERN(int) git_indexer_write(git_indexer *idx); + +/** + * Get the packfile's hash + * + * A packfile's name is derived from the sorted hashing of all object + * names. This is only correct after the index has been written to disk. + * + * @param idx the indexer instance + */ +GIT_EXTERN(const git_oid *) git_indexer_hash(git_indexer *idx); + +/** + * Free the indexer and its resources + * + * @param idx the indexer to free + */ +GIT_EXTERN(void) git_indexer_free(git_indexer *idx); + +GIT_END_DECL + +#endif diff --git a/git2/include/git2/merge.h b/git2/include/git2/merge.h new file mode 100644 index 000000000..5a0b2e7f2 --- /dev/null +++ b/git2/include/git2/merge.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_merge_h__ +#define INCLUDE_git_merge_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" + +/** + * @file git2/merge.h + * @brief Git merge-base routines + * @defgroup git_revwalk Git merge-base routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Find a merge base between two commits + * + * @param out the OID of a merge base between 'one' and 'two' + * @param repo the repository where the commits exist + * @param one one of the commits + * @param two the other commit + */ +GIT_EXTERN(int) git_merge_base(git_oid *out, git_repository *repo, git_oid *one, git_oid *two); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/net.h b/git2/include/git2/net.h new file mode 100644 index 000000000..c2301b6f1 --- /dev/null +++ b/git2/include/git2/net.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_net_h__ +#define INCLUDE_net_h__ + +#include "common.h" +#include "oid.h" +#include "types.h" + +/** + * @file git2/net.h + * @brief Git networking declarations + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +#define GIT_DEFAULT_PORT "9418" + +/* + * We need this because we need to know whether we should call + * git-upload-pack or git-receive-pack on the remote end when get_refs + * gets called. + */ + +#define GIT_DIR_FETCH 0 +#define GIT_DIR_PUSH 1 + + +/** + * Remote head description, given out on `ls` calls. + */ +struct git_remote_head { + int local:1; /* available locally */ + git_oid oid; + git_oid loid; + char *name; +}; + +/** + * Callback for listing the remote heads + */ +typedef int (*git_headlist_cb)(git_remote_head *, void *); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/notes.h b/git2/include/git2/notes.h new file mode 100644 index 000000000..19073abd1 --- /dev/null +++ b/git2/include/git2/notes.h @@ -0,0 +1,139 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_note_h__ +#define INCLUDE_git_note_h__ + +#include "oid.h" + +/** + * @file git2/notes.h + * @brief Git notes management routines + * @defgroup git_note Git notes management routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Read the note for an object + * + * The note must be freed manually by the user. + * + * @param note the note; NULL in case of error + * @param repo the Git repository + * @param notes_ref OID reference to use (optional); defaults to "refs/notes/commits" + * @param oid OID of the object + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_note_read(git_note **note, git_repository *repo, + const char *notes_ref, const git_oid *oid); + +/** + * Get the note message + * + * @param note + * @return the note message + */ +GIT_EXTERN(const char *) git_note_message(git_note *note); + + +/** + * Get the note object OID + * + * @param note + * @return the note object OID + */ +GIT_EXTERN(const git_oid *) git_note_oid(git_note *note); + + +/** + * Add a note for an object + * + * @param oid pointer to store the OID (optional); NULL in case of error + * @param repo the Git repository + * @param author signature of the notes commit author + * @param committer signature of the notes commit committer + * @param notes_ref OID reference to update (optional); defaults to "refs/notes/commits" + * @param oid The OID of the object + * @param oid The note to add for object oid + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_note_create(git_oid *out, git_repository *repo, + git_signature *author, git_signature *committer, + const char *notes_ref, const git_oid *oid, + const char *note); + + +/** + * Remove the note for an object + * + * @param repo the Git repository + * @param notes_ref OID reference to use (optional); defaults to "refs/notes/commits" + * @param author signature of the notes commit author + * @param committer signature of the notes commit committer + * @param oid the oid which note's to be removed + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_note_remove(git_repository *repo, const char *notes_ref, + git_signature *author, git_signature *committer, + const git_oid *oid); + +/** + * Free a git_note object + * + * @param note git_note object + */ +GIT_EXTERN(void) git_note_free(git_note *note); + +/** + * Get the default notes reference for a repository + * + * @param out Pointer to the default notes reference + * @param repo The Git repository + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_note_default_ref(const char **out, git_repository *repo); + +/** + * Basic components of a note + * + * - Oid of the blob containing the message + * - Oid of the git object being annotated + */ +typedef struct { + git_oid blob_oid; + git_oid annotated_object_oid; +} git_note_data; + +/** + * Loop over all the notes within a specified namespace + * and issue a callback for each one. + * + * @param repo Repository where to find the notes. + * + * @param notes_ref OID reference to read from (optional); defaults to "refs/notes/commits". + * + * @param note_cb Callback to invoke per found annotation. + * + * @param payload Extra parameter to callback function. + * + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_note_foreach( + git_repository *repo, + const char *notes_ref, + int (*note_cb)(git_note_data *note_data, void *payload), + void *payload +); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/object.h b/git2/include/git2/object.h new file mode 100644 index 000000000..9e988b7b6 --- /dev/null +++ b/git2/include/git2/object.h @@ -0,0 +1,173 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_object_h__ +#define INCLUDE_git_object_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" + +/** + * @file git2/object.h + * @brief Git revision object management routines + * @defgroup git_object Git revision object management routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Lookup a reference to one of the objects in a repostory. + * + * The generated reference is owned by the repository and + * should be closed with the `git_object_free` method + * instead of free'd manually. + * + * The 'type' parameter must match the type of the object + * in the odb; the method will fail otherwise. + * The special value 'GIT_OBJ_ANY' may be passed to let + * the method guess the object's type. + * + * @param object pointer to the looked-up object + * @param repo the repository to look up the object + * @param id the unique identifier for the object + * @param type the type of the object + * @return a reference to the object + */ +GIT_EXTERN(int) git_object_lookup( + git_object **object, + git_repository *repo, + const git_oid *id, + git_otype type); + +/** + * Lookup a reference to one of the objects in a repostory, + * given a prefix of its identifier (short id). + * + * The object obtained will be so that its identifier + * matches the first 'len' hexadecimal characters + * (packets of 4 bits) of the given 'id'. + * 'len' must be at least GIT_OID_MINPREFIXLEN, and + * long enough to identify a unique object matching + * the prefix; otherwise the method will fail. + * + * The generated reference is owned by the repository and + * should be closed with the `git_object_free` method + * instead of free'd manually. + * + * The 'type' parameter must match the type of the object + * in the odb; the method will fail otherwise. + * The special value 'GIT_OBJ_ANY' may be passed to let + * the method guess the object's type. + * + * @param object_out pointer where to store the looked-up object + * @param repo the repository to look up the object + * @param id a short identifier for the object + * @param len the length of the short identifier + * @param type the type of the object + * @return 0 or an error code + */ +GIT_EXTERN(int) git_object_lookup_prefix( + git_object **object_out, + git_repository *repo, + const git_oid *id, + unsigned int len, + git_otype type); + +/** + * Get the id (SHA1) of a repository object + * + * @param obj the repository object + * @return the SHA1 id + */ +GIT_EXTERN(const git_oid *) git_object_id(const git_object *obj); + +/** + * Get the object type of an object + * + * @param obj the repository object + * @return the object's type + */ +GIT_EXTERN(git_otype) git_object_type(const git_object *obj); + +/** + * Get the repository that owns this object + * + * Freeing or calling `git_repository_close` on the + * returned pointer will invalidate the actual object. + * + * Any other operation may be run on the repository without + * affecting the object. + * + * @param obj the object + * @return the repository who owns this object + */ +GIT_EXTERN(git_repository *) git_object_owner(const git_object *obj); + +/** + * Close an open object + * + * This method instructs the library to close an existing + * object; note that git_objects are owned and cached by the repository + * so the object may or may not be freed after this library call, + * depending on how agressive is the caching mechanism used + * by the repository. + * + * IMPORTANT: + * It *is* necessary to call this method when you stop using + * an object. Failure to do so will cause a memory leak. + * + * @param object the object to close + */ +GIT_EXTERN(void) git_object_free(git_object *object); + +/** + * Convert an object type to it's string representation. + * + * The result is a pointer to a string in static memory and + * should not be free()'ed. + * + * @param type object type to convert. + * @return the corresponding string representation. + */ +GIT_EXTERN(const char *) git_object_type2string(git_otype type); + +/** + * Convert a string object type representation to it's git_otype. + * + * @param str the string to convert. + * @return the corresponding git_otype. + */ +GIT_EXTERN(git_otype) git_object_string2type(const char *str); + +/** + * Determine if the given git_otype is a valid loose object type. + * + * @param type object type to test. + * @return true if the type represents a valid loose object type, + * false otherwise. + */ +GIT_EXTERN(int) git_object_typeisloose(git_otype type); + +/** + * Get the size in bytes for the structure which + * acts as an in-memory representation of any given + * object type. + * + * For all the core types, this would the equivalent + * of calling `sizeof(git_commit)` if the core types + * were not opaque on the external API. + * + * @param type object type to get its size + * @return size in bytes of the object + */ +GIT_EXTERN(size_t) git_object__size(git_otype type); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/git2/include/git2/odb.h b/git2/include/git2/odb.h new file mode 100644 index 000000000..1df193389 --- /dev/null +++ b/git2/include/git2/odb.h @@ -0,0 +1,331 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_odb_h__ +#define INCLUDE_git_odb_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" +#include "odb_backend.h" + +/** + * @file git2/odb.h + * @brief Git object database routines + * @defgroup git_odb Git object database routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Create a new object database with no backends. + * + * Before the ODB can be used for read/writing, a custom database + * backend must be manually added using `git_odb_add_backend()` + * + * @param out location to store the database pointer, if opened. + * Set to NULL if the open failed. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_odb_new(git_odb **out); + +/** + * Create a new object database and automatically add + * the two default backends: + * + * - git_odb_backend_loose: read and write loose object files + * from disk, assuming `objects_dir` as the Objects folder + * + * - git_odb_backend_pack: read objects from packfiles, + * assuming `objects_dir` as the Objects folder which + * contains a 'pack/' folder with the corresponding data + * + * @param out location to store the database pointer, if opened. + * Set to NULL if the open failed. + * @param objects_dir path of the backends' "objects" directory. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_odb_open(git_odb **out, const char *objects_dir); + +/** + * Add a custom backend to an existing Object DB + * + * The backends are checked in relative ordering, based on the + * value of the `priority` parameter. + * + * Read for more information. + * + * @param odb database to add the backend to + * @param backend pointer to a git_odb_backend instance + * @param priority Value for ordering the backends queue + * @return 0 on sucess; error code otherwise + */ +GIT_EXTERN(int) git_odb_add_backend(git_odb *odb, git_odb_backend *backend, int priority); + +/** + * Add a custom backend to an existing Object DB; this + * backend will work as an alternate. + * + * Alternate backends are always checked for objects *after* + * all the main backends have been exhausted. + * + * The backends are checked in relative ordering, based on the + * value of the `priority` parameter. + * + * Writing is disabled on alternate backends. + * + * Read for more information. + * + * @param odb database to add the backend to + * @param backend pointer to a git_odb_backend instance + * @param priority Value for ordering the backends queue + * @return 0 on sucess; error code otherwise + */ +GIT_EXTERN(int) git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, int priority); + +/** + * Close an open object database. + * + * @param db database pointer to close. If NULL no action is taken. + */ +GIT_EXTERN(void) git_odb_free(git_odb *db); + +/** + * Read an object from the database. + * + * This method queries all available ODB backends + * trying to read the given OID. + * + * The returned object is reference counted and + * internally cached, so it should be closed + * by the user once it's no longer in use. + * + * @param out pointer where to store the read object + * @param db database to search for the object in. + * @param id identity of the object to read. + * @return + * - 0 if the object was read; + * - GIT_ENOTFOUND if the object is not in the database. + */ +GIT_EXTERN(int) git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id); + +/** + * Read an object from the database, given a prefix + * of its identifier. + * + * This method queries all available ODB backends + * trying to match the 'len' first hexadecimal + * characters of the 'short_id'. + * The remaining (GIT_OID_HEXSZ-len)*4 bits of + * 'short_id' must be 0s. + * 'len' must be at least GIT_OID_MINPREFIXLEN, + * and the prefix must be long enough to identify + * a unique object in all the backends; the + * method will fail otherwise. + * + * The returned object is reference counted and + * internally cached, so it should be closed + * by the user once it's no longer in use. + * + * @param out pointer where to store the read object + * @param db database to search for the object in. + * @param short_id a prefix of the id of the object to read. + * @param len the length of the prefix + * @return 0 if the object was read; + * GIT_ENOTFOUND if the object is not in the database. + * GIT_EAMBIGUOUS if the prefix is ambiguous (several objects match the prefix) + */ +GIT_EXTERN(int) git_odb_read_prefix(git_odb_object **out, git_odb *db, const git_oid *short_id, unsigned int len); + +/** + * Read the header of an object from the database, without + * reading its full contents. + * + * The header includes the length and the type of an object. + * + * Note that most backends do not support reading only the header + * of an object, so the whole object will be read and then the + * header will be returned. + * + * @param len_p pointer where to store the length + * @param type_p pointer where to store the type + * @param db database to search for the object in. + * @param id identity of the object to read. + * @return + * - 0 if the object was read; + * - GIT_ENOTFOUND if the object is not in the database. + */ +GIT_EXTERN(int) git_odb_read_header(size_t *len_p, git_otype *type_p, git_odb *db, const git_oid *id); + +/** + * Determine if the given object can be found in the object database. + * + * @param db database to be searched for the given object. + * @param id the object to search for. + * @return + * - 1, if the object was found + * - 0, otherwise + */ +GIT_EXTERN(int) git_odb_exists(git_odb *db, const git_oid *id); + +/** + * Write an object directly into the ODB + * + * This method writes a full object straight into the ODB. + * For most cases, it is preferred to write objects through a write + * stream, which is both faster and less memory intensive, specially + * for big objects. + * + * This method is provided for compatibility with custom backends + * which are not able to support streaming writes + * + * @param oid pointer to store the OID result of the write + * @param odb object database where to store the object + * @param data buffer with the data to storr + * @param len size of the buffer + * @param type type of the data to store + * @return 0 or an error code + */ +GIT_EXTERN(int) git_odb_write(git_oid *oid, git_odb *odb, const void *data, size_t len, git_otype type); + +/** + * Open a stream to write an object into the ODB + * + * The type and final length of the object must be specified + * when opening the stream. + * + * The returned stream will be of type `GIT_STREAM_WRONLY` and + * will have the following methods: + * + * - stream->write: write `n` bytes into the stream + * - stream->finalize_write: close the stream and store the object in + * the odb + * - stream->free: free the stream + * + * The streaming write won't be effective until `stream->finalize_write` + * is called and returns without an error + * + * The stream must always be free'd or will leak memory. + * + * @see git_odb_stream + * + * @param stream pointer where to store the stream + * @param db object database where the stream will write + * @param size final size of the object that will be written + * @param type type of the object that will be written + * @return 0 if the stream was created; error code otherwise + */ +GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **stream, git_odb *db, size_t size, git_otype type); + +/** + * Open a stream to read an object from the ODB + * + * Note that most backends do *not* support streaming reads + * because they store their objects as compressed/delta'ed blobs. + * + * It's recommended to use `git_odb_read` instead, which is + * assured to work on all backends. + * + * The returned stream will be of type `GIT_STREAM_RDONLY` and + * will have the following methods: + * + * - stream->read: read `n` bytes from the stream + * - stream->free: free the stream + * + * The stream must always be free'd or will leak memory. + * + * @see git_odb_stream + * + * @param stream pointer where to store the stream + * @param db object database where the stream will read from + * @param oid oid of the object the stream will read from + * @return 0 if the stream was created; error code otherwise + */ +GIT_EXTERN(int) git_odb_open_rstream(git_odb_stream **stream, git_odb *db, const git_oid *oid); + +/** + * Determine the object-ID (sha1 hash) of a data buffer + * + * The resulting SHA-1 OID will the itentifier for the data + * buffer as if the data buffer it were to written to the ODB. + * + * @param id the resulting object-ID. + * @param data data to hash + * @param len size of the data + * @param type of the data to hash + * @return 0 or an error code + */ +GIT_EXTERN(int) git_odb_hash(git_oid *id, const void *data, size_t len, git_otype type); + +/** + * Read a file from disk and fill a git_oid with the object id + * that the file would have if it were written to the Object + * Database as an object of the given type. Similar functionality + * to git.git's `git hash-object` without the `-w` flag. + * + * @param out oid structure the result is written into. + * @param path file to read and determine object id for + * @param type the type of the object that will be hashed + * @return 0 or an error code + */ +GIT_EXTERN(int) git_odb_hashfile(git_oid *out, const char *path, git_otype type); + +/** + * Close an ODB object + * + * This method must always be called once a `git_odb_object` is no + * longer needed, otherwise memory will leak. + * + * @param object object to close + */ +GIT_EXTERN(void) git_odb_object_free(git_odb_object *object); + +/** + * Return the OID of an ODB object + * + * This is the OID from which the object was read from + * + * @param object the object + * @return a pointer to the OID + */ +GIT_EXTERN(const git_oid *) git_odb_object_id(git_odb_object *object); + +/** + * Return the data of an ODB object + * + * This is the uncompressed, raw data as read from the ODB, + * without the leading header. + * + * This pointer is owned by the object and shall not be free'd. + * + * @param object the object + * @return a pointer to the data + */ +GIT_EXTERN(const void *) git_odb_object_data(git_odb_object *object); + +/** + * Return the size of an ODB object + * + * This is the real size of the `data` buffer, not the + * actual size of the object. + * + * @param object the object + * @return the size + */ +GIT_EXTERN(size_t) git_odb_object_size(git_odb_object *object); + +/** + * Return the type of an ODB object + * + * @param object the object + * @return the type + */ +GIT_EXTERN(git_otype) git_odb_object_type(git_odb_object *object); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/odb_backend.h b/git2/include/git2/odb_backend.h new file mode 100644 index 000000000..f4620f5f4 --- /dev/null +++ b/git2/include/git2/odb_backend.h @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_odb_backend_h__ +#define INCLUDE_git_odb_backend_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" + +/** + * @file git2/backend.h + * @brief Git custom backend functions + * @defgroup git_backend Git custom backend API + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +struct git_odb_stream; + +/** An instance for a custom backend */ +struct git_odb_backend { + git_odb *odb; + + int (* read)( + void **, size_t *, git_otype *, + struct git_odb_backend *, + const git_oid *); + + /* To find a unique object given a prefix + * of its oid. + * The oid given must be so that the + * remaining (GIT_OID_HEXSZ - len)*4 bits + * are 0s. + */ + int (* read_prefix)( + git_oid *, + void **, size_t *, git_otype *, + struct git_odb_backend *, + const git_oid *, + unsigned int); + + int (* read_header)( + size_t *, git_otype *, + struct git_odb_backend *, + const git_oid *); + + int (* write)( + git_oid *, + struct git_odb_backend *, + const void *, + size_t, + git_otype); + + int (* writestream)( + struct git_odb_stream **, + struct git_odb_backend *, + size_t, + git_otype); + + int (* readstream)( + struct git_odb_stream **, + struct git_odb_backend *, + const git_oid *); + + int (* exists)( + struct git_odb_backend *, + const git_oid *); + + void (* free)(struct git_odb_backend *); +}; + +/** Streaming mode */ +enum { + GIT_STREAM_RDONLY = (1 << 1), + GIT_STREAM_WRONLY = (1 << 2), + GIT_STREAM_RW = (GIT_STREAM_RDONLY | GIT_STREAM_WRONLY), +}; + +/** A stream to read/write from a backend */ +struct git_odb_stream { + struct git_odb_backend *backend; + int mode; + + int (*read)(struct git_odb_stream *stream, char *buffer, size_t len); + int (*write)(struct git_odb_stream *stream, const char *buffer, size_t len); + int (*finalize_write)(git_oid *oid_p, struct git_odb_stream *stream); + void (*free)(struct git_odb_stream *stream); +}; + +GIT_EXTERN(int) git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir); +GIT_EXTERN(int) git_odb_backend_loose(git_odb_backend **backend_out, const char *objects_dir, int compression_level, int do_fsync); + +GIT_END_DECL + +#endif diff --git a/git2/include/git2/oid.h b/git2/include/git2/oid.h new file mode 100644 index 000000000..c06458d24 --- /dev/null +++ b/git2/include/git2/oid.h @@ -0,0 +1,223 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_oid_h__ +#define INCLUDE_git_oid_h__ + +#include "common.h" +#include "types.h" + +/** + * @file git2/oid.h + * @brief Git object id routines + * @defgroup git_oid Git object id routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** Size (in bytes) of a raw/binary oid */ +#define GIT_OID_RAWSZ 20 + +/** Size (in bytes) of a hex formatted oid */ +#define GIT_OID_HEXSZ (GIT_OID_RAWSZ * 2) + +/** Minimum length (in number of hex characters, + * i.e. packets of 4 bits) of an oid prefix */ +#define GIT_OID_MINPREFIXLEN 4 + +/** Unique identity of any object (commit, tree, blob, tag). */ +typedef struct _git_oid git_oid; +struct _git_oid { + /** raw binary formatted id */ + unsigned char id[GIT_OID_RAWSZ]; +}; + +/** + * Parse a hex formatted object id into a git_oid. + * + * @param out oid structure the result is written into. + * @param str input hex string; must be pointing at the start of + * the hex sequence and have at least the number of bytes + * needed for an oid encoded in hex (40 bytes). + * @return 0 or an error code + */ +GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str); + +/** + * Parse N characters of a hex formatted object id into a git_oid + * + * If N is odd, N-1 characters will be parsed instead. + * The remaining space in the git_oid will be set to zero. + * + * @param out oid structure the result is written into. + * @param str input hex string of at least size `length` + * @param length length of the input string + * @return 0 or an error code + */ +GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length); + +/** + * Copy an already raw oid into a git_oid structure. + * + * @param out oid structure the result is written into. + * @param raw the raw input bytes to be copied. + */ +GIT_EXTERN(void) git_oid_fromraw(git_oid *out, const unsigned char *raw); + +/** + * Format a git_oid into a hex string. + * + * @param str output hex string; must be pointing at the start of + * the hex sequence and have at least the number of bytes + * needed for an oid encoded in hex (40 bytes). Only the + * oid digits are written; a '\\0' terminator must be added + * by the caller if it is required. + * @param oid oid structure to format. + */ +GIT_EXTERN(void) git_oid_fmt(char *str, const git_oid *oid); + +/** + * Format a git_oid into a loose-object path string. + * + * The resulting string is "aa/...", where "aa" is the first two + * hex digitis of the oid and "..." is the remaining 38 digits. + * + * @param str output hex string; must be pointing at the start of + * the hex sequence and have at least the number of bytes + * needed for an oid encoded in hex (41 bytes). Only the + * oid digits are written; a '\\0' terminator must be added + * by the caller if it is required. + * @param oid oid structure to format. + */ +GIT_EXTERN(void) git_oid_pathfmt(char *str, const git_oid *oid); + +/** + * Format a git_oid into a newly allocated c-string. + * + * @param oid the oid structure to format + * @return the c-string; NULL if memory is exhausted. Caller must + * deallocate the string with git__free(). + */ +GIT_EXTERN(char *) git_oid_allocfmt(const git_oid *oid); + +/** + * Format a git_oid into a buffer as a hex format c-string. + * + * If the buffer is smaller than GIT_OID_HEXSZ+1, then the resulting + * oid c-string will be truncated to n-1 characters. If there are + * any input parameter errors (out == NULL, n == 0, oid == NULL), + * then a pointer to an empty string is returned, so that the return + * value can always be printed. + * + * @param out the buffer into which the oid string is output. + * @param n the size of the out buffer. + * @param oid the oid structure to format. + * @return the out buffer pointer, assuming no input parameter + * errors, otherwise a pointer to an empty string. + */ +GIT_EXTERN(char *) git_oid_tostr(char *out, size_t n, const git_oid *oid); + +/** + * Copy an oid from one structure to another. + * + * @param out oid structure the result is written into. + * @param src oid structure to copy from. + */ +GIT_EXTERN(void) git_oid_cpy(git_oid *out, const git_oid *src); + +/** + * Compare two oid structures. + * + * @param a first oid structure. + * @param b second oid structure. + * @return <0, 0, >0 if a < b, a == b, a > b. + */ +GIT_EXTERN(int) git_oid_cmp(const git_oid *a, const git_oid *b); + +/** + * Compare the first 'len' hexadecimal characters (packets of 4 bits) + * of two oid structures. + * + * @param a first oid structure. + * @param b second oid structure. + * @param len the number of hex chars to compare + * @return 0 in case of a match + */ +GIT_EXTERN(int) git_oid_ncmp(const git_oid *a, const git_oid *b, unsigned int len); + +/** + * Check if an oid equals an hex formatted object id. + * + * @param a oid structure. + * @param str input hex string of an object id. + * @return GIT_ENOTOID if str is not a valid hex string, + * 0 in case of a match, GIT_ERROR otherwise. + */ +GIT_EXTERN(int) git_oid_streq(const git_oid *a, const char *str); + +/** + * Check is an oid is all zeros. + */ +GIT_EXTERN(int) git_oid_iszero(const git_oid *a); + +/** + * OID Shortener object + */ +typedef struct git_oid_shorten git_oid_shorten; + +/** + * Create a new OID shortener. + * + * The OID shortener is used to process a list of OIDs + * in text form and return the shortest length that would + * uniquely identify all of them. + * + * E.g. look at the result of `git log --abbrev`. + * + * @param min_length The minimal length for all identifiers, + * which will be used even if shorter OIDs would still + * be unique. + * @return a `git_oid_shorten` instance, NULL if OOM + */ +GIT_EXTERN(git_oid_shorten *) git_oid_shorten_new(size_t min_length); + +/** + * Add a new OID to set of shortened OIDs and calculate + * the minimal length to uniquely identify all the OIDs in + * the set. + * + * The OID is expected to be a 40-char hexadecimal string. + * The OID is owned by the user and will not be modified + * or freed. + * + * For performance reasons, there is a hard-limit of how many + * OIDs can be added to a single set (around ~22000, assuming + * a mostly randomized distribution), which should be enough + * for any kind of program, and keeps the algorithm fast and + * memory-efficient. + * + * Attempting to add more than those OIDs will result in a + * GIT_ENOMEM error + * + * @param os a `git_oid_shorten` instance + * @param text_oid an OID in text form + * @return the minimal length to uniquely identify all OIDs + * added so far to the set; or an error code (<0) if an + * error occurs. + */ +GIT_EXTERN(int) git_oid_shorten_add(git_oid_shorten *os, const char *text_oid); + +/** + * Free an OID shortener instance + * + * @param os a `git_oid_shorten` instance + */ +GIT_EXTERN(void) git_oid_shorten_free(git_oid_shorten *os); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/reflog.h b/git2/include/git2/reflog.h new file mode 100644 index 000000000..f490e29de --- /dev/null +++ b/git2/include/git2/reflog.h @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_reflog_h__ +#define INCLUDE_git_reflog_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" + +/** + * @file git2/reflog.h + * @brief Git reflog management routines + * @defgroup git_reflog Git reflog management routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Read the reflog for the given reference + * + * The reflog must be freed manually by using + * git_reflog_free(). + * + * @param reflog pointer to reflog + * @param ref reference to read the reflog for + * @return 0 or an error code + */ +GIT_EXTERN(int) git_reflog_read(git_reflog **reflog, git_reference *ref); + +/** + * Write a new reflog for the given reference + * + * If there is no reflog file for the given + * reference yet, it will be created. + * + * `oid_old` may be NULL in case it's a new reference. + * + * `msg` is optional and can be NULL. + * + * @param ref the changed reference + * @param oid_old the OID the reference was pointing to + * @param committer the signature of the committer + * @param msg the reflog message + * @return 0 or an error code + */ +GIT_EXTERN(int) git_reflog_write(git_reference *ref, const git_oid *oid_old, const git_signature *committer, const char *msg); + +/** + * Rename the reflog for the given reference + * + * @param ref the reference + * @param new_name the new name of the reference + * @return 0 or an error code + */ +GIT_EXTERN(int) git_reflog_rename(git_reference *ref, const char *new_name); + +/** + * Delete the reflog for the given reference + * + * @param ref the reference + * @return 0 or an error code + */ +GIT_EXTERN(int) git_reflog_delete(git_reference *ref); + +/** + * Get the number of log entries in a reflog + * + * @param reflog the previously loaded reflog + * @return the number of log entries + */ +GIT_EXTERN(unsigned int) git_reflog_entrycount(git_reflog *reflog); + +/** + * Lookup an entry by its index + * + * @param reflog a previously loaded reflog + * @param idx the position to lookup + * @return the entry; NULL if not found + */ +GIT_EXTERN(const git_reflog_entry *) git_reflog_entry_byindex(git_reflog *reflog, unsigned int idx); + +/** + * Get the old oid + * + * @param entry a reflog entry + * @return the old oid + */ +GIT_EXTERN(const git_oid *) git_reflog_entry_oidold(const git_reflog_entry *entry); + +/** + * Get the new oid + * + * @param entry a reflog entry + * @return the new oid at this time + */ +GIT_EXTERN(const git_oid *) git_reflog_entry_oidnew(const git_reflog_entry *entry); + +/** + * Get the committer of this entry + * + * @param entry a reflog entry + * @return the committer + */ +GIT_EXTERN(git_signature *) git_reflog_entry_committer(const git_reflog_entry *entry); + +/** + * Get the log msg + * + * @param entry a reflog entry + * @return the log msg + */ +GIT_EXTERN(char *) git_reflog_entry_msg(const git_reflog_entry *entry); + +/** + * Free the reflog + * + * @param reflog reflog to free + */ +GIT_EXTERN(void) git_reflog_free(git_reflog *reflog); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/refs.h b/git2/include/git2/refs.h new file mode 100644 index 000000000..882e32769 --- /dev/null +++ b/git2/include/git2/refs.h @@ -0,0 +1,329 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_refs_h__ +#define INCLUDE_git_refs_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" + +/** + * @file git2/refs.h + * @brief Git reference management routines + * @defgroup git_reference Git reference management routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Lookup a reference by its name in a repository. + * + * The generated reference must be freed by the user. + * + * @param reference_out pointer to the looked-up reference + * @param repo the repository to look up the reference + * @param name the long name for the reference (e.g. HEAD, ref/heads/master, refs/tags/v0.1.0, ...) + * @return 0 or an error code + */ +GIT_EXTERN(int) git_reference_lookup(git_reference **reference_out, git_repository *repo, const char *name); + +/** + * Lookup a reference by name and resolve immediately to OID. + * + * @param oid Pointer to oid to be filled in + * @param repo The repository in which to look up the reference + * @param name The long name for the reference + * @return 0 on success, -1 if name could not be resolved + */ +GIT_EXTERN(int) git_reference_name_to_oid( + git_oid *out, git_repository *repo, const char *name); + +/** + * Create a new symbolic reference. + * + * The reference will be created in the repository and written + * to the disk. + * + * The generated reference must be freed by the user. + * + * If `force` is true and there already exists a reference + * with the same name, it will be overwritten. + * + * @param ref_out Pointer to the newly created reference + * @param repo Repository where that reference will live + * @param name The name of the reference + * @param target The target of the reference + * @param force Overwrite existing references + * @return 0 or an error code + */ +GIT_EXTERN(int) git_reference_create_symbolic(git_reference **ref_out, git_repository *repo, const char *name, const char *target, int force); + +/** + * Create a new object id reference. + * + * The reference will be created in the repository and written + * to the disk. + * + * The generated reference must be freed by the user. + * + * If `force` is true and there already exists a reference + * with the same name, it will be overwritten. + * + * @param ref_out Pointer to the newly created reference + * @param repo Repository where that reference will live + * @param name The name of the reference + * @param id The object id pointed to by the reference. + * @param force Overwrite existing references + * @return 0 or an error code + */ +GIT_EXTERN(int) git_reference_create_oid(git_reference **ref_out, git_repository *repo, const char *name, const git_oid *id, int force); + +/** + * Get the OID pointed to by a reference. + * + * Only available if the reference is direct (i.e. not symbolic) + * + * @param ref The reference + * @return a pointer to the oid if available, NULL otherwise + */ +GIT_EXTERN(const git_oid *) git_reference_oid(git_reference *ref); + +/** + * Get full name to the reference pointed by this reference + * + * Only available if the reference is symbolic + * + * @param ref The reference + * @return a pointer to the name if available, NULL otherwise + */ +GIT_EXTERN(const char *) git_reference_target(git_reference *ref); + +/** + * Get the type of a reference + * + * Either direct (GIT_REF_OID) or symbolic (GIT_REF_SYMBOLIC) + * + * @param ref The reference + * @return the type + */ +GIT_EXTERN(git_ref_t) git_reference_type(git_reference *ref); + +/** + * Get the full name of a reference + * + * @param ref The reference + * @return the full name for the ref + */ +GIT_EXTERN(const char *) git_reference_name(git_reference *ref); + +/** + * Resolve a symbolic reference + * + * Thie method iteratively peels a symbolic reference + * until it resolves to a direct reference to an OID. + * + * The peeled reference is returned in the `resolved_ref` + * argument, and must be freed manually once it's no longer + * needed. + * + * If a direct reference is passed as an argument, + * a copy of that reference is returned. This copy must + * be manually freed too. + * + * @param resolved_ref Pointer to the peeled reference + * @param ref The reference + * @return 0 or an error code + */ +GIT_EXTERN(int) git_reference_resolve(git_reference **resolved_ref, git_reference *ref); + +/** + * Get the repository where a reference resides + * + * @param ref The reference + * @return a pointer to the repo + */ +GIT_EXTERN(git_repository *) git_reference_owner(git_reference *ref); + +/** + * Set the symbolic target of a reference. + * + * The reference must be a symbolic reference, otherwise + * this method will fail. + * + * The reference will be automatically updated in + * memory and on disk. + * + * @param ref The reference + * @param target The new target for the reference + * @return 0 or an error code + */ +GIT_EXTERN(int) git_reference_set_target(git_reference *ref, const char *target); + +/** + * Set the OID target of a reference. + * + * The reference must be a direct reference, otherwise + * this method will fail. + * + * The reference will be automatically updated in + * memory and on disk. + * + * @param ref The reference + * @param id The new target OID for the reference + * @return 0 or an error code + */ +GIT_EXTERN(int) git_reference_set_oid(git_reference *ref, const git_oid *id); + +/** + * Rename an existing reference + * + * This method works for both direct and symbolic references. + * The new name will be checked for validity and may be + * modified into a normalized form. + * + * The given git_reference will be updated in place. + * + * The reference will be immediately renamed in-memory + * and on disk. + * + * If the `force` flag is not enabled, and there's already + * a reference with the given name, the renaming will fail. + * + * IMPORTANT: + * The user needs to write a proper reflog entry if the + * reflog is enabled for the repository. We only rename + * the reflog if it exists. + * + * @param ref The reference to rename + * @param new_name The new name for the reference + * @param force Overwrite an existing reference + * @return 0 or an error code + * + */ +GIT_EXTERN(int) git_reference_rename(git_reference *ref, const char *new_name, int force); + +/** + * Delete an existing reference + * + * This method works for both direct and symbolic references. + * + * The reference will be immediately removed on disk and from + * memory. The given reference pointer will no longer be valid. + * + * @param ref The reference to remove + * @return 0 or an error code + */ +GIT_EXTERN(int) git_reference_delete(git_reference *ref); + +/** + * Pack all the loose references in the repository + * + * This method will load into the cache all the loose + * references on the repository and update the + * `packed-refs` file with them. + * + * Once the `packed-refs` file has been written properly, + * the loose references will be removed from disk. + * + * @param repo Repository where the loose refs will be packed + * @return 0 or an error code + */ +GIT_EXTERN(int) git_reference_packall(git_repository *repo); + +/** + * Fill a list with all the references that can be found + * in a repository. + * + * The listed references may be filtered by type, or using + * a bitwise OR of several types. Use the magic value + * `GIT_REF_LISTALL` to obtain all references, including + * packed ones. + * + * The string array will be filled with the names of all + * references; these values are owned by the user and + * should be free'd manually when no longer needed, using + * `git_strarray_free`. + * + * @param array Pointer to a git_strarray structure where + * the reference names will be stored + * @param repo Repository where to find the refs + * @param list_flags Filtering flags for the reference + * listing. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_reference_list(git_strarray *array, git_repository *repo, unsigned int list_flags); + + +/** + * Perform an operation on each reference in the repository + * + * The processed references may be filtered by type, or using + * a bitwise OR of several types. Use the magic value + * `GIT_REF_LISTALL` to obtain all references, including + * packed ones. + * + * The `callback` function will be called for each of the references + * in the repository, and will receive the name of the reference and + * the `payload` value passed to this method. + * + * @param repo Repository where to find the refs + * @param list_flags Filtering flags for the reference + * listing. + * @param callback Function which will be called for every listed ref + * @param payload Additional data to pass to the callback + * @return 0 or an error code + */ +GIT_EXTERN(int) git_reference_foreach(git_repository *repo, unsigned int list_flags, int (*callback)(const char *, void *), void *payload); + +/** + * Check if a reference has been loaded from a packfile + * + * @param ref A git reference + * @return 0 in case it's not packed; 1 otherwise + */ +GIT_EXTERN(int) git_reference_is_packed(git_reference *ref); + +/** + * Reload a reference from disk + * + * Reference pointers may become outdated if the Git + * repository is accessed simultaneously by other clients + * whilt the library is open. + * + * This method forces a reload of the reference from disk, + * to ensure that the provided information is still + * reliable. + * + * If the reload fails (e.g. the reference no longer exists + * on disk, or has become corrupted), an error code will be + * returned and the reference pointer will be invalidated. + * + * @param ref The reference to reload + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_reference_reload(git_reference *ref); + +/** + * Free the given reference + * + * @param ref git_reference + */ +GIT_EXTERN(void) git_reference_free(git_reference *ref); + +/** + * Compare two references. + * + * @param ref1 The first git_reference + * @param ref2 The second git_reference + * @return 0 if the same, else a stable but meaningless ordering. + */ +GIT_EXTERN(int) git_reference_cmp(git_reference *ref1, git_reference *ref2); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/refspec.h b/git2/include/git2/refspec.h new file mode 100644 index 000000000..c0a8eabfe --- /dev/null +++ b/git2/include/git2/refspec.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_refspec_h__ +#define INCLUDE_git_refspec_h__ + +#include "common.h" +#include "types.h" + +/** + * @file git2/refspec.h + * @brief Git refspec attributes + * @defgroup git_refspec Git refspec attributes + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Get the source specifier + * + * @param refspec the refspec + * @return the refspec's source specifier + */ +GIT_EXTERN(const char *) git_refspec_src(const git_refspec *refspec); + +/** + * Get the destination specifier + * + * @param refspec the refspec + * @return the refspec's destination specifier + */ +GIT_EXTERN(const char *) git_refspec_dst(const git_refspec *refspec); + +/** + * Check if a refspec's source descriptor matches a reference + * + * @param refspec the refspec + * @param refname the name of the reference to check + * @return 1 if the refspec matches, 0 otherwise + */ +GIT_EXTERN(int) git_refspec_src_matches(const git_refspec *refspec, const char *refname); + +/** + * Transform a reference to its target following the refspec's rules + * + * @param out where to store the target name + * @param outlen the size ouf the `out` buffer + * @param spec the refspec + * @param name the name of the reference to transform + * @return 0, GIT_EBUFS or another error + */ +GIT_EXTERN(int) git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, const char *name); + +GIT_END_DECL + +#endif diff --git a/git2/include/git2/remote.h b/git2/include/git2/remote.h new file mode 100644 index 000000000..865dfef04 --- /dev/null +++ b/git2/include/git2/remote.h @@ -0,0 +1,234 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_remote_h__ +#define INCLUDE_git_remote_h__ + +#include "common.h" +#include "repository.h" +#include "refspec.h" +#include "net.h" +#include "indexer.h" + +/** + * @file git2/remote.h + * @brief Git remote management functions + * @defgroup git_remote remote management functions + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/* + * TODO: This functions still need to be implemented: + * - _listcb/_foreach + * - _add + * - _rename + * - _del (needs support from config) + */ + +/** + * Create a remote in memory + * + * Create a remote with the default refspecs in memory. You can use + * this when you have a URL instead of a remote's name. + * + * @param out pointer to the new remote object + * @param repo the associtated repository + * @param name the remote's name + * @param url the remote repository's URL + * @param fetch the fetch refspec to use for this remote + * @return 0 or an error code + */ +GIT_EXTERN(int) git_remote_new(git_remote **out, git_repository *repo, const char *name, const char *url, const char *fetch); + +/** + * Get the information for a particular remote + * + * @param out pointer to the new remote object + * @param cfg the repository's configuration + * @param name the remote's name + * @return 0 or an error code + */ +GIT_EXTERN(int) git_remote_load(git_remote **out, git_repository *repo, const char *name); + +/** + * Save a remote to its repository's configuration + * + * @param remote the remote to save to config + * @return 0 or an error code + */ +GIT_EXTERN(int) git_remote_save(const git_remote *remote); + +/** + * Get the remote's name + * + * @param remote the remote + * @return a pointer to the name + */ +GIT_EXTERN(const char *) git_remote_name(git_remote *remote); + +/** + * Get the remote's url + * + * @param remote the remote + * @return a pointer to the url + */ +GIT_EXTERN(const char *) git_remote_url(git_remote *remote); + +/** + * Set the remote's fetch refspec + * + * @param remote the remote + * @apram spec the new fetch refspec + * @return 0 or an error value + */ +GIT_EXTERN(int) git_remote_set_fetchspec(git_remote *remote, const char *spec); + +/** + * Get the fetch refspec + * + * @param remote the remote + * @return a pointer to the fetch refspec or NULL if it doesn't exist + */ +GIT_EXTERN(const git_refspec *) git_remote_fetchspec(git_remote *remote); + +/** + * Set the remote's push refspec + * + * @param remote the remote + * @apram spec the new push refspec + * @return 0 or an error value + */ +GIT_EXTERN(int) git_remote_set_pushspec(git_remote *remote, const char *spec); + +/** + * Get the push refspec + * + * @param remote the remote + * @return a pointer to the push refspec or NULL if it doesn't exist + */ + +GIT_EXTERN(const git_refspec *) git_remote_pushspec(git_remote *remote); + +/** + * Open a connection to a remote + * + * The transport is selected based on the URL. The direction argument + * is due to a limitation of the git protocol (over TCP or SSH) which + * starts up a specific binary which can only do the one or the other. + * + * @param remote the remote to connect to + * @param direction whether you want to receive or send data + * @return 0 or an error code + */ +GIT_EXTERN(int) git_remote_connect(git_remote *remote, int direction); + +/** + * Get a list of refs at the remote + * + * The remote (or more exactly its transport) must be connected. The + * memory belongs to the remote. + * + * @param refs where to store the refs + * @param remote the remote + * @return 0 or an error code + */ +GIT_EXTERN(int) git_remote_ls(git_remote *remote, git_headlist_cb list_cb, void *payload); + +/** + * Download the packfile + * + * Negotiate what objects should be downloaded and download the + * packfile with those objects. The packfile is downloaded with a + * temporary filename, as it's final name is not known yet. If there + * was no packfile needed (all the objects were available locally), + * filename will be NULL and the function will return success. + * + * @param remote the remote to download from + * @param filename where to store the temproray filename + * @return 0 or an error code + */ +GIT_EXTERN(int) git_remote_download(git_remote *remote, git_off_t *bytes, git_indexer_stats *stats); + +/** + * Check whether the remote is connected + * + * Check whether the remote's underlying transport is connected to the + * remote host. + * + * @return 1 if it's connected, 0 otherwise. + */ +GIT_EXTERN(int) git_remote_connected(git_remote *remote); + +/** + * Disconnect from the remote + * + * Close the connection to the remote and free the underlying + * transport. + * + * @param remote the remote to disconnect from + */ +GIT_EXTERN(void) git_remote_disconnect(git_remote *remote); + +/** + * Free the memory associated with a remote + * + * This also disconnects from the remote, if the connection + * has not been closed yet (using git_remote_disconnect). + * + * @param remote the remote to free + */ +GIT_EXTERN(void) git_remote_free(git_remote *remote); + +/** + * Update the tips to the new state + * + * @param remote the remote to update + * @param cb callback to run on each ref update. 'a' is the old value, 'b' is then new value + */ +GIT_EXTERN(int) git_remote_update_tips(git_remote *remote, int (*cb)(const char *refname, const git_oid *a, const git_oid *b)); + +/** + * Return whether a string is a valid remote URL + * + * @param tranport the url to check + * @param 1 if the url is valid, 0 otherwise + */ +GIT_EXTERN(int) git_remote_valid_url(const char *url); + +/** + * Return whether the passed URL is supported by this version of the library. + * + * @param url the url to check + * @return 1 if the url is supported, 0 otherwise +*/ +GIT_EXTERN(int) git_remote_supported_url(const char* url); + +/** + * Get a list of the configured remotes for a repo + * + * The string array must be freed by the user. + * + * @param remotes_list a string array with the names of the remotes + * @param repo the repository to query + * @return 0 or an error code + */ +GIT_EXTERN(int) git_remote_list(git_strarray *remotes_list, git_repository *repo); + +/** + * Add a remote with the default fetch refspec to the repository's configuration + * + * @param out the resulting remote + * @param repo the repository in which to create the remote + * @param name the remote's name + * @param url the remote's url + */ +GIT_EXTERN(int) git_remote_add(git_remote **out, git_repository *repo, const char *name, const char *url); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/repository.h b/git2/include/git2/repository.h new file mode 100644 index 000000000..3949438cf --- /dev/null +++ b/git2/include/git2/repository.h @@ -0,0 +1,304 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_repository_h__ +#define INCLUDE_git_repository_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" + +/** + * @file git2/repository.h + * @brief Git repository management routines + * @defgroup git_repository Git repository management routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Open a git repository. + * + * The 'path' argument must point to either a git repository + * folder, or an existing work dir. + * + * The method will automatically detect if 'path' is a normal + * or bare repository or fail is 'path' is neither. + * + * @param repository pointer to the repo which will be opened + * @param path the path to the repository + * @return 0 or an error code + */ +GIT_EXTERN(int) git_repository_open(git_repository **repository, const char *path); + +/** + * Look for a git repository and copy its path in the given buffer. + * The lookup start from base_path and walk across parent directories + * if nothing has been found. The lookup ends when the first repository + * is found, or when reaching a directory referenced in ceiling_dirs + * or when the filesystem changes (in case across_fs is true). + * + * The method will automatically detect if the repository is bare + * (if there is a repository). + * + * @param repository_path The user allocated buffer which will + * contain the found path. + * + * @param size repository_path size + * + * @param start_path The base path where the lookup starts. + * + * @param across_fs If true, then the lookup will not stop when a + * filesystem device change is detected while exploring parent directories. + * + * @param ceiling_dirs A GIT_PATH_LIST_SEPARATOR separated list of + * absolute symbolic link free paths. The lookup will stop when any + * of this paths is reached. Note that the lookup always performs on + * start_path no matter start_path appears in ceiling_dirs ceiling_dirs + * might be NULL (which is equivalent to an empty string) + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_repository_discover( + char *repository_path, + size_t size, + const char *start_path, + int across_fs, + const char *ceiling_dirs); + +enum { + GIT_REPOSITORY_OPEN_NO_SEARCH = (1 << 0), + GIT_REPOSITORY_OPEN_CROSS_FS = (1 << 1), +}; + +/** + * Find and open a repository with extended controls. + */ +GIT_EXTERN(int) git_repository_open_ext( + git_repository **repo, + const char *start_path, + uint32_t flags, + const char *ceiling_dirs); + +/** + * Free a previously allocated repository + * + * Note that after a repository is free'd, all the objects it has spawned + * will still exist until they are manually closed by the user + * with `git_object_free`, but accessing any of the attributes of + * an object without a backing repository will result in undefined + * behavior + * + * @param repo repository handle to close. If NULL nothing occurs. + */ +GIT_EXTERN(void) git_repository_free(git_repository *repo); + +/** + * Creates a new Git repository in the given folder. + * + * TODO: + * - Reinit the repository + * + * @param repo_out pointer to the repo which will be created or reinitialized + * @param path the path to the repository + * @param is_bare if true, a Git repository without a working directory is created + * at the pointed path. If false, provided path will be considered as the working + * directory into which the .git directory will be created. + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_repository_init(git_repository **repo_out, const char *path, unsigned is_bare); + +/** + * Retrieve and resolve the reference pointed at by HEAD. + * + * @param head_out pointer to the reference which will be retrieved + * @param repo a repository object + * + * @return 0 on success; error code otherwise + */ +GIT_EXTERN(int) git_repository_head(git_reference **head_out, git_repository *repo); + +/** + * Check if a repository's HEAD is detached + * + * A repository's HEAD is detached when it points directly to a commit + * instead of a branch. + * + * @param repo Repo to test + * @return 1 if HEAD is detached, 0 if i'ts not; error code if there + * was an error. + */ +GIT_EXTERN(int) git_repository_head_detached(git_repository *repo); + +/** + * Check if the current branch is an orphan + * + * An orphan branch is one named from HEAD but which doesn't exist in + * the refs namespace, because it doesn't have any commit to point to. + * + * @param repo Repo to test + * @return 1 if the current branch is an orphan, 0 if it's not; error + * code if therewas an error + */ +GIT_EXTERN(int) git_repository_head_orphan(git_repository *repo); + +/** + * Check if a repository is empty + * + * An empty repository has just been initialized and contains + * no commits. + * + * @param repo Repo to test + * @return 1 if the repository is empty, 0 if it isn't, error code + * if the repository is corrupted + */ +GIT_EXTERN(int) git_repository_is_empty(git_repository *repo); + +/** + * Get the path of this repository + * + * This is the path of the `.git` folder for normal repositories, + * or of the repository itself for bare repositories. + * + * @param repo A repository object + * @return the path to the repository + */ +GIT_EXTERN(const char *) git_repository_path(git_repository *repo); + +/** + * Get the path of the working directory for this repository + * + * If the repository is bare, this function will always return + * NULL. + * + * @param repo A repository object + * @return the path to the working dir, if it exists + */ +GIT_EXTERN(const char *) git_repository_workdir(git_repository *repo); + +/** + * Set the path to the working directory for this repository + * + * The working directory doesn't need to be the same one + * that contains the `.git` folder for this repository. + * + * If this repository is bare, setting its working directory + * will turn it into a normal repository, capable of performing + * all the common workdir operations (checkout, status, index + * manipulation, etc). + * + * @param repo A repository object + * @param workdir The path to a working directory + * @return 0, or an error code + */ +GIT_EXTERN(int) git_repository_set_workdir(git_repository *repo, const char *workdir); + +/** + * Check if a repository is bare + * + * @param repo Repo to test + * @return 1 if the repository is bare, 0 otherwise. + */ +GIT_EXTERN(int) git_repository_is_bare(git_repository *repo); + +/** + * Get the configuration file for this repository. + * + * If a configuration file has not been set, the default + * config set for the repository will be returned, including + * global and system configurations (if they are available). + * + * The configuration file must be freed once it's no longer + * being used by the user. + * + * @param out Pointer to store the loaded config file + * @param repo A repository object + * @return 0, or an error code + */ +GIT_EXTERN(int) git_repository_config(git_config **out, git_repository *repo); + +/** + * Set the configuration file for this repository + * + * This configuration file will be used for all configuration + * queries involving this repository. + * + * The repository will keep a reference to the config file; + * the user must still free the config after setting it + * to the repository, or it will leak. + * + * @param repo A repository object + * @param config A Config object + */ +GIT_EXTERN(void) git_repository_set_config(git_repository *repo, git_config *config); + +/** + * Get the Object Database for this repository. + * + * If a custom ODB has not been set, the default + * database for the repository will be returned (the one + * located in `.git/objects`). + * + * The ODB must be freed once it's no longer being used by + * the user. + * + * @param out Pointer to store the loaded ODB + * @param repo A repository object + * @return 0, or an error code + */ +GIT_EXTERN(int) git_repository_odb(git_odb **out, git_repository *repo); + +/** + * Set the Object Database for this repository + * + * The ODB will be used for all object-related operations + * involving this repository. + * + * The repository will keep a reference to the ODB; the user + * must still free the ODB object after setting it to the + * repository, or it will leak. + * + * @param repo A repository object + * @param odb An ODB object + */ +GIT_EXTERN(void) git_repository_set_odb(git_repository *repo, git_odb *odb); + +/** + * Get the Index file for this repository. + * + * If a custom index has not been set, the default + * index for the repository will be returned (the one + * located in `.git/index`). + * + * The index must be freed once it's no longer being used by + * the user. + * + * @param out Pointer to store the loaded index + * @param repo A repository object + * @return 0, or an error code + */ +GIT_EXTERN(int) git_repository_index(git_index **out, git_repository *repo); + +/** + * Set the index file for this repository + * + * This index will be used for all index-related operations + * involving this repository. + * + * The repository will keep a reference to the index file; + * the user must still free the index after setting it + * to the repository, or it will leak. + * + * @param repo A repository object + * @param index An index object + */ +GIT_EXTERN(void) git_repository_set_index(git_repository *repo, git_index *index); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/revwalk.h b/git2/include/git2/revwalk.h new file mode 100644 index 000000000..aac6fb7c2 --- /dev/null +++ b/git2/include/git2/revwalk.h @@ -0,0 +1,237 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_revwalk_h__ +#define INCLUDE_git_revwalk_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" + +/** + * @file git2/revwalk.h + * @brief Git revision traversal routines + * @defgroup git_revwalk Git revision traversal routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Sort the repository contents in no particular ordering; + * this sorting is arbitrary, implementation-specific + * and subject to change at any time. + * This is the default sorting for new walkers. + */ +#define GIT_SORT_NONE (0) + +/** + * Sort the repository contents in topological order + * (parents before children); this sorting mode + * can be combined with time sorting. + */ +#define GIT_SORT_TOPOLOGICAL (1 << 0) + +/** + * Sort the repository contents by commit time; + * this sorting mode can be combined with + * topological sorting. + */ +#define GIT_SORT_TIME (1 << 1) + +/** + * Iterate through the repository contents in reverse + * order; this sorting mode can be combined with + * any of the above. + */ +#define GIT_SORT_REVERSE (1 << 2) + +/** + * Allocate a new revision walker to iterate through a repo. + * + * This revision walker uses a custom memory pool and an internal + * commit cache, so it is relatively expensive to allocate. + * + * For maximum performance, this revision walker should be + * reused for different walks. + * + * This revision walker is *not* thread safe: it may only be + * used to walk a repository on a single thread; however, + * it is possible to have several revision walkers in + * several different threads walking the same repository. + * + * @param walker pointer to the new revision walker + * @param repo the repo to walk through + * @return 0 or an error code + */ +GIT_EXTERN(int) git_revwalk_new(git_revwalk **walker, git_repository *repo); + +/** + * Reset the revision walker for reuse. + * + * This will clear all the pushed and hidden commits, and + * leave the walker in a blank state (just like at + * creation) ready to receive new commit pushes and + * start a new walk. + * + * The revision walk is automatically reset when a walk + * is over. + * + * @param walker handle to reset. + */ +GIT_EXTERN(void) git_revwalk_reset(git_revwalk *walker); + +/** + * Mark a commit to start traversal from. + * + * The given OID must belong to a commit on the walked + * repository. + * + * The given commit will be used as one of the roots + * when starting the revision walk. At least one commit + * must be pushed the repository before a walk can + * be started. + * + * @param walk the walker being used for the traversal. + * @param oid the oid of the commit to start from. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_revwalk_push(git_revwalk *walk, const git_oid *oid); + +/** + * Push matching references + * + * The OIDs pinted to by the references that match the given glob + * pattern will be pushed to the revision walker. + * + * A leading 'refs/' is implied it not present as well as a trailing + * '/ *' if the glob lacks '?', '*' or '['. + * + * @param walk the walker being used for the traversal + * @param glob the glob pattern references should match + * @return 0 or an error code + */ +GIT_EXTERN(int) git_revwalk_push_glob(git_revwalk *walk, const char *glob); + +/** + * Push the repository's HEAD + * + * @param walk the walker being used for the traversal + * @return 0 or an error code + */ +GIT_EXTERN(int) git_revwalk_push_head(git_revwalk *walk); + +/** + * Mark a commit (and its ancestors) uninteresting for the output. + * + * The given OID must belong to a commit on the walked + * repository. + * + * The resolved commit and all its parents will be hidden from the + * output on the revision walk. + * + * @param walk the walker being used for the traversal. + * @param oid the oid of commit that will be ignored during the traversal + * @return 0 or an error code + */ +GIT_EXTERN(int) git_revwalk_hide(git_revwalk *walk, const git_oid *oid); + +/** + * Hide matching references. + * + * The OIDs pinted to by the references that match the given glob + * pattern and their ancestors will be hidden from the output on the + * revision walk. + * + * A leading 'refs/' is implied it not present as well as a trailing + * '/ *' if the glob lacks '?', '*' or '['. + * + * @param walk the walker being used for the traversal + * @param glob the glob pattern references should match + * @return 0 or an error code + */ +GIT_EXTERN(int) git_revwalk_hide_glob(git_revwalk *walk, const char *glob); + +/** + * Hide the repository's HEAD + * + * @param walk the walker being used for the traversal + * @return 0 or an error code + */ +GIT_EXTERN(int) git_revwalk_hide_head(git_revwalk *walk); + +/** + * Push the OID pointed to by a reference + * + * The reference must point to a commit. + * + * @param walk the walker being used for the traversal + * @param refname the referece to push + * @return 0 or an error code + */ +GIT_EXTERN(int) git_revwalk_push_ref(git_revwalk *walk, const char *refname); + +/** + * Hide the OID pointed to by a reference + * + * The reference must point to a commit. + * + * @param walk the walker being used for the traversal + * @param refname the referece to hide + * @return 0 or an error code + */ +GIT_EXTERN(int) git_revwalk_hide_ref(git_revwalk *walk, const char *refname); + +/** + * Get the next commit from the revision walk. + * + * The initial call to this method is *not* blocking when + * iterating through a repo with a time-sorting mode. + * + * Iterating with Topological or inverted modes makes the initial + * call blocking to preprocess the commit list, but this block should be + * mostly unnoticeable on most repositories (topological preprocessing + * times at 0.3s on the git.git repo). + * + * The revision walker is reset when the walk is over. + * + * @param oid Pointer where to store the oid of the next commit + * @param walk the walker to pop the commit from. + * @return 0 if the next commit was found; + * GIT_REVWALKOVER if there are no commits left to iterate + */ +GIT_EXTERN(int) git_revwalk_next(git_oid *oid, git_revwalk *walk); + +/** + * Change the sorting mode when iterating through the + * repository's contents. + * + * Changing the sorting mode resets the walker. + * + * @param walk the walker being used for the traversal. + * @param sort_mode combination of GIT_SORT_XXX flags + */ +GIT_EXTERN(void) git_revwalk_sorting(git_revwalk *walk, unsigned int sort_mode); + +/** + * Free a revision walker previously allocated. + * + * @param walk traversal handle to close. If NULL nothing occurs. + */ +GIT_EXTERN(void) git_revwalk_free(git_revwalk *walk); + +/** + * Return the repository on which this walker + * is operating. + * + * @param walk the revision walker + * @return the repository being walked + */ +GIT_EXTERN(git_repository *) git_revwalk_repository(git_revwalk *walk); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/signature.h b/git2/include/git2/signature.h new file mode 100644 index 000000000..cbf94269f --- /dev/null +++ b/git2/include/git2/signature.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_signature_h__ +#define INCLUDE_git_signature_h__ + +#include "common.h" +#include "types.h" + +/** + * @file git2/signature.h + * @brief Git signature creation + * @defgroup git_signature Git signature creation + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Create a new action signature. The signature must be freed + * manually or using git_signature_free + * + * @param sig_out new signature, in case of error NULL + * @param name name of the person + * @param email email of the person + * @param time time when the action happened + * @param offset timezone offset in minutes for the time + * @return 0 or an error code + */ +GIT_EXTERN(int) git_signature_new(git_signature **sig_out, const char *name, const char *email, git_time_t time, int offset); + +/** + * Create a new action signature with a timestamp of 'now'. The + * signature must be freed manually or using git_signature_free + * + * @param sig_out new signature, in case of error NULL + * @param name name of the person + * @param email email of the person + * @return 0 or an error code + */ +GIT_EXTERN(int) git_signature_now(git_signature **sig_out, const char *name, const char *email); + + +/** + * Create a copy of an existing signature. + * + * All internal strings are also duplicated. + * @param sig signature to duplicated + * @return a copy of sig, NULL on out of memory + */ +GIT_EXTERN(git_signature *) git_signature_dup(const git_signature *sig); + +/** + * Free an existing signature + * + * @param sig signature to free + */ +GIT_EXTERN(void) git_signature_free(git_signature *sig); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/status.h b/git2/include/git2/status.h new file mode 100644 index 000000000..6a424dfd6 --- /dev/null +++ b/git2/include/git2/status.h @@ -0,0 +1,164 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_status_h__ +#define INCLUDE_git_status_h__ + +#include "common.h" +#include "types.h" + +/** + * @file git2/status.h + * @brief Git file status routines + * @defgroup git_status Git file status routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +enum { + GIT_STATUS_CURRENT = 0, + + GIT_STATUS_INDEX_NEW = (1 << 0), + GIT_STATUS_INDEX_MODIFIED = (1 << 1), + GIT_STATUS_INDEX_DELETED = (1 << 2), + + GIT_STATUS_WT_NEW = (1 << 3), + GIT_STATUS_WT_MODIFIED = (1 << 4), + GIT_STATUS_WT_DELETED = (1 << 5), + + GIT_STATUS_IGNORED = (1 << 6), +}; + +/** + * Gather file statuses and run a callback for each one. + * + * The callback is passed the path of the file, the status and the data + * pointer passed to this function. If the callback returns something other + * than 0, this function will return that value. + * + * @param repo a repository object + * @param callback the function to call on each file + * @return 0 on success or the return value of the callback that was non-zero + */ +GIT_EXTERN(int) git_status_foreach( + git_repository *repo, + int (*callback)(const char *, unsigned int, void *), + void *payload); + +/** + * Select the files on which to report status. + * + * - GIT_STATUS_SHOW_INDEX_AND_WORKDIR is the default. This is the + * rough equivalent of `git status --porcelain` where each file + * will receive a callback indicating its status in the index and + * in the workdir. + * - GIT_STATUS_SHOW_INDEX_ONLY will only make callbacks for index + * side of status. The status of the index contents relative to + * the HEAD will be given. + * - GIT_STATUS_SHOW_WORKDIR_ONLY will only make callbacks for the + * workdir side of status, reporting the status of workdir content + * relative to the index. + * - GIT_STATUS_SHOW_INDEX_THEN_WORKDIR behaves like index-only + * followed by workdir-only, causing two callbacks to be issued + * per file (first index then workdir). This is slightly more + * efficient than making separate calls. This makes it easier to + * emulate the output of a plain `git status`. + */ +typedef enum { + GIT_STATUS_SHOW_INDEX_AND_WORKDIR = 0, + GIT_STATUS_SHOW_INDEX_ONLY = 1, + GIT_STATUS_SHOW_WORKDIR_ONLY = 2, + GIT_STATUS_SHOW_INDEX_THEN_WORKDIR = 3, +} git_status_show_t; + +/** + * Flags to control status callbacks + * + * - GIT_STATUS_OPT_INCLUDE_UNTRACKED says that callbacks should + * be made on untracked files. These will only be made if the + * workdir files are included in the status "show" option. + * - GIT_STATUS_OPT_INCLUDE_IGNORED says that ignored files should + * get callbacks. Again, these callbacks will only be made if + * the workdir files are included in the status "show" option. + * Right now, there is no option to include all files in + * directories that are ignored completely. + * - GIT_STATUS_OPT_INCLUDE_UNMODIFIED indicates that callback + * should be made even on unmodified files. + * - GIT_STATUS_OPT_EXCLUDE_SUBMODULES indicates that directories + * which appear to be submodules should just be skipped over. + * - GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS indicates that the + * contents of untracked directories should be included in the + * status. Normally if an entire directory is new, then just + * the top-level directory will be included (with a trailing + * slash on the entry name). Given this flag, the directory + * itself will not be included, but all the files in it will. + */ + +enum { + GIT_STATUS_OPT_INCLUDE_UNTRACKED = (1 << 0), + GIT_STATUS_OPT_INCLUDE_IGNORED = (1 << 1), + GIT_STATUS_OPT_INCLUDE_UNMODIFIED = (1 << 2), + GIT_STATUS_OPT_EXCLUDE_SUBMODULED = (1 << 3), + GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS = (1 << 4), +}; + +/** + * Options to control how callbacks will be made by + * `git_status_foreach_ext()`. + */ +typedef struct { + git_status_show_t show; + unsigned int flags; + git_strarray pathspec; +} git_status_options; + +/** + * Gather file status information and run callbacks as requested. + */ +GIT_EXTERN(int) git_status_foreach_ext( + git_repository *repo, + const git_status_options *opts, + int (*callback)(const char *, unsigned int, void *), + void *payload); + +/** + * Get file status for a single file + * + * @param status_flags the status value + * @param repo a repository object + * @param path the file to retrieve status for, rooted at the repo's workdir + * @return GIT_EINVALIDPATH when `path` points at a folder, GIT_ENOTFOUND when + * the file doesn't exist in any of HEAD, the index or the worktree, + * 0 otherwise + */ +GIT_EXTERN(int) git_status_file( + unsigned int *status_flags, + git_repository *repo, + const char *path); + +/** + * Test if the ignore rules apply to a given file. + * + * This function simply checks the ignore rules to see if they would apply + * to the given file. Unlike git_status_file(), this indicates if the file + * would be ignored regardless of whether the file is already in the index + * or in the repository. + * + * @param ignored boolean returning 0 if the file is not ignored, 1 if it is + * @param repo a repository object + * @param path the file to check ignores for, rooted at the repo's workdir. + * @return 0 if ignore rules could be processed for the file (regardless + * of whether it exists or not), or an error < 0 if they could not. + */ +GIT_EXTERN(int) git_status_should_ignore( + int *ignored, + git_repository *repo, + const char *path); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/submodule.h b/git2/include/git2/submodule.h new file mode 100644 index 000000000..930168275 --- /dev/null +++ b/git2/include/git2/submodule.h @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_submodule_h__ +#define INCLUDE_git_submodule_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" + +/** + * @file git2/submodule.h + * @brief Git submodule management utilities + * @defgroup git_submodule Git submodule management routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +typedef enum { + GIT_SUBMODULE_UPDATE_CHECKOUT = 0, + GIT_SUBMODULE_UPDATE_REBASE = 1, + GIT_SUBMODULE_UPDATE_MERGE = 2 +} git_submodule_update_t; + +typedef enum { + GIT_SUBMODULE_IGNORE_ALL = 0, /* never dirty */ + GIT_SUBMODULE_IGNORE_DIRTY = 1, /* only dirty if HEAD moved */ + GIT_SUBMODULE_IGNORE_UNTRACKED = 2, /* dirty if tracked files change */ + GIT_SUBMODULE_IGNORE_NONE = 3 /* any change or untracked == dirty */ +} git_submodule_ignore_t; + +/** + * Description of submodule + * + * This record describes a submodule found in a repository. There + * should be an entry for every submodule found in the HEAD and for + * every submodule described in .gitmodules. The fields are as follows: + * + * - `name` is the name of the submodule from .gitmodules. + * - `path` is the path to the submodule from the repo working directory. + * It is almost always the same as `name`. + * - `url` is the url for the submodule. + * - `oid` is the HEAD SHA1 for the submodule. + * - `update` is a value from above - see gitmodules(5) update. + * - `ignore` is a value from above - see gitmodules(5) ignore. + * - `fetch_recurse` is 0 or 1 - see gitmodules(5) fetchRecurseSubmodules. + * - `refcount` is for internal use. + * + * If the submodule has been added to .gitmodules but not yet git added, + * then the `oid` will be zero. If the submodule has been deleted, but + * the delete has not been committed yet, then the `oid` will be set, but + * the `url` will be NULL. + */ +typedef struct { + char *name; + char *path; + char *url; + git_oid oid; /* sha1 of submodule HEAD ref or zero if not committed */ + git_submodule_update_t update; + git_submodule_ignore_t ignore; + int fetch_recurse; + int refcount; +} git_submodule; + +/** + * Iterate over all submodules of a repository. + * + * @param repo The repository + * @param callback Function to be called with the name of each submodule. + * Return a non-zero value to terminate the iteration. + * @param payload Extra data to pass to callback + * @return 0 on success, -1 on error, or non-zero return value of callback + */ +GIT_EXTERN(int) git_submodule_foreach( + git_repository *repo, + int (*callback)(const char *name, void *payload), + void *payload); + +/** + * Lookup submodule information by name or path. + * + * Given either the submodule name or path (they are ususally the same), + * this returns a structure describing the submodule. If the submodule + * does not exist, this will return GIT_ENOTFOUND and set the submodule + * pointer to NULL. + * + * @param submodule Pointer to submodule description object pointer.. + * @param repo The repository. + * @param name The name of the submodule. Trailing slashes will be ignored. + * @return 0 on success, GIT_ENOTFOUND if submodule does not exist, -1 on error + */ +GIT_EXTERN(int) git_submodule_lookup( + git_submodule **submodule, + git_repository *repo, + const char *name); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/tag.h b/git2/include/git2/tag.h new file mode 100644 index 000000000..859c28995 --- /dev/null +++ b/git2/include/git2/tag.h @@ -0,0 +1,297 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_tag_h__ +#define INCLUDE_git_tag_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" +#include "object.h" + +/** + * @file git2/tag.h + * @brief Git tag parsing routines + * @defgroup git_tag Git tag management + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Lookup a tag object from the repository. + * + * @param tag pointer to the looked up tag + * @param repo the repo to use when locating the tag. + * @param id identity of the tag to locate. + * @return 0 or an error code + */ +GIT_INLINE(int) git_tag_lookup(git_tag **tag, git_repository *repo, const git_oid *id) +{ + return git_object_lookup((git_object **)tag, repo, id, (git_otype)GIT_OBJ_TAG); +} + +/** + * Lookup a tag object from the repository, + * given a prefix of its identifier (short id). + * + * @see git_object_lookup_prefix + * + * @param tag pointer to the looked up tag + * @param repo the repo to use when locating the tag. + * @param id identity of the tag to locate. + * @param len the length of the short identifier + * @return 0 or an error code + */ +GIT_INLINE(int) git_tag_lookup_prefix(git_tag **tag, git_repository *repo, const git_oid *id, unsigned int len) +{ + return git_object_lookup_prefix((git_object **)tag, repo, id, len, (git_otype)GIT_OBJ_TAG); +} + +/** + * Close an open tag + * + * This is a wrapper around git_object_free() + * + * IMPORTANT: + * It *is* necessary to call this method when you stop + * using a tag. Failure to do so will cause a memory leak. + * + * @param tag the tag to close + */ + +GIT_INLINE(void) git_tag_free(git_tag *tag) +{ + git_object_free((git_object *) tag); +} + + +/** + * Get the id of a tag. + * + * @param tag a previously loaded tag. + * @return object identity for the tag. + */ +GIT_EXTERN(const git_oid *) git_tag_id(git_tag *tag); + +/** + * Get the tagged object of a tag + * + * This method performs a repository lookup for the + * given object and returns it + * + * @param target pointer where to store the target + * @param tag a previously loaded tag. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_tag_target(git_object **target, git_tag *tag); + +/** + * Get the OID of the tagged object of a tag + * + * @param tag a previously loaded tag. + * @return pointer to the OID + */ +GIT_EXTERN(const git_oid *) git_tag_target_oid(git_tag *tag); + +/** + * Get the type of a tag's tagged object + * + * @param tag a previously loaded tag. + * @return type of the tagged object + */ +GIT_EXTERN(git_otype) git_tag_type(git_tag *tag); + +/** + * Get the name of a tag + * + * @param tag a previously loaded tag. + * @return name of the tag + */ +GIT_EXTERN(const char *) git_tag_name(git_tag *tag); + +/** + * Get the tagger (author) of a tag + * + * @param tag a previously loaded tag. + * @return reference to the tag's author + */ +GIT_EXTERN(const git_signature *) git_tag_tagger(git_tag *tag); + +/** + * Get the message of a tag + * + * @param tag a previously loaded tag. + * @return message of the tag + */ +GIT_EXTERN(const char *) git_tag_message(git_tag *tag); + + +/** + * Create a new tag in the repository from an object + * + * A new reference will also be created pointing to + * this tag object. If `force` is true and a reference + * already exists with the given name, it'll be replaced. + * + * The message will be cleaned up from excess whitespace + * it will be made sure that the last line ends with a '\n'. + * + * @param oid Pointer where to store the OID of the + * newly created tag. If the tag already exists, this parameter + * will be the oid of the existing tag, and the function will + * return a GIT_EEXISTS error code. + * + * @param repo Repository where to store the tag + * + * @param tag_name Name for the tag; this name is validated + * for consistency. It should also not conflict with an + * already existing tag name + * + * @param target Object to which this tag points. This object + * must belong to the given `repo`. + * + * @param tagger Signature of the tagger for this tag, and + * of the tagging time + * + * @param message Full message for this tag + * + * @param force Overwrite existing references + * + * @return 0 or an error code + * A tag object is written to the ODB, and a proper reference + * is written in the /refs/tags folder, pointing to it + */ +GIT_EXTERN(int) git_tag_create( + git_oid *oid, + git_repository *repo, + const char *tag_name, + const git_object *target, + const git_signature *tagger, + const char *message, + int force); + +/** + * Create a new tag in the repository from a buffer + * + * @param oid Pointer where to store the OID of the newly created tag + * @param repo Repository where to store the tag + * @param buffer Raw tag data + * @param force Overwrite existing tags + * @return 0 on sucess; error code otherwise + */ +GIT_EXTERN(int) git_tag_create_frombuffer( + git_oid *oid, + git_repository *repo, + const char *buffer, + int force); + +/** + * Create a new lightweight tag pointing at a target object + * + * A new direct reference will be created pointing to + * this target object. If `force` is true and a reference + * already exists with the given name, it'll be replaced. + * + * @param oid Pointer where to store the OID of the provided + * target object. If the tag already exists, this parameter + * will be filled with the oid of the existing pointed object + * and the function will return a GIT_EEXISTS error code. + * + * @param repo Repository where to store the lightweight tag + * + * @param tag_name Name for the tag; this name is validated + * for consistency. It should also not conflict with an + * already existing tag name + * + * @param target Object to which this tag points. This object + * must belong to the given `repo`. + * + * @param force Overwrite existing references + * + * @return 0 or an error code + * A proper reference is written in the /refs/tags folder, + * pointing to the provided target object + */ +GIT_EXTERN(int) git_tag_create_lightweight( + git_oid *oid, + git_repository *repo, + const char *tag_name, + const git_object *target, + int force); + +/** + * Delete an existing tag reference. + * + * @param repo Repository where lives the tag + * + * @param tag_name Name of the tag to be deleted; + * this name is validated for consistency. + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_tag_delete( + git_repository *repo, + const char *tag_name); + +/** + * Fill a list with all the tags in the Repository + * + * The string array will be filled with the names of the + * matching tags; these values are owned by the user and + * should be free'd manually when no longer needed, using + * `git_strarray_free`. + * + * @param tag_names Pointer to a git_strarray structure where + * the tag names will be stored + * @param repo Repository where to find the tags + * @return 0 or an error code + */ +GIT_EXTERN(int) git_tag_list( + git_strarray *tag_names, + git_repository *repo); + +/** + * Fill a list with all the tags in the Repository + * which name match a defined pattern + * + * If an empty pattern is provided, all the tags + * will be returned. + * + * The string array will be filled with the names of the + * matching tags; these values are owned by the user and + * should be free'd manually when no longer needed, using + * `git_strarray_free`. + * + * @param tag_names Pointer to a git_strarray structure where + * the tag names will be stored + * @param pattern Standard fnmatch pattern + * @param repo Repository where to find the tags + * @return 0 or an error code + */ +GIT_EXTERN(int) git_tag_list_match( + git_strarray *tag_names, + const char *pattern, + git_repository *repo); + +/** + * Recursively peel a tag until a non tag git_object + * is met + * + * The retrieved `tag_target` object is owned by the repository + * and should be closed with the `git_object_free` method. + * + * @param tag_target Pointer to the peeled git_object + * @param tag The tag to be processed + * @return 0 or an error code + */ +GIT_EXTERN(int) git_tag_peel( + git_object **tag_target, + git_tag *tag); + +/** @} */ +GIT_END_DECL +#endif diff --git a/git2/include/git2/threads.h b/git2/include/git2/threads.h new file mode 100644 index 000000000..567a10487 --- /dev/null +++ b/git2/include/git2/threads.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_threads_h__ +#define INCLUDE_git_threads_h__ + +#include "common.h" + +/** + * @file git2/threads.h + * @brief Library level thread functions + * @defgroup git_thread Threading functions + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Init the threading system. + * + * If libgit2 has been built with GIT_THREADS + * on, this function must be called once before + * any other library functions. + * + * If libgit2 has been built without GIT_THREADS + * support, this function is a no-op. + */ +GIT_EXTERN(void) git_threads_init(void); + +/** + * Shutdown the threading system. + * + * If libgit2 has been built with GIT_THREADS + * on, this function must be called before shutting + * down the library. + * + * If libgit2 has been built without GIT_THREADS + * support, this function is a no-op. + */ +GIT_EXTERN(void) git_threads_shutdown(void); + +/** @} */ +GIT_END_DECL +#endif + diff --git a/git2/include/git2/tree.h b/git2/include/git2/tree.h new file mode 100644 index 000000000..777f8ff0d --- /dev/null +++ b/git2/include/git2/tree.h @@ -0,0 +1,318 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_tree_h__ +#define INCLUDE_git_tree_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" +#include "object.h" + +/** + * @file git2/tree.h + * @brief Git tree parsing, loading routines + * @defgroup git_tree Git tree parsing, loading routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Lookup a tree object from the repository. + * + * @param tree pointer to the looked up tree + * @param repo the repo to use when locating the tree. + * @param id identity of the tree to locate. + * @return 0 or an error code + */ +GIT_INLINE(int) git_tree_lookup(git_tree **tree, git_repository *repo, const git_oid *id) +{ + return git_object_lookup((git_object **)tree, repo, id, GIT_OBJ_TREE); +} + +/** + * Lookup a tree object from the repository, + * given a prefix of its identifier (short id). + * + * @see git_object_lookup_prefix + * + * @param tree pointer to the looked up tree + * @param repo the repo to use when locating the tree. + * @param id identity of the tree to locate. + * @param len the length of the short identifier + * @return 0 or an error code + */ +GIT_INLINE(int) git_tree_lookup_prefix(git_tree **tree, git_repository *repo, const git_oid *id, unsigned int len) +{ + return git_object_lookup_prefix((git_object **)tree, repo, id, len, GIT_OBJ_TREE); +} + +/** + * Close an open tree + * + * This is a wrapper around git_object_free() + * + * IMPORTANT: + * It *is* necessary to call this method when you stop + * using a tree. Failure to do so will cause a memory leak. + * + * @param tree the tree to close + */ + +GIT_INLINE(void) git_tree_free(git_tree *tree) +{ + git_object_free((git_object *) tree); +} + + +/** + * Get the id of a tree. + * + * @param tree a previously loaded tree. + * @return object identity for the tree. + */ +GIT_EXTERN(const git_oid *) git_tree_id(git_tree *tree); + +/** + * Get the number of entries listed in a tree + * + * @param tree a previously loaded tree. + * @return the number of entries in the tree + */ +GIT_EXTERN(unsigned int) git_tree_entrycount(git_tree *tree); + +/** + * Lookup a tree entry by its filename + * + * @param tree a previously loaded tree. + * @param filename the filename of the desired entry + * @return the tree entry; NULL if not found + */ +GIT_EXTERN(const git_tree_entry *) git_tree_entry_byname(git_tree *tree, const char *filename); + +/** + * Lookup a tree entry by its position in the tree + * + * @param tree a previously loaded tree. + * @param idx the position in the entry list + * @return the tree entry; NULL if not found + */ +GIT_EXTERN(const git_tree_entry *) git_tree_entry_byindex(git_tree *tree, unsigned int idx); + +/** + * Get the UNIX file attributes of a tree entry + * + * @param entry a tree entry + * @return attributes as an integer + */ +GIT_EXTERN(unsigned int) git_tree_entry_attributes(const git_tree_entry *entry); + +/** + * Get the filename of a tree entry + * + * @param entry a tree entry + * @return the name of the file + */ +GIT_EXTERN(const char *) git_tree_entry_name(const git_tree_entry *entry); + +/** + * Get the id of the object pointed by the entry + * + * @param entry a tree entry + * @return the oid of the object + */ +GIT_EXTERN(const git_oid *) git_tree_entry_id(const git_tree_entry *entry); + +/** + * Get the type of the object pointed by the entry + * + * @param entry a tree entry + * @return the type of the pointed object + */ +GIT_EXTERN(git_otype) git_tree_entry_type(const git_tree_entry *entry); + +/** + * Convert a tree entry to the git_object it points too. + * + * @param object pointer to the converted object + * @param repo repository where to lookup the pointed object + * @param entry a tree entry + * @return 0 or an error code + */ +GIT_EXTERN(int) git_tree_entry_to_object(git_object **object_out, git_repository *repo, const git_tree_entry *entry); + +/** + * Write a tree to the ODB from the index file + * + * This method will scan the index and write a representation + * of its current state back to disk; it recursively creates + * tree objects for each of the subtrees stored in the index, + * but only returns the OID of the root tree. This is the OID + * that can be used e.g. to create a commit. + * + * The index instance cannot be bare, and needs to be associated + * to an existing repository. + * + * @param oid Pointer where to store the written tree + * @param index Index to write + * @return 0 or an error code + */ +GIT_EXTERN(int) git_tree_create_fromindex(git_oid *oid, git_index *index); + +/** + * Create a new tree builder. + * + * The tree builder can be used to create or modify + * trees in memory and write them as tree objects to the + * database. + * + * If the `source` parameter is not NULL, the tree builder + * will be initialized with the entries of the given tree. + * + * If the `source` parameter is NULL, the tree builder will + * have no entries and will have to be filled manually. + * + * @param builder_p Pointer where to store the tree builder + * @param source Source tree to initialize the builder (optional) + * @return 0 on sucess; error code otherwise + */ +GIT_EXTERN(int) git_treebuilder_create(git_treebuilder **builder_p, const git_tree *source); + +/** + * Clear all the entires in the builder + * + * @param bld Builder to clear + */ +GIT_EXTERN(void) git_treebuilder_clear(git_treebuilder *bld); + +/** + * Free a tree builder + * + * This will clear all the entries and free to builder. + * Failing to free the builder after you're done using it + * will result in a memory leak + * + * @param bld Builder to free + */ +GIT_EXTERN(void) git_treebuilder_free(git_treebuilder *bld); + +/** + * Get an entry from the builder from its filename + * + * The returned entry is owned by the builder and should + * not be freed manually. + * + * @param bld Tree builder + * @param filename Name of the entry + * @return pointer to the entry; NULL if not found + */ +GIT_EXTERN(const git_tree_entry *) git_treebuilder_get(git_treebuilder *bld, const char *filename); + +/** + * Add or update an entry to the builder + * + * Insert a new entry for `filename` in the builder with the + * given attributes. + * + * if an entry named `filename` already exists, its attributes + * will be updated with the given ones. + * + * The optional pointer `entry_out` can be used to retrieve a + * pointer to the newly created/updated entry. + * + * @param entry_out Pointer to store the entry (optional) + * @param bld Tree builder + * @param filename Filename of the entry + * @param id SHA1 oid of the entry + * @param attributes Folder attributes of the entry + * @return 0 or an error code + */ +GIT_EXTERN(int) git_treebuilder_insert(git_tree_entry **entry_out, git_treebuilder *bld, const char *filename, const git_oid *id, unsigned int attributes); + +/** + * Remove an entry from the builder by its filename + * + * @param bld Tree builder + * @param filename Filename of the entry to remove + */ +GIT_EXTERN(int) git_treebuilder_remove(git_treebuilder *bld, const char *filename); + +/** + * Filter the entries in the tree + * + * The `filter` callback will be called for each entry + * in the tree with a pointer to the entry and the + * provided `payload`: if the callback returns 1, the + * entry will be filtered (removed from the builder). + * + * @param bld Tree builder + * @param filter Callback to filter entries + */ +GIT_EXTERN(void) git_treebuilder_filter(git_treebuilder *bld, int (*filter)(const git_tree_entry *, void *), void *payload); + +/** + * Write the contents of the tree builder as a tree object + * + * The tree builder will be written to the given `repo`, and + * it's identifying SHA1 hash will be stored in the `oid` + * pointer. + * + * @param oid Pointer where to store the written OID + * @param repo Repository where to store the object + * @param bld Tree builder to write + * @return 0 or an error code + */ +GIT_EXTERN(int) git_treebuilder_write(git_oid *oid, git_repository *repo, git_treebuilder *bld); + +/** + * Retrieve a subtree contained in a tree, given its + * relative path. + * + * The returned tree is owned by the repository and + * should be closed with the `git_object_free` method. + * + * @param subtree Pointer where to store the subtree + * @param root A previously loaded tree which will be the root of the relative path + * @param subtree_path Path to the contained subtree + * @return 0 on success; GIT_ENOTFOUND if the path does not lead to a subtree + */ +GIT_EXTERN(int) git_tree_get_subtree(git_tree **subtree, git_tree *root, const char *subtree_path); + +/** Callback for the tree traversal method */ +typedef int (*git_treewalk_cb)(const char *root, git_tree_entry *entry, void *payload); + +/** Tree traversal modes */ +enum git_treewalk_mode { + GIT_TREEWALK_PRE = 0, /* Pre-order */ + GIT_TREEWALK_POST = 1, /* Post-order */ +}; + +/** + * Traverse the entries in a tree and its subtrees in + * post or pre order + * + * The entries will be traversed in the specified order, + * children subtrees will be automatically loaded as required, + * and the `callback` will be called once per entry with + * the current (relative) root for the entry and the entry + * data itself. + * + * If the callback returns a negative value, the passed entry + * will be skiped on the traversal. + * + * @param tree The tree to walk + * @param callback Function to call on each tree entry + * @param mode Traversal mode (pre or post-order) + * @param payload Opaque pointer to be passed on each callback + * @return 0 or an error code + */ +GIT_EXTERN(int) git_tree_walk(git_tree *tree, git_treewalk_cb callback, int mode, void *payload); + +/** @} */ + +GIT_END_DECL +#endif diff --git a/git2/include/git2/types.h b/git2/include/git2/types.h new file mode 100644 index 000000000..cfb0acf33 --- /dev/null +++ b/git2/include/git2/types.h @@ -0,0 +1,177 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_types_h__ +#define INCLUDE_git_types_h__ + +#include "common.h" + +/** + * @file git2/types.h + * @brief libgit2 base & compatibility types + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Cross-platform compatibility types for off_t / time_t + * + * NOTE: This needs to be in a public header so that both the library + * implementation and client applications both agree on the same types. + * Otherwise we get undefined behavior. + * + * Use the "best" types that each platform provides. Currently we truncate + * these intermediate representations for compatibility with the git ABI, but + * if and when it changes to support 64 bit types, our code will naturally + * adapt. + * NOTE: These types should match those that are returned by our internal + * stat() functions, for all platforms. + */ +#include + +#if defined(_MSC_VER) + +typedef __int64 git_off_t; +typedef __time64_t git_time_t; + +#elif defined(__MINGW32__) + +typedef off64_t git_off_t; +typedef __time64_t git_time_t; + +#elif defined(__HAIKU__) + +typedef __haiku_std_int64 git_off_t; +typedef __haiku_std_int64 git_time_t; + +#else /* POSIX */ + +/* + * Note: Can't use off_t since if a client program includes + * before us (directly or indirectly), they'll get 32 bit off_t in their client + * app, even though /we/ define _FILE_OFFSET_BITS=64. + */ +typedef int64_t git_off_t; +typedef int64_t git_time_t; + +#endif + +/** Basic type (loose or packed) of any Git object. */ +typedef enum { + GIT_OBJ_ANY = -2, /**< Object can be any of the following */ + GIT_OBJ_BAD = -1, /**< Object is invalid. */ + GIT_OBJ__EXT1 = 0, /**< Reserved for future use. */ + GIT_OBJ_COMMIT = 1, /**< A commit object. */ + GIT_OBJ_TREE = 2, /**< A tree (directory listing) object. */ + GIT_OBJ_BLOB = 3, /**< A file revision object. */ + GIT_OBJ_TAG = 4, /**< An annotated tag object. */ + GIT_OBJ__EXT2 = 5, /**< Reserved for future use. */ + GIT_OBJ_OFS_DELTA = 6, /**< A delta, base is given by an offset. */ + GIT_OBJ_REF_DELTA = 7, /**< A delta, base is given by object id. */ +} git_otype; + +/** An open object database handle. */ +typedef struct git_odb git_odb; + +/** A custom backend in an ODB */ +typedef struct git_odb_backend git_odb_backend; + +/** An object read from the ODB */ +typedef struct git_odb_object git_odb_object; + +/** A stream to read/write from the ODB */ +typedef struct git_odb_stream git_odb_stream; + +/** + * Representation of an existing git repository, + * including all its object contents + */ +typedef struct git_repository git_repository; + +/** Representation of a generic object in a repository */ +typedef struct git_object git_object; + +/** Representation of an in-progress walk through the commits in a repo */ +typedef struct git_revwalk git_revwalk; + +/** Parsed representation of a tag object. */ +typedef struct git_tag git_tag; + +/** In-memory representation of a blob object. */ +typedef struct git_blob git_blob; + +/** Parsed representation of a commit object. */ +typedef struct git_commit git_commit; + +/** Representation of each one of the entries in a tree object. */ +typedef struct git_tree_entry git_tree_entry; + +/** Representation of a tree object. */ +typedef struct git_tree git_tree; + +/** Constructor for in-memory trees */ +typedef struct git_treebuilder git_treebuilder; + +/** Memory representation of an index file. */ +typedef struct git_index git_index; + +/** Memory representation of a set of config files */ +typedef struct git_config git_config; + +/** Interface to access a configuration file */ +typedef struct git_config_file git_config_file; + +/** Representation of a reference log entry */ +typedef struct git_reflog_entry git_reflog_entry; + +/** Representation of a reference log */ +typedef struct git_reflog git_reflog; + +/** Representation of a git note */ +typedef struct git_note git_note; + +/** Time in a signature */ +typedef struct git_time { + git_time_t time; /** time in seconds from epoch */ + int offset; /** timezone offset, in minutes */ +} git_time; + +/** An action signature (e.g. for committers, taggers, etc) */ +typedef struct git_signature { + char *name; /** full name of the author */ + char *email; /** email of the author */ + git_time when; /** time when the action happened */ +} git_signature; + +/** In-memory representation of a reference. */ +typedef struct git_reference git_reference; + +/** Basic type of any Git reference. */ +typedef enum { + GIT_REF_INVALID = 0, /** Invalid reference */ + GIT_REF_OID = 1, /** A reference which points at an object id */ + GIT_REF_SYMBOLIC = 2, /** A reference which points at another reference */ + GIT_REF_PACKED = 4, + GIT_REF_HAS_PEEL = 8, + GIT_REF_LISTALL = GIT_REF_OID|GIT_REF_SYMBOLIC|GIT_REF_PACKED, +} git_ref_t; + +/** Basic type of any Git branch. */ +typedef enum { + GIT_BRANCH_LOCAL = 1, + GIT_BRANCH_REMOTE = 2, +} git_branch_t; + +typedef struct git_refspec git_refspec; +typedef struct git_remote git_remote; + +typedef struct git_remote_head git_remote_head; + +/** @} */ +GIT_END_DECL + +#endif diff --git a/git2/include/git2/version.h b/git2/include/git2/version.h new file mode 100644 index 000000000..8edbe323c --- /dev/null +++ b/git2/include/git2/version.h @@ -0,0 +1,15 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_version_h__ +#define INCLUDE_git_version_h__ + +#define LIBGIT2_VERSION "0.17.0" +#define LIBGIT2_VER_MAJOR 0 +#define LIBGIT2_VER_MINOR 17 +#define LIBGIT2_VER_REVISION 0 + +#endif diff --git a/git2/include/git2/windows.h b/git2/include/git2/windows.h new file mode 100644 index 000000000..8b743f0aa --- /dev/null +++ b/git2/include/git2/windows.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_windows_h__ +#define INCLUDE_git_windows_h__ + +#include "common.h" + +/** + * @file git2/windows.h + * @brief Windows-specific functions + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Set the active codepage for Windows syscalls + * + * All syscalls performed by the library will assume + * this codepage when converting paths and strings + * to use by the Windows kernel. + * + * The default value of UTF-8 will work automatically + * with most Git repositories created on Unix systems. + * + * This settings needs only be changed when working + * with repositories that contain paths in specific, + * non-UTF codepages. + * + * A full list of all available codepage identifiers may + * be found at: + * + * http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx + * + * @param codepage numeric codepage identifier + */ +GIT_EXTERN(void) gitwin_set_codepage(unsigned int codepage); + +/** + * Return the active codepage for Windows syscalls + * + * @return numeric codepage identifier + */ +GIT_EXTERN(unsigned int) gitwin_get_codepage(void); + +/** + * Set the active Windows codepage to UTF-8 (this is + * the default value) + */ +GIT_EXTERN(void) gitwin_set_utf8(void); + +/** @} */ +GIT_END_DECL +#endif +