diff options
author | Vicent Marti <tanoku@gmail.com> | 2011-01-29 01:56:25 +0200 |
---|---|---|
committer | Vicent Marti <tanoku@gmail.com> | 2011-01-30 02:35:29 +0200 |
commit | 2f8a8ab24b034cb91e6ce80a7aa8cc330b7baebf (patch) | |
tree | 4729e6dc622a3c5a5da31f126b2e03091c597e14 /src/git2 | |
parent | 9282e921a394d422188ee43e18a63d418f88ac95 (diff) | |
download | libgit2-2f8a8ab24b034cb91e6ce80a7aa8cc330b7baebf.tar.gz |
Refactor reference parsing code
Several changes have been committed to allow the user to create
in-memory references and write back to disk. Peeling of symbolic
references has been made explicit. Added getter and setter methods for
all attributes on a reference. Added corresponding documentation.
Signed-off-by: Vicent Marti <tanoku@gmail.com>
Diffstat (limited to 'src/git2')
-rw-r--r-- | src/git2/refs.h | 175 | ||||
-rw-r--r-- | src/git2/repository.h | 2 | ||||
-rw-r--r-- | src/git2/types.h | 12 |
3 files changed, 179 insertions, 10 deletions
diff --git a/src/git2/refs.h b/src/git2/refs.h new file mode 100644 index 000000000..c9efac34f --- /dev/null +++ b/src/git2/refs.h @@ -0,0 +1,175 @@ +/* + * This file is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, + * as published by the Free Software Foundation. + * + * In addition to the permissions in the GNU General Public License, + * the authors give you unlimited permission to link the compiled + * version of this file into combinations with other programs, + * and to distribute those combinations without any restriction + * coming from the use of this file. (The General Public License + * restrictions do apply in other respects; for example, they cover + * modification of the file, and distribution when not linked into + * a combined executable.) + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ +#ifndef INCLUDE_git_refs_h__ +#define INCLUDE_git_refs_h__ + +#include "common.h" +#include "types.h" + +/** + * @file git2/refs.h + * @brief Git reference management routines + * @defgroup git_reference Git reference management routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Create a new reference. + * + * The reference will be empty and exclusively + * in-memory until it is filled with the setter + * methods and written back to disk using + * `git_reference_write`. + * + * @param ref_out Pointer to the newly created reference + * @param repo Repository where that reference exists + * @return 0 on success; error code otherwise + */ +GIT_EXTERN(int) git_reference_new(git_reference **ref_out, git_repository *repo); + +/** + * 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_rtype) 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. + * + * If a direct reference is passed as an argument, + * that reference is returned immediately + * + * @param resolved_ref Pointer to the peeled reference + * @param ref The reference + * @return 0 on success; error code otherwise + */ +GIT_EXTERN(int) git_reference_resolve(git_reference **resolved_ref, git_reference *ref); + +/** + * Write a reference back to disk. + * + * The reference must have a valid name and a valid target + * (either direct or symbolic). + * + * If the reference has been loaded from disk and no changes + * have been made, no action will take place. + * + * The writing to disk is atomic. + * + * @param ref The reference + * @return 0 on success; error code otherwise + */ +GIT_EXTERN(int) git_reference_write(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 name of a reference. + * + * This marks the reference as modified; changes + * won't take effect until it is manually written back + * to disk. + * + * @param ref The reference + * @param name The new name for the reference + */ +GIT_EXTERN(void) git_reference_set_name(git_reference *ref, const char *name); + +/** + * Set the target reference of a reference. + * + * This converts the reference into a symbolic + * reference. + * + * This marks the reference as modified; changes + * won't take effect until it is manually written back + * to disk. + * + * @param ref The reference + * @param target The new target for the reference + */ +GIT_EXTERN(void) git_reference_set_target(git_reference *ref, const char *target); + +/** + * Set the OID target of a reference. + * + * This converts the reference into a direct + * reference. + * + * This marks the reference as modified; changes + * won't take effect until it is manually written back + * to disk. + * + * @param ref The reference + * @param target The new target OID for the reference + */ +GIT_EXTERN(void) git_reference_set_oid(git_reference *ref, const git_oid *id); + +/** @} */ +GIT_END_DECL +#endif diff --git a/src/git2/repository.h b/src/git2/repository.h index 129d3bb5f..ecf3db99f 100644 --- a/src/git2/repository.h +++ b/src/git2/repository.h @@ -229,7 +229,7 @@ GIT_EXTERN(int) git_repository_init(git_repository **repo_out, const char *path, * @param name the long name for the reference (e.g. HEAD, ref/heads/master, refs/tags/v0.1.0, ...) * @return a reference to the reference */ -GIT_EXTERN(int) git_repository_reference_lookup(git_reference **reference_out, git_repository *repo, const char *name); +GIT_EXTERN(int) git_repository_lookup_ref(git_reference **reference_out, git_repository *repo, const char *name); /** @} */ GIT_END_DECL diff --git a/src/git2/types.h b/src/git2/types.h index 389f32868..4f66742f0 100644 --- a/src/git2/types.h +++ b/src/git2/types.h @@ -138,17 +138,11 @@ typedef struct git_signature { /** In-memory representation of a reference. */ typedef struct git_reference git_reference; -/** In-memory representation of a reference to an object id. */ -typedef struct git_reference_object_id git_reference_object_id; - -/** In-memory representation of a reference which points at another reference. The target reference is embedded. */ -typedef struct git_reference_symbolic git_reference_symbolic; - /** Basic type of any Git reference. */ typedef enum { - GIT_REF_ANY = -2, /** Reference can be any of the following */ - GIT_REF_OBJECT_ID = 0, /** A reference which points at an object id */ - GIT_REF_SYMBOLIC = 1, /** A reference which points at another reference */ + GIT_REF_INVALID = -1, /** 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_rtype; /** @} */ |