Skip to content

Commit 56e2a85

Browse files
committed
sha256: simplify API changes for sha256 support
There are several places where users may want to specify the type of object IDs (sha1 or sha256) that should be used, for example, when dealing with repositories, indexes, etc. However, given that sha256 support remains disappointingly uncommon in the wild, we should avoid hard API breaks when possible. Instead, update these APIs to have an "extended" format (eg, `git_odb_open_ext`) that provides an options structure with oid type information. This allows callers who do care about sha256 to use it, and callers who do not to avoid gratuitous API breakage.
1 parent 9961198 commit 56e2a85

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+419
-326
lines changed

examples/diff.c

-6
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,9 @@ static void compute_diff_no_index(git_diff **diff, struct diff_options *o) {
189189
git_patch_to_buf(&buf, patch),
190190
"patch to buf", NULL);
191191

192-
#ifdef GIT_EXPERIMENTAL_SHA256
193-
check_lg2(
194-
git_diff_from_buffer(diff, buf.ptr, buf.size, NULL),
195-
"diff from patch", NULL);
196-
#else
197192
check_lg2(
198193
git_diff_from_buffer(diff, buf.ptr, buf.size),
199194
"diff from patch", NULL);
200-
#endif
201195

202196
git_patch_free(patch);
203197
git_buf_dispose(&buf);

examples/show-index.c

-4
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ int lg2_show_index(git_repository *repo, int argc, char **argv)
3030

3131
dirlen = strlen(dir);
3232
if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) {
33-
#ifdef GIT_EXPERIMENTAL_SHA256
34-
check_lg2(git_index_open(&index, dir, NULL), "could not open index", dir);
35-
#else
3633
check_lg2(git_index_open(&index, dir), "could not open index", dir);
37-
#endif
3834
} else {
3935
check_lg2(git_repository_open_ext(&repo, dir, 0, NULL), "could not open repository", dir);
4036
check_lg2(git_repository_index(&index, repo), "could not open repository index", NULL);

fuzzers/packfile_fuzzer.c

-7
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,10 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
3737
abort();
3838
}
3939

40-
#ifdef GIT_EXPERIMENTAL_SHA256
41-
if (git_odb_new(&odb, NULL) < 0) {
42-
fprintf(stderr, "Failed to create the odb\n");
43-
abort();
44-
}
45-
#else
4640
if (git_odb_new(&odb) < 0) {
4741
fprintf(stderr, "Failed to create the odb\n");
4842
abort();
4943
}
50-
#endif
5144

5245
if (git_mempack_new(&mempack) < 0) {
5346
fprintf(stderr, "Failed to create the mempack\n");

include/git2/diff.h

+35-5
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,8 @@ GIT_EXTERN(int) git_diff_buffers(
13211321
*/
13221322
typedef struct {
13231323
unsigned int version;
1324+
1325+
/** Object ID type used in the patch file. */
13241326
git_oid_t oid_type;
13251327
} git_diff_parse_options;
13261328

@@ -1330,8 +1332,7 @@ typedef struct {
13301332
/** Stack initializer for diff parse options. Alternatively use
13311333
* `git_diff_parse_options_init` programmatic initialization.
13321334
*/
1333-
#define GIT_DIFF_PARSE_OPTIONS_INIT \
1334-
{ GIT_DIFF_PARSE_OPTIONS_VERSION, GIT_OID_DEFAULT }
1335+
#define GIT_DIFF_PARSE_OPTIONS_INIT { GIT_DIFF_PARSE_OPTIONS_VERSION }
13351336

13361337
/**
13371338
* Read the contents of a git patch file into a `git_diff` object.
@@ -1347,6 +1348,9 @@ typedef struct {
13471348
* implementation, it will not read unified diffs produced by
13481349
* the `diff` program, nor any other types of patch files.
13491350
*
1351+
* @note This API only supports SHA1 patch files
1352+
* @see git_diff_from_buffer_ext
1353+
*
13501354
* @param out A pointer to a git_diff pointer that will be allocated.
13511355
* @param content The contents of a patch file
13521356
* @param content_len The length of the patch file contents
@@ -1355,11 +1359,37 @@ typedef struct {
13551359
GIT_EXTERN(int) git_diff_from_buffer(
13561360
git_diff **out,
13571361
const char *content,
1358-
size_t content_len
1362+
size_t content_len);
1363+
13591364
#ifdef GIT_EXPERIMENTAL_SHA256
1360-
, git_diff_parse_options *opts
1365+
1366+
/**
1367+
* Read the contents of a git patch file into a `git_diff` object.
1368+
*
1369+
* The diff object produced is similar to the one that would be
1370+
* produced if you actually produced it computationally by comparing
1371+
* two trees, however there may be subtle differences. For example,
1372+
* a patch file likely contains abbreviated object IDs, so the
1373+
* object IDs in a `git_diff_delta` produced by this function will
1374+
* also be abbreviated.
1375+
*
1376+
* This function will only read patch files created by a git
1377+
* implementation, it will not read unified diffs produced by
1378+
* the `diff` program, nor any other types of patch files.
1379+
*
1380+
* @param out A pointer to a git_diff pointer that will be allocated.
1381+
* @param content The contents of a patch file
1382+
* @param content_len The length of the patch file contents
1383+
* @param opts Options controlling diff parsing
1384+
* @return 0 or an error code
1385+
*/
1386+
GIT_EXTERN(int) git_diff_from_buffer_ext(
1387+
git_diff **out,
1388+
const char *content,
1389+
size_t content_len,
1390+
git_diff_parse_options *opts);
1391+
13611392
#endif
1362-
);
13631393

13641394
/**
13651395
* This is an opaque structure which is allocated by `git_diff_get_stats`.

include/git2/index.h

+39-27
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,6 @@ typedef enum {
189189
GIT_INDEX_STAGE_THEIRS = 3
190190
} git_index_stage_t;
191191

192-
#ifdef GIT_EXPERIMENTAL_SHA256
193-
194192
/**
195193
* The options for opening or creating an index.
196194
*
@@ -233,62 +231,76 @@ GIT_EXTERN(int) git_index_options_init(
233231
unsigned int version);
234232

235233
/**
236-
* Creates a new bare Git index object, without a repository to back
237-
* it. This index object is capable of storing SHA256 objects.
234+
* Create a new bare Git index object as a memory representation
235+
* of the Git index file in 'index_path', without a repository
236+
* to back it.
237+
*
238+
* Since there is no ODB or working directory behind this index,
239+
* any Index methods which rely on these (e.g. index_add_bypath)
240+
* will fail with the GIT_ERROR error code.
241+
*
242+
* If you need to access the index of an actual repository,
243+
* use the `git_repository_index` wrapper.
244+
*
245+
* The index must be freed once it's no longer in use.
246+
*
247+
* @note This API only supports SHA1 indexes
248+
* @see git_index_open_ext
238249
*
239250
* @param index_out the pointer for the new index
240251
* @param index_path the path to the index file in disk
241-
* @param opts the options for opening the index, or NULL
242252
* @return 0 or an error code
243253
*/
244254
GIT_EXTERN(int) git_index_open(
245255
git_index **index_out,
246-
const char *index_path,
247-
const git_index_options *opts);
256+
const char *index_path);
257+
258+
#ifdef GIT_EXPERIMENTAL_SHA256
248259

249260
/**
250-
* Create an in-memory index object.
261+
* Creates a new bare Git index object, without a repository to back
262+
* it. This index object is capable of storing SHA256 objects.
251263
*
252264
* @param index_out the pointer for the new index
265+
* @param index_path the path to the index file in disk
253266
* @param opts the options for opening the index, or NULL
254267
* @return 0 or an error code
255268
*/
256-
GIT_EXTERN(int) git_index_new(git_index **index_out, const git_index_options *opts);
269+
GIT_EXTERN(int) git_index_open_ext(
270+
git_index **index_out,
271+
const char *index_path,
272+
const git_index_options *opts);
257273

258-
#else
274+
#endif
259275

260276
/**
261-
* Create a new bare Git index object as a memory representation
262-
* of the Git index file in 'index_path', without a repository
263-
* to back it.
264-
*
265-
* Since there is no ODB or working directory behind this index,
266-
* any Index methods which rely on these (e.g. index_add_bypath)
267-
* will fail with the GIT_ERROR error code.
277+
* Create an in-memory index object.
268278
*
269-
* If you need to access the index of an actual repository,
270-
* use the `git_repository_index` wrapper.
279+
* This index object cannot be read/written to the filesystem,
280+
* but may be used to perform in-memory index operations.
271281
*
272282
* The index must be freed once it's no longer in use.
273283
*
284+
* @note This API only supports SHA1 indexes
285+
* @see git_index_new_ext
286+
*
274287
* @param index_out the pointer for the new index
275-
* @param index_path the path to the index file in disk
276288
* @return 0 or an error code
277289
*/
278-
GIT_EXTERN(int) git_index_open(git_index **index_out, const char *index_path);
290+
GIT_EXTERN(int) git_index_new(git_index **index_out);
291+
292+
#ifdef GIT_EXPERIMENTAL_SHA256
279293

280294
/**
281295
* Create an in-memory index object.
282296
*
283-
* This index object cannot be read/written to the filesystem,
284-
* but may be used to perform in-memory index operations.
285-
*
286-
* The index must be freed once it's no longer in use.
287-
*
288297
* @param index_out the pointer for the new index
298+
* @param opts the options for opening the index, or NULL
289299
* @return 0 or an error code
290300
*/
291-
GIT_EXTERN(int) git_index_new(git_index **index_out);
301+
GIT_EXTERN(int) git_index_new_ext(
302+
git_index **index_out,
303+
const git_index_options *opts);
292304

293305
#endif
294306

include/git2/odb.h

+35-24
Original file line numberDiff line numberDiff line change
@@ -62,44 +62,34 @@ typedef struct {
6262
*/
6363
#define GIT_ODB_OPTIONS_INIT { GIT_ODB_OPTIONS_VERSION }
6464

65-
#ifdef GIT_EXPERIMENTAL_SHA256
66-
6765
/**
6866
* Create a new object database with no backends.
6967
*
70-
* @param[out] odb location to store the database pointer, if opened.
71-
* @param opts the options for this object database or NULL for defaults
72-
* @return 0 or an error code
73-
*/
74-
GIT_EXTERN(int) git_odb_new(git_odb **odb, const git_odb_options *opts);
75-
76-
/**
77-
* Create a new object database and automatically add loose and packed
78-
* backends.
68+
* Before the ODB can be used for read/writing, a custom database
69+
* backend must be manually added using `git_odb_add_backend()`
7970
*
80-
* @param[out] odb_out location to store the database pointer, if opened.
81-
* Set to NULL if the open failed.
82-
* @param objects_dir path of the backends' "objects" directory.
83-
* @param opts the options for this object database or NULL for defaults
71+
* @note This API only supports SHA1 object databases
72+
* @see git_odb_new_ext
73+
*
74+
* @param[out] odb location to store the database pointer, if opened.
8475
* @return 0 or an error code
8576
*/
86-
GIT_EXTERN(int) git_odb_open(
87-
git_odb **odb_out,
88-
const char *objects_dir,
89-
const git_odb_options *opts);
77+
GIT_EXTERN(int) git_odb_new(git_odb **odb);
9078

91-
#else
79+
#ifdef GIT_EXPERIMENTAL_SHA256
9280

9381
/**
9482
* Create a new object database with no backends.
9583
*
96-
* Before the ODB can be used for read/writing, a custom database
97-
* backend must be manually added using `git_odb_add_backend()`
98-
*
9984
* @param[out] odb location to store the database pointer, if opened.
85+
* @param opts the options for this object database or NULL for defaults
10086
* @return 0 or an error code
10187
*/
102-
GIT_EXTERN(int) git_odb_new(git_odb **odb);
88+
GIT_EXTERN(int) git_odb_new_ext(
89+
git_odb **odb,
90+
const git_odb_options *opts);
91+
92+
#endif
10393

10494
/**
10595
* Create a new object database and automatically add
@@ -112,12 +102,33 @@ GIT_EXTERN(int) git_odb_new(git_odb **odb);
112102
* assuming `objects_dir` as the Objects folder which
113103
* contains a 'pack/' folder with the corresponding data
114104
*
105+
* @note This API only supports SHA1 object databases
106+
* @see git_odb_open_ext
107+
*
115108
* @param[out] odb_out location to store the database pointer, if opened.
116109
* Set to NULL if the open failed.
117110
* @param objects_dir path of the backends' "objects" directory.
118111
* @return 0 or an error code
119112
*/
120113
GIT_EXTERN(int) git_odb_open(git_odb **odb_out, const char *objects_dir);
114+
115+
#ifdef GIT_EXPERIMENTAL_SHA256
116+
117+
/**
118+
* Create a new object database and automatically add loose and packed
119+
* backends.
120+
*
121+
* @param[out] odb_out location to store the database pointer, if opened.
122+
* Set to NULL if the open failed.
123+
* @param objects_dir path of the backends' "objects" directory.
124+
* @param opts the options for this object database or NULL for defaults
125+
* @return 0 or an error code
126+
*/
127+
GIT_EXTERN(int) git_odb_open_ext(
128+
git_odb **odb_out,
129+
const char *objects_dir,
130+
const git_odb_options *opts);
131+
121132
#endif
122133

123134
/**

include/git2/sys/repository.h

+16-12
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
*/
2121
GIT_BEGIN_DECL
2222

23-
#ifdef GIT_EXPERIMENTAL_SHA256
24-
2523
/**
2624
* The options for creating an repository from scratch.
2725
*
@@ -64,16 +62,6 @@ GIT_EXTERN(int) git_repository_new_options_init(
6462
git_repository_new_options *opts,
6563
unsigned int version);
6664

67-
/**
68-
* Create a new repository with no backends.
69-
*
70-
* @param[out] out The blank repository
71-
* @param opts the options for repository creation, or NULL for defaults
72-
* @return 0 on success, or an error code
73-
*/
74-
GIT_EXTERN(int) git_repository_new(git_repository **out, git_repository_new_options *opts);
75-
#else
76-
7765
/**
7866
* Create a new repository with neither backends nor config object
7967
*
@@ -84,11 +72,27 @@ GIT_EXTERN(int) git_repository_new(git_repository **out, git_repository_new_opti
8472
* can fail to function properly: locations under $GIT_DIR, $GIT_COMMON_DIR,
8573
* or $GIT_INFO_DIR are impacted.
8674
*
75+
* @note This API only creates SHA1 repositories
76+
* @see git_repository_new_ext
77+
*
8778
* @param[out] out The blank repository
8879
* @return 0 on success, or an error code
8980
*/
9081
GIT_EXTERN(int) git_repository_new(git_repository **out);
9182

83+
#ifdef GIT_EXPERIMENTAL_SHA256
84+
85+
/**
86+
* Create a new repository with no backends.
87+
*
88+
* @param[out] out The blank repository
89+
* @param opts the options for repository creation, or NULL for defaults
90+
* @return 0 on success, or an error code
91+
*/
92+
GIT_EXTERN(int) git_repository_new_ext(
93+
git_repository **out,
94+
git_repository_new_options *opts);
95+
9296
#endif
9397

9498
/**

0 commit comments

Comments
 (0)