Add support for in-memory objects All repository objects can now be created from scratch in memory using either the git_object_new() method, or the corresponding git_XXX_new() for each object. So far, only git_commits can be written back to disk once created in memory. Signed-off-by: Vicent Marti <tanoku@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
diff --git a/src/commit.c b/src/commit.c
index 7cb8f24..90a30e0 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -63,9 +63,14 @@ void git_commit__free(git_commit *commit)
free(commit);
}
+git_commit *git_commit_new(git_repository *repo)
+{
+ return (git_commit *)git_object_new(repo, GIT_OBJ_COMMIT);
+}
+
const git_oid *git_commit_id(git_commit *c)
{
- return &c->object.id;
+ return git_object_id((git_object *)c);
}
int git_commit__parse(git_commit *commit)
diff --git a/src/git/commit.h b/src/git/commit.h
index 4387925..a584273 100644
--- a/src/git/commit.h
+++ b/src/git/commit.h
@@ -30,6 +30,18 @@ typedef struct git_commit git_commit;
*/
GIT_EXTERN(git_commit *) git_commit_lookup(git_repository *repo, const git_oid *id);
+/*
+ * Create a new in-memory git_commit.
+ *
+ * The commit object must be manually filled using
+ * setter methods before it can be written to its
+ * repository.
+ *
+ * @param repo The repository where the object will reside
+ * @return the object if creation was posible; NULL otherwise
+ */
+GIT_EXTERN(git_commit *) git_commit_new(git_repository *repo);
+
/**
* Get the id of a commit.
* @param commit a previously loaded commit.
diff --git a/src/git/repository.h b/src/git/repository.h
index 33bb2fc..9417d7b 100644
--- a/src/git/repository.h
+++ b/src/git/repository.h
@@ -57,9 +57,53 @@ GIT_EXTERN(git_object *) git_repository_lookup(git_repository *repo, const git_o
*/
GIT_EXTERN(git_odb *) git_repository_database(git_repository *repo);
+/*
+ * Create a new in-memory repository object with
+ * the given type.
+ *
+ * The object's attributes can be filled in using the
+ * correspondign setter methods.
+ *
+ * The object will be written back to given git_repository
+ * when the git_object_write() function is called; objects
+ * cannot be written to disk until all their main
+ * attributes have been properly filled.
+ *
+ * Objects are instantiated with no SHA1 id; their id
+ * will be automatically generated when writing to the
+ * repository.
+ *
+ * @parem repo Repository where the object belongs
+ * @param type Type of the object to be created
+ * @return the new object
+ */
+GIT_EXTERN(git_object *) git_object_new(git_repository *repo, git_otype type);
+
+/*
+ * Write back an object to disk.
+ *
+ * The object will be written to its corresponding
+ * repository.
+ *
+ * If the object has no changes since it was first
+ * read from the repository, no actions will take place.
+ *
+ * If the object has been modified since it was read from
+ * the repository, or it has been created from scratch
+ * in memory, it will be written to the repository and
+ * its SHA1 ID will be updated accordingly.
+ *
+ * @param object Git object to write back
+ * @return 0 on success; otherwise an error code
+ */
+int git_object_write(git_object *object);
+
/**
* Get the id (SHA1) of a repository object
*
+ * In-memory objects created by git_object_new() do not
+ * have a SHA1 ID until they are written on a repository.
+ *
* @param obj the repository object
* @return the SHA1 id
*/
diff --git a/src/git/tag.h b/src/git/tag.h
index a6efabb..33e3d6f 100644
--- a/src/git/tag.h
+++ b/src/git/tag.h
@@ -29,6 +29,18 @@ typedef struct git_tag git_tag;
*/
GIT_EXTERN(git_tag *) git_tag_lookup(git_repository *repo, const git_oid *id);
+/*
+ * Create a new in-memory git_tag.
+ *
+ * The tag object must be manually filled using
+ * setter methods before it can be written to its
+ * repository.
+ *
+ * @param repo The repository where the object will reside
+ * @return the object if creation was posible; NULL otherwise
+ */
+GIT_EXTERN(git_tag *) git_tag_new(git_repository *repo);
+
/**
* Get the id of a tag.
* @param tag a previously loaded tag.
diff --git a/src/git/tree.h b/src/git/tree.h
index 646c085..32fd652 100644
--- a/src/git/tree.h
+++ b/src/git/tree.h
@@ -32,6 +32,18 @@ typedef struct git_tree git_tree;
*/
GIT_EXTERN(git_tree *) git_tree_lookup(git_repository *repo, const git_oid *id);
+/*
+ * Create a new in-memory git_tree.
+ *
+ * The tree object must be manually filled using
+ * setter methods before it can be written to its
+ * repository.
+ *
+ * @param repo The repository where the object will reside
+ * @return the object if creation was posible; NULL otherwise
+ */
+GIT_EXTERN(git_tree *) git_tree_new(git_repository *repo);
+
/**
* Get the id of a tree.
* @param tree a previously loaded tree.
diff --git a/src/repository.c b/src/repository.c
index f353ce0..dc1fe0e 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -32,6 +32,15 @@
static const int default_table_size = 32;
static const double max_load_factor = 0.65;
+static const size_t object_sizes[] = {
+ 0,
+ sizeof(git_commit),
+ sizeof(git_tree),
+ sizeof(git_object), /* TODO: sizeof(git_blob) */
+ sizeof(git_tag)
+};
+
+
uint32_t git_object_hash(const void *key)
{
uint32_t r;
@@ -160,7 +169,7 @@ int git__source_write(git_odb_source *source, const void *bytes, size_t len)
return GIT_SUCCESS;
}
-void git_object__source_prepare_write(git_object *object)
+static void prepare_write(git_object *object)
{
const size_t base_size = 4096; /* 4Kb base size */
@@ -175,10 +184,9 @@ void git_object__source_prepare_write(git_object *object)
object->source.written_bytes = 0;
object->source.open = 1;
- object->source.out_of_sync = 1;
}
-int git_object__source_writeback(git_object *object)
+static int write_back(git_object *object)
{
int error;
git_oid new_id;
@@ -188,8 +196,7 @@ int git_object__source_writeback(git_object *object)
if (!object->source.open)
return GIT_ERROR;
- if (!object->source.out_of_sync)
- return GIT_SUCCESS;
+ assert(object->modified);
object->source.raw.len = object->source.written_bytes;
@@ -198,7 +205,9 @@ int git_object__source_writeback(git_object *object)
if ((error = git_odb_write(&new_id, object->repo->db, &object->source.raw)) < 0)
return error;
- git_hashtable_remove(object->repo->objects, &object->id);
+ if (!object->in_memory)
+ git_hashtable_remove(object->repo->objects, &object->id);
+
git_oid_cpy(&object->id, &new_id);
git_hashtable_insert(object->repo->objects, &object->id, object);
@@ -206,6 +215,7 @@ int git_object__source_writeback(git_object *object)
object->source.written_bytes = 0;
object->modified = 0;
+ object->in_memory = 0;
git_object__source_close(object);
return GIT_SUCCESS;
@@ -215,9 +225,9 @@ int git_object__source_open(git_object *object)
{
int error;
- assert(object);
+ assert(object && !object->in_memory);
- if (object->source.open && object->source.out_of_sync)
+ if (object->source.open)
git_object__source_close(object);
if (object->source.open)
@@ -228,7 +238,6 @@ int git_object__source_open(git_object *object)
return error;
object->source.open = 1;
- object->source.out_of_sync = 0;
return GIT_SUCCESS;
}
@@ -239,7 +248,6 @@ void git_object__source_close(git_object *object)
if (!object->source.open) {
git_obj_close(&object->source.raw);
object->source.open = 0;
- object->source.out_of_sync = 0;
}
}
@@ -253,7 +261,7 @@ int git_object_write(git_object *object)
if (object->modified == 0)
return GIT_SUCCESS;
- git_object__source_prepare_write(object);
+ prepare_write(object);
source = &object->source;
switch (source->raw.type) {
@@ -265,14 +273,15 @@ int git_object_write(git_object *object)
case GIT_OBJ_TAG:
default:
error = GIT_ERROR;
+ break;
}
if (error < 0) {
git_object__source_close(object);
return error;
}
-
- return git_object__source_writeback(object);
+
+ return write_back(object);
}
void git_object_free(git_object *object)
@@ -310,6 +319,10 @@ git_odb *git_repository_database(git_repository *repo)
const git_oid *git_object_id(git_object *obj)
{
assert(obj);
+
+ if (obj->in_memory)
+ return NULL;
+
return &obj->id;
}
@@ -319,16 +332,38 @@ git_otype git_object_type(git_object *obj)
return obj->source.raw.type;
}
-git_object *git_repository_lookup(git_repository *repo, const git_oid *id, git_otype type)
+git_object *git_object_new(git_repository *repo, git_otype type)
{
- static const size_t object_sizes[] = {
- 0,
- sizeof(git_commit),
- sizeof(git_tree),
- sizeof(git_object), /* TODO: sizeof(git_blob) */
- sizeof(git_tag)
- };
+ git_object *object = NULL;
+ switch (type) {
+ case GIT_OBJ_COMMIT:
+ case GIT_OBJ_TAG:
+ case GIT_OBJ_TREE:
+ case GIT_OBJ_BLOB:
+ break;
+
+ default:
+ return NULL;
+ }
+
+ object = git__malloc(object_sizes[type]);
+
+ if (object == NULL)
+ return NULL;
+
+ memset(object, 0x0, object_sizes[type]);
+ object->repo = repo;
+ object->in_memory = 1;
+ object->modified = 1;
+
+ object->source.raw.type = type;
+
+ return object;
+}
+
+git_object *git_repository_lookup(git_repository *repo, const git_oid *id, git_otype type)
+{
git_object *object = NULL;
git_rawobj obj_file;
diff --git a/src/repository.h b/src/repository.h
index 5074c61..9a7bd24 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -12,7 +12,7 @@ typedef struct {
git_rawobj raw;
void *write_ptr;
size_t written_bytes;
- int open:1, out_of_sync:1;
+ int open:1;
} git_odb_source;
struct git_object {
@@ -30,8 +30,6 @@ struct git_repository {
int git_object__source_open(git_object *object);
void git_object__source_close(git_object *object);
-void git_object__source_prepare_write(git_object *object);
-int git_object__source_writeback(git_object *object);
int git__source_printf(git_odb_source *source, const char *format, ...);
int git__source_write(git_odb_source *source, const void *bytes, size_t len);
diff --git a/src/tag.c b/src/tag.c
index 13679ec..734556b 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -38,9 +38,14 @@ void git_tag__free(git_tag *tag)
free(tag);
}
-const git_oid *git_tag_id(git_tag *t)
+git_tag *git_tag_new(git_repository *repo)
{
- return &t->object.id;
+ return (git_tag *)git_object_new(repo, GIT_OBJ_TAG);
+}
+
+const git_oid *git_tag_id(git_tag *c)
+{
+ return git_object_id((git_object *)c);
}
const git_object *git_tag_target(git_tag *t)
diff --git a/src/tree.c b/src/tree.c
index f1c191c..670f998 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -40,9 +40,14 @@ void git_tree__free(git_tree *tree)
free(tree);
}
-const git_oid *git_tree_id(git_tree *tree)
+git_tree *git_tree_new(git_repository *repo)
{
- return &tree->object.id;
+ return (git_tree *)git_object_new(repo, GIT_OBJ_TREE);
+}
+
+const git_oid *git_tree_id(git_tree *c)
+{
+ return git_object_id((git_object *)c);
}
git_tree *git_tree_lookup(git_repository *repo, const git_oid *id)