]>
git.proxmox.com Git - libgit2.git/blob - src/odb_mempack.c
2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
9 #include "git2/object.h"
10 #include "git2/sys/odb_backend.h"
17 #include "git2/odb_backend.h"
18 #include "git2/types.h"
19 #include "git2/pack.h"
27 char data
[GIT_FLEX_ARRAY
];
30 struct memory_packer_db
{
31 git_odb_backend parent
;
33 git_array_t(struct memobject
*) commits
;
36 static int impl__write(git_odb_backend
*_backend
, const git_oid
*oid
, const void *data
, size_t len
, git_otype type
)
38 struct memory_packer_db
*db
= (struct memory_packer_db
*)_backend
;
39 struct memobject
*obj
= NULL
;
44 pos
= kh_put(oid
, db
->objects
, oid
, &rval
);
51 GITERR_CHECK_ALLOC_ADD(&alloc_len
, sizeof(struct memobject
), len
);
52 obj
= git__malloc(alloc_len
);
53 GITERR_CHECK_ALLOC(obj
);
55 memcpy(obj
->data
, data
, len
);
56 git_oid_cpy(&obj
->oid
, oid
);
60 kh_key(db
->objects
, pos
) = &obj
->oid
;
61 kh_val(db
->objects
, pos
) = obj
;
63 if (type
== GIT_OBJ_COMMIT
) {
64 struct memobject
**store
= git_array_alloc(db
->commits
);
65 GITERR_CHECK_ALLOC(store
);
72 static int impl__exists(git_odb_backend
*backend
, const git_oid
*oid
)
74 struct memory_packer_db
*db
= (struct memory_packer_db
*)backend
;
76 return git_oidmap_exists(db
->objects
, oid
);
79 static int impl__read(void **buffer_p
, size_t *len_p
, git_otype
*type_p
, git_odb_backend
*backend
, const git_oid
*oid
)
81 struct memory_packer_db
*db
= (struct memory_packer_db
*)backend
;
82 struct memobject
*obj
= NULL
;
85 pos
= kh_get(oid
, db
->objects
, oid
);
86 if (!git_oidmap_valid_index(db
->objects
, pos
))
89 obj
= kh_val(db
->objects
, pos
);
93 *buffer_p
= git__malloc(obj
->len
);
94 GITERR_CHECK_ALLOC(*buffer_p
);
96 memcpy(*buffer_p
, obj
->data
, obj
->len
);
100 static int impl__read_header(size_t *len_p
, git_otype
*type_p
, git_odb_backend
*backend
, const git_oid
*oid
)
102 struct memory_packer_db
*db
= (struct memory_packer_db
*)backend
;
103 struct memobject
*obj
= NULL
;
106 pos
= kh_get(oid
, db
->objects
, oid
);
107 if (!git_oidmap_valid_index(db
->objects
, pos
))
108 return GIT_ENOTFOUND
;
110 obj
= kh_val(db
->objects
, pos
);
117 int git_mempack_dump(git_buf
*pack
, git_repository
*repo
, git_odb_backend
*_backend
)
119 struct memory_packer_db
*db
= (struct memory_packer_db
*)_backend
;
120 git_packbuilder
*packbuilder
;
124 if (git_packbuilder_new(&packbuilder
, repo
) < 0)
127 for (i
= 0; i
< db
->commits
.size
; ++i
) {
128 struct memobject
*commit
= db
->commits
.ptr
[i
];
130 err
= git_packbuilder_insert_commit(packbuilder
, &commit
->oid
);
135 err
= git_packbuilder_write_buf(pack
, packbuilder
);
138 git_packbuilder_free(packbuilder
);
142 void git_mempack_reset(git_odb_backend
*_backend
)
144 struct memory_packer_db
*db
= (struct memory_packer_db
*)_backend
;
145 struct memobject
*object
= NULL
;
147 git_oidmap_foreach_value(db
->objects
, object
, {
151 git_array_clear(db
->commits
);
153 git_oidmap_clear(db
->objects
);
156 static void impl__free(git_odb_backend
*_backend
)
158 struct memory_packer_db
*db
= (struct memory_packer_db
*)_backend
;
160 git_oidmap_free(db
->objects
);
164 int git_mempack_new(git_odb_backend
**out
)
166 struct memory_packer_db
*db
;
170 db
= git__calloc(1, sizeof(struct memory_packer_db
));
171 GITERR_CHECK_ALLOC(db
);
173 db
->objects
= git_oidmap_alloc();
175 db
->parent
.version
= GIT_ODB_BACKEND_VERSION
;
176 db
->parent
.read
= &impl__read
;
177 db
->parent
.write
= &impl__write
;
178 db
->parent
.read_header
= &impl__read_header
;
179 db
->parent
.exists
= &impl__exists
;
180 db
->parent
.free
= &impl__free
;
182 *out
= (git_odb_backend
*)db
;