summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVicent Martí <vicent@github.com>2013-04-22 04:06:11 -0700
committerVicent Martí <vicent@github.com>2013-04-22 04:06:11 -0700
commitf063a75882769cb6fc652de425ac16ba4b88b616 (patch)
tree8c9f02ac9b2e46846367098d3b6328c01cac7781 /src
parentbfb4facb3a5e0b650811560fc49d27247fd9d85d (diff)
parent21ca045100337bcb2905a20a72d42721d18871f9 (diff)
downloadlibgit2-f063a75882769cb6fc652de425ac16ba4b88b616.tar.gz
Merge pull request #1485 from libgit2/include-git2-sys
Create include/git2/sys and move backend APIs there
Diffstat (limited to 'src')
-rw-r--r--src/blob.c1
-rw-r--r--src/commit.c87
-rw-r--r--src/config.c1
-rw-r--r--src/config_file.c1
-rw-r--r--src/odb.c22
-rw-r--r--src/odb_loose.c2
-rw-r--r--src/odb_pack.c3
-rw-r--r--src/refdb.c37
-rw-r--r--src/refdb.h2
-rw-r--r--src/refdb_fs.c31
-rw-r--r--src/refs.c49
-rw-r--r--src/repository.c7
-rw-r--r--src/submodule.c1
-rw-r--r--src/tag.c1
-rw-r--r--src/transports/smart_protocol.c1
-rw-r--r--src/util.h2
16 files changed, 159 insertions, 89 deletions
diff --git a/src/blob.c b/src/blob.c
index c0514fc13..11e1f4d77 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -8,6 +8,7 @@
#include "git2/common.h"
#include "git2/object.h"
#include "git2/repository.h"
+#include "git2/odb_backend.h"
#include "common.h"
#include "blob.h"
diff --git a/src/commit.c b/src/commit.c
index c7b83ed43..dd416920d 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -9,6 +9,7 @@
#include "git2/object.h"
#include "git2/repository.h"
#include "git2/signature.h"
+#include "git2/sys/commit.h"
#include "common.h"
#include "odb.h"
@@ -44,16 +45,16 @@ void git_commit__free(git_commit *commit)
}
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_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,
+ ...)
{
va_list ap;
int i, res;
@@ -76,30 +77,29 @@ int git_commit_create_v(
return res;
}
-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[])
+int git_commit_create_from_oids(
+ 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_oid *tree,
+ int parent_count,
+ const git_oid *parents[])
{
git_buf commit = GIT_BUF_INIT;
int i;
git_odb *odb;
+ assert(oid && repo && tree && parent_count >= 0);
assert(git_object_owner((const git_object *)tree) == repo);
- git_oid__writebuf(&commit, "tree ", git_object_id((const git_object *)tree));
+ git_oid__writebuf(&commit, "tree ", tree);
- for (i = 0; i < parent_count; ++i) {
- assert(git_object_owner((const git_object *)parents[i]) == repo);
- git_oid__writebuf(&commit, "parent ", git_object_id((const git_object *)parents[i]));
- }
+ for (i = 0; i < parent_count; ++i)
+ git_oid__writebuf(&commit, "parent ", parents[i]);
git_signature__writebuf(&commit, "author ", author);
git_signature__writebuf(&commit, "committer ", committer);
@@ -131,6 +131,41 @@ on_error:
return -1;
}
+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[])
+{
+ int retval, i;
+ const git_oid **parent_oids;
+
+ assert(parent_count >= 0);
+
+ parent_oids = git__malloc(parent_count * sizeof(git_oid *));
+ GITERR_CHECK_ALLOC(parent_oids);
+
+ for (i = 0; i < parent_count; ++i) {
+ assert(git_object_owner((const git_object *)parents[i]) == repo);
+ parent_oids[i] = git_object_id((const git_object *)parents[i]);
+ }
+
+ retval = git_commit_create_from_oids(
+ oid, repo, update_ref, author, committer,
+ message_encoding, message,
+ git_object_id((const git_object *)tree), parent_count, parent_oids);
+
+ git__free((void *)parent_oids);
+
+ return retval;
+}
+
int git_commit__parse_buffer(git_commit *commit, const void *data, size_t len)
{
const char *buffer = data;
diff --git a/src/config.c b/src/config.c
index 5379b0ec5..1283522ca 100644
--- a/src/config.c
+++ b/src/config.c
@@ -9,6 +9,7 @@
#include "fileops.h"
#include "config.h"
#include "git2/config.h"
+#include "git2/sys/config.h"
#include "vector.h"
#include "buf_text.h"
#include "config_file.h"
diff --git a/src/config_file.c b/src/config_file.c
index 8b51ab21b..a4ff8bb94 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -12,6 +12,7 @@
#include "buffer.h"
#include "buf_text.h"
#include "git2/config.h"
+#include "git2/sys/config.h"
#include "git2/types.h"
#include "strmap.h"
diff --git a/src/odb.c b/src/odb.c
index c98df247c..ecdaf7ac2 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -8,6 +8,7 @@
#include "common.h"
#include <zlib.h>
#include "git2/object.h"
+#include "git2/sys/odb_backend.h"
#include "fileops.h"
#include "hash.h"
#include "odb.h"
@@ -403,6 +404,27 @@ int git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, int priority)
return add_backend_internal(odb, backend, priority, 1);
}
+size_t git_odb_num_backends(git_odb *odb)
+{
+ assert(odb);
+ return odb->backends.length;
+}
+
+int git_odb_get_backend(git_odb_backend **out, git_odb *odb, size_t pos)
+{
+ backend_internal *internal;
+
+ assert(odb && odb);
+ internal = git_vector_get(&odb->backends, pos);
+
+ if (internal && internal->backend) {
+ *out = internal->backend;
+ return 0;
+ }
+
+ return GIT_ENOTFOUND;
+}
+
static int add_default_backends(git_odb *db, const char *objects_dir, int as_alternates, int alternate_depth)
{
git_odb_backend *loose, *packed;
diff --git a/src/odb_loose.c b/src/odb_loose.c
index 68083f7fd..e78172cf6 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -8,7 +8,7 @@
#include "common.h"
#include <zlib.h>
#include "git2/object.h"
-#include "git2/oid.h"
+#include "git2/sys/odb_backend.h"
#include "fileops.h"
#include "hash.h"
#include "odb.h"
diff --git a/src/odb_pack.c b/src/odb_pack.c
index 7240a4ac7..773e14974 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -8,7 +8,8 @@
#include "common.h"
#include <zlib.h>
#include "git2/repository.h"
-#include "git2/oid.h"
+#include "git2/indexer.h"
+#include "git2/sys/odb_backend.h"
#include "fileops.h"
#include "hash.h"
#include "odb.h"
diff --git a/src/refdb.c b/src/refdb.c
index 2a0fd702c..33a1934d1 100644
--- a/src/refdb.c
+++ b/src/refdb.c
@@ -7,15 +7,16 @@
#include "common.h"
#include "posix.h"
+
#include "git2/object.h"
#include "git2/refs.h"
#include "git2/refdb.h"
+#include "git2/sys/refdb_backend.h"
+
#include "hash.h"
#include "refdb.h"
#include "refs.h"
-#include "git2/refdb_backend.h"
-
int git_refdb_new(git_refdb **out, git_repository *repo)
{
git_refdb *db;
@@ -57,15 +58,19 @@ int git_refdb_open(git_refdb **out, git_repository *repo)
return 0;
}
-int git_refdb_set_backend(git_refdb *db, git_refdb_backend *backend)
+static void refdb_free_backend(git_refdb *db)
{
if (db->backend) {
- if(db->backend->free)
+ if (db->backend->free)
db->backend->free(db->backend);
else
git__free(db->backend);
}
+}
+int git_refdb_set_backend(git_refdb *db, git_refdb_backend *backend)
+{
+ refdb_free_backend(db);
db->backend = backend;
return 0;
@@ -74,23 +79,16 @@ int git_refdb_set_backend(git_refdb *db, git_refdb_backend *backend)
int git_refdb_compress(git_refdb *db)
{
assert(db);
-
- if (db->backend->compress) {
+
+ if (db->backend->compress)
return db->backend->compress(db->backend);
- }
-
+
return 0;
}
static void refdb_free(git_refdb *db)
{
- if (db->backend) {
- if(db->backend->free)
- db->backend->free(db->backend);
- else
- git__free(db->backend);
- }
-
+ refdb_free_backend(db);
git__free(db);
}
@@ -114,14 +112,13 @@ int git_refdb_lookup(git_reference **out, git_refdb *db, const char *ref_name)
git_reference *ref;
int error;
- assert(db && db->backend && ref_name);
-
- *out = NULL;
+ assert(db && db->backend && out && ref_name);
- if ((error = db->backend->lookup(&ref, db->backend, ref_name)) == 0)
- {
+ if (!(error = db->backend->lookup(&ref, db->backend, ref_name))) {
ref->db = db;
*out = ref;
+ } else {
+ *out = NULL;
}
return error;
diff --git a/src/refdb.h b/src/refdb.h
index 0969711b9..047113ac8 100644
--- a/src/refdb.h
+++ b/src/refdb.h
@@ -41,6 +41,6 @@ int git_refdb_foreach_glob(
int git_refdb_write(git_refdb *refdb, const git_reference *ref);
-int git_refdb_delete(struct git_refdb *refdb, const git_reference *ref);
+int git_refdb_delete(git_refdb *refdb, const git_reference *ref);
#endif
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 56b2b6a99..443871005 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -18,7 +18,8 @@
#include <git2/tag.h>
#include <git2/object.h>
#include <git2/refdb.h>
-#include <git2/refdb_backend.h>
+#include <git2/sys/refdb_backend.h>
+#include <git2/sys/refs.h>
GIT__USE_STRMAP;
@@ -61,7 +62,7 @@ static int reference_read(
/* Determine the full path of the file */
if (git_buf_joinpath(&path, repo_path, ref_name) < 0)
return -1;
-
+
result = git_futils_readbuffer_updated(file_content, path.ptr, mtime, NULL, updated);
git_buf_free(&path);
@@ -174,7 +175,7 @@ static int packed_load(refdb_fs_backend *backend)
ref_cache->packfile = git_strmap_alloc();
GITERR_CHECK_ALLOC(ref_cache->packfile);
}
-
+
result = reference_read(&packfile, &ref_cache->packfile_time,
backend->path, GIT_PACKEDREFS_FILE, &updated);
@@ -192,7 +193,7 @@ static int packed_load(refdb_fs_backend *backend)
if (result < 0)
return -1;
-
+
if (!updated)
return 0;
@@ -433,7 +434,7 @@ static int loose_lookup(
} else {
if ((error = loose_parse_oid(&oid, &ref_file)) < 0)
goto done;
-
+
*out = git_reference__alloc(ref_name, &oid, NULL);
}
@@ -455,19 +456,19 @@ static int packed_map_entry(
if (packed_load(backend) < 0)
return -1;
-
+
/* Look up on the packfile */
packfile_refs = backend->refcache.packfile;
*pos = git_strmap_lookup_index(packfile_refs, ref_name);
-
+
if (!git_strmap_valid_index(packfile_refs, *pos)) {
giterr_set(GITERR_REFERENCE, "Reference '%s' not found", ref_name);
return GIT_ENOTFOUND;
}
*entry = git_strmap_value_at(packfile_refs, *pos);
-
+
return 0;
}
@@ -479,14 +480,14 @@ static int packed_lookup(
struct packref *entry;
khiter_t pos;
int error = 0;
-
+
if ((error = packed_map_entry(&entry, &pos, backend, ref_name)) < 0)
return error;
if ((*out = git_reference__alloc(ref_name,
&entry->oid, &entry->peel)) == NULL)
return -1;
-
+
return 0;
}
@@ -582,7 +583,7 @@ static int refdb_fs_backend__foreach(
git_buf refs_path = GIT_BUF_INIT;
const char *ref_name;
void *ref = NULL;
-
+
GIT_UNUSED(ref);
assert(_backend);
@@ -590,7 +591,7 @@ static int refdb_fs_backend__foreach(
if (packed_load(backend) < 0)
return -1;
-
+
/* list all the packed references first */
if (list_type & GIT_REF_OID) {
git_strmap_foreach(backend->refcache.packfile, ref_name, ref, {
@@ -924,7 +925,7 @@ static int refdb_fs_backend__delete(
repo = backend->repo;
/* If a loose reference exists, remove it from the filesystem */
-
+
if (git_buf_joinpath(&loose_path, repo->path_repository, ref->name) < 0)
return -1;
@@ -932,7 +933,7 @@ static int refdb_fs_backend__delete(
error = p_unlink(loose_path.ptr);
loose_deleted = 1;
}
-
+
git_buf_free(&loose_path);
if (error != 0)
@@ -946,7 +947,7 @@ static int refdb_fs_backend__delete(
error = packed_write(backend);
}
-
+
if (pack_error == GIT_ENOTFOUND)
error = loose_deleted ? 0 : GIT_ENOTFOUND;
else
diff --git a/src/refs.c b/src/refs.c
index 5b5812aae..9c6684a5a 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -19,7 +19,7 @@
#include <git2/branch.h>
#include <git2/refs.h>
#include <git2/refdb.h>
-#include <git2/refdb_backend.h>
+#include <git2/sys/refs.h>
GIT__USE_STRMAP;
@@ -45,8 +45,7 @@ static git_reference *alloc_ref(const char *name)
}
git_reference *git_reference__alloc_symbolic(
- const char *name,
- const char *target)
+ const char *name, const char *target)
{
git_reference *ref;
@@ -255,10 +254,10 @@ int git_reference_lookup_resolved(
max_nesting = MAX_NESTING_LEVEL;
else if (max_nesting < 0)
max_nesting = DEFAULT_NESTING_LEVEL;
-
+
strncpy(scan_name, name, GIT_REFNAME_MAX);
scan_type = GIT_REF_SYMBOLIC;
-
+
if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
return -1;
@@ -276,7 +275,7 @@ int git_reference_lookup_resolved(
if ((error = git_refdb_lookup(&ref, refdb, scan_name)) < 0)
return error;
-
+
scan_type = ref->type;
}
@@ -354,7 +353,7 @@ static int reference__create(
git_refdb *refdb;
git_reference *ref = NULL;
int error = 0;
-
+
if (ref_out)
*ref_out = NULL;
@@ -369,7 +368,7 @@ static int reference__create(
} else {
ref = git_reference__alloc_symbolic(name, symbolic);
}
-
+
GITERR_CHECK_ALLOC(ref);
ref->db = refdb;
@@ -377,7 +376,7 @@ static int reference__create(
git_reference_free(ref);
return error;
}
-
+
if (ref_out == NULL)
git_reference_free(ref);
else
@@ -397,17 +396,17 @@ int git_reference_create(
int error = 0;
assert(repo && name && oid);
-
+
/* Sanity check the reference being created - target must exist. */
if ((error = git_repository_odb__weakptr(&odb, repo)) < 0)
return error;
-
+
if (!git_odb_exists(odb, oid)) {
giterr_set(GITERR_REFERENCE,
"Target OID for the reference doesn't exist on the repository");
return -1;
}
-
+
return reference__create(ref_out, repo, name, oid, NULL, force);
}
@@ -422,7 +421,7 @@ int git_reference_symbolic_create(
int error = 0;
assert(repo && name && target);
-
+
if ((error = git_reference__normalize_name_lax(
normalized, sizeof(normalized), target)) < 0)
return error;
@@ -436,7 +435,7 @@ int git_reference_set_target(
const git_oid *id)
{
assert(out && ref && id);
-
+
if (ref->type != GIT_REF_OID) {
giterr_set(GITERR_REFERENCE, "Cannot set OID on symbolic reference");
return -1;
@@ -451,13 +450,13 @@ int git_reference_symbolic_set_target(
const char *target)
{
assert(out && ref && target);
-
+
if (ref->type != GIT_REF_SYMBOLIC) {
giterr_set(GITERR_REFERENCE,
"Cannot set symbolic target on a direct reference");
return -1;
}
-
+
return git_reference_symbolic_create(out, ref->db->repo, ref->name, target, 1);
}
@@ -473,7 +472,7 @@ int git_reference_rename(
git_reference *result = NULL;
int error = 0;
int reference_has_log;
-
+
*out = NULL;
normalization_flags = ref->type == GIT_REF_SYMBOLIC ?
@@ -488,7 +487,7 @@ int git_reference_rename(
* Create the new reference.
*/
if (ref->type == GIT_REF_OID) {
- result = git_reference__alloc(new_name, &ref->target.oid, &ref->peel);
+ result = git_reference__alloc(new_name, &ref->target.oid, &ref->peel);
} else if (ref->type == GIT_REF_SYMBOLIC) {
result = git_reference__alloc_symbolic(new_name, ref->target.symbolic);
} else {
@@ -509,11 +508,11 @@ int git_reference_rename(
/* Now delete the old ref and save the new one. */
if ((error = git_refdb_delete(ref->db, ref)) < 0)
goto on_error;
-
+
/* Save the new reference. */
if ((error = git_refdb_write(ref->db, result)) < 0)
goto rollback;
-
+
/* Update HEAD it was poiting to the reference being renamed. */
if (should_head_be_updated && (error = git_repository_set_head(ref->db->repo, new_name)) < 0) {
giterr_set(GITERR_REFERENCE, "Failed to update HEAD after renaming reference");
@@ -547,7 +546,7 @@ int git_reference_resolve(git_reference **ref_out, const git_reference *ref)
switch (git_reference_type(ref)) {
case GIT_REF_OID:
return git_reference_lookup(ref_out, ref->db->repo, ref->name);
-
+
case GIT_REF_SYMBOLIC:
return git_reference_lookup_resolved(ref_out, ref->db->repo, ref->target.symbolic, -1);
@@ -846,7 +845,7 @@ static int reference__update_terminal(
if (nesting > MAX_NESTING_LEVEL)
return GIT_ENOTFOUND;
-
+
error = git_reference_lookup(&ref, repo, ref_name);
/* If we haven't found the reference at all, create a new reference. */
@@ -854,10 +853,10 @@ static int reference__update_terminal(
giterr_clear();
return git_reference_create(NULL, repo, ref_name, oid, 0);
}
-
+
if (error < 0)
return error;
-
+
/* If the ref is a symbolic reference, follow its target. */
if (git_reference_type(ref) == GIT_REF_SYMBOLIC) {
error = reference__update_terminal(repo, git_reference_symbolic_target(ref), oid,
@@ -867,7 +866,7 @@ static int reference__update_terminal(
git_reference_free(ref);
error = git_reference_create(NULL, repo, ref_name, oid, 1);
}
-
+
return error;
}
diff --git a/src/repository.c b/src/repository.c
index 64ab2f4db..a16f69da4 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -9,6 +9,7 @@
#include "git2/object.h"
#include "git2/refdb.h"
+#include "git2/sys/repository.h"
#include "common.h"
#include "repository.h"
@@ -129,6 +130,12 @@ static git_repository *repository_alloc(void)
return repo;
}
+int git_repository_new(git_repository **out)
+{
+ *out = repository_alloc();
+ return 0;
+}
+
static int load_config_data(git_repository *repo)
{
int is_bare;
diff --git a/src/submodule.c b/src/submodule.c
index 2fdaf2f77..0a22e3b13 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -7,6 +7,7 @@
#include "common.h"
#include "git2/config.h"
+#include "git2/sys/config.h"
#include "git2/types.h"
#include "git2/repository.h"
#include "git2/index.h"
diff --git a/src/tag.c b/src/tag.c
index 735ba7e1d..c82decbe3 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -13,6 +13,7 @@
#include "git2/object.h"
#include "git2/repository.h"
#include "git2/signature.h"
+#include "git2/odb_backend.h"
void git_tag__free(git_tag *tag)
{
diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c
index 8acedeb49..90851980c 100644
--- a/src/transports/smart_protocol.c
+++ b/src/transports/smart_protocol.c
@@ -5,6 +5,7 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "git2.h"
+#include "git2/odb_backend.h"
#include "smart.h"
#include "refs.h"
diff --git a/src/util.h b/src/util.h
index c0f271997..af3ef0b46 100644
--- a/src/util.h
+++ b/src/util.h
@@ -7,6 +7,8 @@
#ifndef INCLUDE_util_h__
#define INCLUDE_util_h__
+#include "common.h"
+
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#define bitsizeof(x) (CHAR_BIT * sizeof(x))
#define MSB(x, bits) ((x) & (~0ULL << (bitsizeof(x) - (bits))))