Skip to content

Commit 3fbf580

Browse files
committed
oid: give oids a type
`git_oid`s now have a type, and we require the oid type when creating the object id from creation functions.
1 parent 6183829 commit 3fbf580

File tree

190 files changed

+1152
-1007
lines changed

Some content is hidden

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

190 files changed

+1152
-1007
lines changed

examples/general.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ static void oid_parsing(git_oid *oid)
142142
* this throughout the example for storing the value of the current SHA
143143
* key we're working with.
144144
*/
145-
git_oid_fromstr(oid, hex);
145+
git_oid_fromstr(oid, hex, GIT_OID_SHA1);
146146

147147
/*
148148
* Once we've converted the string into the oid value, we can get the raw
@@ -287,9 +287,9 @@ static void commit_writing(git_repository *repo)
287287
* parents. Here we're creating oid objects to create the commit with,
288288
* but you can also use
289289
*/
290-
git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1");
290+
git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1", GIT_OID_SHA1);
291291
git_tree_lookup(&tree, repo, &tree_id);
292-
git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
292+
git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644", GIT_OID_SHA1);
293293
git_commit_lookup(&parent, repo, &parent_id);
294294

295295
/**
@@ -353,7 +353,7 @@ static void commit_parsing(git_repository *repo)
353353

354354
printf("\n*Commit Parsing*\n");
355355

356-
git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479");
356+
git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479", GIT_OID_SHA1);
357357

358358
error = git_commit_lookup(&commit, repo, &oid);
359359
check_error(error, "looking up commit");
@@ -422,7 +422,7 @@ static void tag_parsing(git_repository *repo)
422422
* We create an oid for the tag object if we know the SHA and look it up
423423
* the same way that we would a commit (or any other object).
424424
*/
425-
git_oid_fromstr(&oid, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
425+
git_oid_fromstr(&oid, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1", GIT_OID_SHA1);
426426

427427
error = git_tag_lookup(&tag, repo, &oid);
428428
check_error(error, "looking up tag");
@@ -470,7 +470,7 @@ static void tree_parsing(git_repository *repo)
470470
/**
471471
* Create the oid and lookup the tree object just like the other objects.
472472
*/
473-
git_oid_fromstr(&oid, "f60079018b664e4e79329a7ef9559c8d9e0378d1");
473+
git_oid_fromstr(&oid, "f60079018b664e4e79329a7ef9559c8d9e0378d1", GIT_OID_SHA1);
474474
git_tree_lookup(&tree, repo, &oid);
475475

476476
/**
@@ -524,7 +524,7 @@ static void blob_parsing(git_repository *repo)
524524

525525
printf("\n*Blob Parsing*\n");
526526

527-
git_oid_fromstr(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08");
527+
git_oid_fromstr(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OID_SHA1);
528528
git_blob_lookup(&blob, repo, &oid);
529529

530530
/**
@@ -566,7 +566,7 @@ static void revwalking(git_repository *repo)
566566

567567
printf("\n*Revwalking*\n");
568568

569-
git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
569+
git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644", GIT_OID_SHA1);
570570

571571
/**
572572
* To use the revwalker, create a new walker, tell it how you want to sort

examples/rev-list.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ static int revwalk_parse_revs(git_repository *repo, git_revwalk *walk, struct ar
140140
if (push_spec(repo, walk, curr, hide) == 0)
141141
continue;
142142

143-
if ((error = git_oid_fromstr(&oid, curr)))
143+
if ((error = git_oid_fromstr(&oid, curr, GIT_OID_SHA1)))
144144
return error;
145145
if ((error = push_commit(walk, &oid, hide)))
146146
return error;

fuzzers/commit_graph_fuzzer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
3737
git_commit_graph_entry e;
3838
git_str commit_graph_buf = GIT_STR_INIT;
3939
unsigned char hash[GIT_HASH_SHA1_SIZE];
40-
git_oid oid = {{0}};
40+
git_oid oid = GIT_OID_NONE;
4141
bool append_hash = false;
4242

4343
if (size < 4)

fuzzers/midx_fuzzer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
3636
git_midx_entry e;
3737
git_str midx_buf = GIT_STR_INIT;
3838
unsigned char hash[GIT_HASH_SHA1_SIZE];
39-
git_oid oid = {{0}};
39+
git_oid oid = GIT_OID_NONE;
4040
bool append_hash = false;
4141

4242
if (size < 4)

include/git2/oid.h

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,56 +21,66 @@ GIT_BEGIN_DECL
2121

2222
/** The type of object id, currently only SHA1. */
2323
typedef enum {
24-
GIT_OID_SHA1 = 1 /**< SHA1 */
24+
GIT_OID_SHA1 = 1 /**< SHA1 */
2525
} git_oid_t;
2626

2727
/** Size (in bytes) of a raw/binary oid */
28-
#define GIT_OID_SHA1_SIZE 20
29-
#define GIT_OID_MAX_SIZE GIT_OID_SHA1_SIZE
28+
#define GIT_OID_SHA1_SIZE 20
29+
#define GIT_OID_MAX_SIZE GIT_OID_SHA1_SIZE
3030

3131
/** Size (in bytes) of a hex formatted oid */
32-
#define GIT_OID_SHA1_HEXSIZE (GIT_OID_SHA1_SIZE * 2)
33-
#define GIT_OID_MAX_HEXSIZE GIT_OID_SHA1_HEXSIZE
32+
#define GIT_OID_SHA1_HEXSIZE (GIT_OID_SHA1_SIZE * 2)
33+
#define GIT_OID_MAX_HEXSIZE GIT_OID_SHA1_HEXSIZE
3434

3535
/** Minimum length (in number of hex characters,
3636
* i.e. packets of 4 bits) of an oid prefix */
3737
#define GIT_OID_MINPREFIXLEN 4
3838

3939
/** Unique identity of any object (commit, tree, blob, tag). */
4040
typedef struct git_oid {
41+
/** type of object id */
42+
unsigned char type;
43+
4144
/** raw binary formatted id */
42-
unsigned char id[GIT_OID_SHA1_SIZE];
45+
unsigned char id[GIT_OID_MAX_SIZE];
4346
} git_oid;
4447

4548
/**
4649
* The binary representation of the null object ID.
4750
*/
48-
#define GIT_OID_SHA1_ZERO { { 0 } }
51+
#define GIT_OID_SHA1_ZERO { GIT_OID_SHA1, { 0 } }
4952

5053
/**
5154
* The string representation of the null object ID.
5255
*/
53-
#define GIT_OID_SHA1_HEXZERO "0000000000000000000000000000000000000000"
56+
#define GIT_OID_SHA1_HEXZERO "0000000000000000000000000000000000000000"
5457

5558
/**
5659
* Parse a hex formatted object id into a git_oid.
5760
*
61+
* The appropriate number of bytes for the given object ID type will
62+
* be read from the string - 40 bytes for SHA1, 64 bytes for SHA256.
63+
* The given string need not be NUL terminated.
64+
*
5865
* @param out oid structure the result is written into.
5966
* @param str input hex string; must be pointing at the start of
6067
* the hex sequence and have at least the number of bytes
61-
* needed for an oid encoded in hex (40 bytes).
68+
* needed for an oid encoded in hex (40 bytes for sha1,
69+
* 256 bytes for sha256).
70+
* @param type the type of object id
6271
* @return 0 or an error code
6372
*/
64-
GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str);
73+
GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str, git_oid_t type);
6574

6675
/**
67-
* Parse a hex formatted null-terminated string into a git_oid.
76+
* Parse a hex formatted NUL-terminated string into a git_oid.
6877
*
6978
* @param out oid structure the result is written into.
7079
* @param str input hex string; must be null-terminated.
80+
* @param type the type of object id
7181
* @return 0 or an error code
7282
*/
73-
GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str);
83+
GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str, git_oid_t type);
7484

7585
/**
7686
* Parse N characters of a hex formatted object id into a git_oid.
@@ -81,9 +91,10 @@ GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str);
8191
* @param out oid structure the result is written into.
8292
* @param str input hex string of at least size `length`
8393
* @param length length of the input string
94+
* @param type the type of object id
8495
* @return 0 or an error code
8596
*/
86-
GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length);
97+
GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length, git_oid_t type);
8798

8899
/**
89100
* Copy an already raw oid into a git_oid structure.
@@ -92,16 +103,17 @@ GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length);
92103
* @param raw the raw input bytes to be copied.
93104
* @return 0 on success or error code
94105
*/
95-
GIT_EXTERN(int) git_oid_fromraw(git_oid *out, const unsigned char *raw);
106+
GIT_EXTERN(int) git_oid_fromraw(git_oid *out, const unsigned char *raw, git_oid_t type);
96107

97108
/**
98109
* Format a git_oid into a hex string.
99110
*
100111
* @param out output hex string; must be pointing at the start of
101112
* the hex sequence and have at least the number of bytes
102-
* needed for an oid encoded in hex (40 bytes). Only the
103-
* oid digits are written; a '\\0' terminator must be added
104-
* by the caller if it is required.
113+
* needed for an oid encoded in hex (40 bytes for SHA1,
114+
* 64 bytes for SHA256). Only the oid digits are written;
115+
* a '\\0' terminator must be added by the caller if it is
116+
* required.
105117
* @param id oid structure to format.
106118
* @return 0 on success or error code
107119
*/
@@ -127,9 +139,10 @@ GIT_EXTERN(int) git_oid_nfmt(char *out, size_t n, const git_oid *id);
127139
*
128140
* @param out output hex string; must be pointing at the start of
129141
* the hex sequence and have at least the number of bytes
130-
* needed for an oid encoded in hex (41 bytes). Only the
131-
* oid digits are written; a '\\0' terminator must be added
132-
* by the caller if it is required.
142+
* needed for an oid encoded in hex (41 bytes for SHA1,
143+
* 65 bytes for SHA256). Only the oid digits are written;
144+
* a '\\0' terminator must be added by the caller if it
145+
* is required.
133146
* @param id oid structure to format.
134147
* @return 0 on success, non-zero callback return value, or error code
135148
*/
@@ -151,7 +164,9 @@ GIT_EXTERN(char *) git_oid_tostr_s(const git_oid *oid);
151164
/**
152165
* Format a git_oid into a buffer as a hex format c-string.
153166
*
154-
* If the buffer is smaller than GIT_OID_SHA1_HEXSIZE+1, then the resulting
167+
* If the buffer is smaller than the size of a hex-formatted oid string
168+
* plus an additional byte (GIT_OID_SHA_HEXSIZE + 1 for SHA1 or
169+
* GIT_OID_SHA256_HEXSIZE + 1 for SHA256), then the resulting
155170
* oid c-string will be truncated to n-1 characters (but will still be
156171
* NUL-byte terminated).
157172
*

src/libgit2/attr_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ int git_attr_file__load(
135135
break;
136136
case GIT_ATTR_FILE_SOURCE_INDEX: {
137137
if ((error = attr_file_oid_from_index(&id, repo, entry->path)) < 0 ||
138-
(error = git_blob_lookup(&blob, repo, &id)) < 0)
138+
(error = git_blob_lookup(&blob, repo, &id)) < 0)
139139
return error;
140140

141141
/* Do not assume that data straight from the ODB is NULL-terminated;

src/libgit2/blame.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ static git_blame_hunk *new_hunk(
7272
hunk->final_start_line_number = start;
7373
hunk->orig_start_line_number = orig_start;
7474
hunk->orig_path = path ? git__strdup(path) : NULL;
75+
git_oid_clear(&hunk->orig_commit_id, GIT_OID_SHA1);
76+
git_oid_clear(&hunk->final_commit_id, GIT_OID_SHA1);
7577

7678
return hunk;
7779
}

src/libgit2/commit.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,8 @@ static int commit_parse(git_commit *commit, const char *data, size_t size, unsig
411411
/* The tree is always the first field */
412412
if (!(flags & GIT_COMMIT_PARSE_QUICK)) {
413413
if (git_object__parse_oid_header(&commit->tree_id,
414-
&buffer, buffer_end, "tree ") < 0)
414+
&buffer, buffer_end, "tree ",
415+
GIT_OID_SHA1) < 0)
415416
goto bad_buffer;
416417
} else {
417418
size_t tree_len = strlen("tree ") + GIT_OID_SHA1_HEXSIZE + 1;
@@ -425,7 +426,8 @@ static int commit_parse(git_commit *commit, const char *data, size_t size, unsig
425426
*/
426427

427428
while (git_object__parse_oid_header(&parent_id,
428-
&buffer, buffer_end, "parent ") == 0) {
429+
&buffer, buffer_end, "parent ",
430+
GIT_OID_SHA1) == 0) {
429431
git_oid *new_id = git_array_alloc(commit->parent_ids);
430432
GIT_ERROR_CHECK_ALLOC(new_id);
431433

src/libgit2/commit_graph.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static int commit_graph_parse_oid_lookup(
150150
file->oid_lookup = oid = (unsigned char *)(data + chunk_oid_lookup->offset);
151151
prev_oid = zero_oid;
152152
for (i = 0; i < file->num_commits; ++i, oid += GIT_OID_SHA1_SIZE) {
153-
if (git_oid_raw_cmp(prev_oid, oid) >= 0)
153+
if (git_oid_raw_cmp(prev_oid, oid, GIT_OID_SHA1_SIZE) >= 0)
154154
return commit_graph_error("OID Lookup index is non-monotonic");
155155
prev_oid = oid;
156156
}
@@ -437,7 +437,7 @@ static int git_commit_graph_entry_get_byindex(
437437
}
438438

439439
commit_data = file->commit_data + pos * (GIT_OID_SHA1_SIZE + 4 * sizeof(uint32_t));
440-
git_oid_fromraw(&e->tree_oid, commit_data);
440+
git_oid_fromraw(&e->tree_oid, commit_data, GIT_OID_SHA1);
441441
e->parent_indices[0] = ntohl(*((uint32_t *)(commit_data + GIT_OID_SHA1_SIZE)));
442442
e->parent_indices[1] = ntohl(
443443
*((uint32_t *)(commit_data + GIT_OID_SHA1_SIZE + sizeof(uint32_t))));
@@ -471,7 +471,7 @@ static int git_commit_graph_entry_get_byindex(
471471
}
472472
}
473473

474-
git_oid_fromraw(&e->sha1, &file->oid_lookup[pos * GIT_OID_SHA1_SIZE]);
474+
git_oid_fromraw(&e->sha1, &file->oid_lookup[pos * GIT_OID_SHA1_SIZE], GIT_OID_SHA1);
475475
return 0;
476476
}
477477

src/libgit2/diff.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ int git_diff_patchid(git_oid *out, git_diff *diff, git_diff_patchid_options *opt
381381
if ((error = (flush_hunk(&args.result, &args.ctx))) < 0)
382382
goto out;
383383

384+
args.result.type = GIT_OID_SHA1;
384385
git_oid_cpy(out, &args.result);
385386

386387
out:

0 commit comments

Comments
 (0)