Skip to content

Commit 7d7cd88

Browse files
author
Vicent Marti
committed
Decouple storage from ODB logic
Comes with two default backends: loose object and packfiles. Signed-off-by: Vicent Marti <tanoku@gmail.com>
1 parent 86bfec3 commit 7d7cd88

15 files changed

Lines changed: 2201 additions & 1874 deletions

‎src/git/odb.h‎

Lines changed: 72 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,52 @@ GIT_BEGIN_DECL
1717
/** An open object database handle. */
1818
typedef struct git_odb git_odb;
1919

20+
/** A custom backend in an ODB */
21+
typedef struct git_odb_backend git_odb_backend;
22+
2023
/**
21-
* Open an object database for read/write access.
24+
* Create a new object database with no backends.
25+
*
26+
* Before the ODB can be used for read/writing, a custom database
27+
* backend must be manually added using `git_odb_add_backend()`
28+
*
29+
* @param out location to store the database pointer, if opened.
30+
* Set to NULL if the open failed.
31+
* @return GIT_SUCCESS if the database was created; otherwise an error
32+
* code describing why the open was not possible.
33+
*/
34+
GIT_EXTERN(int) git_odb_new(git_odb **out);
35+
36+
/**
37+
* Create a new object database and automatically add
38+
* the two default backends:
39+
*
40+
* - git_odb_backend_loose: read and write loose object files
41+
* from disk, assuming `objects_dir` as the Objects folder
42+
*
43+
* - git_odb_backend_pack: read objects from packfiles,
44+
* assuming `objects_dir` as the Objects folder which
45+
* contains a 'pack/' folder with the corresponding data
46+
*
2247
* @param out location to store the database pointer, if opened.
2348
* Set to NULL if the open failed.
24-
* @param objects_dir path of the database's "objects" directory.
49+
* @param objects_dir path of the backends' "objects" directory.
2550
* @return GIT_SUCCESS if the database opened; otherwise an error
2651
* code describing why the open was not possible.
2752
*/
2853
GIT_EXTERN(int) git_odb_open(git_odb **out, const char *objects_dir);
2954

55+
/**
56+
* Add a custom backend to an existing Object DB
57+
*
58+
* Read <odb_backends.h> for more information.
59+
*
60+
* @param odb database to add the backend to
61+
* @paramm backend pointer to a git_odb_backend instance
62+
* @return 0 on sucess; error code otherwise
63+
*/
64+
GIT_EXTERN(int) git_odb_add_backend(git_odb *odb, git_odb_backend *backend);
65+
3066
/**
3167
* Close an open object database.
3268
* @param db database pointer to close. If NULL no action is taken.
@@ -76,7 +112,7 @@ GIT_EXTERN(int) git_odb_read(git_rawobj *out, git_odb *db, const git_oid *id);
76112
* are filled. The 'data' pointer will always be NULL.
77113
*
78114
* The raw object pointed by 'out' doesn't need to be manually
79-
* closed with git_obj_close().
115+
* closed with git_rawobj_close().
80116
*
81117
* @param out object descriptor to populate upon reading.
82118
* @param db database to search for the object in.
@@ -88,44 +124,45 @@ GIT_EXTERN(int) git_odb_read(git_rawobj *out, git_odb *db, const git_oid *id);
88124
GIT_EXTERN(int) git_odb_read_header(git_rawobj *out, git_odb *db, const git_oid *id);
89125

90126
/**
91-
* Read an object from the database using only pack files.
92-
*
93-
* If GIT_ENOTFOUND then out->data is set to NULL.
127+
* Write an object to the database.
94128
*
95-
* @param out object descriptor to populate upon reading.
96-
* @param db database to search for the object in.
97-
* @param id identity of the object to read.
129+
* @param id identity of the object written.
130+
* @param db database to which the object should be written.
131+
* @param obj object descriptor for the object to write.
98132
* @return
99-
* - GIT_SUCCESS if the object was read.
100-
* - GIT_ENOTFOUND if the object is not in the database.
133+
* - GIT_SUCCESS if the object was written;
134+
* - GIT_ERROR otherwise.
101135
*/
102-
GIT_EXTERN(int) git_odb__read_packed(git_rawobj *out, git_odb *db, const git_oid *id);
136+
GIT_EXTERN(int) git_odb_write(git_oid *id, git_odb *db, git_rawobj *obj);
103137

104138
/**
105-
* Read an object from the database using only loose object files.
106-
*
107-
* If GIT_ENOTFOUND then out->data is set to NULL.
139+
* Determine if the given object can be found in the object database.
108140
*
109-
* @param out object descriptor to populate upon reading.
110-
* @param db database to search for the object in.
111-
* @param id identity of the object to read.
141+
* @param db database to be searched for the given object.
142+
* @param id the object to search for.
112143
* @return
113-
* - GIT_SUCCESS if the object was read.
114-
* - GIT_ENOTFOUND if the object is not in the database.
144+
* - true, if the object was found
145+
* - false, otherwise
115146
*/
116-
GIT_EXTERN(int) git_odb__read_loose(git_rawobj *out, git_odb *db, const git_oid *id);
147+
GIT_EXTERN(int) git_odb_exists(git_odb *db, const git_oid *id);
148+
149+
150+
151+
117152

118153
/**
119-
* Write an object to the database.
154+
* Determine the object-ID (sha1 hash) of the given git_rawobj.
120155
*
121-
* @param id identity of the object written.
122-
* @param db database to which the object should be written.
123-
* @param obj object descriptor for the object to write.
156+
* The input obj must be a valid loose object type and the data
157+
* pointer must not be NULL, unless the len field is also zero.
158+
*
159+
* @param id the resulting object-ID.
160+
* @param obj the object whose hash is to be determined.
124161
* @return
125-
* - GIT_SUCCESS if the object was written;
126-
* - GIT_ERROR otherwise.
162+
* - GIT_SUCCESS if the object-ID was correctly determined.
163+
* - GIT_ERROR if the given object is malformed.
127164
*/
128-
GIT_EXTERN(int) git_odb_write(git_oid *id, git_odb *db, git_rawobj *obj);
165+
GIT_EXTERN(int) git_rawobj_hash(git_oid *id, git_rawobj *obj);
129166

130167
/**
131168
* Release all memory used by the obj structure.
@@ -136,12 +173,15 @@ GIT_EXTERN(int) git_odb_write(git_oid *id, git_odb *db, git_rawobj *obj);
136173
*
137174
* @param obj object descriptor to free.
138175
*/
139-
GIT_INLINE(void) git_obj_close(git_rawobj *obj)
176+
GIT_INLINE(void) git_rawobj_close(git_rawobj *obj)
140177
{
141178
free(obj->data);
142179
obj->data = NULL;
143180
}
144181

182+
183+
184+
145185
/**
146186
* Convert an object type to it's string representation.
147187
*
@@ -151,15 +191,15 @@ GIT_INLINE(void) git_obj_close(git_rawobj *obj)
151191
* @param type object type to convert.
152192
* @return the corresponding string representation.
153193
*/
154-
GIT_EXTERN(const char *) git_obj_type_to_string(git_otype type);
194+
GIT_EXTERN(const char *) git_otype_tostring(git_otype type);
155195

156196
/**
157197
* Convert a string object type representation to it's git_otype.
158198
*
159199
* @param str the string to convert.
160200
* @return the corresponding git_otype.
161201
*/
162-
GIT_EXTERN(git_otype) git_obj_string_to_type(const char *str);
202+
GIT_EXTERN(git_otype) git_otype_fromstring(const char *str);
163203

164204
/**
165205
* Determine if the given git_otype is a valid loose object type.
@@ -168,32 +208,7 @@ GIT_EXTERN(git_otype) git_obj_string_to_type(const char *str);
168208
* @return true if the type represents a valid loose object type,
169209
* false otherwise.
170210
*/
171-
GIT_EXTERN(int) git_obj__loose_object_type(git_otype type);
172-
173-
/**
174-
* Determine the object-ID (sha1 hash) of the given git_rawobj.
175-
*
176-
* The input obj must be a valid loose object type and the data
177-
* pointer must not be NULL, unless the len field is also zero.
178-
*
179-
* @param id the resulting object-ID.
180-
* @param obj the object whose hash is to be determined.
181-
* @return
182-
* - GIT_SUCCESS if the object-ID was correctly determined.
183-
* - GIT_ERROR if the given object is malformed.
184-
*/
185-
GIT_EXTERN(int) git_obj_hash(git_oid *id, git_rawobj *obj);
186-
187-
/**
188-
* Determine if the given object can be found in the object database.
189-
*
190-
* @param db database to be searched for the given object.
191-
* @param id the object to search for.
192-
* @return
193-
* - true, if the object was found
194-
* - false, otherwise
195-
*/
196-
GIT_EXTERN(int) git_odb_exists(git_odb *db, const git_oid *id);
211+
GIT_EXTERN(int) git_otype_is_loose(git_otype type);
197212

198213
/** @} */
199214
GIT_END_DECL

0 commit comments

Comments
 (0)